Link to home
Start Free TrialLog in
Avatar of InteractiveMind
InteractiveMindFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Edit FileTreeModel, as to display names only [urgent]

Here's the TreeModel that I'm using to produce a file tree:


package org.efileuk.uploader ;

import javax.swing.* ;
import javax.swing.tree.* ;
import javax.swing.event.* ;
import java.awt.event.* ;
import java.util.* ;
import java.io.* ;

public class FileTreeModel implements TreeModel, Serializable, Cloneable
{
      
      // V A R I A B L E S
      
      protected EventListenerList listeners ;
      private static final Object LEAF = new Serializable() {} ;
      private Map map ;
      private File root ;
      
      
      // C O N S T R U C T O R S
      
      public FileTreeModel( File root )
      {
            this.root = root ;

            if ( !root.isDirectory() )
                  map.put( root, LEAF ) ;

            this.listeners = new EventListenerList() ;

            this.map = new HashMap() ;
      }
      
      
      // P U B L I C   M E T H O D S
      
      public Object getRoot()
      {
            return root ;
      }

      public boolean isLeaf( Object node )
      {
            return map.get( node ) == LEAF ;
      }

      public int getChildCount( Object node )
      {
            List children = children( node ) ;

            if ( children == null )
                  return 0 ;

            return children.size() ;
      }

      public Object getChild( Object parent, int index )
      {
            return children( parent ).get( index ) ;
      }

      public int getIndexOfChild( Object parent, Object child )
      {
            return children( parent ).indexOf( child ) ;
      }
      
      protected List children( Object node )
      {
            File f = (File ) node ;

            Object value = map.get( f ) ;

            if ( value == LEAF )
                  return null ;

            List children = (List ) value ;

            if ( children == null )
            {
                  File [] c = f.listFiles() ;

                  if ( c != null )
                  {
                        children = new ArrayList( c.length ) ;

                        for ( int len = c.length, i = 0 ; i < len ; i++ )
                        {
                              children.add( c [i] ) ;
                              if ( !c [i].isDirectory() )
                                    map.put( c [i], LEAF ) ;
                        }
                  } else
                        children = new ArrayList( 0 ) ;

                  map.put( f, children ) ;
            }

            return children ;
      }

      public void valueForPathChanged( TreePath path, Object value )
      {}

      public void addTreeModelListener( TreeModelListener l )
      {
            listeners.add( TreeModelListener.class, l ) ;
      }

      public void removeTreeModelListener( TreeModelListener l )
      {
            listeners.remove( TreeModelListener.class, l ) ;
      }
      
      
      // O V E R R I D D E N   M E T H O D S
      
      public Object clone()
      {
            try
            {
                  FileTreeModel clone = (FileTreeModel ) super.clone() ;

                  clone.listeners = new EventListenerList() ;

                  clone.map = new HashMap( map ) ;

                  return clone ;
            } catch ( CloneNotSupportedException e )
            {
                  throw new InternalError() ;
            }
      }
}


The problem is, that all of nodes within the JTree have the value of the entire path, but I only want it to be file name itself. How can I edit this (or my JTree) so that it displays just file names?

Can I have it so that it displays one thing, but when I get it's value, it returns another? (So, displays file names, but when I get the value, it's the entire path name of that file?).

Thanks !!
Avatar of Mayank S
Mayank S
Flag of India image

That should be all right - maintain a hash-map or a hash-table which maintains file-names and file-paths. Display only the file-names and when the node is selected, get the value from the hash-map.

>> children = new ArrayList( 0 ) ;

Not required, better keep it to children = new ArrayList () ; because it will give it the default size (16) so at least until you add 16 objects, you will not need any expansions. If you keep it to 0, it will have to expand the first time you add an object.
Avatar of InteractiveMind

ASKER

But would there not be problems in the event of multiple files having the same file name ?
Yes, I thought of that. You might try to maintain it with the TreeNode object as the key and the full file-path as the value. But for that, the tree-node will need to give its own implementation of hashCode () and equals (). As of now, that's what I can think of - let me think of an alternative. BTW, if you can use DefaultMutableTreeNodes, maybe you can set your own UserObject (a wrapper which has file-name and file-path together, and whose toString () returns only the file-name so that is what is displayed).
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fantastic !

Thank you :)
:-)