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

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

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