/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

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

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
			}
		}


	}
