/* ======================================================================
   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.tree.*;
import javax.swing.event.*;
import java.beans.*;
import java.awt.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;

public class LocalFileTree extends javax.swing.JTree
implements java.awt.dnd.DragGestureListener,
java.awt.dnd.DragSourceListener,
java.awt.dnd.DropTargetListener
{
    private boolean ok;
    private String message;
    /**
     * enables this component to be a Drag Source
     */
    DragSource dragSource = null;
    DropTarget dropTarget = null;
    FileDownloader downloader;
    
    // prevent use of this consuctructor
    public LocalFileTree()
    {
	super((TreeModel)null);			// Create the JTree itself
	getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
	setRootVisible( false );
	// Use horizontal and vertical lines
	putClientProperty("JTree.lineStyle", "Angled");
	
	// Listen for Tree Selection Events
	addTreeExpansionListener(new TreeExpansionHandler());
	
	try
	{
	    setPath( null );
	}
	catch ( Exception ex )
	{
	    setMessage( "No files to show." );
	    setRootVisible( true );
	}
	
	//dragSource = new DragSource();
	//dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_COPY_OR_MOVE, this);
	//dropTarget = new DropTarget( this, this );
    }
    
    public LocalFileTree( String path )
    throws FileNotFoundException, SecurityException
    {
	this();
	setPath( path );
    }
    
    public void setFileDownloader( FileDownloader downloader )
    {
	this.downloader = downloader;
    }
    
    public File getSelectedFolder()
    {
	TreePath tpath = getSelectionPath();
	if ( tpath == null ) return null;
	FileTreeNode selection = (FileTreeNode)tpath.getLastPathComponent();
	if ( selection == null ) return null;
	
	System.out.println( selection.file.getAbsolutePath() );
	
	return selection.file;
    }
    
    public void dragEnter( DropTargetDragEvent dtde )
    {
	if ( dtde.getSource() == this )
	    dtde.rejectDrag();
	else
	    dtde.acceptDrag( DnDConstants.ACTION_COPY );
    }
    
    public void dragExit( DropTargetEvent dte )
    {
    }
    
    public void drop( DropTargetDropEvent dtde )
    {
	System.out.println( "Drop requested" );
	Point p = dtde.getLocation();
	TreePath targetpath;
	FileTreeNode targetnode;
	DefaultTreeModel model = (DefaultTreeModel)getModel();
	int position;
	
	try
	{
	    Transferable transferable = dtde.getTransferable();
	    
	    // right kind of data dragged here?
	    if ( !transferable.isDataFlavorSupported( RemoteFileSelection.remoteFileSelectionFlavor ) )
	    {
		JOptionPane.showMessageDialog(null, "You can only drag files from the remote file system here.", "Alert", JOptionPane.ERROR_MESSAGE);
		dtde.rejectDrop();
		return;
	    }
	    
	    //which directory is target?
	    targetpath = getPathForLocation( p.x, p.y );
	    if ( targetpath == null )
	    {
		JOptionPane.showMessageDialog(null, "You must drag to a specific folder to download files.", "Alert", JOptionPane.ERROR_MESSAGE);
		dtde.rejectDrop();
		return;
	    }
	    
	    targetnode = (FileTreeNode)targetpath.getLastPathComponent();
	    
	    RemoteFileSelection rflist =(RemoteFileSelection)transferable.getTransferData(
	    RemoteFileSelection.remoteFileSelectionFlavor );
	    
	    if ( downloader == null )
	    {
		JOptionPane.showMessageDialog(null, "Can't find file downloader.", "Alert", JOptionPane.ERROR_MESSAGE);
		dtde.rejectDrop();
		return;
	    }
	    
	    dtde.acceptDrop( DnDConstants.ACTION_COPY );
	    downloader.transferFiles( targetnode.file, rflist.getFileList(), true );
	    
	    setSelectionPath( targetpath );
	}
	catch (IOException exception)
	{
	    exception.printStackTrace();
	    System.err.println( "Exception" + exception.getMessage());
	    dtde.rejectDrop();
	}
	catch (UnsupportedFlavorException ufException )
	{
	    ufException.printStackTrace();
	    System.err.println( "Exception" + ufException.getMessage());
	    dtde.rejectDrop();
	}
    }
    
    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 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 event)
    {
	Point p = event.getDragOrigin();
	TreePath sourcepath = getPathForLocation( p.x, p.y );
	FileTreeNode sourcenode;
	
	if ( sourcepath != null )
	{
	    sourcenode = (FileTreeNode)sourcepath.getLastPathComponent();
	    if ( sourcenode.file != null  )
	    {
		if ( sourcenode.file.exists() )
		{
		    LocalFileSelection selection = new LocalFileSelection( sourcenode.file );
		    dragSource.startDrag(event, 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 setMessage( String message )
    {
	ok=false;
	this.message=message;
	setModel( new DefaultTreeModel( new DefaultMutableTreeNode( message ) ) );
	setRootVisible( true );
    }
    
    
    
    public void setPath(String path) throws FileNotFoundException
    {
	// Create the first node
	FileTreeNode rootNode;
	if ( path == null )
	    rootNode = new FileTreeNode( null );
	else
	    rootNode = new FileTreeNode( new File( path ) );
	
	// Populate the root node with its subdirectories
	try
	    {
	    boolean addedNodes = rootNode.populateDirectories( true );

	    // if trying to look for root file systems no exceptions are thrown
	    // but if no root file systems are found it is probably because of
	    // security settings.
	    if ( path == null && !addedNodes )
		{
		setMessage( "Security choices deny access to local files." );
		return;
		}
	    }
	catch ( SecurityException secex )
	    {
	    setMessage( "Security choices deny access to local files here." );
	    return;
	    }
	catch ( Exception ex )
	    {
	    setMessage( "A technical fault prevents the listing files here." );
	    return;
	    }
	setModel( new DefaultTreeModel(rootNode) );
	setRootVisible( false );
	ok=true;
    }
    
    // Returns the full pathname for a path, or null if not a known path
    public String getPathName(TreePath path)
    {
	Object o = path.getLastPathComponent();
	if (o instanceof FileTreeNode)
	{
	    return ((FileTreeNode)o).file.getAbsolutePath();
	}
	return null;
    }
    
    public Vector getPaths()
    {
	Vector paths = new Vector();
	for ( int i = 0 ; i < getRowCount() ; i++)
	{
	    String s = getPathName(getPathForRow(i));
	    paths.addElement(s);
	}
	return paths;
    }
    
    // Inner class that represents a node in this file system tree
    protected static class FileTreeNode extends DefaultMutableTreeNode
    implements java.io.FileFilter
    {
	public FileTreeNode( File file )
	throws SecurityException, FileNotFoundException
	{
	    this.file = file;
	    
	    if ( file!=null )
	    {
		if ( !file.exists() )
		    throw new FileNotFoundException( "File " + file.getAbsolutePath() + " does not exist");
		if ( file.isFile() )
		    throw new FileNotFoundException( "File " + file.getAbsolutePath() + " is not a directory");
	    }
	}
	
	// Override isLeaf to check whether this is a directory
	public boolean isLeaf()
	{
	    return file!=null && file.isFile();
	}
	
	// Override getAllowsChildren to check whether this is a directory
	public boolean getAllowsChildren()
	{
	    return file==null || !file.isFile();
	}
	
	// For display purposes, we return our own name
	public String toString()
	{
	    if ( file==null)
		return "local files";
	    if ( file.getName() == null || file.getName().length()==0 )
		return file.getAbsolutePath();
	    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 )
	{
	    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)
		File[] files;
		if ( file == null )
		    files = File.listRoots();
		else
		    files = file.listFiles( this );
		
		if ( files!=null )
		{
		    Arrays.sort( files );
		    
		    for (int i = 0; files!=null && i < files.length; i++)
		    {
			try
			{
			    if ( !files[i].isFile() )
			    {
				FileTreeNode node = new FileTreeNode( files[i] );
				this.add(node);
				addedNodes = true;
				if (descend == false)
				{
				    // Only add one node if not descending
				    break;
				}
			    }
			}
			catch (Throwable t)
			{
			    t.printStackTrace();
			    // 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( File pathname )
	{
	    return pathname.isDirectory();
	}
	
	protected File file;
	
	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
	}
    }
    
    class SymAncestor implements javax.swing.event.AncestorListener
    {
	public void ancestorMoved(javax.swing.event.AncestorEvent event)
	{
	}
	
	public void ancestorAdded(javax.swing.event.AncestorEvent event)
	{
	}
	
	public void ancestorRemoved(javax.swing.event.AncestorEvent event)
	{
	    Object object = event.getSource();
	    if (object == LocalFileTree.this)
		FileTree_ancestorRemoved(event);
	}
    }
    
    void FileTree_ancestorRemoved(javax.swing.event.AncestorEvent event)
    {
	// to do: code goes here.
    }
    
    
}
