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 Sergey Burlak
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.beans.PropertyChangeEvent;
024: import java.beans.PropertyChangeListener;
025: import java.io.File;
026: import java.util.Collections;
027: import java.util.Comparator;
028: import java.util.Vector;
029:
030: import javax.swing.AbstractListModel;
031: import javax.swing.JFileChooser;
032: import javax.swing.event.ListDataEvent;
033: import javax.swing.filechooser.FileFilter;
034:
035: public class BasicDirectoryModel extends AbstractListModel implements
036: PropertyChangeListener {
037: private Vector<java.io.File> fileList;
038: private JFileChooser fc;
039: private final Comparator<File> fileComparator = new Comparator<File>() {
040: public int compare(final File o1, final File o2) {
041: return lt(o1, o2) ? -1 : 1;
042: }
043: };
044:
045: public Vector<java.io.File> getFiles() {
046: return fileList;
047: }
048:
049: public BasicDirectoryModel(final JFileChooser filechooser) {
050: fc = filechooser;
051: fc.addPropertyChangeListener(this );
052: fileList = new Vector<java.io.File>();
053: }
054:
055: public Object getElementAt(final int index) {
056: return fileList.get(index);
057: }
058:
059: public int getSize() {
060: return fileList.size();
061: }
062:
063: public void propertyChange(final PropertyChangeEvent event) {
064: String changedProperty = event.getPropertyName();
065: if (JFileChooser.FILE_FILTER_CHANGED_PROPERTY
066: .equals(changedProperty)
067: || JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY
068: .equals(changedProperty)
069: || JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY
070: .equals(changedProperty)) {
071:
072: fc.rescanCurrentDirectory();
073: }
074: }
075:
076: public void invalidateFileCache() {
077: }
078:
079: public Vector<java.io.File> getDirectories() {
080: return null;
081: }
082:
083: public void validateFileCache() {
084: fileList.clear();
085:
086: if (fc.getCurrentDirectory() == null
087: || !fc.getCurrentDirectory().isDirectory()) {
088: fireContentsChanged();
089: return;
090: }
091:
092: File[] files = fc.getFileSystemView().getFiles(
093: fc.getCurrentDirectory(), fc.isFileHidingEnabled());
094: if (files == null || files.length == 0) {
095: fireContentsChanged();
096: return;
097: }
098:
099: FileFilter filter = fc.getFileFilter();
100: for (int i = 0; i < files.length; i++) {
101: File file = files[i];
102:
103: if ((file.isDirectory()
104: && (fc.isDirectorySelectionEnabled() || fc
105: .isTraversable(file)) || file.isFile()
106: && fc.isFileSelectionEnabled())
107: && (filter == null || filter.accept(file))) {
108:
109: fileList.add(file);
110: }
111: }
112: sort(fileList);
113:
114: fireContentsChanged();
115: }
116:
117: public boolean renameFile(final File oldFile, final File newFile) {
118: if (oldFile.renameTo(newFile)) {
119: validateFileCache();
120: fc.setSelectedFile(newFile);
121:
122: return true;
123: }
124:
125: return false;
126: }
127:
128: public void fireContentsChanged() {
129: fireContentsChanged(this , -1, -1);
130: }
131:
132: public boolean contains(final Object o) {
133: return fileList.contains(o);
134: }
135:
136: public int indexOf(final Object o) {
137: return fileList.indexOf(o);
138: }
139:
140: public void intervalAdded(final ListDataEvent e) {
141: }
142:
143: public void intervalRemoved(final ListDataEvent e) {
144: }
145:
146: protected void sort(final Vector<? extends java.io.File> v) {
147: Collections.sort(v, fileComparator);
148: }
149:
150: protected boolean lt(final File file1, final File file2) {
151: if (file1.isDirectory() && !file2.isDirectory()) {
152: return true;
153: }
154: if (!file1.isDirectory() && file2.isDirectory()) {
155: return false;
156: }
157:
158: return fileList.indexOf(file1) < fileList.indexOf(file2);
159: }
160: }
|