/* ======================================================================
The Bodington System Software License, Version 1.0
  
Copyright (c) 2001 The University of Leeds.  All rights reserved.
  
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1.  Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3.  The end-user documentation included with the redistribution, if any,
must include the following acknowledgement:  "This product includes
software developed by the University of Leeds
(http://www.bodington.org/)."  Alternately, this acknowledgement may
appear in the software itself, if and wherever such third-party
acknowledgements normally appear.

4.  The names "Bodington", "Nathan Bodington", "Bodington System",
"Bodington Open Source Project", and "The University of Leeds" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
d.gardner@leeds.ac.uk.

5.  The name "Bodington" may not appear in the name of products derived
from this software without prior written permission of the University of
Leeds.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  TITLE,  THE IMPLIED WARRANTIES 
OF QUALITY  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO 
EVENT SHALL THE UNIVERSITY OF LEEDS OR ITS CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
=========================================================

This software was originally created by the University of Leeds and may contain voluntary 
contributions from others.  For more information on the Bodington Open Source Project, please 
see http://bodington.org/

====================================================================== */

package org.bodington.applet.components;

import org.bodington.applet.data.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.beans.*;
import java.io.*;
import java.awt.*;
import java.util.*;


public class RemoteFileTree extends javax.swing.JTree 
	implements java.awt.dnd.DragGestureListener, 
               java.awt.dnd.DragSourceListener,
	           java.awt.dnd.DropTargetListener

	{
	private boolean ok;
	private String message;

	DragSource dragSource = null;
	
	RemoteFile base;
	FileTreeNode rootNode;

	FileUploader uploader = null;
	
	public RemoteFileTree()
		{
   		super((TreeModel)null);			// Create the JTree itself
   		getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
   		
		setRootVisible( false );
		setShowsRootHandles( true );
		putClientProperty("JTree.lineStyle", "Angled");

		addTreeExpansionListener(new TreeExpansionHandler());
		
		setMessage( "No files to show" );
		
		//dragSource = new DragSource();
		//dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_COPY_OR_MOVE, this);
		}

	public RemoteFileTree( RemoteFile base )
		{
		this();
		setBase( base );
		}


	public void setFileUploader( FileUploader uploader )
		{
		this.uploader = uploader;
		}
		
    public void drop( DropTargetDropEvent event )
        {
	    System.out.println( "Drop requested" );

	    try
		    {
		    Transferable transferable = event.getTransferable();
		    Point p = event.getLocation();
		    TreePath targetpath;
		    DefaultTreeModel model = (DefaultTreeModel)getModel();
		    int position;
            FileTreeNode targetnode;

		    if ( !transferable.isDataFlavorSupported( LocalFileSelection.localFileSelectionFlavor ) )
		        {
    			JOptionPane.showMessageDialog(null, "You can only drag files from the local file system here.", "Alert", JOptionPane.ERROR_MESSAGE); 
    		    event.rejectDrop();
		        return;
		        }

    		targetpath = getPathForLocation( p.x, p.y );
    		if ( targetpath == null )
		        {
    			JOptionPane.showMessageDialog(null, "Drag to a folder in the tree diagram or to the file list box.", "Alert", JOptionPane.ERROR_MESSAGE); 
    		    event.rejectDrop();
		        return;
		        }
    		
    		targetnode = (FileTreeNode)targetpath.getLastPathComponent();
    		
		    // we accept only    FilePublishRequest.filePublishRequestFlavor
		    System.out.println( "FilePublishRequest dropped" );
		    LocalFileSelection request =(LocalFileSelection)transferable.getTransferData( 
						LocalFileSelection.localFileSelectionFlavor );
    			
            if ( uploader == null )
                {
    			JOptionPane.showMessageDialog(null, "Can't find file uploader.", "Alert", JOptionPane.ERROR_MESSAGE); 
    		    event.rejectDrop();
		        return;
                }
                
			event.acceptDrop( DnDConstants.ACTION_COPY );
            uploader.transferFiles( targetnode.file, request.getFileList(), true );
		    }
	    catch (IOException exception)
		    {
		    exception.printStackTrace();
		    System.err.println( "Exception" + exception.getMessage());
		    event.rejectDrop();
		    } 
	    catch (UnsupportedFlavorException ufException )
		    {
		    ufException.printStackTrace();
		    System.err.println( "Exception" + ufException.getMessage());
		    event.rejectDrop();
		    }   
        }

    public void dragDropEnd(DragSourceDropEvent dsde)
    	{
        // This method is derived from interface java.awt.dnd.DragSourceListener
        // to do: code goes here
    	}

    public void dragEnter(DragSourceDragEvent dsde)
        {
        // This method is derived from interface java.awt.dnd.DragSourceListener
        // to do: code goes here
        }

    public void dragExit(DragSourceEvent dse)
        {
        // This method is derived from interface java.awt.dnd.DragSourceListener
        // to do: code goes here
        }

    public void dragGestureRecognized(DragGestureEvent dge)
        {
    	Point p = dge.getDragOrigin();
		TreePath sourcepath = getPathForLocation( p.x, p.y );
		FileTreeNode sourcenode;
		 
    	if ( sourcepath != null )
   			{
			sourcenode = (FileTreeNode)sourcepath.getLastPathComponent();
    		if ( sourcenode.file != null  )
        		{
         		RemoteFileSelection selection = new RemoteFileSelection( sourcenode.file );
         		dragSource.startDrag( dge, DragSource.DefaultCopyDrop, selection, this );
         		}
        	}
		else
   			{
    		System.out.println( "nothing was selected");   
   			}
        }

    public void dragOver(DragSourceDragEvent dsde)
        {
        // This method is derived from interface java.awt.dnd.DragSourceListener
        // to do: code goes here
        }

    public void dropActionChanged(DragSourceDragEvent dsde)
        {
        // This method is derived from interface java.awt.dnd.DragSourceListener
        // to do: code goes here
        }

    public void dragEnter(DropTargetDragEvent dtde)
        {
        if ( dtde.getSource() == this )
        	dtde.rejectDrag();
        else
        	dtde.acceptDrag( DnDConstants.ACTION_COPY );
        }

    public void dragExit(DropTargetEvent dte)
    {
        // This method is derived from interface java.awt.dnd.DropTargetListener
        // to do: code goes here
    }

    public void dragOver(DropTargetDragEvent dtde)
        {
        dtde.acceptDrag(DnDConstants.ACTION_COPY);
        }

    public void dropActionChanged(DropTargetDragEvent dtde)
    {
        // This method is derived from interface java.awt.dnd.DropTargetListener
        // to do: code goes here
    }

	public void setMessage( String message )
		{
		ok=false;
		this.message=message;
		this.setModel( new DefaultTreeModel( new DefaultMutableTreeNode( message ) ) );
		}

	public void setBase( RemoteFile base )
		{
		this.base = base;
		// Create the first node
		FileTreeNode rootNode = new FileTreeNode( null, false );
		FileTreeNode fileNode = new FileTreeNode( base, false );
		FileTreeNode deletedNode = new FileTreeNode( base, true );
		// Populate the root node with its subdirectories
		fileNode.populateDirectories( true );
		deletedNode.populateDirectories( true );
		rootNode.add( fileNode );
		rootNode.add( deletedNode );

		setModel( new DefaultTreeModel(rootNode) );
		FileTreeNode[] list = new FileTreeNode[2];
		list[0] = rootNode;
		list[1] = fileNode;
		TreePath path = new TreePath( list );
		setSelectionPath( path );
		ok=true;
		}

	public RemoteFile getSelectedFolder()
		{
		TreePath tpath = getSelectionPath();
		if ( tpath == null ) return null;
		FileTreeNode selection = (FileTreeNode)tpath.getLastPathComponent();
		if ( selection == null ) return null;
		
		return selection.file;
		}

	public boolean isSelectionAmongDeleted()
	    {
		TreePath tpath = getSelectionPath();
		if ( tpath == null ) return false;
		FileTreeNode selection = (FileTreeNode)tpath.getLastPathComponent();
		if ( selection == null ) return false;
		return selection.deleted;
	    }

	// Inner class that represents a node in this file system tree
	protected static class FileTreeNode extends DefaultMutableTreeNode
		{
		public FileTreeNode( RemoteFile file, boolean deleted )
			{
			this.file = file;
			this.deleted = deleted;
			}

		// Override isLeaf to check whether this is a directory
		public boolean isLeaf()
			{
			if ( file==null )
			    return false;
			return file.isFile();
			}

		// Override getAllowsChildren to check whether this is a directory
		public boolean getAllowsChildren()
			{
			if ( file==null )
			    return true;
			return !file.isFile();
			}

		// For display purposes, we return our own name
		public String toString()
			{
			if ( file == null )
			    return "";
			
			if ( file.getParent() == null )
			    {
			    if ( deleted )
				return "<Deleted files>";
			    else
				return "<Root folder>";
			    }
			
			return file.getName();
			}

		// If we are a directory, scan our contents and populate 
		// with children. In addition, populate those children
		// if the "descend" flag is true. We only descend once,
		// to avoid recursing the whole subtree. 
		// Returns true if some nodes were added
		boolean populateDirectories( boolean descend ) 
			{
			if ( file == null )
			    return true;
			    
			boolean addedNodes = false;

			// Do this only once
			if (populated == false)
			    {
			    if ( interim == true )
				{
				// We have had a quick look here before:
				// remove the dummy node that we added last time
				removeAllChildren();
				interim = false;					
				}

			    // Get list of contents (folders only)
			    RemoteFile[] files = file.listFolders();
			    if ( files!=null )
				{
				Arrays.sort( files );

				for (int i = 0; i < files.length; i++)
				    {
				    try
					{
					// skip if it isn't a folder
					if ( !files[i].isFolder() )
					    continue;
					
					// are we in the deleted files branch?
					if ( this.deleted )
					    {
					    // skip folders that aren't deleted and
					    // don't contain deleted files/folders
					    if ( !files[i].isDeleted() &&
						 !files[i].containsDeletedFiles() )
						    continue;
					    }
					else
					    {
					    // skip deleted folders
					    if ( files[i].isDeleted() )
						continue;
					    }
					
					FileTreeNode node = new FileTreeNode( files[i], this.deleted );
					this.add(node);
					addedNodes = true;
					if (descend == false)
					    {
					    // Only add one node if not descending
					    break;
					    }
					}
				    catch (Throwable t)
					{
					// Ignore phantoms or access problems
					}					
				    }
				}    				
			    // If we were scanning to get all subdirectories,
			    // or if we found no subdirectories, there is no
			    // reason to look at this directory again, so
			    // set populated to true. Otherwise, we set interim
			    // so that we look again in the future if we need to
			    if ( descend == true || addedNodes == false)
				{
				populated = true;					
				}
			    else
				{
				// Just set interim state
				interim = true;					
				}
			    }
			return addedNodes;
			}

			
		public boolean accept( RemoteFile pathname )
			{
			return pathname.isFolder();
			}

			
		protected RemoteFile file;
		protected boolean deleted;	// true if this node is in the deleted file branch
		protected boolean populated;	// true if we have been populated
		protected boolean interim;	// true if we are in interim state
		}

	// Inner class that handles Tree Expansion Events
	protected class TreeExpansionHandler implements TreeExpansionListener
		{
		public void treeExpanded(TreeExpansionEvent evt)
			{
			TreePath path = evt.getPath();			// The expanded path
			JTree tree = (JTree)evt.getSource();	// The tree

			// Get the last component of the path and
			// arrange to have it fully populated.
			FileTreeNode node = (FileTreeNode)path.getLastPathComponent();
			if (node.populateDirectories(true)) {
				((DefaultTreeModel)tree.getModel()).nodeStructureChanged(node);	
			}
		}

        public void treeCollapsed(TreeExpansionEvent evt)
        	{
			// Nothing to do
			}
		}


	}
