source: trunk/gli/src/org/greenstone/gatherer/file/FileSystemModel.java@ 5153

Last change on this file since 5153 was 5057, checked in by jmt12, 21 years ago

Patch for Mike Jensen.

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