package org.das2.util.filesystem; import java.awt.Dimension; import java.io.FileNotFoundException; import java.io.IOException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.logging.ConsoleHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.SwingUtilities; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; import org.das2.util.LoggerManager; /** * present a FileSystem as a TreeModel to display in JTrees. * @author jbf */ public class FSTreeModel extends DefaultTreeModel { private static final Logger logger= LoggerManager.getLogger( "das2.filesystem.fstree"); public static final String PENDING_NOTE = " PENDING"; FileSystem fs; List listCachePath= new ArrayList(); Map listCachePendingFolders= new HashMap(); Map listCache= new HashMap(); public FSTreeModel(FileSystem fs) { super( new FSTreeNode( "/",fs.getRootURI().toString()) ); this.fs = fs; } private static class FSTreeNode extends DefaultMutableTreeNode { String path; String label; boolean pending; FSTreeNode( String fileSystemPath, String label ) { this.path= fileSystemPath; this.label= label; this.pending= false; } @Override public String toString() { return this.label + ( pending ? PENDING_NOTE : "" ); } public String getFileSystemPath() { return this.path; } public boolean isPending() { return pending; } public void setPending( boolean pending ) { this.pending= pending; } } @Override public boolean isLeaf(Object node) { boolean isFolder= ((FSTreeNode)node).path.endsWith("/"); logger.log(Level.FINEST, "isLeaf({0}) -> {1}", new Object[] { node, !isFolder } ); return !isFolder; } @Override public int getChildCount(Object parent) { int count= getChildren(parent).length; logger.log(Level.FINER, "getChildCount({0}) -> {1}", new Object[] { parent, count } ) ; return count; } @Override public Object getChild(Object parent, int index) { Object[] kids= getChildren(parent); logger.log(Level.FINEST, "getChild({0},{1}) -> {2}", new Object[]{parent, index,kids[index] } ); return kids[index]; } boolean stopTest= false; private void listingImmediately( final Object listCachePendingFolder ) { logger.log(Level.FINE, "listingImmediatey({0})", new Object[]{ listCachePendingFolder } ); try { final String folder= folderForNode(listCachePendingFolder); long t0= System.currentTimeMillis(); logger.log(Level.FINE, "listImmediately {0}", folder); final String[] folderKids= fs.listDirectory(folder); logger.fine( String.format( Locale.US, "done in %5.2f sec: listImmediately %s", (System.currentTimeMillis()-t0)/1000.0, folder ) ); final DefaultMutableTreeNode[] listCache1 = new DefaultMutableTreeNode[folderKids.length]; final int[] nodes= new int[folderKids.length]; for ( int i=0; i array[{1}]", new Object[]{ listCachePath.get(listCachePath.size()-1), nodes.length } ); for (MutableTreeNode rm1 : rm) { FSTreeModel.this.removeNodeFromParent(rm1); } for ( int i=0; i {1}", new Object[]{parent, result}); return result; } else { listCache.put( key, new DefaultMutableTreeNode[] { } ); listCachePendingFolders.put( key, theFolder ); boolean async= true; if ( async ) { if ( theFolder.equals("/") ) { listCachePath.clear(); listCachePath.add( new TreePath( fs ) ); } else { listCachePath.add( new TreePath( parent ) ); } startListing(parent); result= listCache.get( key ); logger.log( Level.FINEST, "getChildren({0}) -> {1}", new Object[]{parent, result}); return result; } else { listingImmediately(parent); result= listCache.get( key ); logger.log( Level.FINEST, "getChildren({0}) -> {1}", new Object[]{parent, result}); return result; } } } } @Override public int getIndexOfChild(Object parent, Object child) { Object[] cc= getChildren(parent); int result= -1; for ( int i=0; i {2}", new Object[]{parent, child, result }); return result; } public static void main( String[] args ) throws FileNotFoundException, UnknownHostException, FileSystem.FileSystemOfflineException { logger.setLevel(Level.FINE); for ( Handler h: logger.getHandlers() ) { logger.removeHandler(h); } Handler h= new ConsoleHandler(); h.setLevel(Level.FINE); logger.addHandler(h ); //FileSystem fs= FileSystem.create("file:///home/jbf/tmp/"); //FileSystem fs= FileSystem.create("http://autoplot.org/data/vap/"); //FileSystem fs= FileSystem.create("http://emfisis.physics.uiowa.edu/pub/jyds/"); FileSystem fs= FileSystem.create("http://sarahandjeremy.net/~jbf/"); TreeModel tm= new FSTreeModel(fs) ; JTree mytree= new JTree( tm ); mytree.setMinimumSize( new Dimension(400,600) ); mytree.setPreferredSize( new Dimension(400,600) ); JOptionPane.showMessageDialog( null, new JScrollPane(mytree), "Test FSTREE", JOptionPane.INFORMATION_MESSAGE ); } }