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

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

Debug comments while trying to find 203B240

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