1 package de.webman.wminstaller.tools; 2 3 import javax.swing.*; 4 import javax.swing.tree.*; 5 import javax.swing.event.*; 6 import java.io.*; 7 import java.util.*; 8 9 15 public class FileTree extends JTree { 16 17 18 String selectedPath = null; 19 24 public FileTree( TreeSelectionListener tsl) { 25 26 super( ( TreeModel) null); 28 TreeCellRenderer cr = getCellRenderer(); 30 if ( cr instanceof DefaultTreeCellRenderer) { 31 DefaultTreeCellRenderer dcr = ( DefaultTreeCellRenderer) cr; 32 dcr.setLeafIcon( dcr.getClosedIcon()); 33 } 34 35 putClientProperty("JTree.lineStyle", "Angled"); 37 38 FileTreeNode diskNode = new FileTreeNode( null, ""); 39 diskNode.populateDirectories(); setModel(new DefaultTreeModel( diskNode)); 41 addTreeExpansionListener( new TreeExpansionHandler()); 43 if ( tsl == null) tsl = new TreeSelectionHandler(); 44 addTreeSelectionListener( tsl); 45 expandRow( 0); 46 } 47 48 54 public String getPathName( TreePath path) { 55 56 Object o = path.getLastPathComponent(); 57 if ( o instanceof FileTreeNode) { return ( ( FileTreeNode) o).fullName; } 58 return ""; 59 } 60 61 public boolean redisplayChildren( TreePath path) { 62 Object obj = path.getLastPathComponent(); 63 if ( ! (obj instanceof FileTreeNode)) return false; 64 FileTreeNode node = ( FileTreeNode) obj; 65 node.removeAllChildren(); 66 node.hasVisibleSubElements = node.checkVisibleSubElements(); 67 node.populateDirectories(); 68 ( ( DefaultTreeModel) getModel()).nodeStructureChanged(node); 69 expandPath( path); 70 return true; 71 } 72 73 public boolean redisplayChildren( TreePath path, String selDirName) { 74 75 setEnabled( false); 76 if (! redisplayChildren( path)) { 77 setEnabled( true); 78 return false; 79 } 80 Enumeration e = ( (FileTreeNode) path.getLastPathComponent()).children(); 81 String find = getPathName( path) + File.separator + selDirName; 82 FileTreeNode child; 83 while ( e.hasMoreElements()) { 84 child = (FileTreeNode) e.nextElement(); 85 if ( child.toString().equals( selDirName)) { 86 TreePath np = path.pathByAddingChild( child); 87 makeVisible( np); 88 setSelectionPath( np); 89 scrollPathToVisible( np); 90 break; 91 } 92 } 93 setEnabled( true); 94 return true; 95 } 96 97 98 101 protected static class FileTreeNode extends DefaultMutableTreeNode { 102 103 107 protected String fullName; 108 112 protected String name; 113 116 protected boolean hasVisibleSubElements; 117 120 protected FileTreeElementFilter filter = new FileTreeElementFilter(); 121 122 123 129 public FileTreeNode( String parent, String name) { 130 131 this.name = name; 132 133 139 fullName = parent == null ? name : parent + name; 140 if (! fullName.endsWith( File.separator)) fullName = fullName + File.separator; 142 if ( name.length() == 0) fullName = ""; 143 hasVisibleSubElements = checkVisibleSubElements(); 144 } 145 146 149 public boolean isLeaf() { 150 return !hasVisibleSubElements; 151 } 152 153 156 public boolean getAllowsChildren() { 157 return hasVisibleSubElements; 158 } 159 160 163 public String toString() { 164 if ( name.length() == 0) return "Laufwerke"; 165 return name; 166 } 167 168 171 public boolean checkVisibleSubElements() { 172 if ( name.length() == 0) { 174 File[] drives = File.listRoots(); 175 for( int i = 0; i < drives.length; i++) { 177 String d = drives[i].getPath(); 178 if (! d.endsWith( File.separator)) d = d + File.separator; 179 if ( d.equals("A:"+File.separator)) continue; 181 if ( d.equals("B:"+File.separator)) continue; 182 File drive = new File( d); 183 try { 184 if ( !drive.isHidden() ) return true; 185 } 186 catch ( SecurityException e) { 187 } 189 } 190 return false; 191 } 192 193 File f = new File( fullName); 195 String [] names = new String [] {}; 196 try{ 198 if ((! f.isDirectory()) || (! f.canRead())) return false; 199 names = f.list( filter ); 200 } 201 catch (SecurityException e) { 202 } 204 for( int i = 0; i < names.length; i++) { 205 if ( names[i].equals(".") || names[i].equals("..") ) continue; 206 return true; 207 } 208 return false; 209 } 210 214 void populateDirectories() { 215 216 boolean nodesAdded = false; 217 218 removeAllChildren(); 219 if (! hasVisibleSubElements) return; 220 221 if ( name.length() == 0) { 223 File[] drives = File.listRoots(); 224 Arrays.sort( drives); 225 for( int i = 0; i < drives.length; i++) { 226 String d = drives[i].getPath(); 227 if (! d.endsWith( File.separator)) d = d + File.separator; 228 if (d.equals("A:"+File.separator)) continue; 230 if (d.equals("B:"+File.separator)) continue; 231 File drive = new File( d); 232 try { 233 FileTreeNode node = new FileTreeNode( null, drive.getPath()); 235 this.add( node); 236 nodesAdded = true; 237 } 239 catch (SecurityException e) { } 240 } 241 } 242 else { 244 File f; 245 f = new File(fullName); 246 String [] names = f.list( filter ); Arrays.sort( names); 248 249 for (int i = 0; i < names.length; i++) { 251 String name = names[i]; 252 File d = new File( fullName, name); 253 FileTreeNode node = new FileTreeNode( fullName, name); 254 this.add( node); 255 nodesAdded = true; 256 } 257 } 258 if (! nodesAdded) hasVisibleSubElements = false; 259 } 260 261 } 262 263 266 protected static class FileTreeElementFilter implements FilenameFilter { 267 270 public boolean accept(File dir, String name) { 271 try{ 272 File f = new File( dir, name); 273 if ( ( f.isDirectory()) && (! f.isHidden())) { return true; } 274 } 275 catch ( SecurityException e) { } 276 return false; 277 } 278 } 279 280 protected class TreeExpansionHandler implements TreeExpansionListener { 281 284 285 public void treeExpanded( TreeExpansionEvent evt) { 286 287 TreePath path = evt.getPath(); JTree tree = ( JTree) evt.getSource(); 290 FileTreeNode node = ( FileTreeNode) path.getLastPathComponent(); 293 node.removeAllChildren(); 294 node.hasVisibleSubElements = node.checkVisibleSubElements(); 295 node.populateDirectories(); 296 ( ( DefaultTreeModel) tree.getModel()).nodeStructureChanged(node); 297 } 298 299 public void treeCollapsed(TreeExpansionEvent evt) { 300 System.out.println( "Collaps: " + evt.getPath()); 301 } 302 } 303 304 protected class TreeSelectionHandler implements TreeSelectionListener { 305 306 public void valueChanged( TreeSelectionEvent evt) { 307 TreePath path = evt.getPath(); 308 String name = getPathName( path); 309 if ( evt.isAddedPath()) selectedPath = name; else selectedPath = null; 310 } 312 313 } 314 315 } 316 317 | Popular Tags |