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

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

Removed some dead code.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 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 int counter = 0;
21 private DragTree tree;
22 private FileFilter current_filter;
23 private FileFilter[] filters;
24 /** The filters in place for any file system model. */
25 private FileFilter[] default_filters = { new FileFilter("\\..*", true), new FileFilter("metadata\\.xml", true) };
26
27 public FileSystemModel(FileNode root) {
28 super(root);
29 current_filter = null;
30 filters = null;
31 tree = null;
32 root.setModel(this);
33 root.map();
34 }
35
36 public int getChildCount(Object parent) {
37 return ((TreeNode)parent).getChildCount();
38 }
39
40 public FileFilter[] getFilters() {
41 if(filters == null) {
42 if(current_filter != null) {
43 filters = new FileFilter[default_filters.length + 1];
44 filters[default_filters.length] = current_filter;
45 }
46 else {
47 filters = new FileFilter[default_filters.length];
48 }
49 System.arraycopy(default_filters, 0, filters, 0, default_filters.length);
50 }
51 return filters;
52 }
53
54 /** 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. */
55 public FileNode getNode(TreePath path) {
56 ///atherer.println("**** getNode(" + path + ") ****");
57 FileNode current = (FileNode)root;
58 // Special case for the root node. Check the first path component is the root node.
59 FileNode first_node = (FileNode)path.getPathComponent(0);
60 if(current.equals(first_node)) {
61 Gatherer.println("First path component matches root node.");
62 // For each path with this tree path
63 for(int i = 1; current != null && i < path.getPathCount(); i++) {
64 // Retrieve the stale path
65 Object stale_object = path.getPathComponent(i);
66 FileNode stale_node = null;
67 if(stale_object instanceof FileNode) {
68 stale_node = (FileNode) stale_object;
69 }
70 Gatherer.print("Searching for '" + stale_object + "': ");
71 // Locate the fresh node by searching current's children. Remember to ensure that current is mapped.
72 //current.unmap();
73 boolean found = false;
74
75 // First we search through the mapped children
76 for(int j = 0; !found && j < current.getChildCount(); j++) {
77 FileNode child_node = (FileNode) current.getChildAt(j);
78 Gatherer.print(child_node + " ");
79 if((stale_node != null && stale_node.equals(child_node)) || stale_object.toString().equals(child_node.toString())) {
80 found = true;
81 current = child_node;
82 Gatherer.println("Found!");
83 }
84 child_node = null;
85 }
86 // Failing that we search through all the children, including filtered files
87 for(int j = 0; !found && j < current.size(); j++) {
88 FileNode child_node = (FileNode) current.get(j);
89 Gatherer.print(child_node + " ");
90 if((stale_node != null && stale_node.equals(child_node)) || stale_object.toString().equals(child_node.toString())) {
91 found = true;
92 current = child_node;
93 Gatherer.println("Found!");
94 }
95 child_node = null;
96 }
97 // If no match is found, then set current to null and exit.
98 if(!found) {
99 current = null;
100 Gatherer.println("Not Found!");
101 }
102 else {
103 Gatherer.println("Returning node: " + new TreePath(current.getPath()));
104 }
105 // Repeat as necessary
106 }
107 }
108 return current;
109 }
110
111 public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) {
112 ///ystem.err.println("insertNodeInto(" + newChild + ", " + parent + ", " + index + ")");
113 super.insertNodeInto(newChild, parent, index);
114 }
115
116 public void mapDirectory(File directory, String title) {
117 FileNode node = new FileNode(directory, this, title, true);
118 SynchronizedTreeModelTools.insertNodeInto(this, (FileNode)root, node);
119 }
120
121
122 public void refresh(TreePath path)
123 {
124 // Can only refresh if the model is currently being displayed in a tree
125 if (tree == null) {
126 return;
127 }
128
129 // If no path is set, take the path to the root node (ie. update the whole tree)
130 if (path == null) {
131 // System.err.println("\nFileSystemModel.refresh(entire tree).");
132 path = new TreePath(((FileNode) root).getPath());
133
134 // Make sure the root node is expanded
135 tree.expandPath(path);
136 }
137 // else {
138 // System.err.println("\nFileSystemModel.refresh(" + path + ").");
139 // }
140
141 // Record all the expanded paths under this node
142 Enumeration old_expanded_paths_enumeration = tree.getExpandedDescendants(path);
143 if (old_expanded_paths_enumeration == null) {
144 return;
145 }
146
147 // Map and unmap the node to refresh its contents
148 FileNode node = (FileNode) path.getLastPathComponent();
149 node.unmap();
150 node.map();
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.