1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package wjhk.jupload2.gui.filepanel.treeview; |
27 | |
|
28 | |
import java.io.File; |
29 | |
import java.io.IOException; |
30 | |
import java.io.InputStream; |
31 | |
import java.lang.reflect.Array; |
32 | |
import java.util.ArrayList; |
33 | |
import java.util.Date; |
34 | |
import java.util.List; |
35 | |
|
36 | |
import javax.swing.tree.TreeModel; |
37 | |
import javax.swing.tree.TreePath; |
38 | |
|
39 | |
import wjhk.jupload2.exception.JUploadException; |
40 | |
import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles; |
41 | |
import wjhk.jupload2.exception.JUploadIOException; |
42 | |
import wjhk.jupload2.filedata.FileData; |
43 | |
import wjhk.jupload2.gui.filepanel.FilePanelFlatDataModel2; |
44 | |
import wjhk.jupload2.policies.UploadPolicy; |
45 | |
import wjhk.jupload2.upload.helper.ByteArrayEncoder; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 116 | public class FolderNode implements TreeFileDataNode { |
51 | |
|
52 | |
|
53 | |
|
54 | 694 | MyTreeTableModel<TreeFileDataNode> treeModel = null; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 694 | FilePanelFlatDataModel2 flatModel = null; |
64 | |
|
65 | |
|
66 | 694 | File file = null; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | 694 | TreeFileDataNode parent = null; |
73 | |
|
74 | |
|
75 | 694 | List<TreeFileDataNode> children = null; |
76 | |
|
77 | |
Date fileModified; |
78 | |
|
79 | 694 | boolean uploadFlag = true; |
80 | |
|
81 | 694 | UploadPolicy uploadPolicy = null; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
static public String getFilename(File file) { |
92 | 2641 | String filename = file.getName(); |
93 | |
try { |
94 | 2641 | filename = (filename.equals("")) ? file.getCanonicalPath() : filename; |
95 | 0 | } catch (IOException e) { |
96 | 0 | throw new IllegalArgumentException(e.getMessage() |
97 | 0 | + " exception, when trying to resolve the canonical path for " + file.getAbsolutePath(), e); |
98 | 2641 | } |
99 | 2641 | return filename; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
static public String getAbsolutePath(TreeFileDataNode node) { |
110 | 3 | StringBuffer sb = new StringBuffer(); |
111 | 3 | int depth = 0; |
112 | 15 | for (Object o : node.getTreePath().getPath()) { |
113 | 12 | String oName = o.toString(); |
114 | |
|
115 | 12 | sb.append(oName); |
116 | |
|
117 | 12 | if (depth == 1 && oName.substring(1, 2).equals(":")) { |
118 | |
|
119 | |
|
120 | |
|
121 | 1 | sb.setLength(0); |
122 | 1 | sb.append(oName); |
123 | 11 | } else if (o != node) { |
124 | |
|
125 | 8 | sb.append(File.separator); |
126 | |
} |
127 | 12 | depth += 1; |
128 | |
} |
129 | 3 | return sb.toString(); |
130 | |
} |
131 | |
|
132 | |
public static int[] getIntArray(int... indexOf) { |
133 | 698 | int[] ints = new int[indexOf.length]; |
134 | 1396 | for (int i = 0; i < indexOf.length; i += 1) { |
135 | 698 | ints[i] = indexOf[i]; |
136 | |
} |
137 | 698 | return ints; |
138 | |
} |
139 | |
|
140 | |
public static TreeFileDataNode[] getItemArray(TreeFileDataNode... child) { |
141 | 698 | TreeFileDataNode[] treeNodes = (TreeFileDataNode[]) Array.newInstance(TreeFileDataNode.class, child.length); |
142 | 1396 | for (int i = 0; i < child.length; i += 1) { |
143 | 698 | treeNodes[i] = child[i]; |
144 | |
} |
145 | 698 | return treeNodes; |
146 | |
} |
147 | |
|
148 | |
protected FolderNode(UploadPolicy uploadPolicy, MyTreeTableModel<TreeFileDataNode> model, |
149 | 694 | FilePanelFlatDataModel2 flatModel) { |
150 | 694 | this.uploadPolicy = uploadPolicy; |
151 | 694 | this.treeModel = model; |
152 | 694 | this.flatModel = flatModel; |
153 | 694 | this.children = new ArrayList<TreeFileDataNode>(); |
154 | 694 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public FolderNode(File file, UploadPolicy uploadPolicy, MyTreeTableModel<TreeFileDataNode> model, |
163 | |
FilePanelFlatDataModel2 flatModel) { |
164 | |
|
165 | 427 | this(uploadPolicy, model, flatModel); |
166 | |
|
167 | 427 | if (!file.isDirectory()) { |
168 | 1 | throw new IllegalArgumentException("Internal error: " + file.getAbsolutePath() + " should be a folder"); |
169 | |
} |
170 | 426 | this.file = file; |
171 | 426 | } |
172 | |
|
173 | |
|
174 | |
public int getTotalChildCount() { |
175 | 176 | int total = 0; |
176 | 176 | for (TreeFileDataNode child : children) { |
177 | 224 | total += 1 + child.getTotalChildCount(); |
178 | 224 | } |
179 | 176 | return total; |
180 | |
} |
181 | |
|
182 | |
@SuppressWarnings("unchecked") |
183 | |
|
184 | |
public List<MyTreeNode> getChildren() { |
185 | 434 | return (List<MyTreeNode>) (List<?>) children; |
186 | |
} |
187 | |
|
188 | |
|
189 | |
public void appendFileProperties(ByteArrayEncoder bae, int index) throws JUploadIOException { |
190 | 1 | throw new IllegalAccessError("Internal error: appendFileProperties should not be called from " |
191 | 1 | + this.getClass().getName()); |
192 | |
} |
193 | |
|
194 | |
|
195 | |
public void beforeUpload(String uploadPathRoot) throws JUploadException { |
196 | 1 | throw new IllegalAccessError("Internal error: beforeUpload should not be called from " |
197 | 1 | + this.getClass().getName()); |
198 | |
} |
199 | |
|
200 | |
|
201 | |
public long getUploadLength() { |
202 | 1 | throw new IllegalAccessError("Internal error: getUploadLength should not be called from " |
203 | 1 | + this.getClass().getName()); |
204 | |
} |
205 | |
|
206 | |
|
207 | |
public void afterUpload() { |
208 | 1 | throw new IllegalAccessError("Internal error: afterUpload should not be called from " |
209 | 1 | + this.getClass().getName()); |
210 | |
} |
211 | |
|
212 | |
|
213 | |
public InputStream getInputStream() throws JUploadException { |
214 | 1 | throw new IllegalAccessError("Internal error: getInputStream should not be called from " |
215 | 1 | + this.getClass().getName()); |
216 | |
} |
217 | |
|
218 | |
|
219 | |
public String getFileName() { |
220 | |
|
221 | |
|
222 | 1277 | return FolderNode.getFilename(file); |
223 | |
} |
224 | |
|
225 | |
|
226 | |
public String getFileExtension() { |
227 | 1 | throw new IllegalAccessError("Internal error: getFileExtension should not be called from " |
228 | 1 | + this.getClass().getName()); |
229 | |
} |
230 | |
|
231 | |
|
232 | |
public long getFileLength() { |
233 | 1 | return -1; |
234 | |
} |
235 | |
|
236 | |
|
237 | |
public Date getLastModified() { |
238 | 1 | if (this.fileModified == null) { |
239 | 1 | this.fileModified = new Date(this.file.lastModified()); |
240 | |
} |
241 | 1 | return this.fileModified; |
242 | |
} |
243 | |
|
244 | |
|
245 | |
public boolean getUploadFlag() { |
246 | 6 | return uploadFlag; |
247 | |
} |
248 | |
|
249 | |
|
250 | |
public void setUploadFlag(boolean uploadFlag) { |
251 | 4 | if (this.uploadFlag != uploadFlag) { |
252 | 4 | this.uploadFlag = uploadFlag; |
253 | 4 | for (TreeFileDataNode tfdn : children) { |
254 | 10 | tfdn.setUploadFlag(uploadFlag); |
255 | 10 | } |
256 | 4 | if (getParent() == null) { |
257 | |
|
258 | 0 | treeModel.fireTreeNodesChanged(this, null, null, null); |
259 | |
} else { |
260 | 8 | treeModel.fireTreeNodesChanged(this, treeModel.getTreePath((TreeFileDataNode) getParent()), |
261 | 4 | getIntArray((getParent().getChildren()).indexOf(this)), getItemArray((TreeFileDataNode) this)); |
262 | |
} |
263 | |
} |
264 | 4 | } |
265 | |
|
266 | |
|
267 | |
public String getDirectory() { |
268 | 1 | return this.file.getAbsoluteFile().getParent(); |
269 | |
} |
270 | |
|
271 | |
|
272 | |
public String getMD5() throws JUploadException { |
273 | 1 | throw new IllegalAccessError("Internal error: getMD5 should not be called from " + this.getClass().getName()); |
274 | |
} |
275 | |
|
276 | |
|
277 | |
public String getMimeType() { |
278 | 1 | throw new IllegalAccessError("Internal error: getMimeType should not be called from " |
279 | 1 | + this.getClass().getName()); |
280 | |
} |
281 | |
|
282 | |
|
283 | |
public boolean canRead() { |
284 | 1 | return file.canRead(); |
285 | |
} |
286 | |
|
287 | |
protected File getFile() { |
288 | 0 | throw new IllegalAccessError("Internal error: getFile is deprecated and should not be called from " |
289 | 0 | + this.getClass().getName()); |
290 | |
} |
291 | |
|
292 | |
|
293 | |
public String getRelativeDir() { |
294 | 0 | throw new IllegalAccessError("Internal error: getRelativeDir should not be called from " |
295 | 0 | + this.getClass().getName()); |
296 | |
} |
297 | |
|
298 | |
|
299 | |
public String getAbsolutePath() { |
300 | 2 | return FolderNode.getAbsolutePath(this); |
301 | |
} |
302 | |
|
303 | |
|
304 | |
public boolean isPreparedForUpload() { |
305 | 1 | throw new IllegalAccessError("Internal error: isPreparedForUpload should not be called from " |
306 | 1 | + this.getClass().getName()); |
307 | |
} |
308 | |
|
309 | |
|
310 | |
public int getChildCount() { |
311 | 311 | return (children == null) ? 0 : children.size(); |
312 | |
} |
313 | |
|
314 | |
|
315 | |
public TreeFileDataNode getChild(int index) { |
316 | 117 | return (children == null) ? null : children.get(index); |
317 | |
} |
318 | |
|
319 | |
|
320 | |
public TreeFileDataNode getChild(String name) { |
321 | 2053 | for (TreeFileDataNode node : children) { |
322 | 2796 | if (node.getFileName().equals(name)) { |
323 | 351 | return node; |
324 | |
} |
325 | 2445 | } |
326 | |
|
327 | |
|
328 | 1702 | return null; |
329 | |
} |
330 | |
|
331 | |
|
332 | |
public TreeFileDataNode getChild(File file) { |
333 | 1364 | return getChild(FolderNode.getFilename(file)); |
334 | |
} |
335 | |
|
336 | |
|
337 | |
public MyTreeNode getParent() { |
338 | 10069 | return this.parent; |
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
public void setParent(MyTreeNode parent) { |
344 | 437 | this.parent = (TreeFileDataNode) parent; |
345 | 437 | } |
346 | |
|
347 | |
|
348 | |
@SuppressWarnings("unchecked") |
349 | |
public void setTreeModel(TreeModel model) { |
350 | 969 | this.treeModel = (MyTreeTableModel<TreeFileDataNode>) model; |
351 | 969 | } |
352 | |
|
353 | |
|
354 | |
public void setFlatModel(FilePanelFlatDataModel2 flatModel) { |
355 | 12 | this.flatModel = flatModel; |
356 | 12 | } |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public void removeChild(MyTreeNode child) { |
364 | 35 | if (!children.remove(child)) { |
365 | 1 | throw new IllegalArgumentException(child.toString() + " is not a child of " + getFileName()); |
366 | |
} |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | 34 | child.setParent(null); |
374 | 34 | child.setTreeModel(null); |
375 | 34 | child.setFlatModel(null); |
376 | |
; |
377 | |
|
378 | |
|
379 | |
|
380 | 41 | while (child.getChildCount() > 0) { |
381 | |
|
382 | 7 | ((FolderNode) child).removeChild(child.getChild(0)); |
383 | |
} |
384 | 34 | } |
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
public TreeFileDataNode addChild(FileData fileData) { |
394 | 1 | return addChild(new FileDataNode(fileData)); |
395 | |
} |
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
public TreeFileDataNode addChild(TreeFileDataNode child) { |
405 | 688 | TreeFileDataNode alreadyExistingChild = (TreeFileDataNode) getChild(child.getFileName()); |
406 | 688 | if (alreadyExistingChild == null) { |
407 | |
|
408 | 686 | children.add(child); |
409 | 686 | child.setTreeModel(this.treeModel); |
410 | 686 | child.setParent(this); |
411 | 1372 | treeModel.fireTreeNodesInserted(this, treeModel.getTreePath(this), getIntArray(children.indexOf(child)), |
412 | 686 | getItemArray((TreeFileDataNode) child)); |
413 | 686 | return child; |
414 | |
} else { |
415 | 4 | uploadPolicy.displayWarn("The FileData for " + child.getAbsolutePath() + " already exists for the folder " |
416 | 2 | + getFileName()); |
417 | 2 | return alreadyExistingChild; |
418 | |
} |
419 | |
} |
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
public FolderNode getSubfolderOrCreateIt(File file) throws JUploadExceptionStopAddingFiles { |
428 | 2 | if (!file.isDirectory()) { |
429 | 0 | throw new JUploadExceptionStopAddingFiles(file.getAbsolutePath() + " must be a directory"); |
430 | |
} |
431 | 2 | TreeFileDataNode child = addChild(new FolderNode(file, uploadPolicy, treeModel, flatModel)); |
432 | |
|
433 | 2 | if (!(child instanceof FolderNode)) { |
434 | 0 | throw new JUploadExceptionStopAddingFiles("A child with the same name (" + file.getName() |
435 | |
+ ") already exists, but is not a folder"); |
436 | |
} else { |
437 | 2 | return (FolderNode) child; |
438 | |
} |
439 | |
} |
440 | |
|
441 | |
|
442 | |
public boolean isLeaf() { |
443 | 377 | return false; |
444 | |
} |
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
@Override |
452 | |
public String toString() { |
453 | 244 | return getFileName(); |
454 | |
} |
455 | |
|
456 | |
|
457 | |
public TreeFileDataNode getTreeFileDataNode() { |
458 | 0 | return this; |
459 | |
} |
460 | |
|
461 | |
|
462 | |
public void setTreeFileDataNode(TreeFileDataNode node) { |
463 | 0 | throw new IllegalStateException("setTreeFileDataNode may not be called againts a FolderNode"); |
464 | |
} |
465 | |
|
466 | |
|
467 | |
public TreePath getTreePath() { |
468 | 14 | if (parent == null) { |
469 | 3 | return new TreePath(this); |
470 | |
} else { |
471 | 11 | return parent.getTreePath().pathByAddingChild(this); |
472 | |
} |
473 | |
} |
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
public TreeFileDataNode addChild(File f) throws JUploadExceptionStopAddingFiles { |
484 | 408 | if (getChild(f) != null) { |
485 | 0 | throw new JUploadExceptionStopAddingFiles("Internal error: " + f.getAbsolutePath() |
486 | 0 | + " is already a child of the node for " + this.file.getAbsolutePath()); |
487 | |
} |
488 | |
|
489 | 408 | if (f.isDirectory()) { |
490 | 407 | return addChild(new FolderNode(f, uploadPolicy, treeModel, flatModel)); |
491 | |
} else { |
492 | 1 | FileData fd = flatModel.addFile(f); |
493 | 1 | return addChild(new FileDataNode(fd)); |
494 | |
} |
495 | |
} |
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
public int addChildAndDescendants(File f) throws JUploadExceptionStopAddingFiles { |
505 | 313 | int nbFiles = 0; |
506 | |
|
507 | 313 | TreeFileDataNode child = getChild(f); |
508 | 313 | if (child == null) { |
509 | |
|
510 | 300 | if (f.isDirectory()) { |
511 | 102 | child = addChild(f); |
512 | |
} else { |
513 | 198 | FileData fd = flatModel.addFile(f); |
514 | |
|
515 | 198 | if (fd != null) { |
516 | 198 | child = addChild(new FileDataNode(fd)); |
517 | 198 | fd.setTreeFileDataNode(child); |
518 | 198 | nbFiles += 1; |
519 | |
} |
520 | |
} |
521 | |
} |
522 | |
|
523 | |
|
524 | |
|
525 | 313 | if (f.isDirectory()) { |
526 | |
|
527 | 398 | for (File file : f.listFiles()) { |
528 | 288 | nbFiles += ((FolderNode) child).addChildAndDescendants(file); |
529 | |
} |
530 | |
} |
531 | 313 | return nbFiles; |
532 | |
} |
533 | |
|
534 | |
} |