1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52:
53:
56: public class BasicDirectoryModel extends AbstractListModel
57: implements PropertyChangeListener
58: {
59:
60: private Vector contents;
61:
62:
63: private int directories;
64:
65:
66: private int listingMode;
67:
68:
69: private JFileChooser filechooser;
70:
71:
72: private Comparator comparator = new Comparator()
73: {
74: public int compare(Object o1, Object o2)
75: {
76: if (lt((File) o1, (File) o2))
77: return -1;
78: else
79: return 1;
80: }
81: };
82:
83:
88: public BasicDirectoryModel(JFileChooser filechooser)
89: {
90: this.filechooser = filechooser;
91: filechooser.addPropertyChangeListener(this);
92: listingMode = filechooser.getFileSelectionMode();
93: contents = new Vector();
94: }
95:
96:
103: public boolean contains(Object o)
104: {
105: return contents.contains(o);
106: }
107:
108:
111: public void fireContentsChanged()
112: {
113: fireContentsChanged(this, 0, getSize() - 1);
114: }
115:
116:
121: public Vector getDirectories()
122: {
123: Vector tmp = new Vector();
124: for (int i = 0; i < directories; i++)
125: tmp.add(contents.get(i));
126: return tmp;
127: }
128:
129:
136: public Object getElementAt(int index)
137: {
138: if (index > getSize() - 1)
139: return null;
140: if (listingMode == JFileChooser.FILES_ONLY)
141: return contents.get(directories + index);
142: else
143: return contents.elementAt(index);
144: }
145:
146:
151: public Vector getFiles()
152: {
153: Vector tmp = new Vector();
154: for (int i = directories; i < getSize(); i++)
155: tmp.add(contents.get(i));
156: return tmp;
157: }
158:
159:
164: public int getSize()
165: {
166: if (listingMode == JFileChooser.DIRECTORIES_ONLY)
167: return directories;
168: else if (listingMode == JFileChooser.FILES_ONLY)
169: return contents.size() - directories;
170: return contents.size();
171: }
172:
173:
180: public int indexOf(Object o)
181: {
182: if (listingMode == JFileChooser.FILES_ONLY)
183: return contents.indexOf(o) - directories;
184: return contents.indexOf(o);
185: }
186:
187:
192: public void intervalAdded(ListDataEvent e)
193: {
194:
195: }
196:
197:
202: public void intervalRemoved(ListDataEvent e)
203: {
204:
205: }
206:
207:
210: public void invalidateFileCache()
211: {
212:
213: }
214:
215:
223: protected boolean lt(File a, File b)
224: {
225: boolean aTrav = filechooser.isTraversable(a);
226: boolean bTrav = filechooser.isTraversable(b);
227:
228: if (aTrav == bTrav)
229: {
230: String aname = a.getName().toLowerCase();
231: String bname = b.getName().toLowerCase();
232: return ((aname.compareTo(bname) < 0) ? true : false);
233: }
234: else
235: {
236: if (aTrav)
237: return true;
238: else
239: return false;
240: }
241: }
242:
243:
248: public void propertyChange(PropertyChangeEvent e)
249: {
250: if (e.getPropertyName().equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY))
251: listingMode = filechooser.getFileSelectionMode();
252: }
253:
254:
262: public boolean renameFile(File oldFile, File newFile)
263: {
264:
265: return false;
266: }
267:
268:
273: protected void sort(Vector v)
274: {
275: Collections.sort(v, comparator);
276: Enumeration e = Collections.enumeration(v);
277: Vector tmp = new Vector();
278: for (; e.hasMoreElements();)
279: tmp.add(e.nextElement());
280:
281: contents = tmp;
282: }
283:
284:
287: public void validateFileCache()
288: {
289: contents.clear();
290: directories = 0;
291: FileSystemView fsv = filechooser.getFileSystemView();
292: File[] list = fsv.getFiles(filechooser.getCurrentDirectory(),
293: filechooser.isFileHidingEnabled());
294:
295: if (list == null)
296: return;
297:
298: for (int i = 0; i < list.length; i++)
299: {
300: if (list[i] == null)
301: continue;
302: if (filechooser.accept(list[i]))
303: {
304: contents.add(list[i]);
305: if (filechooser.isTraversable(list[i]))
306: directories++;
307: }
308: }
309: sort(contents);
310: filechooser.revalidate();
311: filechooser.repaint();
312: }
313: }