Source code: com/paradoxpoint/libitina/gui/JTreeFileSystemLoader.java
1 /*
2 * Libitina - Funeral Monument Image Compositor
3 * Copyright (C) 2003,2004 Luke Imhoff
4 *
5 * Contact Info:
6 * luke@paradoxpoint.com
7 * Luke Imhoff
8 * 2514 Pied Piper Lane
9 * Wausau, WI 54403
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 * JTreeFileSystemLoader.java
26 *
27 * Created on May 15, 2003, 7:23 PM
28 */
29
30 package com.paradoxpoint.libitina.gui;
31
32 import javax.swing.tree.DefaultMutableTreeNode;
33 import java.io.File;
34 import com.paradoxpoint.libitina.util.FileInfo;
35
36 /** GUI constructor class for loading file systems into a <CODE>JTree</CODE>
37 * @author Luke Imhoff
38 */
39 public class JTreeFileSystemLoader {
40
41 private java.io.File root;
42 private javax.swing.JTree tree = new javax.swing.JTree(new DefaultMutableTreeNode());
43
44 /** Creates a new instance of JTreeFileSystemLoader. Invoke <CODE>load()</CODE> to
45 * actually load the file system
46 * @param rootFile root of file system
47 * @param tree tree into which to load the file system
48 */
49 public JTreeFileSystemLoader(java.io.File rootFile, javax.swing.JTree tree) {
50 this.root = rootFile;
51 this.tree = tree;
52 }
53
54 /** Loads the file system into the tree */
55 public void load() {
56 if (root.isDirectory() && tree.getModel().getRoot() instanceof DefaultMutableTreeNode) {
57 DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
58 // remove old filesys
59 rootNode.removeAllChildren();
60 // update root to folder name
61 rootNode.setUserObject(new FileInfo(root.getName(),root.getAbsolutePath()));
62 createNodes(rootNode,root);
63 }
64 }
65
66 private void createNodes(DefaultMutableTreeNode rootNode, File rootFile) {
67 if (!rootFile.exists()) {
68 System.err.println("Folder does not exist: " + rootFile.getAbsolutePath());
69 return;
70 }
71 File[] rootFiles = rootFile.listFiles(new com.paradoxpoint.libitina.util.ImageFilter());
72 for (int i = 0; i < rootFiles.length; i++) {
73 DefaultMutableTreeNode temp = new DefaultMutableTreeNode(new FileInfo(rootFiles[i].getName(),rootFiles[i].getAbsolutePath()));
74 rootNode.add(temp);
75 if (rootFiles[i].isDirectory())
76 createNodes(temp,rootFiles[i]);
77 }
78 }
79
80 /** Getter for property root.
81 * @return Value of property root.
82 *
83 */
84 public java.io.File getRoot() {
85 return root;
86 }
87
88 /** Setter for property root.
89 * @param root New value of property root.
90 *
91 */
92 public void setRoot(java.io.File root) {
93 if (!root.equals(this.root)) {
94 this.root = root;
95 load();
96 }
97 }
98
99 /** Getter for property tree.
100 * @return Value of property tree.
101 *
102 */
103 public javax.swing.JTree getTree() {
104 return tree;
105 }
106
107 /** Setter for property tree.
108 * @param tree New value of property tree.
109 *
110 */
111 public void setTree(javax.swing.JTree tree) {
112 if (!tree.equals(this.tree)) {
113 this.tree = tree;
114 load();
115 }
116 }
117
118 }