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

Last change on this file since 6656 was 6656, checked in by jmt12, 20 years ago

Unfortunately my quick-fix to the WGet not updating FileNodes problem doesn't work. It in fact causes several NPE's when attempting to copy files regularily - the problem being that the getNode() method calls unmap() on a different thread than that which calls map(), and so you often get both at the same time. More-over at east one of the thread is from the AWT Event thread so placing synchronization other the two problematic methods causes GUI deadlock. Solution - remove unmap() from getNode(). Result - WGet won't properly update workspace.

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