001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anton Avtamonov, Sergey Burlak
020: * @version $Revision$
021: */package javax.swing.filechooser;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.text.MessageFormat;
026: import java.util.Arrays;
027: import java.util.Comparator;
028: import java.util.LinkedList;
029: import java.util.List;
030:
031: import javax.swing.Icon;
032: import javax.swing.UIManager;
033:
034: import org.apache.harmony.misc.SystemUtils;
035: import org.apache.harmony.x.swing.filechooser.PlatformFile;
036: import org.apache.harmony.x.swing.filechooser.PlatformFileManager;
037:
038: import org.apache.harmony.x.swing.internal.nls.Messages;
039:
040: public abstract class FileSystemView {
041: private static FileSystemView instance;
042:
043: private static abstract class AbstractFileSystemView extends
044: FileSystemView {
045: public File createNewFolder(final File containingDir)
046: throws IOException {
047: String fileName = getFirstFolderName();
048: int i = 1;
049: while (exists(containingDir, fileName)) {
050: fileName = getSubsequentFolderName(i++);
051: }
052:
053: File result = createFileObject(containingDir, fileName);
054: result.mkdir();
055:
056: return result;
057: }
058:
059: private boolean exists(final File dir, final String fileName) {
060: File[] files = getFiles(dir, false);
061: if (files == null) {
062: return false;
063: }
064: for (int i = 0; i < files.length; i++) {
065: if (fileName.equals(files[i].getName())) {
066: return true;
067: }
068: }
069:
070: return false;
071: }
072:
073: private String getSubsequentFolderName(
074: final int sequencialNumber) {
075: return MessageFormat.format(
076: getSubsequentFolderNamePattern(),
077: new Object[] { new Integer(sequencialNumber) });
078: }
079:
080: protected abstract String getFirstFolderName();
081:
082: protected abstract String getSubsequentFolderNamePattern();
083: }
084:
085: private static class WindowsFileSystemView extends
086: AbstractFileSystemView {
087: private static final String NEW_FOLDER_NAME = UIManager
088: .getString("FileChooser.win32.newFolder");
089: private static final String NEW_FOLDER_SUBSEQUENT_NAME = UIManager
090: .getString("FileChooser.win32.newFolder.subsequent");
091:
092: private static PlatformFileManager fileManager = createManager();
093:
094: protected String getFirstFolderName() {
095: return NEW_FOLDER_NAME;
096: }
097:
098: protected String getSubsequentFolderNamePattern() {
099: return NEW_FOLDER_SUBSEQUENT_NAME;
100: }
101:
102: public boolean isRoot(final File f) {
103: File[] roots = getRoots();
104: for (int i = 0; i < roots.length; i++) {
105: if (roots[i].equals(f)) {
106: return true;
107: }
108: }
109: return false;
110: }
111:
112: public String getSystemDisplayName(final File f) {
113: if (f == null) {
114: return null;
115: }
116:
117: PlatformFile ef = getPlatformFile(f);
118: return ef != null ? ef.getDisplayName() : super
119: .getSystemDisplayName(f);
120: }
121:
122: public String getSystemTypeDescription(final File f) {
123: if (f == null) {
124: return null;
125: }
126:
127: PlatformFile ef = getPlatformFile(f);
128: return ef != null ? ef.getTypeName() : super
129: .getSystemTypeDescription(f);
130: }
131:
132: public Icon getSystemIcon(final File f) {
133: if (f == null) {
134: return null;
135: }
136:
137: PlatformFile ef = getPlatformFile(f);
138: return ef != null ? ef.getIcon() : super .getSystemIcon(f);
139: }
140:
141: public boolean isFileSystem(final File f) {
142: PlatformFile ef = getPlatformFile(f);
143: return ef != null ? ef.isFileSystem() : true;
144: }
145:
146: public boolean isFileSystemRoot(final File dir) {
147: return isDrive(dir);
148: }
149:
150: public boolean isDrive(final File dir) {
151: PlatformFile ef = getPlatformFile(dir);
152: return ef != null ? ef.isDrive() : super
153: .isFileSystemRoot(dir);
154: }
155:
156: public boolean isFloppyDrive(final File dir) {
157: PlatformFile ef = getPlatformFile(dir);
158: return ef != null ? ef.isFloppyDrive() : false;
159: }
160:
161: public boolean isHiddenFile(final File f) {
162: PlatformFile ef = getPlatformFile(f);
163: return ef != null ? ef.isHidden() : false;
164: }
165:
166: public boolean isComputerNode(final File dir) {
167: PlatformFile ef = getPlatformFile(dir);
168: return ef != null ? ef.isComputerNode() : false;
169: }
170:
171: public File[] getRoots() {
172: return new File[] { fileManager.getHomeFolder() };
173: }
174:
175: public File getDefaultDirectory() {
176: return fileManager.getDefaultFolder();
177: }
178:
179: public File getHomeDirectory() {
180: return fileManager.getHomeFolder();
181: }
182:
183: public File[] getFiles(final File dir,
184: final boolean useFileHiding) {
185: File ef = getPlatformFile(dir);
186: File root = ef != null ? ef : dir;
187: File[] files = root.listFiles();
188: if (useFileHiding) {
189: List result = new LinkedList();
190: for (int i = 0; i < files.length; i++) {
191: if (!files[i].isHidden()) {
192: result.add(files[i]);
193: }
194: }
195:
196: return (File[]) result.toArray(new File[result.size()]);
197: }
198: return files;
199: }
200:
201: public File getParentDirectory(final File dir) {
202: if (dir == null) {
203: return null;
204: }
205:
206: File ef = getPlatformFile(dir);
207: File root = ef != null ? ef : dir;
208: return root.getParentFile();
209: }
210:
211: private PlatformFile getPlatformFile(final File file) {
212: return fileManager.getPlatformFile(file);
213: }
214:
215: private static PlatformFileManager createManager() {
216: try {
217: return (PlatformFileManager) Class
218: .forName(
219: "org.apache.harmony.x.swing.filechooser.windows.WinFileManager")
220: .newInstance();
221: } catch (Exception e) {
222: throw new RuntimeException(e);
223: }
224: }
225: }
226:
227: private static class LinuxFileSystemView extends
228: OtherFileSystemView {
229: public File[] getRoots() {
230: return new File[] { new File("/") };
231: }
232: }
233:
234: private static class OtherFileSystemView extends
235: AbstractFileSystemView {
236: private static final String NEW_FOLDER_NAME = UIManager
237: .getString("FileChooser.other.newFolder");
238: private static final String NEW_FOLDER_SUBSEQUENT_NAME = UIManager
239: .getString("FileChooser.other.newFolder.subsequent");
240:
241: protected String getFirstFolderName() {
242: return NEW_FOLDER_NAME;
243: }
244:
245: protected String getSubsequentFolderNamePattern() {
246: return NEW_FOLDER_SUBSEQUENT_NAME;
247: }
248: }
249:
250: private Comparator CASE_INSENSITIVE_COMPARATOR = new Comparator() {
251: public int compare(final Object o1, final Object o2) {
252: return o1.toString().compareToIgnoreCase(o2.toString());
253: }
254: };
255:
256: public static FileSystemView getFileSystemView() {
257: if (instance == null) {
258: if (SystemUtils.getOS() == SystemUtils.OS_WINDOWS) {
259: instance = new WindowsFileSystemView();
260: } else if (SystemUtils.getOS() == SystemUtils.OS_LINUX) {
261: instance = new LinuxFileSystemView();
262: } else {
263: instance = new OtherFileSystemView();
264: }
265: }
266:
267: return instance;
268: }
269:
270: public abstract File createNewFolder(final File containingDir)
271: throws IOException;
272:
273: public File createFileObject(final File dir, final String fileName) {
274: return new File(dir, fileName);
275: }
276:
277: public File createFileObject(final String path) {
278: return new File(path);
279: }
280:
281: public File getChild(final File parent, final String fileName) {
282: if (parent == null || !parent.exists()) {
283: return createFileObject(parent, fileName);
284: }
285: File[] files = parent.listFiles();
286: for (int i = 0; i < files.length; i++) {
287: if (files[i].getName().equals(fileName)) {
288: return files[i];
289: }
290: }
291:
292: return createFileObject(parent, fileName);
293: }
294:
295: public File getDefaultDirectory() {
296: return new File(System.getProperty("user.home"));
297: }
298:
299: public File[] getFiles(final File dir, final boolean useFileHiding) {
300: File[] result = dir.listFiles();
301: if (useFileHiding) {
302: List filtered = new LinkedList();
303: for (int i = 0; i < result.length; i++) {
304: if (!result[i].isHidden()) {
305: filtered.add(result[i]);
306: }
307: }
308:
309: result = (File[]) filtered
310: .toArray(new File[filtered.size()]);
311: }
312:
313: if (result != null) {
314: Arrays.sort(result, CASE_INSENSITIVE_COMPARATOR);
315: } else {
316: result = new File[0];
317: }
318:
319: return result;
320: }
321:
322: public File getHomeDirectory() {
323: return new File(System.getProperty("user.home"));
324: }
325:
326: public File getParentDirectory(final File dir) {
327: return dir != null && !isRoot(dir) ? dir.getParentFile() : null;
328: }
329:
330: public File[] getRoots() {
331: return File.listRoots();
332: }
333:
334: public String getSystemDisplayName(final File f) {
335: if (f == null) {
336: return null;
337: }
338:
339: return f.getName();
340: }
341:
342: public Icon getSystemIcon(final File f) {
343: return null;
344: }
345:
346: public String getSystemTypeDescription(final File f) {
347: if (f.isDirectory()) {
348: return "Folder";
349: } else if (f.isFile()) {
350: return "File";
351: }
352: return null;
353: }
354:
355: public boolean isComputerNode(final File dir) {
356: return false;
357: }
358:
359: public boolean isDrive(final File dir) {
360: return false;
361: }
362:
363: public boolean isFileSystem(final File f) {
364: return true;
365: }
366:
367: public boolean isFileSystemRoot(final File dir) {
368: File[] roots = File.listRoots();
369: for (int i = 0; i < roots.length; i++) {
370: if (roots[i].equals(dir)) {
371: return true;
372: }
373: }
374:
375: return false;
376: }
377:
378: public boolean isFloppyDrive(final File dir) {
379: return false;
380: }
381:
382: public boolean isHiddenFile(final File f) {
383: return f.isHidden();
384: }
385:
386: public boolean isParent(final File folder, final File file) {
387: return getParentDirectory(file).equals(folder);
388: }
389:
390: public boolean isRoot(final File f) {
391: File[] roots = getRoots();
392: for (int i = 0; i < roots.length; i++) {
393: if (roots[i].equals(f)) {
394: return true;
395: }
396: }
397: return false;
398: }
399:
400: public Boolean isTraversable(final File f) {
401: return Boolean.valueOf(isDrive(f) || f.isDirectory()
402: && f.canRead());
403: }
404:
405: protected File createFileSystemRoot(final File f) {
406: throw new UnsupportedOperationException(Messages
407: .getString("swing.27")); //$NON-NLS-1$
408: }
409: }
|