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

Last change on this file since 34246 was 34246, checked in by ak19, 4 years ago

Still part of commit 34241 and now also 34245.

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