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

Last change on this file since 6622 was 6622, checked in by jmt12, 20 years ago

More modifications to mirroring including testing for a valid version of Wget (and complaining if its missing or it is old) and rearranging buttons on the GProgressBar

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