1 | package org.greenstone.gatherer.file;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.*;
|
---|
5 | import java.util.regex.*;
|
---|
6 | import javax.swing.event.*;
|
---|
7 | import javax.swing.tree.*;
|
---|
8 | import org.greenstone.gatherer.Dictionary;
|
---|
9 | import org.greenstone.gatherer.Gatherer;
|
---|
10 | import org.greenstone.gatherer.file.FileFilter;
|
---|
11 | import org.greenstone.gatherer.file.FileNode;
|
---|
12 | import org.greenstone.gatherer.file.FileOpenActionListener;
|
---|
13 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
14 | import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
|
---|
15 |
|
---|
16 | public class FileSystemModel
|
---|
17 | extends DefaultTreeModel
|
---|
18 | implements TreeExpansionListener, TreeWillExpandListener {
|
---|
19 |
|
---|
20 | private int counter = 0;
|
---|
21 | private DragTree tree;
|
---|
22 | private FileFilter current_filter;
|
---|
23 | private FileFilter[] filters;
|
---|
24 | /** The filters in place for any file system model. */
|
---|
25 | private FileFilter[] default_filters = { new FileFilter("\\..*", true), new FileFilter("metadata\\.xml", true) };
|
---|
26 |
|
---|
27 | public FileSystemModel(FileNode root) {
|
---|
28 | super(root);
|
---|
29 | current_filter = null;
|
---|
30 | filters = null;
|
---|
31 | tree = null;
|
---|
32 | root.setModel(this);
|
---|
33 | root.map();
|
---|
34 | }
|
---|
35 |
|
---|
36 | public int getChildCount(Object parent) {
|
---|
37 | return ((TreeNode)parent).getChildCount();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public FileFilter[] getFilters() {
|
---|
41 | if(filters == null) {
|
---|
42 | if(current_filter != null) {
|
---|
43 | filters = new FileFilter[default_filters.length + 1];
|
---|
44 | filters[default_filters.length] = current_filter;
|
---|
45 | }
|
---|
46 | else {
|
---|
47 | filters = new FileFilter[default_filters.length];
|
---|
48 | }
|
---|
49 | System.arraycopy(default_filters, 0, filters, 0, default_filters.length);
|
---|
50 | }
|
---|
51 | return filters;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** Retrieve the node denoted by the given tree path. Note that this isn't equivelent to saying path.lastPathComponent, as the references within the path may be stale. */
|
---|
55 | public FileNode getNode(TreePath path) {
|
---|
56 | ///atherer.println("**** getNode(" + path + ") ****");
|
---|
57 | FileNode current = (FileNode)root;
|
---|
58 | // Special case for the root node. Check the first path component is the root node.
|
---|
59 | FileNode first_node = (FileNode)path.getPathComponent(0);
|
---|
60 | if(current.equals(first_node)) {
|
---|
61 | Gatherer.println("First path component matches root node.");
|
---|
62 | // For each path with this tree path
|
---|
63 | for(int i = 1; current != null && i < path.getPathCount(); i++) {
|
---|
64 | // Retrieve the stale path
|
---|
65 | Object stale_object = path.getPathComponent(i);
|
---|
66 | FileNode stale_node = null;
|
---|
67 | if(stale_object instanceof FileNode) {
|
---|
68 | stale_node = (FileNode) stale_object;
|
---|
69 | }
|
---|
70 | Gatherer.print("Searching for '" + stale_object + "': ");
|
---|
71 | // Locate the fresh node by searching current's children. Remember to ensure that current is mapped.
|
---|
72 | //current.unmap();
|
---|
73 | boolean found = false;
|
---|
74 |
|
---|
75 | // First we search through the mapped children
|
---|
76 | for(int j = 0; !found && j < current.getChildCount(); j++) {
|
---|
77 | FileNode child_node = (FileNode) current.getChildAt(j);
|
---|
78 | Gatherer.print(child_node + " ");
|
---|
79 | if((stale_node != null && stale_node.equals(child_node)) || stale_object.toString().equals(child_node.toString())) {
|
---|
80 | found = true;
|
---|
81 | current = child_node;
|
---|
82 | Gatherer.println("Found!");
|
---|
83 | }
|
---|
84 | child_node = null;
|
---|
85 | }
|
---|
86 | // Failing that we search through all the children, including filtered files
|
---|
87 | for(int j = 0; !found && j < current.size(); j++) {
|
---|
88 | FileNode child_node = (FileNode) current.get(j);
|
---|
89 | Gatherer.print(child_node + " ");
|
---|
90 | if((stale_node != null && stale_node.equals(child_node)) || stale_object.toString().equals(child_node.toString())) {
|
---|
91 | found = true;
|
---|
92 | current = child_node;
|
---|
93 | Gatherer.println("Found!");
|
---|
94 | }
|
---|
95 | child_node = null;
|
---|
96 | }
|
---|
97 | // If no match is found, then set current to null and exit.
|
---|
98 | if(!found) {
|
---|
99 | current = null;
|
---|
100 | Gatherer.println("Not Found!");
|
---|
101 | }
|
---|
102 | else {
|
---|
103 | Gatherer.println("Returning node: " + new TreePath(current.getPath()));
|
---|
104 | }
|
---|
105 | // Repeat as necessary
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return current;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) {
|
---|
112 | ///ystem.err.println("insertNodeInto(" + newChild + ", " + parent + ", " + index + ")");
|
---|
113 | super.insertNodeInto(newChild, parent, index);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void mapDirectory(File directory, String title) {
|
---|
117 | FileNode node = new FileNode(directory, this, title, true);
|
---|
118 | SynchronizedTreeModelTools.insertNodeInto(this, (FileNode)root, node);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | public void refresh(TreePath path)
|
---|
123 | {
|
---|
124 | // If no path is set, take the path to the root node (ie. update the whole tree)
|
---|
125 | if (path == null) {
|
---|
126 | // System.err.println("\nFileSystemModel.refresh(entire tree).");
|
---|
127 | path = new TreePath(((FileNode) root).getPath());
|
---|
128 | }
|
---|
129 | // else {
|
---|
130 | // System.err.println("\nFileSystemModel.refresh(" + path + ").");
|
---|
131 | // }
|
---|
132 |
|
---|
133 | // Can only refresh if the model is currently being displayed in a tree
|
---|
134 | if (tree == null) {
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // Record all the expanded paths under this node
|
---|
139 | Enumeration old_expanded_paths_enumeration = tree.getExpandedDescendants(path);
|
---|
140 | if (old_expanded_paths_enumeration == null) {
|
---|
141 | return;
|
---|
142 | }
|
---|
143 |
|
---|
144 | // Map and unmap the node to refresh its contents
|
---|
145 | FileNode node = (FileNode) path.getLastPathComponent();
|
---|
146 | node.unmap();
|
---|
147 | node.map();
|
---|
148 |
|
---|
149 | // Fire the appropriate event
|
---|
150 | nodeStructureChanged(node);
|
---|
151 |
|
---|
152 | // Sort the old expanded paths by length, smallest first
|
---|
153 | ArrayList old_expanded_paths_list = Collections.list(old_expanded_paths_enumeration);
|
---|
154 | Collections.sort(old_expanded_paths_list, new TreePathComparator());
|
---|
155 |
|
---|
156 | // Restore each of the expanded paths
|
---|
157 | for (int i = 0; i < old_expanded_paths_list.size(); i++) {
|
---|
158 | TreePath old_expanded_path = (TreePath) old_expanded_paths_list.get(i);
|
---|
159 | // System.err.println("Expanded path: " + old_expanded_path);
|
---|
160 |
|
---|
161 | // Build up the new path in the tree
|
---|
162 | TreePath current_path = new TreePath(path.getPath());
|
---|
163 | FileNode current_node = node;
|
---|
164 |
|
---|
165 | // Traverse the tree to find the node to expand (or find it no longer exists)
|
---|
166 | while (!current_path.toString().equals(old_expanded_path.toString())) {
|
---|
167 | // System.err.println("Current path: " + current_path);
|
---|
168 |
|
---|
169 | FileNode old_expanded_node =
|
---|
170 | (FileNode) old_expanded_path.getPathComponent(current_path.getPathCount());
|
---|
171 | // System.err.println("Looking for: " + old_expanded_node);
|
---|
172 |
|
---|
173 | // Find the child node that matches the next element in the path
|
---|
174 | boolean found = false;
|
---|
175 | for (int j = 0; j < current_node.getChildCount(); j++) {
|
---|
176 | FileNode child_node = (FileNode) current_node.getChildAt(j);
|
---|
177 | // System.err.println("Child node: " + child_node);
|
---|
178 | if (child_node.equals(old_expanded_node)) {
|
---|
179 | // System.err.println("Found!");
|
---|
180 | current_path = current_path.pathByAddingChild(child_node);
|
---|
181 | current_node = child_node;
|
---|
182 | found = true;
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | // The node was not found, so we cannot expand this path
|
---|
188 | if (!found) {
|
---|
189 | // System.err.println("Not found...");
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | // If we have built up the correct path, expand it
|
---|
195 | if (current_path.toString().equals(old_expanded_path.toString())) {
|
---|
196 | tree.expandPath(current_path);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | private class TreePathComparator
|
---|
203 | implements Comparator {
|
---|
204 |
|
---|
205 | public int compare(Object o1, Object o2)
|
---|
206 | {
|
---|
207 | return (((TreePath) o1).getPathCount() - ((TreePath) o2).getPathCount());
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | // /** Used to refresh the contents of the FileNode indicated by the tree path. While the refresh itself is a piece of the proverbial, the restoring of expanded folders afterwards is a nightmare, mainly because all of the tree paths are no longer valid (remember that refresh involves throwing away the node currents children and remapping them).
|
---|
213 | // * @param path The TreePath to the node to be refreshed.
|
---|
214 | // */
|
---|
215 | // public void oldRefresh(TreePath path) {
|
---|
216 | // // If no path is set, take the path to the root node (ie update the whole tree)
|
---|
217 | // if(path == null) {
|
---|
218 | // System.err.println("Refresh entire tree.");
|
---|
219 | // path = new TreePath(((FileNode)root).getPath());
|
---|
220 | // }
|
---|
221 | // else {
|
---|
222 | // System.err.println("FileSystemModel.refresh: " + path.getLastPathComponent());
|
---|
223 | // }
|
---|
224 | // // Only a valid action if this model is currently being displayed in a tree.
|
---|
225 | // if(tree != null) {
|
---|
226 | // System.err.println("Refreshing tree " + tree);
|
---|
227 | // // Retrieve the error node.
|
---|
228 | // FileNode node = (FileNode) path.getLastPathComponent();
|
---|
229 | // // If this error node is a dummy node (ie has no associated file) we can't unmap it, so we iterate through its children refreshing each in turn. The exception being Greenstone Collections, as it is a dummy node but we can map/unmap it
|
---|
230 | // if(node.getFile() == null && !node.toString().equals(Dictionary.get("Tree.World"))) {
|
---|
231 | // for(int i = 0; i < node.getChildCount(); i++) {
|
---|
232 | // FileNode child = (FileNode) node.getChildAt(i);
|
---|
233 | // refresh(new TreePath(child.getPath()));
|
---|
234 | // child = null;
|
---|
235 | // }
|
---|
236 | // }
|
---|
237 | // // Otherwise we refresh this node.
|
---|
238 | // else {
|
---|
239 | // // Record all of the expanded paths under this node. How come getExpandedDescendants returns more results each time.
|
---|
240 | // Enumeration old_tree_paths = tree.getExpandedDescendants(path);
|
---|
241 | // // Refresh the tree structure.
|
---|
242 | // node.unmap();
|
---|
243 | // node.map();
|
---|
244 | // // Fire the appropriate event.
|
---|
245 | // nodeStructureChanged(node);
|
---|
246 | // // Then (painfully) restore the expanded paths. Note that the paths stored in the enumeration no longer exist, so I have to restore by node matching.
|
---|
247 | // //counter = 0;
|
---|
248 | // ///ystem.err.println("After:");
|
---|
249 | // while(old_tree_paths != null && old_tree_paths.hasMoreElements()) {
|
---|
250 | // //counter++;
|
---|
251 | // FileNode current_node = node;
|
---|
252 | // TreePath old_tree_path = (TreePath) old_tree_paths.nextElement();
|
---|
253 | // ///ystem.err.println(counter + ". " + old_tree_path);
|
---|
254 | // TreePath new_tree_path = new TreePath(path.getPath()); // Why isn't there a treepath copy constructor!
|
---|
255 | // boolean not_found = false;
|
---|
256 | // while(!not_found && old_tree_path.getPathCount() > new_tree_path.getPathCount()) {
|
---|
257 | // // Retrieve the new node in the old tree path
|
---|
258 | // FileNode old_path_node = (FileNode) old_tree_path.getPathComponent(new_tree_path.getPathCount());
|
---|
259 | // // Ensure the current node is mapped
|
---|
260 | // current_node.map();
|
---|
261 | // // Now attempt to match it to a child of the new paths last node.
|
---|
262 | // boolean found = false;
|
---|
263 | // for(int i = 0; !found && i < current_node.getChildCount(); i++) {
|
---|
264 | // FileNode target = (FileNode) current_node.getChildAt(i);
|
---|
265 | // ///ystem.err.println("Comparing " + old_path_node.toString() + " with " + target);
|
---|
266 | // if(target.toString().equals(old_path_node.toString())) {
|
---|
267 | // current_node = target;
|
---|
268 | // found = true;
|
---|
269 | // }
|
---|
270 | // target = null;
|
---|
271 | // }
|
---|
272 | // old_path_node = null;
|
---|
273 | // // If we found a match, we add that to the new Tree Path and continue.
|
---|
274 | // if(found) {
|
---|
275 | // ///ystem.err.println("Found a match!");
|
---|
276 | // new_tree_path = new_tree_path.pathByAddingChild(current_node);
|
---|
277 | // }
|
---|
278 | // // We also have to record if we were unable to find a match in the current nodes children.
|
---|
279 | // else {
|
---|
280 | // ///ystem.err.println("Node not found.");
|
---|
281 | // not_found = true;
|
---|
282 | // }
|
---|
283 | // }
|
---|
284 | // old_tree_path = null;
|
---|
285 | // // If we've got this far, and haven't hit a not found, then we can assume we have the appropriate path, and ask the program to expand the new tree path
|
---|
286 | // if(!not_found) {
|
---|
287 | // tree.expandPath(new_tree_path);
|
---|
288 | // ///ystem.err.println(" -> Expanded " + new_tree_path);
|
---|
289 | // }
|
---|
290 | // else {
|
---|
291 | // ///ystem.err.println(" -> Cannot Expand " + new_tree_path);
|
---|
292 | // }
|
---|
293 | // new_tree_path = null;
|
---|
294 | // current_node = null;
|
---|
295 | // }
|
---|
296 | // node = null;
|
---|
297 | // }
|
---|
298 | // }
|
---|
299 | // else {
|
---|
300 | // ///ystem.err.println("No Tree!");
|
---|
301 | // }
|
---|
302 | // }
|
---|
303 |
|
---|
304 | public void setFilter(String pattern) {
|
---|
305 | if(pattern != null) {
|
---|
306 | current_filter = new FileFilter(pattern, false);
|
---|
307 | }
|
---|
308 | else {
|
---|
309 | current_filter = null;
|
---|
310 | }
|
---|
311 | filters = null;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /* private void setPermanentFilter(String pattern) {
|
---|
315 | if(pattern != null) {
|
---|
316 | FileFilter new_filter = new FileFilter(pattern, false);
|
---|
317 | FileFilter temp[] = new FileFilter[default_filters.length + 1];
|
---|
318 | System.arraycopy(default_filters, 0, temp, 0, default_filters.length);
|
---|
319 | temp[default_filters.length] = new_filter;
|
---|
320 | default_filters = temp;
|
---|
321 | temp = null;
|
---|
322 | }
|
---|
323 | filters = null;
|
---|
324 | } */
|
---|
325 |
|
---|
326 | public void setTree(DragTree tree) {
|
---|
327 | this.tree = tree;
|
---|
328 | }
|
---|
329 |
|
---|
330 | public String toString() {
|
---|
331 | if(tree != null) {
|
---|
332 | return tree.toString();
|
---|
333 | }
|
---|
334 | return "FileSystemModel";
|
---|
335 | }
|
---|
336 |
|
---|
337 | /** Called whenever an item in the tree has been collapsed. */
|
---|
338 | public void treeCollapsed(TreeExpansionEvent event) {
|
---|
339 | // Deallocate the affected nodes children. Don't need to do this in a swing worker, as the nodes children are currently not visable.
|
---|
340 | TreePath path = event.getPath();
|
---|
341 | FileNode node = (FileNode) path.getLastPathComponent();
|
---|
342 | ///ystem.err.println("Unmap: " + node);
|
---|
343 | node.unmap();
|
---|
344 | // Fire the appropriate event.
|
---|
345 | nodeStructureChanged(node);
|
---|
346 | }
|
---|
347 |
|
---|
348 | /** Called whenever an item in the tree has been expanded. */
|
---|
349 | public void treeExpanded(TreeExpansionEvent event) {
|
---|
350 | }
|
---|
351 |
|
---|
352 | /** Invoked whenever a node in the tree is about to be collapsed. */
|
---|
353 | public void treeWillCollapse(TreeExpansionEvent event)
|
---|
354 | throws ExpandVetoException {
|
---|
355 | // Veto the event if the user is attempting to collapse the root node (regardless of whether it is visible).
|
---|
356 | TreePath path = event.getPath();
|
---|
357 | if(path.getPathCount() == 1) {
|
---|
358 | throw new ExpandVetoException(event, "Cannot collapse root node!");
|
---|
359 | }
|
---|
360 | }
|
---|
361 |
|
---|
362 | /** Invoked whenever a node in the tree is about to be expanded. */
|
---|
363 | public void treeWillExpand(TreeExpansionEvent event)
|
---|
364 | throws ExpandVetoException {
|
---|
365 | // Set the wait cursor.
|
---|
366 | Gatherer.g_man.wait(true);
|
---|
367 | // Allocate the children. Don't need to do this in a swing worker, as the nodes children are currently not visable.
|
---|
368 | TreePath path = event.getPath();
|
---|
369 | FileNode node = (FileNode) path.getLastPathComponent();
|
---|
370 | ///ystem.err.println("Mapping: " + node);
|
---|
371 | node.map();
|
---|
372 | ///ystem.err.println(" -> node has " + node.getChildCount() + " children");
|
---|
373 | nodeStructureChanged(node);
|
---|
374 | // Restore the cursor.
|
---|
375 | Gatherer.g_man.wait(false);
|
---|
376 | }
|
---|
377 | }
|
---|