/* ======================================================================
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.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.
    }
    
    
}
