All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.base.gui.filetree.DirNode Maven / Gradle / Ivy

There is a newer version: 2.4
Show newest version
package jadex.base.gui.filetree;

import jadex.base.gui.asynctree.AsyncTreeModel;
import jadex.base.gui.asynctree.ITreeNode;
import jadex.commons.IRemoteFilter;
import jadex.commons.collection.SortedList;
import jadex.commons.future.CollectionResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.commons.gui.TreeExpansionHandler;
import jadex.commons.gui.future.SwingDefaultResultListener;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreePath;

/**
 *  Node object representing a service container.
 */
public class DirNode extends FileNode
{
	//-------- attributes --------
	
	// hack: should belong to the model
	/** The factory. */
	protected INodeFactory factory;
	
	//-------- constructors --------
	
	/**
	 *  Create a new service container node.
	 */
	public DirNode(ITreeNode parent, AsyncTreeModel model, JTree tree, File file, 
		IIconCache iconcache, INodeFactory factory)
	{
		super(parent, model, tree, file, iconcache);
		
//		assert file.isDirectory(): file;
		this.factory = factory;
//		System.out.println("node: "+getClass()+" "+desc.getName());
	}
	
	//-------- AbstractComponentTreeNode methods --------
	
	/**
	 *  Asynchronously search for children.
	 *  Should call setChildren() once children are found.
	 */
	protected void	searchChildren()
	{
		listFiles().addResultListener(new SwingDefaultResultListener()
		{
			public void customResultAvailable(Object result)
			{
				Collection files = (Collection)result;
//				CollectionResultListener lis = new CollectionResultListener(files.size(), true, 
//					new DefaultResultListener()
//				{
//					public void resultAvailable(Object result)
//					{
//						setChildren((List)result);
//					}
//				});
				
				List	nodes	= new SortedList(FILENODE_COMPARATOR, true);
//				List	nodes	= new ArrayList();
				for(Iterator it=files.iterator(); it.hasNext();)
				{
					File file = (File)it.next();
					ITreeNode node = getModel().getNode(file);//.getAbsolutePath());
					if(node!=null)
					{
//						lis.resultAvailable(node);
						nodes.add(node);
					}
					else
					{
//						lis.resultAvailable(ModelTreePanel.createNode(DirNode.this, model, tree, file, iconcache, filter, null));
						nodes.add(factory.createNode(DirNode.this, model, tree, file, iconcache, null, factory));
					}
				}

				setChildren(nodes);
			}
		});
	}
	
	/**
	 *	Get a file filter according to current file type settings. 
	 */
	protected IFuture listFiles()
	{
		if(file==null)
			return new Future(new ArrayList());
		
		final Future ret = new Future();
		final File[] files = file.listFiles();
		final CollectionResultListener lis = new CollectionResultListener(files==null? 0: files.length, true, new DelegationResultListener(ret));
		
		if(files!=null)
		{
//			System.out.println("name: "+toString()+" files length: "+files.length);
			for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy