001: /*
002: * Copyright 2008 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.gui.tree.model;
017:
018: import java.io.File;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import javax.swing.filechooser.FileSystemView;
023:
024: /**
025: * FileModel that uses the FileSystemView from the FileChooser
026: * TODO handle DOS with no root filesystem
027: * @author teknopaul
028: *
029: */
030: public class FileSystemModel implements ItemModel {
031:
032: private static final FileSystemView fsv = FileSystemView
033: .getFileSystemView();
034: private File file;
035: private String defaultName;
036: private boolean showHidden;
037:
038: public static FileSystemModel factory(String defaultName,
039: boolean showHidden) {
040: File[] roots = fsv.getRoots();
041: if (roots.length == 1) {
042: return new FileSystemModel(roots[0], defaultName,
043: showHidden);
044: } else {
045: return new FileSystemModel(new MockFile(null, "/"),
046: defaultName, showHidden);
047: }
048: }
049:
050: public FileSystemModel(File file, String defaultName,
051: boolean showHidden) {
052: this .file = file;
053: this .defaultName = defaultName;
054: this .showHidden = showHidden;
055: }
056:
057: public String getName() {
058: String typeDesc = fsv.getSystemTypeDescription(file);
059: if (typeDesc == null) {
060: return file.getName();
061: }
062: return typeDesc;
063: }
064:
065: public boolean exists() {
066: return file.exists();
067: }
068:
069: public boolean isHidden() {
070: if (showHidden) {
071: return true;
072: } else {
073: return fsv.isHiddenFile(file);
074: }
075: }
076:
077: public String getFullPath() {
078: return file.getAbsolutePath();
079: }
080:
081: public boolean isLeaf() {
082: if (!file.exists()) {
083: return false;
084: }
085: return file.isFile();
086: }
087:
088: public Object getData() {
089: return file;
090: }
091:
092: public ItemModel[] getChildren() {
093: File[] children = null;
094: if (file instanceof MockFile) {
095: children = fsv.getRoots();
096: } else if (!exists()) {
097: return new ItemModel[0];
098: } else {
099: children = fsv.getFiles(file, !showHidden);
100: }
101:
102: List toShow = new ArrayList();
103: boolean containsDefault = false;
104: for (int i = 0; i < children.length; i++) {
105: if (children[i].getName().equals(defaultName)) {
106: containsDefault = true;
107: }
108: toShow.add(new FileSystemModel(children[i], defaultName,
109: showHidden));
110: }
111: if (!containsDefault) {
112: toShow.add(0, new FileSystemModel(new File(file,
113: defaultName), defaultName, showHidden));
114: }
115: ItemModel[] toReturn = new ItemModel[toShow.size()];
116: return (ItemModel[]) toShow.toArray(toReturn);
117: }
118:
119: public int compareTo(Object o) {
120: if (!file.exists()) {
121: return -1;
122: }
123: ItemModel comp = (ItemModel) o;
124: return file.compareTo(comp.getData());
125: }
126:
127: public void setShowHidden(boolean showHidden) {
128: this .showHidden = showHidden;
129: }
130:
131: /**
132: * File holder for the situation where the filesystem
133: * has not root i.e. Windoze95
134: * @author teknopaul
135: *
136: */
137: public static class MockFile extends File {
138:
139: public MockFile(File parent, String child) {
140: super(parent, child);
141: }
142:
143: }
144:
145: }
|