source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/com/gs3/testGXT/server/Greenstone/BaseFileNode.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 9.0 KB
Line 
1package com.gs3.testGXT.server.Greenstone;
2
3import java.io.*;
4import java.util.ArrayList;
5import java.util.Enumeration;
6
7import javax.swing.filechooser.*;
8import javax.swing.tree.*;
9
10import org.greenstone.gatherer.DebugStream;
11import org.greenstone.gatherer.file.FileFilter;
12import org.greenstone.gatherer.file.FileSystemModel;
13import org.greenstone.gatherer.metadata.FilenameEncoding;
14import org.greenstone.gatherer.util.ArrayTools;
15
16
17public abstract class BaseFileNode
18implements MutableTreeNode
19{
20 protected boolean allows_children = true;
21 protected ArrayList<BaseFileNode> child_nodes = null;
22 protected ArrayList<BaseFileNode> child_nodes_unfiltered = null;
23 protected File file = null;
24 protected FileSystemModel model = null;
25 protected MutableTreeNode parent = null;
26
27 protected String urlEncodedFileName = "";
28 protected String urlEncodedFilePath = "";
29 protected String filenameEncoding = "";
30 /** The string that is displayed as the filename. Attempts to be in the correct encoding. */
31 protected String displayFileName = null;
32
33
34 public BaseFileNode(File file)
35 {
36 this.file = file;
37
38 if (file != null) {
39 // Files cannot have children
40 if(file.isFile()) {
41 // Cache this result to prevent unceasing missing disk messages being thrown if the
42 // removable media was, um, removed after directory mapped
43 this.allows_children = false;
44 }
45 filenameEncoding = "";
46 urlEncodedFilePath = FilenameEncoding.calcURLEncodedFilePath(file);
47 urlEncodedFileName = FilenameEncoding.calcURLEncodedFileName(urlEncodedFilePath);
48
49 // work out the display string (extra special processing for CollectionTreeNodes)
50 displayFileName = calcDisplayString();
51 }
52 }
53
54 public String getURLEncodedFileName() { return urlEncodedFileName; }
55
56 public String getURLEncodedFilePath() { return urlEncodedFilePath; }
57
58 public String getFilenameEncoding() { return filenameEncoding; }
59
60
61 /** This method returns a string representation of the filenodes in the tree,
62 * that can then be displayed in the tree. Overridden in subclass CollectionTreeNode.
63 * Turn FilenameEncoding.DEBUGGING on to see URLEncoded filenames.
64 */
65 protected String calcDisplayString() {
66 if(FilenameEncoding.DEBUGGING) {
67 return getURLEncodedFileName();
68 } else {
69 return file.getName();
70 }
71 }
72
73 /** Returns the children of the node as an Enumeration. */
74 public Enumeration<?> children()
75 {
76 return new FileEnumeration();
77 }
78
79
80 /** Returns true if the receiver allows children. */
81 public boolean getAllowsChildren()
82 {
83 return allows_children;
84 }
85
86
87 /** Returns the child TreeNode at index childIndex. */
88 public TreeNode getChildAt(int index)
89 {
90 return (TreeNode) child_nodes.get(index);
91 }
92
93
94 /** Returns the number of children TreeNodes the receiver contains. */
95 public int getChildCount()
96 {
97 map();
98
99 // Use the number of (filtered) child nodes
100 if (child_nodes != null) {
101 return child_nodes.size();
102 }
103
104 return 0;
105 }
106
107
108 /** Returns the index of node in the receivers children. */
109 public int getIndex(TreeNode node)
110 {
111 if (child_nodes != null) {
112 return child_nodes.indexOf(node);
113 }
114
115 return -1;
116 }
117
118
119 /** Returns the parent TreeNode of the receiver. */
120 public TreeNode getParent()
121 {
122 return parent;
123 }
124
125
126 /** Adds child to the receiver at index. */
127 public void insert(MutableTreeNode child, int index)
128 {
129 DebugStream.println("Insert " + child + " in " + this + " at index " + index + " [Model: " + model + "]");
130 if (child == null) {
131 return;
132 }
133
134 try {
135 BaseFileNode child_node = (BaseFileNode) child;
136 child_nodes.add(index, child_node);
137 child_node.model = model;
138 child_node.parent = this;
139 }
140 catch (Exception exception) {
141 DebugStream.printStackTrace(exception);
142 }
143 }
144
145
146 /** Returns true if the receiver is a leaf. */
147 public boolean isLeaf()
148 {
149 return (allows_children == false);
150 }
151
152
153 /** Removes the child at index from the receiver. */
154 public void remove(int index)
155 {
156 if (index >= 0 && index < child_nodes.size()) {
157 child_nodes.remove(index);
158 }
159 }
160
161
162 /** Removes node from the receiver. */
163 public void remove(MutableTreeNode node)
164 {
165 remove(getIndex(node));
166 }
167
168
169 /** Removes the receiver from its parent. */
170 public void removeFromParent()
171 {
172 parent.remove(this);
173 parent = null;
174 }
175
176
177 /** Resets the user object of the receiver to object. */
178 public void setUserObject(Object object) {
179 try {
180 file = (File) object;
181 }
182 catch (Exception exception) {
183 DebugStream.printStackTrace(exception);
184 }
185 }
186
187
188 // -------------------------------------------------------------------------------
189
190
191 public void add(MutableTreeNode child)
192 {
193 insert(child, child_nodes.size());
194 }
195
196
197 protected abstract BaseFileNode addChildNode(File file);
198
199
200 /** Compare two FileNodes for equality. */
201 public boolean equals(BaseFileNode node)
202 {
203 if (node == null) {
204 // Definitely not a match
205 return false;
206 }
207
208 if (file != null) {
209 return file.equals(node.getFile());
210 }
211 else {
212 return toString().equals(node.toString());
213 }
214 }
215
216
217 /** Retrieve the file node at the given index, regardless of filters set. */
218 public BaseFileNode getChildAtUnfiltered(int index)
219 {
220 if (index >= 0 && index < size()) {
221 return (BaseFileNode) child_nodes_unfiltered.get(index);
222 }
223 return null;
224 }
225
226
227 public File getFile() {
228 return file;
229 }
230
231
232 /** Retrieves the tree path from the root node to this node. */
233 public TreeNode[] getPath() {
234 int count = 0;
235 TreeNode current = this;
236 while(current != null) {
237 count++;
238 current = current.getParent();
239 }
240 TreeNode[] path = new TreeNode[count];
241 current = this;
242 while(current != null) {
243 path[count - 1] = current;
244 count--;
245 current = current.getParent();
246 }
247 return path;
248 }
249
250
251 public boolean isFileSystemRoot() {
252 if (file != null) {
253 return FileSystemView.getFileSystemView().isFileSystemRoot(file);
254 }
255 else {
256 return false;
257 }
258 }
259
260
261 /** Overridden if necessary by subclasses. */
262 public boolean isInLoadedCollection()
263 {
264 return false;
265 }
266
267
268 /** Overridden if necessary by subclasses. */
269 public boolean isReadOnly()
270 {
271 return false;
272 }
273
274
275 public void map()
276 {
277 // If this node has already been mapped, don't bother doing it again
278 if (child_nodes != null) {
279 return;
280 }
281 child_nodes = new ArrayList<BaseFileNode>();
282
283 // General case, only map if children are allowed
284 if (file != null && getAllowsChildren()) {
285 File[] files = file.listFiles();
286 if (files != null && files.length > 0) {
287 // Sort the child files
288 ArrayTools.sort(files);
289
290 // Now add them to child_nodes_unfiltered
291 child_nodes_unfiltered = new ArrayList<BaseFileNode>();
292 for (int i = 0; i < files.length; i++) {
293 BaseFileNode child_node = this.addChildNode(files[i]);
294 child_nodes_unfiltered.add(child_node);
295 }
296
297 // Apply the filters set in the model
298 FileFilter[] filters = model.getFilters();
299 for (int i = 0; filters != null && i < filters.length; i++) {
300 files = ArrayTools.filter(files, filters[i].filter, filters[i].exclude);
301 }
302
303 // Add the files left after filtering to child_nodes
304 for (int i = 0, j = 0; (i < child_nodes_unfiltered.size() && j < files.length); i++) {
305 // Use the FileNode object in child_nodes_unfiltered rather than creating another
306 BaseFileNode file_node = (BaseFileNode) child_nodes_unfiltered.get(i);
307 if (file_node.getFile().equals(files[j])) {
308 child_nodes.add(file_node);
309 j++;
310 }
311 }
312
313 // in case any filename encodings had gone stale,
314 // (recalculate these and) refresh the display name
315 refreshDescendantEncodings();
316
317 }
318
319 model.nodeStructureChanged(this);
320 }
321 }
322
323
324 public void refresh()
325 {
326 unmap();
327 map();
328 }
329
330 // overridden in subclass CollectionTreeNode to reset and reencode display strings
331 public void resetDescendantEncodings() {}
332 public void refreshDescendantEncodings() {}
333
334
335 public void setModel(FileSystemModel model) {
336 this.model = model;
337 }
338
339 public void setParent(MutableTreeNode parent) {
340 this.parent = parent;
341 }
342
343
344 /** Return the total number of child files for the file this node represents, irrespective of filters set. */
345 public int size() {
346 if (child_nodes_unfiltered != null) {
347 return child_nodes_unfiltered.size();
348 }
349 return 0;
350 }
351
352
353 public String toString()
354 {
355 if (isFileSystemRoot()) {
356 return file.getAbsolutePath();
357 }
358 else {
359 if(displayFileName == null) {
360 displayFileName = calcDisplayString();
361 }
362 return displayFileName; //return file.getName();
363 }
364 }
365
366
367 /** Unmap this node's children. */
368 public void unmap()
369 {
370 DebugStream.println("Unmapping " + this + "...");
371 child_nodes_unfiltered = null;
372 child_nodes = null;
373 }
374
375
376 private class FileEnumeration
377 implements Enumeration<Object>
378 {
379 private int index = 0;
380
381 /** Tests if this enumeration contains more elements. */
382 public boolean hasMoreElements() {
383 return (index < child_nodes.size());
384 }
385
386 /** Returns the next element of this enumeration if this enumeration object has at least one more element to provide. */
387 public Object nextElement() {
388 Object result = null;
389 if (index < child_nodes.size()) {
390 result = child_nodes.get(index);
391 index++;
392 }
393 return result;
394 }
395 }
396}
Note: See TracBrowser for help on using the repository browser.