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

Last change on this file since 5785 was 5785, checked in by mdewsnip, 21 years ago

Commented out about 60 unused functions.

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