blob: fe891c6ca160b17b6c8cbb71eb0695b46416755d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.awt.shell;
27
28import java.awt.Image;
29import java.awt.Toolkit;
30import java.io.*;
31import java.io.FileNotFoundException;
32import java.util.*;
33
34/**
35 * @author Michael Martak
36 * @since 1.4
37 */
38
39public abstract class ShellFolder extends File {
40 protected ShellFolder parent;
41
42 /**
43 * Create a file system shell folder from a file
44 */
45 ShellFolder(ShellFolder parent, String pathname) {
46 super((pathname != null) ? pathname : "ShellFolder");
47 this.parent = parent;
48 }
49
50 /**
51 * @return Whether this is a file system shell folder
52 */
53 public boolean isFileSystem() {
54 return (!getPath().startsWith("ShellFolder"));
55 }
56
57 /**
58 * This method must be implemented to make sure that no instances
59 * of <code>ShellFolder</code> are ever serialized. If <code>isFileSystem()</code> returns
60 * <code>true</code>, then the object should be representable with an instance of
61 * <code>java.io.File</code> instead. If not, then the object is most likely
62 * depending on some internal (native) state and cannot be serialized.
63 *
64 * @returns a <code>java.io.File</code> replacement object, or <code>null</code>
65 * if no suitable replacement can be found.
66 */
67 protected abstract Object writeReplace() throws java.io.ObjectStreamException;
68
69 /**
70 * Returns the path for this object's parent,
71 * or <code>null</code> if this object does not name a parent
72 * folder.
73 *
74 * @return the path as a String for this object's parent,
75 * or <code>null</code> if this object does not name a parent
76 * folder
77 *
78 * @see java.io.File#getParent()
79 * @since 1.4
80 */
81 public String getParent() {
82 if (parent == null && isFileSystem()) {
83 return super.getParent();
84 }
85 if (parent != null) {
86 return (parent.getPath());
87 } else {
88 return null;
89 }
90 }
91
92 /**
93 * Returns a File object representing this object's parent,
94 * or <code>null</code> if this object does not name a parent
95 * folder.
96 *
97 * @return a File object representing this object's parent,
98 * or <code>null</code> if this object does not name a parent
99 * folder
100 *
101 * @see java.io.File#getParentFile()
102 * @since 1.4
103 */
104 public File getParentFile() {
105 if (parent != null) {
106 return parent;
107 } else if (isFileSystem()) {
108 return super.getParentFile();
109 } else {
110 return null;
111 }
112 }
113
114 public File[] listFiles() {
115 return listFiles(true);
116 }
117
118 public File[] listFiles(boolean includeHiddenFiles) {
119 File[] files = super.listFiles();
120
121 if (!includeHiddenFiles) {
122 Vector v = new Vector();
123 int nameCount = (files == null) ? 0 : files.length;
124 for (int i = 0; i < nameCount; i++) {
125 if (!files[i].isHidden()) {
126 v.addElement(files[i]);
127 }
128 }
129 files = (File[])v.toArray(new File[v.size()]);
130 }
131
132 return files;
133 }
134
135
136 /**
137 * @return Whether this shell folder is a link
138 */
139 public abstract boolean isLink();
140
141 /**
142 * @return The shell folder linked to by this shell folder, or null
143 * if this shell folder is not a link
144 */
145 public abstract ShellFolder getLinkLocation() throws FileNotFoundException;
146
147 /**
148 * @return The name used to display this shell folder
149 */
150 public abstract String getDisplayName();
151
152 /**
153 * @return The type of shell folder as a string
154 */
155 public abstract String getFolderType();
156
157 /**
158 * @return The executable type as a string
159 */
160 public abstract String getExecutableType();
161
162 /**
163 * Compares this ShellFolder with the specified ShellFolder for order.
164 *
165 * @see #compareTo(Object)
166 */
167 public int compareTo(File file2) {
168 if (file2 == null || !(file2 instanceof ShellFolder)
169 || ((file2 instanceof ShellFolder) && ((ShellFolder)file2).isFileSystem())) {
170
171 if (isFileSystem()) {
172 return super.compareTo(file2);
173 } else {
174 return -1;
175 }
176 } else {
177 if (isFileSystem()) {
178 return 1;
179 } else {
180 return getName().compareTo(file2.getName());
181 }
182 }
183 }
184
185 /**
186 * @param getLargeIcon whether to return large icon (ignored in base implementation)
187 * @return The icon used to display this shell folder
188 */
189 public Image getIcon(boolean getLargeIcon) {
190 return null;
191 }
192
193
194 // Static
195
196 private static ShellFolderManager shellFolderManager;
197
198 static {
199 Class managerClass = (Class)Toolkit.getDefaultToolkit().
200 getDesktopProperty("Shell.shellFolderManager");
201 if (managerClass == null) {
202 managerClass = ShellFolderManager.class;
203 }
204 try {
205 shellFolderManager =
206 (ShellFolderManager)managerClass.newInstance();
207 } catch (InstantiationException e) {
208 throw new Error("Could not instantiate Shell Folder Manager: "
209 + managerClass.getName());
210 } catch (IllegalAccessException e) {
211 throw new Error ("Could not access Shell Folder Manager: "
212 + managerClass.getName());
213 }
214 }
215
216 /**
217 * Return a shell folder from a file object
218 * @exception FileNotFoundException if file does not exist
219 */
220 public static ShellFolder getShellFolder(File file) throws FileNotFoundException {
221 if (file instanceof ShellFolder) {
222 return (ShellFolder)file;
223 }
224 if (!file.exists()) {
225 throw new FileNotFoundException();
226 }
227 return shellFolderManager.createShellFolder(file);
228 }
229
230 /**
231 * @param key a <code>String</code>
232 * @return An Object matching the string <code>key</code>.
233 * @see ShellFolderManager#get(String)
234 */
235 public static Object get(String key) {
236 return shellFolderManager.get(key);
237 }
238
239 /**
240 * Does <code>dir</code> represent a "computer" such as a node on the network, or
241 * "My Computer" on the desktop.
242 */
243 public static boolean isComputerNode(File dir) {
244 return shellFolderManager.isComputerNode(dir);
245 }
246
247 /**
248 * @return Whether this is a file system root directory
249 */
250 public static boolean isFileSystemRoot(File dir) {
251 return shellFolderManager.isFileSystemRoot(dir);
252 }
253
254 /**
255 * Canonicalizes files that don't have symbolic links in their path.
256 * Normalizes files that do, preserving symbolic links from being resolved.
257 */
258 public static File getNormalizedFile(File f) throws IOException {
259 File canonical = f.getCanonicalFile();
260 if (f.equals(canonical)) {
261 // path of f doesn't contain symbolic links
262 return canonical;
263 }
264
265 // preserve symbolic links from being resolved
266 return new File(f.toURI().normalize());
267 }
268
269 // Override File methods
270
271 public static void sortFiles(List files) {
272 shellFolderManager.sortFiles(files);
273 }
274
275 public boolean isAbsolute() {
276 return (!isFileSystem() || super.isAbsolute());
277 }
278
279 public File getAbsoluteFile() {
280 return (isFileSystem() ? super.getAbsoluteFile() : this);
281 }
282
283 public boolean canRead() {
284 return (isFileSystem() ? super.canRead() : true); // ((Fix?))
285 }
286
287 /**
288 * Returns true if folder allows creation of children.
289 * True for the "Desktop" folder, but false for the "My Computer"
290 * folder.
291 */
292 public boolean canWrite() {
293 return (isFileSystem() ? super.canWrite() : false); // ((Fix?))
294 }
295
296 public boolean exists() {
297 // Assume top-level drives exist, because state is uncertain for
298 // removable drives.
299 return (!isFileSystem() || isFileSystemRoot(this) || super.exists()) ;
300 }
301
302 public boolean isDirectory() {
303 return (isFileSystem() ? super.isDirectory() : true); // ((Fix?))
304 }
305
306 public boolean isFile() {
307 return (isFileSystem() ? super.isFile() : !isDirectory()); // ((Fix?))
308 }
309
310 public long lastModified() {
311 return (isFileSystem() ? super.lastModified() : 0L); // ((Fix?))
312 }
313
314 public long length() {
315 return (isFileSystem() ? super.length() : 0L); // ((Fix?))
316 }
317
318 public boolean createNewFile() throws IOException {
319 return (isFileSystem() ? super.createNewFile() : false);
320 }
321
322 public boolean delete() {
323 return (isFileSystem() ? super.delete() : false); // ((Fix?))
324 }
325
326 public void deleteOnExit() {
327 if (isFileSystem()) {
328 super.deleteOnExit();
329 } else {
330 // Do nothing // ((Fix?))
331 }
332 }
333
334 public boolean mkdir() {
335 return (isFileSystem() ? super.mkdir() : false);
336 }
337
338 public boolean mkdirs() {
339 return (isFileSystem() ? super.mkdirs() : false);
340 }
341
342 public boolean renameTo(File dest) {
343 return (isFileSystem() ? super.renameTo(dest) : false); // ((Fix?))
344 }
345
346 public boolean setLastModified(long time) {
347 return (isFileSystem() ? super.setLastModified(time) : false); // ((Fix?))
348 }
349
350 public boolean setReadOnly() {
351 return (isFileSystem() ? super.setReadOnly() : false); // ((Fix?))
352 }
353
354 public String toString() {
355 return (isFileSystem() ? super.toString() : getDisplayName());
356 }
357
358 public static ShellFolderColumnInfo[] getFolderColumns(File dir) {
359 return shellFolderManager.getFolderColumns(dir);
360 }
361
362 public static Object getFolderColumnValue(File file, int column) {
363 return shellFolderManager.getFolderColumnValue(file, column);
364 }
365
366 public ShellFolderColumnInfo[] getFolderColumns() {
367 return null;
368 }
369
370 public Object getFolderColumnValue(int column) {
371 return null;
372 }
373}