[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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 !!
0
InteractiveMind
Asked:
InteractiveMind
  • 2
  • 2
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
InteractiveMindAuthor Commented:
Fantastic !

Thank you :)
0
 
CEHJCommented:
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now