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

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

In the previous commit 1. Codec.java change was still related to commit 34241. 2. Fixed my own silly bug: it's windowClosed not windowClose. Because the real method was not overridden, it never got called when the configFileEditor was closed, as a result of which the GLI's GUI interface wasn't getting immediately repainted. 3. Fixed a bug in FileNode.java (nullpointer exception case). 4. Accidentally committed experimental work to export metadata from GLI to csv. This commit undoes the experimental stuff, only some of which is promising and still needs work.

  • 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.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 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 FileNode last = (FileNode)path.getLastPathComponent();
56 DebugStream.println("Last Path Component = " + last);
57 return last;
58 /*
59 ///atherer.println("**** getNode(" + path + ") ****");
60 FileNode current = (FileNode)root;
61 // Special case for the root node. Check the first path component is the root node.
62 FileNode first_node = (FileNode)path.getPathComponent(0);
63 if(current.equals(first_node)) {
64 DebugStream.println("First path component matches root node.");
65 // For each path with this tree path
66 for(int i = 1; current != null && i < path.getPathCount(); i++) {
67 // Retrieve the stale path
68 Object stale_object = path.getPathComponent(i);
69 FileNode stale_node = null;
70 if(stale_object instanceof FileNode) {
71 stale_node = (FileNode) stale_object;
72 }
73 DebugStream.print("Searching for '" + stale_object + "': ");
74 // Locate the fresh node by searching current's children. Remember to ensure that current is mapped.
75 boolean found = false;
76
77 // First we search through the mapped children
78 for(int j = 0; !found && j < current.getChildCount(); j++) {
79 FileNode child_node = (FileNode) current.getChildAt(j);
80 DebugStream.print(child_node + " ");
81 if((stale_node != null && stale_node.equals(child_node)) || stale_object.toString().equals(child_node.toString())) {
82 found = true;
83 current = child_node;
84 DebugStream.println("Found!");
85 }
86 child_node = null;
87 }
88 // Failing that we search through all the children, including filtered files
89 for(int j = 0; !found && j < current.size(); j++) {
90 FileNode child_node = (FileNode) current.getChildAtUnfiltered(j);
91 DebugStream.print(child_node + " ");
92 if((stale_node != null && stale_node.equals(child_node)) || stale_object.toString().equals(child_node.toString())) {
93 found = true;
94 current = child_node;
95 DebugStream.println("Found!");
96 }
97 child_node = null;
98 }
99 // If no match is found, then set current to null and exit.
100 if(!found) {
101 current = null;
102 DebugStream.println("Not Found!");
103 }
104 else {
105 DebugStream.println("Returning node: " + new TreePath(current.getPath()));
106 }
107 // Repeat as necessary
108 }
109 }
110 return current;
111 */
112 }
113
114 public void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) {
115 ///ystem.err.println("insertNodeInto(" + newChild + ", " + parent + ", " + index + ")");
116 super.insertNodeInto(newChild, parent, index);
117 }
118
119
120 public void refresh(TreePath path)
121 {
122 // Can only refresh if the model is currently being displayed in a tree
123 if (tree == null) {
124 return;
125 }
126
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());
131
132 // Make sure the root node is expanded
133 tree.expandPath(path);
134 }
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();
147 node.refresh();
148
149 // Fire the appropriate event
150 nodeStructureChanged(node);
151
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;
184 }
185 }
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 }
192 }
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 }
198 }
199 }
200
201
202 // The file tree can be filtered, so check that the node to be removed is actually part of the model
203 public void removeNodeFromParent(MutableTreeNode node)
204 {
205 TreeNode parent_node = ((FileNode) node).getParent();
206 if (parent_node != null && parent_node.getIndex((FileNode) node) != -1) {
207 super.removeNodeFromParent(node);
208 }
209 }
210
211
212 private class TreePathComparator
213 implements Comparator {
214
215 public int compare(Object o1, Object o2)
216 {
217 return (((TreePath) o1).getPathCount() - ((TreePath) o2).getPathCount());
218 }
219 }
220
221
222 public void setFilter(String pattern) {
223 if(pattern != null) {
224 current_filter = new FileFilter(pattern, false);
225 }
226 else {
227 current_filter = null;
228 }
229 filters = null;
230 }
231
232 public void setTree(DragTree tree) {
233 this.tree = tree;
234 }
235
236 public String toString() {
237 if(tree != null) {
238 return tree.toString();
239 }
240 return "FileSystemModel";
241 }
242
243 /** Called whenever an item in the tree has been collapsed. */
244 public void treeCollapsed(TreeExpansionEvent event) {
245 // Deallocate the affected nodes children. Don't need to do this in a swing worker, as the nodes children are currently not visable.
246 TreePath path = event.getPath();
247 FileNode node = (FileNode) path.getLastPathComponent();
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
268 /** Invoked whenever a node in the tree is about to be expanded. */
269 public void treeWillExpand(TreeExpansionEvent event)
270 throws ExpandVetoException
271 {
272 // Set the wait cursor
273 Gatherer.g_man.wait(true);
274
275 // Allocate the children (don't need a swing worker since the node's children are currently not visible)
276 TreePath path = event.getPath();
277 FileNode node = (FileNode) path.getLastPathComponent();
278 node.refresh();
279 nodeStructureChanged(node);
280
281 // Restore the cursor
282 Gatherer.g_man.wait(false);
283 }
284}
Note: See TracBrowser for help on using the repository browser.