View Javadoc

1   package net.sf.bse.gui;
2   
3   /*
4    * Copyright (c) 2002-2003 BSE project contributors 
5    * (http://bse.sourceforge.net/)
6    * 
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  
26  import java.io.File;
27  import java.util.Enumeration;
28  import java.util.Hashtable;
29  import java.util.Vector;
30  
31  import javax.swing.event.TreeModelEvent;
32  import javax.swing.event.TreeModelListener;
33  import javax.swing.tree.TreeModel;
34  import javax.swing.tree.TreePath;
35  import javax.swing.tree.TreeSelectionModel;
36  
37  /***
38   * @author Aleksi Peebles (aleksi.peebles@infocast.fi)
39   * @version $Revision: 1.1 $ $Date: 2003/01/17 14:40:31 $
40   */
41  public class XletTreeModel implements TreeModel
42  {
43      private File root;
44      private Vector listeners;
45      private Hashtable signFiles;
46      private boolean recursive;
47      
48      public XletTreeModel()
49      {
50          this(new File(System.getProperty("user.dir")));
51      }
52      
53      public XletTreeModel(File root)
54      {
55          this.root = root;
56          listeners = new Vector();
57          signFiles = new Hashtable();
58          recursive = true;
59      }
60      
61      //
62      // Public methods
63      //
64      
65      public void setRoot(File root)
66      {
67          this.root = root;
68          signFiles.clear();
69          for (Enumeration e = listeners.elements(); e.hasMoreElements();)
70          {
71              ((TreeModelListener)e.nextElement()).treeStructureChanged(
72                  new TreeModelEvent(this, new File[] { root }));
73          }        
74      }
75      
76      public void setRecursiveSelection(boolean recursive)
77      {
78          this.recursive = recursive;
79      }
80      
81      public boolean isRecursiveSelection()
82      {
83          return recursive;
84      }
85      
86      public String getAlgorithm(File file)
87      {
88          return (String)signFiles.get(file);
89      }
90      
91      public String[] getSignFileNames()
92      {
93          String[] fileNames = new String[signFiles.size()];
94          Enumeration e = signFiles.keys();
95          int i = 0;
96          while (e.hasMoreElements())
97          {
98              fileNames[i] = ((File)e.nextElement()).getAbsolutePath();
99              i++;
100         }
101         return fileNames;
102     }
103     
104     public void setSigned(TreeSelectionModel selection, String algorithm)
105     {
106         TreePath[] paths = selection.getSelectionPaths();
107         for (int i = 0; i < paths.length; i++)
108         {
109             File f = (File)paths[i].getLastPathComponent();
110             if (f.isFile())
111             {
112                 signFiles.put(f, algorithm);
113             }
114             else if (f.isDirectory() && recursive)
115             {
116                 setSignedDirectory(f, algorithm);                
117             }
118         }
119         fileTreeNodesChangedEvent();
120     }
121     
122     public void setUnsigned(TreeSelectionModel selection)
123     {
124         TreePath[] paths = selection.getSelectionPaths();
125         for (int i = 0; i < paths.length; i++)
126         {
127             File f = (File)paths[i].getLastPathComponent();            
128             if (f.isFile())
129             {
130                 signFiles.remove(f);
131             }
132             else if (f.isDirectory() && recursive)
133             {
134                 setUnsignedDirectory(f);
135             }
136         }
137         fileTreeNodesChangedEvent();
138     }
139     
140     //
141     // TreeModel interface methods
142     //
143     
144     public void addTreeModelListener(TreeModelListener l)
145     {
146         listeners.add(l);
147     }
148     
149     public Object getChild(Object parent, int index)
150     {
151         File[] files = ((File)parent).listFiles();
152         if (index > -1 && index < files.length)
153         {
154             return files[index];
155         }
156         else
157         {
158             return null;
159         }
160     }
161     
162     public int getChildCount(Object parent)
163     {
164         if (((File)parent).isDirectory())
165         {
166             return ((File)parent).list().length;
167         }
168         else
169         {
170             return 0;
171         }
172     }
173     
174     public int getIndexOfChild(Object parent, Object child)
175     {
176         if (parent == null || child == null)
177         {
178             return -1;
179         }
180         
181         File[] files = ((File)parent).listFiles();
182         for (int i = 0; i < files.length; i++)
183         {
184             if (files[i] == child)
185             {
186                 return i;
187             }
188         }
189         return -1;
190     }
191     
192     public Object getRoot()
193     {
194         return root;
195     }
196     
197     public boolean isLeaf(Object node)
198     {
199         return !((File)node).isDirectory();
200     }
201     
202     public void removeTreeModelListener(TreeModelListener l)
203     {
204         listeners.remove(l);
205     }
206     
207     public void valueForPathChanged(TreePath path, Object newValue)
208     {
209     }    
210 
211     //
212     // Private methods
213     //
214     
215     private void setSignedDirectory(File dir, String algorithm)
216     {
217         File[] files = dir.listFiles();
218         for (int i = 0; i < files.length; i++)
219         {
220             if (files[i].isDirectory())
221             {
222                 setSignedDirectory(files[i], algorithm);
223             }
224             else
225             {
226                 signFiles.put(files[i], algorithm);
227             }
228         }
229     }
230     
231     private void setUnsignedDirectory(File dir)
232     {
233         File[] files = dir.listFiles();
234         for (int i = 0; i < files.length; i++)
235         {
236             if (files[i].isDirectory())
237             {
238                 setUnsignedDirectory(files[i]);
239             }
240             else
241             {
242                 signFiles.remove(files[i]);
243             }
244         }
245     }
246     
247     private void fileTreeNodesChangedEvent()
248     {
249         for (Enumeration e = listeners.elements(); e.hasMoreElements();)
250         {
251             ((TreeModelListener)e.nextElement()).treeNodesChanged(
252                 new TreeModelEvent(this, new File[] { root }));
253         }        
254     }    
255 }