1 20 21 package com.methodhead.res; 22 23 import com.methodhead.tree.Tree; 24 import java.io.File ; 25 import com.methodhead.tree.FoldingTreeNode; 26 import java.util.Comparator ; 27 import java.util.Arrays ; 28 29 public class FileTree 30 extends 31 Tree { 32 33 35 37 39 41 45 protected FoldingTreeNode newNode( 46 String path ) { 47 48 FoldingTreeNode node = new FoldingTreeNode(); 49 node.setOpened( false ); 50 node.setIconHint( "dir" ); 51 52 updateNode( node, path ); 53 54 return node; 55 } 56 57 61 protected void updateNode( 62 FoldingTreeNode node, 63 String path ) { 64 65 String fileName = path; 69 int i = path.lastIndexOf( "/" ); 70 if ( i != -1 ) { 71 fileName = path.substring( i + 1 ); 72 } 73 74 node.setLabel( fileName ); 78 node.setUrl( "listFiles.do?path=" + path ); 79 } 80 81 85 protected FoldingTreeNode buildNode( 86 String path, 87 File dir ) { 88 89 FoldingTreeNode node = newNode( path ); 90 91 File [] files = dir.listFiles(); 95 96 if ( files == null ) 97 throw new ResException( "Could list files for path \"" + path + "\"." ); 98 99 Arrays.sort( files, new Comparator () { 103 public int compare( Object o1, Object o2 ) { 104 return 105 ( ( File )o1 ).getName().compareToIgnoreCase( 106 ( ( File )o2 ).getName() ); 107 } 108 } ); 109 110 for ( int i = 0; i < files.length; i++ ) { 114 if ( files[ i ].isDirectory() ) 115 node.add( buildNode( path + "/" + files[ i ].getName(), files[ i ] ) ); 116 } 117 118 return node; 119 } 120 121 125 public void build( 126 FileManager fileManager ) { 127 128 FoldingTreeNode root = new FoldingTreeNode(); 132 root.setOpened( true ); 133 134 Directory[] dirs = fileManager.getDirectories(); 138 139 for ( int i = 0; i < dirs.length; i++ ) { 140 FoldingTreeNode node = 141 buildNode( dirs[ i ].getName(), dirs[ i ].getFile() ); 142 143 node.setLabel( dirs[ i ].getName() ); 147 148 root.add( node ); 149 } 150 151 setRoot( root ); 152 } 153 154 158 private FoldingTreeNode findNode( 159 String [] path, 160 int index, 161 FoldingTreeNode node ) { 162 163 if ( !path[ index ].equals( node.getLabel() ) ) 167 return null; 168 169 if ( ( index + 1 ) == path.length ) 173 return node; 174 175 for ( int i = 0; i < node.getChildCount(); i++ ) { 179 FoldingTreeNode n = 180 findNode( path, index + 1, ( FoldingTreeNode )node.getChildAt( i ) ); 181 182 if ( n != null ) 183 return n; 184 } 185 186 return null; 187 } 188 189 193 public FoldingTreeNode find( 194 String path ) { 195 196 FoldingTreeNode root = ( FoldingTreeNode )getRoot(); 197 198 if ( root == null ) 202 return null; 203 204 if ( root.getChildCount() == 0 ) 208 return null; 209 210 String [] pathArr = path.split( "/" ); 214 215 for ( int i = 0; i < root.getChildCount(); i++ ) { 219 FoldingTreeNode n = 220 findNode( pathArr, 0, ( FoldingTreeNode )root.getChildAt( i ) ); 221 222 if ( n != null ) 223 return n; 224 } 225 226 return null; 227 } 228 229 233 public FoldingTreeNode find( 234 String path, 235 String file ) { 236 237 return find( path + "/" + file ); 238 } 239 240 245 public void move( 246 String srcPath, 247 String [] srcFiles, 248 String destPath, 249 String destFile ) { 250 251 FoldingTreeNode dest = find( destPath ); 252 253 for ( int i = 0; i < srcFiles.length; i++ ) { 254 FoldingTreeNode src = find( srcPath, srcFiles[ i ] ); 255 if ( src != null ) { 256 moveUnder( dest, src ); 257 258 if ( srcFiles.length == 1 ) 259 updateNode( src, destPath + "/" + destFile ); 260 } 261 } 262 263 sort( dest, new Comparator () { 264 public int compare( Object o1, Object o2 ) { 265 return 266 ( ( FoldingTreeNode )o1 ).getLabel().compareToIgnoreCase( 267 ( ( FoldingTreeNode )o2 ).getLabel() ); 268 } 269 } ); 270 } 271 272 277 public void copy( 278 String srcPath, 279 String [] srcFiles, 280 String destPath, 281 String destFile ) { 282 283 FoldingTreeNode dest = find( destPath ); 284 285 for ( int i = 0; i < srcFiles.length; i++ ) { 286 FoldingTreeNode src = find( srcPath, srcFiles[ i ] ); 287 if ( src != null ) { 288 FoldingTreeNode copy = ( FoldingTreeNode )copy( src ); 289 insertUnder( dest, copy ); 290 291 if ( srcFiles.length == 1 ) 292 updateNode( copy, destPath + "/" + destFile ); 293 } 294 } 295 296 sort( dest, new Comparator () { 297 public int compare( Object o1, Object o2 ) { 298 return 299 ( ( FoldingTreeNode )o1 ).getLabel().compareToIgnoreCase( 300 ( ( FoldingTreeNode )o2 ).getLabel() ); 301 } 302 } ); 303 } 304 305 308 public void delete( 309 String srcPath, 310 String [] srcFiles ) { 311 312 for ( int i = 0; i < srcFiles.length; i++ ) { 313 FoldingTreeNode src = find( srcPath, srcFiles[ i ] ); 314 if ( src != null ) { 315 remove( src ); 316 } 317 } 318 } 319 320 323 public void create( 324 String path, 325 String file, 326 boolean isDir ) { 327 328 FoldingTreeNode dest = find( path ); 329 330 if ( isDir ) { 331 if ( dest == null ) 332 throw new ResException( 333 "Couldn't find node for path \"" + path + "\"" ); 334 335 FoldingTreeNode n = newNode( path + "/" + file ); 336 insertUnder( dest, n ); 337 338 sort( dest, new Comparator () { 339 public int compare( Object o1, Object o2 ) { 340 return 341 ( ( FoldingTreeNode )o1 ).getLabel().compareToIgnoreCase( 342 ( ( FoldingTreeNode )o2 ).getLabel() ); 343 } 344 } ); 345 } 346 } 347 348 350 } 352 | Popular Tags |