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 !!
LVL 25
InteractiveMindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
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.
0
InteractiveMindAuthor Commented:
But would there not be problems in the event of multiple files having the same file name ?
0
Mayank SAssociate Director - Product EngineeringCommented:
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).
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

CEHJCommented:
Set this as the cell renderer for your JTree:


class NameOnlyRenderer extends DefaultTreeCellRenderer {

      public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            File f = (File)value;
            String text = f.getName();
            return super.getTreeCellRendererComponent(tree, text, selected, expanded, leaf, row, hasFocus);
      }
      
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InteractiveMindAuthor Commented:
Fantastic !

Thank you :)
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.