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

Last change on this file since 8783 was 8783, checked in by mdewsnip, 19 years ago

Big tidy up of the workspace and collection trees (Gather/Enrich panes). Still a few more changes and bug fixes to come.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.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.DebugStream;
9import org.greenstone.gatherer.Gatherer;
10import org.greenstone.gatherer.gui.tree.DragTree;
11import org.greenstone.gatherer.util.SynchronizedTreeModelTools;
12
13public class FileSystemModel
14 extends DefaultTreeModel
15 implements TreeExpansionListener, TreeWillExpandListener
16{
17 private DragTree tree = null;
18 private FileFilter current_filter = null;
19 private FileFilter[] filters = null;
20 /** The filters in place for any file system model. */
21 private FileFilter[] default_filters = { new FileFilter("\\..*", true), new FileFilter("metadata\\.xml", true) };
22
23 public FileSystemModel(FileNode root) {
24 super(root);
25 root.setModel(this);
26 root.map();
27 }
28
29 public int getChildCount(Object parent) {
30 return ((TreeNode)parent).getChildCount();
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 ///atherer.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 DebugStream.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 Object stale_object = path.getPathComponent(i);
59 FileNode stale_node = null;
60 if(stale_object instanceof FileNode) {
61 stale_node = (FileNode) stale_object;
62 }
63 DebugStream.print("Searching for '" + stale_object + "': ");
64 // Locate the fresh node by searching current's children. Remember to ensure that current is mapped.
65 boolean found = false;
66
67 // First we search through the mapped children
68 for(int j = 0; !found && j < current.getChildCount(); j++) {
69 FileNode child_node = (FileNode) current.getChildAt(j);
70 DebugStream.print(child_node + " ");
71 if((stale_node != null && stale_node.equals(child_node)) || stale_object.toString().equals(child_node.toString())) {
72 found = true;
73 current = child_node;
74 DebugStream.println("Found!");
75 }
76 child_node = null;
77 }
78 // Failing that we search through all the children, including filtered files
79 for(int j = 0; !found && j < current.size(); j++) {
80 FileNode child_node = (FileNode) current.getChildAtUnfiltered(j);
81 DebugStream.print(child_node + " ");
82 if((stale_node != null && stale_node.equals(child_node)) || stale_object.toString().equals(child_node.toString())) {
83 found = true;
84 current = child_node;
85 DebugStream.println("Found!");
86 }
87 child_node = null;
88 }
89 // If no match is found, then set current to null and exit.
90 if(!found) {
91 current = null;
92 DebugStream.println("Not Found!");
93 }
94 else {
95 DebugStream.println("Returning node: " + new TreePath(current.getPath()));
96 }
97 // Repeat as necessary
98 }
99 }
100 return current;
101 }
102
103 public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) {
104 ///ystem.err.println("insertNodeInto(" + newChild + ", " + parent + ", " + index + ")");
105 super.insertNodeInto(newChild, parent, index);
106 }
107
108
109 public void refresh(TreePath path)
110 {
111 // Can only refresh if the model is currently being displayed in a tree
112 if (tree == null) {
113 return;
114 }
115
116 // If no path is set, take the path to the root node (ie. update the whole tree)
117 if (path == null) {
118 // System.err.println("\nFileSystemModel.refresh(entire tree).");
119 path = new TreePath(((FileNode) root).getPath());
120
121 // Make sure the root node is expanded
122 tree.expandPath(path);
123 }
124 // else {
125 // System.err.println("\nFileSystemModel.refresh(" + path + ").");
126 // }
127
128 // Record all the expanded paths under this node
129 Enumeration old_expanded_paths_enumeration = tree.getExpandedDescendants(path);
130 if (old_expanded_paths_enumeration == null) {
131 return;
132 }
133
134 // Map and unmap the node to refresh its contents
135 FileNode node = (FileNode) path.getLastPathComponent();
136 node.refresh();
137
138 // Fire the appropriate event
139 nodeStructureChanged(node);
140
141 // Sort the old expanded paths by length, smallest first
142 ArrayList old_expanded_paths_list = Collections.list(old_expanded_paths_enumeration);
143 Collections.sort(old_expanded_paths_list, new TreePathComparator());
144
145 // Restore each of the expanded paths
146 for (int i = 0; i < old_expanded_paths_list.size(); i++) {
147 TreePath old_expanded_path = (TreePath) old_expanded_paths_list.get(i);
148 // System.err.println("Expanded path: " + old_expanded_path);
149
150 // Build up the new path in the tree
151 TreePath current_path = new TreePath(path.getPath());
152 FileNode current_node = node;
153
154 // Traverse the tree to find the node to expand (or find it no longer exists)
155 while (!current_path.toString().equals(old_expanded_path.toString())) {
156 // System.err.println("Current path: " + current_path);
157
158 FileNode old_expanded_node =
159 (FileNode) old_expanded_path.getPathComponent(current_path.getPathCount());
160 // System.err.println("Looking for: " + old_expanded_node);
161
162 // Find the child node that matches the next element in the path
163 boolean found = false;
164 for (int j = 0; j < current_node.getChildCount(); j++) {
165 FileNode child_node = (FileNode) current_node.getChildAt(j);
166 // System.err.println("Child node: " + child_node);
167 if (child_node.equals(old_expanded_node)) {
168 // System.err.println("Found!");
169 current_path = current_path.pathByAddingChild(child_node);
170 current_node = child_node;
171 found = true;
172 break;
173 }
174 }
175
176 // The node was not found, so we cannot expand this path
177 if (!found) {
178 // System.err.println("Not found...");
179 break;
180 }
181 }
182
183 // If we have built up the correct path, expand it
184 if (current_path.toString().equals(old_expanded_path.toString())) {
185 tree.expandPath(current_path);
186 }
187 }
188 }
189
190
191 private class TreePathComparator
192 implements Comparator {
193
194 public int compare(Object o1, Object o2)
195 {
196 return (((TreePath) o1).getPathCount() - ((TreePath) o2).getPathCount());
197 }
198 }
199
200
201 public void setFilter(String pattern) {
202 if(pattern != null) {
203 current_filter = new FileFilter(pattern, false);
204 }
205 else {
206 current_filter = null;
207 }
208 filters = null;
209 }
210
211 public void setTree(DragTree tree) {
212 this.tree = tree;
213 }
214
215 public String toString() {
216 if(tree != null) {
217 return tree.toString();
218 }
219 return "FileSystemModel";
220 }
221
222 /** Called whenever an item in the tree has been collapsed. */
223 public void treeCollapsed(TreeExpansionEvent event) {
224 // Deallocate the affected nodes children. Don't need to do this in a swing worker, as the nodes children are currently not visable.
225 TreePath path = event.getPath();
226 FileNode node = (FileNode) path.getLastPathComponent();
227 node.unmap();
228 // Fire the appropriate event.
229 nodeStructureChanged(node);
230 }
231
232 /** Called whenever an item in the tree has been expanded. */
233 public void treeExpanded(TreeExpansionEvent event) {
234 }
235
236 /** Invoked whenever a node in the tree is about to be collapsed. */
237 public void treeWillCollapse(TreeExpansionEvent event)
238 throws ExpandVetoException {
239 // Veto the event if the user is attempting to collapse the root node (regardless of whether it is visible).
240 TreePath path = event.getPath();
241 if(path.getPathCount() == 1) {
242 throw new ExpandVetoException(event, "Cannot collapse root node!");
243 }
244 }
245
246 /** Invoked whenever a node in the tree is about to be expanded. */
247 public void treeWillExpand(TreeExpansionEvent event)
248 throws ExpandVetoException {
249 // Set the wait cursor.
250 Gatherer.g_man.wait(true);
251 // Allocate the children. Don't need to do this in a swing worker, as the nodes children are currently not visable.
252 TreePath path = event.getPath();
253 FileNode node = (FileNode) path.getLastPathComponent();
254 ///ystem.err.println("Mapping: " + node);
255 node.map();
256 ///ystem.err.println(" -> node has " + node.getChildCount() + " children");
257 nodeStructureChanged(node);
258 // Restore the cursor.
259 Gatherer.g_man.wait(false);
260 }
261}
Note: See TracBrowser for help on using the repository browser.