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

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

Removed all occurrences of classes explicitly importing other classes in the same package.

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