Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/gui/imprt/ImportTree.java


1   /*
2    * ImportTree.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.gui.imprt;
12  
13  import java.util.ArrayList;
14  import java.util.StringTokenizer;
15  
16  import com.flexstor.common.awt.tree.DefaultTreeImages;
17  import com.flexstor.common.awt.tree.Tree;
18  import com.flexstor.common.importprocessor.ImportExclude;
19  import com.flexstor.common.io.FileAccessProtocol;
20  import com.flexstor.common.io.xfile.FlexXFile;
21  import com.flexstor.common.settings.Settings;
22  
23  /**
24   * Import Tree for Import Frame.
25   * @author Praveen Jani
26   * @see com.flexstor.common.awt.tree.Tree
27   */
28  
29  
30  public class ImportTree extends Tree
31  {
32    protected ImportTreeNode root           = null;
33    public  static final int LEFT             = 0;
34    public  static final int RIGHT            = 1;
35    public  static String SEPARATOR           = System.getProperty ("file.separator");
36    private int treeType                      = -1;
37    private String path                       = null;
38    private boolean       toClassify          = false;
39    protected ImportExclude importExclude   = null;
40    private DefaultTreeImages defaultImages = new DefaultTreeImages();
41    private java.awt.Image FILE_IMAGE         = defaultImages.getFileImage();
42  
43    public ImportTree( String rootPath, int treeType, boolean toClassify )
44    {
45      path = rootPath;
46      path = fixPath ( path );
47      setAsRoot ( path );
48      this.treeType   = treeType;
49      this.toClassify = toClassify;
50    }
51    // public functions
52  
53    /**
54     * Clears the tree.
55     */
56    public  void clear()
57    {
58      if ( root != null )
59       root.removeChildren();
60    }
61    /**
62     * Sets the string path as root.
63     */
64  
65    public void setAsRoot ( String path )
66    {
67      if ( path == null )
68       return;
69  
70      root = new ImportTreeNode ( path, true, false );
71      addRootNode    ( root );
72    }
73    /**
74     * Returns the tree type. LEFT or RIGHT.
75     */
76  
77    public int getTreeType ()
78    {
79      return treeType;
80    }
81    /**
82     * Initializes the tree. Browses the directory structure. The initial path is
83     * the top level dir.
84     */
85  
86    public void init ()
87    {
88        if  ( path == null )
89        {
90          return;
91        }
92  
93        // clear the tree first 
94        clear();
95  
96        importExclude = new ImportExclude ( getExcludePath(), true );
97        FlexXFile file   = new FlexXFile( path );
98  
99        String[] paths = null;
100 
101       if ( file.exists() )
102         paths = file.list();
103 
104       if ( paths != null )
105       {
106         FlexXFile cFile   = null;
107         String  absFile = null;
108         for ( int i=0; i<paths.length; i++ )
109         {
110           absFile = file.getAbsolutePath() + paths[ i ];
111           cFile   = new FlexXFile( absFile );
112           if ( cFile.isDirectory() )
113           {
114             addDirectory ( cFile, root );
115           }
116           else
117           {
118             addFile ( cFile, root );
119           }
120         }
121       }
122   }
123   /**
124    * Adds a new node. Creates the dir structure if not found if bCreateStructureIfNotFound
125    * is true.
126    */
127 
128   public void addNewNode ( ImportTreeNode newNode, boolean bCreateStructureIfNotFound )
129   {
130     if ( ( newNode == null ) ||
131          ( newNode.getStructure() == null ) )
132     return;
133 
134     java.util.StringTokenizer tokenizer = new java.util.StringTokenizer ( newNode.getStructure(), SEPARATOR );
135 
136     String token = null;
137     ImportTreeNode nodeToFindIn     = root; // starts tracing from the root
138     ImportTreeNode currNode         = null;
139 
140     while ( tokenizer.hasMoreTokens () )
141     {
142       token = tokenizer.nextToken();
143       if ( token != null )
144       {
145         currNode = findNode ( nodeToFindIn, token );
146         if ( ( currNode == null ) && ( bCreateStructureIfNotFound ) ) // not found; create please !
147         {
148           currNode = new ImportTreeNode ( token, false, false );
149           nodeToFindIn.addChild ( currNode, ImportTreeNode.LAST );
150           currNode.setStructure ( nodeToFindIn.getStructure() );
151           currNode.addBranch    ( token );
152         }
153         nodeToFindIn = currNode;
154       }
155     } // for loop ends
156     if ( currNode != null )
157     {
158       currNode.setAlwaysShowPlusMinus ( newNode.getAlwaysShowPlusMinus () );
159       currNode.setImages ( newNode.getExpandedImage(), newNode.getCollapsedImage() );
160     }
161   }
162   /**
163    * Finds and returns a node specified by the label.
164    */
165   public ImportTreeNode findNode ( ImportTreeNode findInNode, String withLabel )
166   {
167      if ( findInNode == null )
168        return null;
169 
170      return findInNode.findNode ( withLabel );
171   }
172   /**
173    * Removes the specified node..
174    */
175 
176   public void removeNode ( ImportTreeNode node )
177   {
178       if ( node == null )
179         return;
180 
181       removeNode ( node.getStructure() );
182 
183   }
184   /**
185    * Removes node specified by the label.
186    */
187 
188   public void removeNode ( String structure )
189   {
190       if ( structure == null )
191         return;
192 
193       StringTokenizer tokenizer = new StringTokenizer ( structure, SEPARATOR );
194 
195       String token = null;
196       ImportTreeNode nodeToFindIn     = root; // starts tracing from the root
197       ImportTreeNode currNode         = null;
198 
199       while ( tokenizer.hasMoreTokens() )
200       {
201         token = tokenizer.nextToken();
202 
203         if ( token != null )
204         {
205           currNode = findNode ( nodeToFindIn, token );
206           if ( currNode != null )
207           {
208              nodeToFindIn = currNode;
209           }
210           else
211           {
212             break;
213           }
214         }
215       }
216       if ( currNode != null )
217       {
218 //        ImportTreeNode parent = ( ImportTreeNode ) currNode.getParent();
219 //        if ( parent != null )
220 //        {
221           currNode.remove();
222 //          if ( ( parent.getChildren() == null ) ||
223 //               ( parent.getChildren().size() == 0 ) )
224 //           parent.remove();
225 //        }
226       }
227   }
228   /**
229    * Returns the initial path.
230    */
231 
232   public String getPath ()
233   {
234     return path;
235   }
236   /**
237    * Sets the initial path.
238    */
239 
240   public void setPath ( String newPath )
241   {
242     path = fixPath ( newPath ) ;
243     setAsRoot ( path );
244   }
245   /**
246    * Returns all the files. All the leaves representing the files (not folders).
247    */
248 
249   public ArrayList getAllFiles ()
250   {
251     ArrayList list = new ArrayList ();
252     if ( root != null )
253     {
254       ArrayList leaves = root.getLeafNodes();
255       if ( leaves != null )
256       {
257         ImportTreeNode node = null;
258         String           stru = null;
259         for ( int i=0; i<leaves.size(); i++ )
260         {
261           node = ( ImportTreeNode )leaves.get ( i );
262           stru = node.getStructure();
263           if ( ( node == null )    ||
264                ( stru == null )    ||
265                ( isFolder( path + stru ) ) )
266           {
267             continue;
268           }
269 
270           if ( stru.endsWith ("/") || stru.endsWith ("\\") )
271           {
272             stru = new String ( stru.substring ( 0, stru.length() - 1 ) );
273           }
274           list.add ( path + stru );
275         }
276       }
277     }
278     return list;
279   }
280   /**
281    * Checks is there are any other nodes in the tree than the root.
282    */
283 
284   public boolean hasElements ()
285   {
286     if ( root != null )
287       return ( root.getChildren().size() > 0 );
288 
289     return false;
290   }
291   /**
292    * Returns all the childs of the root. This function can also be called when
293    * you need all the dirs and files listed on the tree.
294    */
295   public ArrayList getRootChildren ()
296   {
297     if ( root != null )
298       return root.getAllChildren ();
299 
300     return null;  
301   }
302   // private functions
303   /**
304    * Fixes the path for file SEPARATOR.
305    */
306 
307   private String  fixPath ( String location )
308   {
309     String strToReturn = null;
310     if ( ( location != null ) &&
311          ( location.trim().length() != 0 ) )
312     {
313       if ( !isLocalPath( location ) )
314       {
315          SEPARATOR = "/";
316       }
317       else
318       {
319          SEPARATOR = System.getProperty ("file.separator");
320       }
321       if ( !location.endsWith ( SEPARATOR ) )
322       {
323         strToReturn =  new String ( location + SEPARATOR );
324       }
325       else
326       {
327         strToReturn =  new String ( location );
328       }
329     }
330     return strToReturn;
331   }
332   /**
333    * Adds a file. Also checks for exclude.
334    */
335   private void addFile ( FlexXFile file, ImportTreeNode node )
336   {
337     if ( ( file == null ) || ( node == null ) )
338       return;
339 
340     String absFile = file.getAbsolutePath();
341 
342     if ( importExclude.isExcludeFile  ( absFile ) )
343     {
344       return;
345     }
346 
347     ImportTreeNode cNode = new ImportTreeNode( file.getName(), false, false );
348     node.addChild  ( cNode, ImportTreeNode.LAST );
349     cNode.setStructure ( node.getStructure() );
350     cNode.addBranch ( file.getName() );
351     cNode.setAlwaysShowPlusMinus( false );
352     cNode.setImages( FILE_IMAGE, FILE_IMAGE );
353   }  
354 
355   /**
356    * Adds a dir. Also checks for exclude.
357    */
358 
359   private void addDirectory ( FlexXFile file, ImportTreeNode node )
360   {
361     if ( ( file == null ) || ( node == null ) )
362       return;
363 
364 
365     String absFile = file.getAbsolutePath();
366 
367     // check if the dir/file is excludable.
368     if ( importExclude.isExcludeFile  ( absFile ) )
369     {
370       return;
371     }
372 
373     ImportTreeNode dirNode = new ImportTreeNode( file.getName(), false, false );
374     node.addChild        ( dirNode, ImportTreeNode.LAST );
375     dirNode.setStructure ( node.getStructure() );
376     dirNode.addBranch    ( file.getName() );
377 
378     String[] files   = null;
379 
380     if ( file.exists() )
381     {
382       files   = file.list ();
383     }
384 
385     if ( files != null )
386     {
387       FlexXFile  cFile   = null;
388       for ( int i=0; i<files.length; i++ )
389       {
390          absFile = fixPath ( file.getAbsolutePath() ) + files[ i ];
391          cFile   = new FlexXFile ( absFile );
392 
393          if ( cFile.isFile() )
394          {
395            addFile ( cFile, dirNode );
396          }
397          else
398          {
399            addDirectory ( cFile, dirNode );
400          }
401       }
402     }
403   }
404   /**
405    * Checks if the path is local.
406    */
407   private boolean isLocalPath( String loc )
408   {
409      String[] protocols = FileAccessProtocol.getRemoteProtocolsNames ();
410      for ( int i=0; i<protocols.length; i++ )
411      {
412        if ( loc.startsWith ( protocols[ i ] ) )
413          return false;
414      }
415      return true;
416   }
417   /**
418    * Checks if the string path represents a file or dir.
419    */
420 
421   private boolean isFolder( String file )
422   {
423     return ( ( new FlexXFile ( file ) ).isDirectory() );
424   }
425   /**
426    * Return the full path for exclude.txt.
427    */
428 
429   private String getExcludePath ()
430   {
431     String configDir = Settings.getString ( Settings.CONFIG_DIRECTORY );
432     return configDir+"exclude.txt";
433   }
434 }
435