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.Gatherer;
|
---|
9 | import org.greenstone.gatherer.file.FileFilter;
|
---|
10 | import org.greenstone.gatherer.file.FileNode;
|
---|
11 | import org.greenstone.gatherer.file.FileOpenActionListener;
|
---|
12 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
13 | import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
|
---|
14 |
|
---|
15 | public class FileSystemModel
|
---|
16 | extends DefaultTreeModel
|
---|
17 | implements TreeExpansionListener, TreeWillExpandListener {
|
---|
18 |
|
---|
19 | private DragTree tree;
|
---|
20 | private FileFilter current_filter;
|
---|
21 | private FileFilter[] filters;
|
---|
22 | static private FileFilter[] default_filters = { new FileFilter("\\..*", true), new FileFilter("metadata\\.xml", true) };
|
---|
23 |
|
---|
24 | public FileSystemModel(FileNode root) {
|
---|
25 | super(root);
|
---|
26 | current_filter = null;
|
---|
27 | filters = null;
|
---|
28 | tree = null;
|
---|
29 | root.setModel(this);
|
---|
30 | root.map();
|
---|
31 | }
|
---|
32 |
|
---|
33 | public FileFilter[] getFilters() {
|
---|
34 | if(filters == null) {
|
---|
35 | if(current_filter != null) {
|
---|
36 | filters = new FileFilter[default_filters.length + 1];
|
---|
37 | filters[default_filters.length] = current_filter;
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | filters = new FileFilter[default_filters.length];
|
---|
41 | }
|
---|
42 | System.arraycopy(default_filters, 0, filters, 0, default_filters.length);
|
---|
43 | }
|
---|
44 | return filters;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /** 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. */
|
---|
48 | public FileNode getNode(TreePath path) {
|
---|
49 | ///ystem.err.println("**** getNode(" + path + ") ****");
|
---|
50 | FileNode current = (FileNode)root;
|
---|
51 | // Special case for the root node. Check the first path component is the root node.
|
---|
52 | FileNode first_node = (FileNode)path.getPathComponent(0);
|
---|
53 | if(current.equals(first_node)) {
|
---|
54 | ///ystem.err.println("First path component matches root node.");
|
---|
55 | // For each path with this tree path
|
---|
56 | for(int i = 1; current != null && i < path.getPathCount(); i++) {
|
---|
57 | // Retrieve the stale path
|
---|
58 | FileNode stale_node = (FileNode) path.getPathComponent(i);
|
---|
59 | ///ystem.err.print("Searching for '" + stale_node + "': ");
|
---|
60 | // Locate the fresh node by searching current's children. Remember to ensure that current is mapped.
|
---|
61 | current.map();
|
---|
62 | boolean found = false;
|
---|
63 | for(int j = 0; !found && j < current.getChildCount(); j++) {
|
---|
64 | FileNode child_node = (FileNode) current.getChildAt(j);
|
---|
65 | ///ystem.err.print(child_node + " ");
|
---|
66 | if(stale_node.equals(child_node)) {
|
---|
67 | found = true;
|
---|
68 | current = child_node;
|
---|
69 | ///ystem.err.println("Found!");
|
---|
70 | }
|
---|
71 | child_node = null;
|
---|
72 | }
|
---|
73 | // If no match is found, then set current to null and exit.
|
---|
74 | if(!found) {
|
---|
75 | current = null;
|
---|
76 | ///ystem.err.println("Not Found!");
|
---|
77 | }
|
---|
78 | // Repeat as necessary
|
---|
79 | }
|
---|
80 | }
|
---|
81 | ///ystem.err.println("Returning node: " + new TreePath(current.getPath()));
|
---|
82 | return current;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) {
|
---|
86 | ///ystem.err.println("insertNodeInto(" + newChild + ", " + parent + ", " + index + ")");
|
---|
87 | super.insertNodeInto(newChild, parent, index);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void mapDirectory(File directory, String title) {
|
---|
91 | FileNode node = new FileNode(directory, this, title, true);
|
---|
92 | SynchronizedTreeModelTools.insertNodeInto(this, (FileNode)root, node);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** 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).
|
---|
96 | * @param path The TreePath to the node to be refreshed.
|
---|
97 | */
|
---|
98 | public void refresh(TreePath path) {
|
---|
99 | // If no path is set, take the path to the root node (ie update the whole tree)
|
---|
100 | if(path == null) {
|
---|
101 | ///ystem.err.println("Refresh entire tree.");
|
---|
102 | path = new TreePath(((FileNode)root).getPath());
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | ///ystem.err.println("Refresh: " + path.getLastPathComponent());
|
---|
106 | }
|
---|
107 | // Only a valid action if this model is currently being displayed in a tree.
|
---|
108 | if(tree != null) {
|
---|
109 | // Retrieve the error node.
|
---|
110 | FileNode node = (FileNode) path.getLastPathComponent();
|
---|
111 | // 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.
|
---|
112 | if(node.getFile() == null) {
|
---|
113 | for(int i = 0; i < node.getChildCount(); i++) {
|
---|
114 | FileNode child = (FileNode) node.getChildAt(i);
|
---|
115 | refresh(new TreePath(child.getPath()));
|
---|
116 | child = null;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | // Otherwise we refresh this node.
|
---|
120 | else {
|
---|
121 | // Record all of the expanded paths under this node. How come getExpandedDescendants returns more results each time.
|
---|
122 | Enumeration old_tree_paths = tree.getExpandedDescendants(path);
|
---|
123 | // Refresh the tree structure.
|
---|
124 | node.unmap();
|
---|
125 | node.map();
|
---|
126 | // Fire the appropriate event.
|
---|
127 | nodeStructureChanged(node);
|
---|
128 | // 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.
|
---|
129 | //counter = 0;
|
---|
130 | ///ystem.err.println("After:");
|
---|
131 | while(old_tree_paths != null && old_tree_paths.hasMoreElements()) {
|
---|
132 | //counter++;
|
---|
133 | FileNode current_node = node;
|
---|
134 | TreePath old_tree_path = (TreePath) old_tree_paths.nextElement();
|
---|
135 | ///ystem.err.println(counter + ". " + old_tree_path);
|
---|
136 | TreePath new_tree_path = new TreePath(path.getPath()); // Why isn't there a treepath copy constructor!
|
---|
137 | boolean not_found = false;
|
---|
138 | while(!not_found && old_tree_path.getPathCount() > new_tree_path.getPathCount()) {
|
---|
139 | // Retrieve the new node in the old tree path
|
---|
140 | FileNode old_path_node = (FileNode) old_tree_path.getPathComponent(new_tree_path.getPathCount());
|
---|
141 | // Ensure the current node is mapped
|
---|
142 | current_node.map();
|
---|
143 | // Now attempt to match it to a child of the new paths last node.
|
---|
144 | boolean found = false;
|
---|
145 | for(int i = 0; !found && i < current_node.getChildCount(); i++) {
|
---|
146 | FileNode target = (FileNode) current_node.getChildAt(i);
|
---|
147 | ///ystem.err.println("Comparing " + old_path_node.toString() + " with " + target);
|
---|
148 | if(target.toString().equals(old_path_node.toString())) {
|
---|
149 | current_node = target;
|
---|
150 | found = true;
|
---|
151 | }
|
---|
152 | target = null;
|
---|
153 | }
|
---|
154 | old_path_node = null;
|
---|
155 | // If we found a match, we add that to the new Tree Path and continue.
|
---|
156 | if(found) {
|
---|
157 | ///ystem.err.println("Found a match!");
|
---|
158 | new_tree_path = new_tree_path.pathByAddingChild(current_node);
|
---|
159 | }
|
---|
160 | // We also have to record if we were unable to find a match in the current nodes children.
|
---|
161 | else {
|
---|
162 | ///ystem.err.println("Node not found.");
|
---|
163 | not_found = true;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | old_tree_path = null;
|
---|
167 | // 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
|
---|
168 | if(!not_found) {
|
---|
169 | tree.expandPath(new_tree_path);
|
---|
170 | ///ystem.err.println(" -> Expanded " + new_tree_path);
|
---|
171 | }
|
---|
172 | else {
|
---|
173 | ///ystem.err.println(" -> Cannot Expand " + new_tree_path);
|
---|
174 | }
|
---|
175 | new_tree_path = null;
|
---|
176 | current_node = null;
|
---|
177 | }
|
---|
178 | node = null;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | else {
|
---|
182 | ///ystem.err.println("No Tree!");
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void setFilter(String pattern) {
|
---|
187 | if(pattern != null) {
|
---|
188 | current_filter = new FileFilter(pattern, false);
|
---|
189 | }
|
---|
190 | else {
|
---|
191 | current_filter = null;
|
---|
192 | }
|
---|
193 | filters = null;
|
---|
194 | }
|
---|
195 |
|
---|
196 | public void setTree(DragTree tree) {
|
---|
197 | this.tree = tree;
|
---|
198 | }
|
---|
199 |
|
---|
200 | public String toString() {
|
---|
201 | if(tree != null) {
|
---|
202 | return tree.toString();
|
---|
203 | }
|
---|
204 | return "FileSystemModel";
|
---|
205 | }
|
---|
206 |
|
---|
207 | /** Called whenever an item in the tree has been collapsed. */
|
---|
208 | public void treeCollapsed(TreeExpansionEvent event) {
|
---|
209 | // Deallocate the affected nodes children. Don't need to do this in a swing worker, as the nodes children are currently not visable.
|
---|
210 | TreePath path = event.getPath();
|
---|
211 | FileNode node = (FileNode) path.getLastPathComponent();
|
---|
212 | node.unmap();
|
---|
213 | // Fire the appropriate event.
|
---|
214 | nodeStructureChanged(node);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Called whenever an item in the tree has been expanded. */
|
---|
218 | public void treeExpanded(TreeExpansionEvent event) {
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** Invoked whenever a node in the tree is about to be collapsed. */
|
---|
222 | public void treeWillCollapse(TreeExpansionEvent event)
|
---|
223 | throws ExpandVetoException {
|
---|
224 | // Veto the event if the user is attempting to collapse the root node (regardless of whether it is visible.
|
---|
225 | TreePath path = event.getPath();
|
---|
226 | if(path.getPathCount() == 1) {
|
---|
227 | throw new ExpandVetoException(event, "Cannot collapse root node!");
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | /** Invoked whenever a node in the tree is about to be expanded. */
|
---|
232 | public void treeWillExpand(TreeExpansionEvent event)
|
---|
233 | throws ExpandVetoException {
|
---|
234 | // Set the wait cursor.
|
---|
235 | Gatherer.g_man.wait(true);
|
---|
236 | // Allocate the children. Don't need to do this in a swing worker, as the nodes children are currently not visable.
|
---|
237 | TreePath path = event.getPath();
|
---|
238 | FileNode node = (FileNode) path.getLastPathComponent();
|
---|
239 | node.map();
|
---|
240 | nodeStructureChanged(node);
|
---|
241 | // Restore the cursor.
|
---|
242 | Gatherer.g_man.wait(false);
|
---|
243 | }
|
---|
244 | }
|
---|