blob: 592fa1b580f43039ca000eb72e30d8598c24f463 [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.io.File;
29import java.io.FileNotFoundException;
30import java.util.*;
31import javax.swing.SwingConstants;
32
33/**
34 * @author Michael Martak
35 * @since 1.4
36 */
37
38class ShellFolderManager {
39 private static final String COLUMN_NAME = "FileChooser.fileNameHeaderText";
40 private static final String COLUMN_SIZE = "FileChooser.fileSizeHeaderText";
41 private static final String COLUMN_DATE = "FileChooser.fileDateHeaderText";
42
43 /**
44 * Create a shell folder from a file.
45 * Override to return machine-dependent behavior.
46 */
47 public ShellFolder createShellFolder(File file) throws FileNotFoundException {
48 return new DefaultShellFolder(null, file);
49 }
50
51 /**
52 * @param key a <code>String</code>
53 * "fileChooserDefaultFolder":
54 * Returns a <code>File</code> - the default shellfolder for a new filechooser
55 * "roots":
56 * Returns a <code>File[]</code> - containing the root(s) of the displayable hieararchy
57 * "fileChooserComboBoxFolders":
58 * Returns a <code>File[]</code> - an array of shellfolders representing the list to
59 * show by default in the file chooser's combobox
60 * "fileChooserShortcutPanelFolders":
61 * Returns a <code>File[]</code> - an array of shellfolders representing well-known
62 * folders, such as Desktop, Documents, History, Network, Home, etc.
63 * This is used in the shortcut panel of the filechooser on Windows 2000
64 * and Windows Me.
65 * "fileChooserIcon nn":
66 * Returns an <code>Image</code> - icon nn from resource 124 in comctl32.dll (Windows only).
67 *
68 * @return An Object matching the key string.
69 */
70 public Object get(String key) {
71 if (key.equals("fileChooserDefaultFolder")) {
72 // Return the default shellfolder for a new filechooser
73 File homeDir = new File(System.getProperty("user.home"));
74 try {
75 return createShellFolder(homeDir);
76 } catch (FileNotFoundException e) {
77 return homeDir;
78 }
79 } else if (key.equals("roots")) {
80 // The root(s) of the displayable hieararchy
81 return File.listRoots();
82 } else if (key.equals("fileChooserComboBoxFolders")) {
83 // Return an array of ShellFolders representing the list to
84 // show by default in the file chooser's combobox
85 return get("roots");
86 } else if (key.equals("fileChooserShortcutPanelFolders")) {
87 // Return an array of ShellFolders representing well-known
88 // folders, such as Desktop, Documents, History, Network, Home, etc.
89 // This is used in the shortcut panel of the filechooser on Windows 2000
90 // and Windows Me
91 return new File[] { (File)get("fileChooserDefaultFolder") };
92 }
93 return null;
94 }
95
96 /**
97 * Does <code>dir</code> represent a "computer" such as a node on the network, or
98 * "My Computer" on the desktop.
99 */
100 public boolean isComputerNode(File dir) {
101 return false;
102 }
103
104 public boolean isFileSystemRoot(File dir) {
105 if (dir instanceof ShellFolder && !((ShellFolder)dir).isFileSystem()) {
106 return false;
107 }
108 return (dir.getParentFile() == null);
109 }
110
111 public void sortFiles(List files) {
112 Collections.sort(files, fileComparator);
113 }
114
115 private Comparator fileComparator = new Comparator() {
116 public int compare(Object a, Object b) {
117 return compare((File)a, (File)b);
118 }
119
120 public int compare(File f1, File f2) {
121 ShellFolder sf1 = null;
122 ShellFolder sf2 = null;
123
124 if (f1 instanceof ShellFolder) {
125 sf1 = (ShellFolder)f1;
126 if (sf1.isFileSystem()) {
127 sf1 = null;
128 }
129 }
130 if (f2 instanceof ShellFolder) {
131 sf2 = (ShellFolder)f2;
132 if (sf2.isFileSystem()) {
133 sf2 = null;
134 }
135 }
136
137 if (sf1 != null && sf2 != null) {
138 return sf1.compareTo(sf2);
139 } else if (sf1 != null) {
140 return -1; // Non-file shellfolders sort before files
141 } else if (sf2 != null) {
142 return 1;
143 } else {
144 String name1 = f1.getName();
145 String name2 = f2.getName();
146
147 // First ignore case when comparing
148 int diff = name1.toLowerCase().compareTo(name2.toLowerCase());
149 if (diff != 0) {
150 return diff;
151 } else {
152 // May differ in case (e.g. "mail" vs. "Mail")
153 // We need this test for consistent sorting
154 return name1.compareTo(name2);
155 }
156 }
157 }
158 };
159
160 public ShellFolderColumnInfo[] getFolderColumns(File dir) {
161 ShellFolderColumnInfo[] columns = null;
162
163 if (dir instanceof ShellFolder) {
164 columns = ((ShellFolder)dir).getFolderColumns();
165 }
166
167 if (columns == null) {
168 columns = new ShellFolderColumnInfo[]{
169 new ShellFolderColumnInfo(COLUMN_NAME, 150,
170 SwingConstants.LEADING, true, null,
171 fileComparator),
172 new ShellFolderColumnInfo(COLUMN_SIZE, 75,
173 SwingConstants.RIGHT, true, null,
174 ComparableComparator.getInstance(), true),
175 new ShellFolderColumnInfo(COLUMN_DATE, 130,
176 SwingConstants.LEADING, true, null,
177 ComparableComparator.getInstance(), true)
178 };
179 }
180
181 return columns;
182 }
183
184 public Object getFolderColumnValue(File file, int column) {
185 if (file instanceof ShellFolder) {
186 Object value = ((ShellFolder)file).getFolderColumnValue(column);
187 if (value != null) {
188 return value;
189 }
190 }
191
192 if (file == null || !file.exists()) {
193 return null;
194 }
195
196 switch (column) {
197 case 0:
198 // By default, file name will be rendered using getSystemDisplayName()
199 return file;
200
201 case 1: // size
202 return file.isDirectory() ? null : new Long(file.length());
203
204 case 2: // date
205 if (isFileSystemRoot(file)) {
206 return null;
207 }
208 long time = file.lastModified();
209 return (time == 0L) ? null : new Date(time);
210
211 default:
212 return null;
213 }
214 }
215
216 /**
217 * This class provides a default comparator for the default column set
218 */
219 private static class ComparableComparator implements Comparator {
220 private static Comparator instance;
221
222 public static Comparator getInstance() {
223 if (instance == null) {
224 instance = new ComparableComparator();
225 }
226 return instance;
227 }
228
229 public int compare(Object o1, Object o2) {
230 int gt;
231
232 if (o1 == null && o2 == null) {
233 gt = 0;
234 } else if (o1 != null && o2 == null) {
235 gt = 1;
236 } else if (o1 == null && o2 != null) {
237 gt = -1;
238 } else if (o1 instanceof Comparable) {
239 gt = ((Comparable) o1).compareTo(o2);
240 } else {
241 gt = 0;
242 }
243
244 return gt;
245 }
246 }
247
248}