KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > res > FileTree


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.res;
22
23 import com.methodhead.tree.Tree;
24 import java.io.File JavaDoc;
25 import com.methodhead.tree.FoldingTreeNode;
26 import java.util.Comparator JavaDoc;
27 import java.util.Arrays JavaDoc;
28
29 public class FileTree
30 extends
31   Tree {
32
33   // constructors /////////////////////////////////////////////////////////////
34

35   // constants ////////////////////////////////////////////////////////////////
36

37   // classes //////////////////////////////////////////////////////////////////
38

39   // methods //////////////////////////////////////////////////////////////////
40

41   /**
42    * Instantiates and initializes a new node for <tt>path</tt> and
43    * <tt>dir</tt>.
44    */

45   protected FoldingTreeNode newNode(
46     String JavaDoc 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   /**
58    * Updates the label and url of <tt>node</tt> for <tt>path</tt> and
59    * <tt>dir</tt>.
60    */

61   protected void updateNode(
62     FoldingTreeNode node,
63     String JavaDoc path ) {
64
65     //
66
// get the file name
67
//
68
String JavaDoc fileName = path;
69     int i = path.lastIndexOf( "/" );
70     if ( i != -1 ) {
71       fileName = path.substring( i + 1 );
72     }
73
74     //
75
// update the node
76
//
77
node.setLabel( fileName );
78     node.setUrl( "listFiles.do?path=" + path );
79   }
80   
81   /**
82    * Recursively builds nodes for <tt>path</tt> and <tt>dir</tt> and its
83    * subdirectories.
84    */

85   protected FoldingTreeNode buildNode(
86     String JavaDoc path,
87     File JavaDoc dir ) {
88
89     FoldingTreeNode node = newNode( path );
90
91     //
92
// get directory files
93
//
94
File JavaDoc[] files = dir.listFiles();
95
96     if ( files == null )
97       throw new ResException( "Could list files for path \"" + path + "\"." );
98
99     //
100
// sort the files
101
//
102
Arrays.sort( files, new Comparator JavaDoc() {
103       public int compare( Object JavaDoc o1, Object JavaDoc o2 ) {
104         return
105           ( ( File JavaDoc )o1 ).getName().compareToIgnoreCase(
106             ( ( File JavaDoc )o2 ).getName() );
107       }
108     } );
109     
110     //
111
// recurse into directories
112
//
113
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   /**
122    * Builds a file tree representing the the directories managed by
123    * <tt>fileManager</tt>.
124    */

125   public void build(
126     FileManager fileManager ) {
127
128     //
129
// set up the root node
130
//
131
FoldingTreeNode root = new FoldingTreeNode();
132     root.setOpened( true );
133
134     //
135
// add nodes for each directory
136
//
137
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       //
144
// force the label to the logical name
145
//
146
node.setLabel( dirs[ i ].getName() );
147
148       root.add( node );
149     }
150
151     setRoot( root );
152   }
153
154   /**
155    * Recurses the tree rooted by <tt>node</tt> searching for the node
156    * corresponding to <tt>path</tt>.
157    */

158   private FoldingTreeNode findNode(
159     String JavaDoc[] path,
160     int index,
161     FoldingTreeNode node ) {
162
163     //
164
// are we on the right path?
165
//
166
if ( !path[ index ].equals( node.getLabel() ) )
167       return null;
168     
169     //
170
// have we reached the last directory in the path?
171
//
172
if ( ( index + 1 ) == path.length )
173       return node;
174
175     //
176
// recurse into child nodes
177
//
178
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   /**
190    * Returns the node associated with <tt>path</tt>, or <tt>null</tt> if no
191    * such node exists.
192    */

193   public FoldingTreeNode find(
194     String JavaDoc path ) {
195
196     FoldingTreeNode root = ( FoldingTreeNode )getRoot();
197
198     //
199
// no root?
200
//
201
if ( root == null )
202       return null;
203
204     //
205
// no directories?
206
//
207
if ( root.getChildCount() == 0 )
208       return null;
209
210     //
211
// split path
212
//
213
String JavaDoc[] pathArr = path.split( "/" );
214
215     //
216
// search directories (root node doesn't count)
217
//
218
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   /**
230    * NOT UNIT TESTED Returns the node associated with <tt>path</tt>, or
231    * <tt>null</tt> if no such node exists.
232    */

233   public FoldingTreeNode find(
234     String JavaDoc path,
235     String JavaDoc file ) {
236
237     return find( path + "/" + file );
238   }
239
240   /**
241    * Moves any nodes specified by <tt>srcPath</tt> and <tt>srcFiles</tt> to
242    * <tt>destPath</tt>. If <tt>srcFiles</tt> contains a single file name,
243    * <tt>destFile</tt> is assumed to be its new name.
244    */

245   public void move(
246     String JavaDoc srcPath,
247     String JavaDoc[] srcFiles,
248     String JavaDoc destPath,
249     String JavaDoc 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 JavaDoc() {
264       public int compare( Object JavaDoc o1, Object JavaDoc o2 ) {
265         return
266           ( ( FoldingTreeNode )o1 ).getLabel().compareToIgnoreCase(
267             ( ( FoldingTreeNode )o2 ).getLabel() );
268       }
269     } );
270   }
271
272   /**
273    * Copies any nodes specified by <tt>srcPath</tt> and <tt>srcFiles</tt> to
274    * <tt>destPath</tt>. If <tt>srcFiles</tt> contains a single file name,
275    * <tt>destFile</tt> is assumed to be its new name.
276    */

277   public void copy(
278     String JavaDoc srcPath,
279     String JavaDoc[] srcFiles,
280     String JavaDoc destPath,
281     String JavaDoc 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 JavaDoc() {
297       public int compare( Object JavaDoc o1, Object JavaDoc o2 ) {
298         return
299           ( ( FoldingTreeNode )o1 ).getLabel().compareToIgnoreCase(
300             ( ( FoldingTreeNode )o2 ).getLabel() );
301       }
302     } );
303   }
304
305   /**
306    * Deletes any nodes specified by <tt>srcPath</tt> and <tt>srcFiles</tt>.
307    */

308   public void delete(
309     String JavaDoc srcPath,
310     String JavaDoc[] 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   /**
321    * Deletes any nodes specified by <tt>srcPath</tt> and <tt>srcFiles</tt>.
322    */

323   public void create(
324     String JavaDoc path,
325     String JavaDoc 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 JavaDoc() {
339         public int compare( Object JavaDoc o1, Object JavaDoc o2 ) {
340           return
341             ( ( FoldingTreeNode )o1 ).getLabel().compareToIgnoreCase(
342               ( ( FoldingTreeNode )o2 ).getLabel() );
343         }
344       } );
345     }
346   }
347
348   // properties ///////////////////////////////////////////////////////////////
349

350   // attributes ///////////////////////////////////////////////////////////////
351
}
352
Popular Tags