1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
/* BasicDirectoryModel.java --
Copyright (C) 2005, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.plaf.basic;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.AbstractListModel;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.event.ListDataEvent;
import javax.swing.filechooser.FileSystemView;
/**
* Implements an AbstractListModel for directories where the source
* of the files is a JFileChooser object.
*
* This class is used for sorting and ordering the file list in
* a JFileChooser L&F object.
*/
public class BasicDirectoryModel extends AbstractListModel
implements PropertyChangeListener
{
/** The list of files itself */
private Vector contents;
/**
* The directories in the list.
*/
private Vector directories;
/**
* The files in the list.
*/
private Vector files;
/** The listing mode of the associated JFileChooser,
either FILES_ONLY, DIRECTORIES_ONLY or FILES_AND_DIRECTORIES */
private int listingMode;
/** The JFileCooser associated with this model */
private JFileChooser filechooser;
/**
* The thread that loads the file view.
*/
private DirectoryLoadThread loadThread;
/**
* This thread is responsible for loading file lists from the
* current directory and updating the model.
*/
private class DirectoryLoadThread extends Thread
{
/**
* Updates the Swing list model.
*/
private class UpdateSwingRequest
implements Runnable
{
private List added;
private int addIndex;
private List removed;
private int removeIndex;
private boolean cancel;
UpdateSwingRequest(List add, int ai, List rem, int ri)
{
added = add;
addIndex = ai;
removed = rem;
removeIndex = ri;
cancel = false;
}
public void run()
{
if (! cancel)
{
int numRemoved = removed == null ? 0 : removed.size();
int numAdded = added == null ? 0 : added.size();
synchronized (contents)
{
if (numRemoved > 0)
contents.removeAll(removed);
if (numAdded > 0)
contents.addAll(added);
files = null;
directories = null;
}
if (numRemoved > 0 && numAdded == 0)
fireIntervalRemoved(BasicDirectoryModel.this, removeIndex,
removeIndex + numRemoved - 1);
else if (numRemoved == 0 && numAdded > 0)
fireIntervalAdded(BasicDirectoryModel.this, addIndex,
addIndex + numAdded - 1);
else
fireContentsChanged();
}
}
void cancel()
{
cancel = true;
}
}
/**
* The directory beeing loaded.
*/
File directory;
/**
* Stores all UpdateSwingRequests that are sent to the event queue.
*/
private UpdateSwingRequest pending;
/**
* Creates a new DirectoryLoadThread that loads the specified
* directory.
*
* @param dir the directory to load
*/
DirectoryLoadThread(File dir)
{
super("Basic L&F directory loader");
directory = dir;
}
public void run()
{
FileSystemView fsv = filechooser.getFileSystemView();
File[] files = fsv.getFiles(directory,
filechooser.isFileHidingEnabled());
// Occasional check if we have been interrupted.
if (isInterrupted())
return;
// Check list for accepted files.
Vector accepted = new Vector();
for (int i = 0; i < files.length; i++)
{
if (filechooser.accept(files[i]))
accepted.add(files[i]);
}
// Occasional check if we have been interrupted.
if (isInterrupted())
return;
// Sort list.
sort(accepted);
// Now split up directories from files so that we get the directories
// listed before the files.
Vector newFiles = new Vector();
Vector newDirectories = new Vector();
for (Iterator i = accepted.iterator(); i.hasNext();)
{
File f = (File) i.next();
boolean traversable = filechooser.isTraversable(f);
if (traversable)
newDirectories.add(f);
else if (! traversable && filechooser.isFileSelectionEnabled())
newFiles.add(f);
// Occasional check if we have been interrupted.
if (isInterrupted())
return;
}
// Build up new file cache. Try to update only the changed elements.
// This will be important for actions like adding new files or
// directories inside a large file list.
Vector newCache = new Vector(newDirectories);
newCache.addAll(newFiles);
int newSize = newCache.size();
int oldSize = contents.size();
if (newSize < oldSize)
{
// Check for removed interval.
int start = -1;
int end = -1;
boolean found = false;
for (int i = 0; i < newSize && !found; i++)
{
if (! newCache.get(i).equals(contents.get(i)))
{
start = i;
end = i + oldSize - newSize;
found = true;
}
}
if (start >= 0 && end > start
&& contents.subList(end, oldSize)
.equals(newCache.subList(start, newSize)))
{
// Occasional check if we have been interrupted.
if (isInterrupted())
return;
Vector removed = new Vector(contents.subList(start, end));
UpdateSwingRequest r = new UpdateSwingRequest(null, 0,
removed, start);
invokeLater(r);
newCache = null;
}
}
else if (newSize > oldSize)
{
// Check for inserted interval.
int start = oldSize;
int end = newSize;
boolean found = false;
for (int i = 0; i < oldSize && ! found; i++)
{
if (! newCache.get(i).equals(contents.get(i)))
{
start = i;
boolean foundEnd = false;
for (int j = i; j < newSize && ! foundEnd; j++)
{
if (newCache.get(j).equals(contents.get(i)))
{
end = j;
foundEnd = true;
}
}
end = i + oldSize - newSize;
}
}
if (start >= 0 && end > start
&& newCache.subList(end, newSize)
.equals(contents.subList(start, oldSize)))
{
// Occasional check if we have been interrupted.
if (isInterrupted())
return;
List added = newCache.subList(start, end);
UpdateSwingRequest r = new UpdateSwingRequest(added, start,
null, 0);
invokeLater(r);
newCache = null;
}
}
// Handle complete list changes (newCache != null).
if (newCache != null && ! contents.equals(newCache))
{
// Occasional check if we have been interrupted.
if (isInterrupted())
return;
UpdateSwingRequest r = new UpdateSwingRequest(newCache, 0,
contents, 0);
invokeLater(r);
}
}
/**
* Wraps SwingUtilities.invokeLater() and stores the request in
* a Vector so that we can still cancel it later.
*
* @param update the request to invoke
*/
private void invokeLater(UpdateSwingRequest update)
{
pending = update;
SwingUtilities.invokeLater(update);
}
/**
* Cancels all pending update requests that might be in the AWT
* event queue.
*/
void cancelPending()
{
if (pending != null)
pending.cancel();
}
}
/** A Comparator class/object for sorting the file list. */
private Comparator comparator = new Comparator()
{
public int compare(Object o1, Object o2)
{
if (lt((File) o1, (File) o2))
return -1;
else
return 1;
}
};
/**
* Creates a new BasicDirectoryModel object.
*
* @param filechooser DOCUMENT ME!
*/
public BasicDirectoryModel(JFileChooser filechooser)
{
this.filechooser = filechooser;
filechooser.addPropertyChangeListener(this);
listingMode = filechooser.getFileSelectionMode();
contents = new Vector();
validateFileCache();
}
/**
* Returns whether a given (File) object is included in the list.
*
* @param o - The file object to test.
*
* @return <code>true</code> if the list contains the given object.
*/
public boolean contains(Object o)
{
return contents.contains(o);
}
/**
* Fires a content change event.
*/
public void fireContentsChanged()
{
fireContentsChanged(this, 0, getSize() - 1);
}
/**
* Returns a Vector of (java.io.File) objects containing
* the directories in this list.
*
* @return a Vector
*/
public Vector<File> getDirectories()
{
// Synchronize this with the UpdateSwingRequest for the case when
// contents is modified.
synchronized (contents)
{
Vector dirs = directories;
if (dirs == null)
{
// Initializes this in getFiles().
getFiles();
dirs = directories;
}
return dirs;
}
}
/**
* Returns the (java.io.File) object at
* an index in the list.
*
* @param index The list index
* @return a File object
*/
public Object getElementAt(int index)
{
if (index > getSize() - 1)
return null;
return contents.elementAt(index);
}
/**
* Returns a Vector of (java.io.File) objects containing
* the files in this list.
*
* @return a Vector
*/
public Vector<File> getFiles()
{
synchronized (contents)
{
Vector f = files;
if (f == null)
{
f = new Vector();
Vector d = new Vector(); // Directories;
for (Iterator i = contents.iterator(); i.hasNext();)
{
File file = (File) i.next();
if (filechooser.isTraversable(file))
d.add(file);
else
f.add(file);
}
files = f;
directories = d;
}
return f;
}
}
/**
* Returns the size of the list, which only includes directories
* if the JFileChooser is set to DIRECTORIES_ONLY.
*
* Otherwise, both directories and files are included in the count.
*
* @return The size of the list.
*/
public int getSize()
{
return contents.size();
}
/**
* Returns the index of an (java.io.File) object in the list.
*
* @param o The object - normally a File.
*
* @return the index of that object, or -1 if it is not in the list.
*/
public int indexOf(Object o)
{
return contents.indexOf(o);
}
/**
* Obsoleted method which does nothing.
*/
public void intervalAdded(ListDataEvent e)
{
// obsoleted
}
/**
* Obsoleted method which does nothing.
*/
public void intervalRemoved(ListDataEvent e)
{
// obsoleted
}
/**
* Obsoleted method which does nothing.
*/
public void invalidateFileCache()
{
// obsoleted
}
/**
* Less than, determine the relative order in the list of two files
* for sorting purposes.
*
* The order is: directories < files, and thereafter alphabetically,
* using the default locale collation.
*
* @param a the first file
* @param b the second file
*
* @return <code>true</code> if a > b, <code>false</code> if a < b.
*/
protected boolean lt(File a, File b)
{
boolean aTrav = filechooser.isTraversable(a);
boolean bTrav = filechooser.isTraversable(b);
if (aTrav == bTrav)
{
String aname = a.getName().toLowerCase();
String bname = b.getName().toLowerCase();
return (aname.compareTo(bname) < 0) ? true : false;
}
else
{
if (aTrav)
return true;
else
return false;
}
}
/**
* Listens for a property change; the change in file selection mode of the
* associated JFileChooser. Reloads the file cache on that event.
*
* @param e - A PropertyChangeEvent.
*/
public void propertyChange(PropertyChangeEvent e)
{
String property = e.getPropertyName();
if (property.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)
|| property.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)
|| property.equals(JFileChooser.FILE_HIDING_CHANGED_PROPERTY)
|| property.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)
|| property.equals(JFileChooser.FILE_VIEW_CHANGED_PROPERTY)
)
{
validateFileCache();
}
}
/**
* Renames a file - However, does <I>not</I> re-sort the list
* or replace the old file with the new one in the list.
*
* @param oldFile The old file
* @param newFile The new file name
*
* @return <code>true</code> if the rename succeeded
*/
public boolean renameFile(File oldFile, File newFile)
{
return oldFile.renameTo( newFile );
}
/**
* Sorts a Vector of File objects.
*
* @param v The Vector to sort.
*/
protected void sort(Vector<? extends File> v)
{
Collections.sort(v, comparator);
}
/**
* Re-loads the list of files
*/
public void validateFileCache()
{
File dir = filechooser.getCurrentDirectory();
if (dir != null)
{
// Cancel all pending requests.
if (loadThread != null)
{
loadThread.interrupt();
loadThread.cancelPending();
}
loadThread = new DirectoryLoadThread(dir);
loadThread.start();
}
}
}
|