KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MyTreeNode


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 import java.io.File JavaDoc;
22 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
23 import javax.swing.JOptionPane JavaDoc;
24
25
26 /**
27  * JDIC API demo class.
28  * <p>
29  * A redefined MutableTreeNode class.
30  */

31
32 class MyTreeNode extends DefaultMutableTreeNode JavaDoc {
33     private static String JavaDoc osName = System.getProperty("os.name").toLowerCase();
34     private boolean explored = false;
35
36     private void errorDisplayingPermission() {
37         JOptionPane.showMessageDialog(null,
38                 "You do not have the permissions necessary to view the contents of \""
39                 + getFile() + "\"", "Error Displaying Folder",
40                 JOptionPane.ERROR_MESSAGE);
41     }
42
43     private void errorDisplayingNoDisk() {
44         File JavaDoc currentFile = getFile();
45         String JavaDoc fileName = currentFile.getName();
46         String JavaDoc errorMsg =
47             "The file : "
48                 + fileName
49                 + " does not exist any"
50                 + " longer!";
51         if (osName.startsWith("windows")) {
52             //Windows platform, check if it's disk
53
File JavaDoc parentFile = currentFile.getParentFile();
54             if (currentFile.getParentFile() == null) {
55                 //parent file is null, should be a disk
56
errorMsg = "Please insert disk into driver: " + currentFile;
57             }
58         }
59         JOptionPane.showMessageDialog(
60             null,
61             errorMsg,
62             "Error Displaying Folder",
63             JOptionPane.ERROR_MESSAGE);
64     }
65
66     // Assocoates a file object with this node.
67
public MyTreeNode(File JavaDoc file) {
68         setUserObject(file);
69     }
70
71     public boolean getAllowsChildren() {
72         return isDirectory();
73     }
74
75     public boolean isLeaf() {
76         return !isDirectory();
77     }
78
79     public File JavaDoc getFile() {
80         return (File JavaDoc) getUserObject();
81     }
82
83     public boolean isExplored() {
84         return explored;
85     }
86
87     public boolean isDirectory() {
88         File JavaDoc file = getFile();
89
90         return file.isDirectory();
91     }
92
93     public void explore() {
94         File JavaDoc file = getFile();
95       
96         if (!isDirectory()) {
97             return;
98         }
99
100         //Check if the file exists
101
if (!file.exists()) {
102             errorDisplayingNoDisk();
103             return;
104         }
105         
106         // Check if the file is readable.
107
if (!file.canRead()) {
108             errorDisplayingPermission();
109             return;
110         }
111         
112         if (!isExplored()) {
113             File JavaDoc[] children = file.listFiles();
114
115             if (children != null) {
116                 for (int i = 0; i < children.length; ++i) {
117                     if (children[i].isDirectory()) {
118                         add(new MyTreeNode(children[i]));
119                     }
120                 }
121             }
122
123             explored = true;
124         }
125     }
126
127     public String JavaDoc toString() {
128         File JavaDoc file = (File JavaDoc) getUserObject();
129         String JavaDoc filename = file.toString();
130
131         int index = filename.lastIndexOf(File.separator);
132
133         return (index != -1 && index != filename.length() - 1)
134                 ? filename.substring(index + 1)
135                 : filename;
136     }
137
138     /**
139      * Gets the files and subdirectories under this file object.
140      */

141     public int getChildrenCount() {
142         File JavaDoc file = getFile();
143
144         //Check if the file exists
145
if (!file.exists()) {
146             errorDisplayingNoDisk();
147             return 0;
148         }
149
150         if (!file.canRead()) {
151             errorDisplayingPermission();
152             return 0;
153         }
154         if (!isDirectory()) {
155             return 0;
156         } else {
157             File JavaDoc[] children = file.listFiles();
158       
159             return (children != null) ? children.length : 0;
160         }
161     }
162
163     /**
164      * Gets size of this file object.
165      */

166     public long getSize() {
167         File JavaDoc file = getFile();
168
169         if (!file.canRead()) {
170             return 0;
171         }
172       
173         if (!isDirectory()) {
174             return (file.length());
175         }
176
177         File JavaDoc[] children = file.listFiles();
178
179         long size = 0;
180
181         if (children != null) {
182             for (int i = 0; i < children.length; ++i) {
183                 size += children[i].length();
184             }
185         }
186
187         return size;
188     }
189 }
190
Popular Tags