-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
BasicDirectoryModel.java
565 lines (506 loc) · 19.2 KB
/
BasicDirectoryModel.java
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
/*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.swing.plaf.basic;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.AbstractListModel;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.event.ListDataEvent;
import javax.swing.filechooser.FileSystemView;
import sun.awt.shell.ShellFolder;
/**
* Basic implementation of a file list.
*
* @author Jeff Dinkins
*/
@SuppressWarnings("serial") // Superclass is not serializable across versions
public class BasicDirectoryModel extends AbstractListModel<Object> implements PropertyChangeListener {
private final JFileChooser filechooser;
// PENDING(jeff) pick the size more sensibly
private final Vector<File> fileCache = new Vector<File>(50);
private FilesLoader filesLoader = null;
private Vector<File> files = null;
private Vector<File> directories = null;
private final AtomicInteger fetchID = new AtomicInteger();
private PropertyChangeSupport changeSupport;
private boolean busy = false;
/**
* Constructs a new instance of {@code BasicDirectoryModel}.
*
* @param filechooser an instance of {JFileChooser}
*/
public BasicDirectoryModel(JFileChooser filechooser) {
this.filechooser = filechooser;
validateFileCache();
}
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if(prop == JFileChooser.DIRECTORY_CHANGED_PROPERTY ||
prop == JFileChooser.FILE_VIEW_CHANGED_PROPERTY ||
prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY ||
prop == JFileChooser.FILE_HIDING_CHANGED_PROPERTY ||
prop == JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY) {
validateFileCache();
} else if ("UI".equals(prop)) {
Object old = e.getOldValue();
if (old instanceof BasicFileChooserUI) {
BasicFileChooserUI ui = (BasicFileChooserUI) old;
BasicDirectoryModel model = ui.getModel();
if (model != null) {
model.invalidateFileCache();
}
}
} else if ("JFileChooserDialogIsClosingProperty".equals(prop)) {
invalidateFileCache();
}
}
/**
* This method is used to interrupt file loading thread.
*/
public void invalidateFileCache() {
if (filesLoader != null) {
filesLoader.loadThread.interrupt();
filesLoader.cancelRunnables();
filesLoader = null;
}
}
/**
* Returns a list of directories.
*
* @return a list of directories
*/
public Vector<File> getDirectories() {
synchronized(fileCache) {
if (directories != null) {
return directories;
}
Vector<File> fls = getFiles();
return directories;
}
}
/**
* Returns a list of files.
*
* @return a list of files
*/
public Vector<File> getFiles() {
synchronized(fileCache) {
if (files != null) {
return files;
}
files = new Vector<File>();
directories = new Vector<File>();
directories.addElement(filechooser.getFileSystemView().createFileObject(
filechooser.getCurrentDirectory(), "..")
);
for (int i = 0; i < getSize(); i++) {
File f = fileCache.get(i);
if (filechooser.isTraversable(f)) {
directories.add(f);
} else {
files.add(f);
}
}
return files;
}
}
/**
* Validates content of file cache.
*/
public void validateFileCache() {
File currentDirectory = filechooser.getCurrentDirectory();
if (currentDirectory == null) {
return;
}
if (filesLoader != null) {
filesLoader.loadThread.interrupt();
filesLoader.cancelRunnables();
}
int fid = fetchID.incrementAndGet();
setBusy(true, fid);
filesLoader = new FilesLoader(currentDirectory, fid);
}
/**
* Renames a file in the underlying file system.
*
* @param oldFile a <code>File</code> object representing
* the existing file
* @param newFile a <code>File</code> object representing
* the desired new file name
* @return <code>true</code> if rename succeeded,
* otherwise <code>false</code>
* @since 1.4
*/
public boolean renameFile(File oldFile, File newFile) {
synchronized(fileCache) {
if (oldFile.renameTo(newFile)) {
validateFileCache();
return true;
}
return false;
}
}
/**
* Invoked when a content is changed.
*/
public void fireContentsChanged() {
fireContentsChanged(this, 0, getSize() - 1);
}
public int getSize() {
return fileCache.size();
}
/**
* Returns {@code true} if an element {@code o} is in file cache,
* otherwise, returns {@code false}.
*
* @param o an element
* @return {@code true} if an element {@code o} is in file cache
*/
public boolean contains(Object o) {
return fileCache.contains(o);
}
/**
* Returns an index of element {@code o} in file cache.
*
* @param o an element
* @return an index of element {@code o} in file cache
*/
public int indexOf(Object o) {
return fileCache.indexOf(o);
}
public Object getElementAt(int index) {
return fileCache.get(index);
}
/**
* Obsolete - not used. This method is a no-op.
* @param e list data event
* @deprecated Obsolete method, not used anymore.
*/
@Deprecated(since = "17", forRemoval = true)
public void intervalAdded(ListDataEvent e) {
}
/**
* Obsolete - not used. This method is a no-op.
* @param e list data event
* @deprecated Obsolete method, not used anymore.
*/
@Deprecated(since = "17", forRemoval = true)
public void intervalRemoved(ListDataEvent e) {
}
/**
* Sorts a list of files.
*
* @param v a list of files
*/
protected void sort(Vector<? extends File> v){
ShellFolder.sort(v);
}
/**
* Obsolete - not used
* @return a comparison of the file names
* @param a a file
* @param b another file
* @deprecated Obsolete method, not used anymore.
*/
@Deprecated(since = "17", forRemoval = true)
protected boolean lt(File a, File b) {
// First ignore case when comparing
int diff = a.getName().toLowerCase().compareTo(b.getName().toLowerCase());
if (diff != 0) {
return diff < 0;
} else {
// May differ in case (e.g. "mail" vs. "Mail")
return a.getName().compareTo(b.getName()) < 0;
}
}
private final class FilesLoader implements Runnable {
private final FileSystemView fileSystemView;
private final boolean useFileHiding;
private final boolean fileSelectionEnabled;
private final int fid;
private final File currentDirectory;
private volatile DoChangeContents runnable;
private final Thread loadThread;
private FilesLoader(File currentDirectory, int fid) {
this.currentDirectory = currentDirectory;
this.fid = fid;
fileSystemView = filechooser.getFileSystemView();
useFileHiding = filechooser.isFileHidingEnabled();
fileSelectionEnabled = filechooser.isFileSelectionEnabled();
String name = "Basic L&F File Loading Thread";
loadThread = new Thread(null, this, name, 0, false);
loadThread.start();
}
@Override
public void run() {
run0();
setBusy(false, fid);
}
private void run0() {
FileSystemView fileSystem = fileSystemView;
if (loadThread.isInterrupted()) {
return;
}
File[] list = fileSystem.getFiles(currentDirectory, useFileHiding);
if (loadThread.isInterrupted()) {
return;
}
final Vector<File> newFileCache = new Vector<File>();
Vector<File> newFiles = new Vector<File>();
// run through the file list, add directories and selectable files to fileCache
// Note that this block must be OUTSIDE of Invoker thread because of
// deadlock possibility with custom synchronized FileSystemView
for (File file : list) {
if (filechooser.accept(file)) {
boolean isTraversable = filechooser.isTraversable(file);
if (isTraversable) {
newFileCache.addElement(file);
} else if (fileSelectionEnabled) {
newFiles.addElement(file);
}
if (loadThread.isInterrupted()) {
return;
}
}
}
// First sort alphabetically by filename
sort(newFileCache);
sort(newFiles);
newFileCache.addAll(newFiles);
// To avoid loads of synchronizations with Invoker and improve performance we
// execute the whole block on the COM thread
runnable = ShellFolder.invoke(new Callable<DoChangeContents>() {
public DoChangeContents call() {
int newSize = newFileCache.size();
int oldSize = fileCache.size();
if (newSize > oldSize) {
//see if interval is added
int start = oldSize;
int end = newSize;
for (int i = 0; i < oldSize; i++) {
if (!newFileCache.get(i).equals(fileCache.get(i))) {
start = i;
for (int j = i; j < newSize; j++) {
if (newFileCache.get(j).equals(fileCache.get(i))) {
end = j;
break;
}
}
break;
}
}
if (start >= 0 && end > start) {
List<File> listStart_OldSize = new Vector<>(fileCache.subList(start, oldSize));
if (newFileCache.subList(end, newSize).equals(listStart_OldSize)) {
if (loadThread.isInterrupted()) {
return null;
}
return new DoChangeContents(newFileCache.subList(start, end), start, null, 0, fid);
}
}
} else if (newSize < oldSize) {
//see if interval is removed
int start = -1;
int end = -1;
for (int i = 0; i < newSize; i++) {
if (!newFileCache.get(i).equals(fileCache.get(i))) {
start = i;
end = i + oldSize - newSize;
break;
}
}
if (start >= 0 && end > start) {
List<File> listEnd_OldSize = new Vector<>(fileCache.subList(end, oldSize));
if (listEnd_OldSize.equals(newFileCache.subList(start, newSize))) {
if (loadThread.isInterrupted()) {
return null;
}
return new DoChangeContents(null, 0, new Vector<>(fileCache.subList(start, end)), start, fid);
}
}
}
if (!fileCache.equals(newFileCache)) {
if (loadThread.isInterrupted()) {
cancelRunnables();
}
return new DoChangeContents(newFileCache, 0, fileCache, 0, fid);
}
return null;
}
});
if (runnable != null && !loadThread.isInterrupted()) {
SwingUtilities.invokeLater(runnable);
}
}
private void cancelRunnables() {
if (runnable != null) {
runnable.cancel();
}
}
}
/**
* Adds a PropertyChangeListener to the listener list. The listener is
* registered for all bound properties of this class.
* <p>
* If <code>listener</code> is <code>null</code>,
* no exception is thrown and no action is performed.
*
* @param listener the property change listener to be added
*
* @see #removePropertyChangeListener
* @see #getPropertyChangeListeners
*
* @since 1.6
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
if (changeSupport == null) {
changeSupport = new PropertyChangeSupport(this);
}
changeSupport.addPropertyChangeListener(listener);
}
/**
* Removes a PropertyChangeListener from the listener list.
* <p>
* If listener is null, no exception is thrown and no action is performed.
*
* @param listener the PropertyChangeListener to be removed
*
* @see #addPropertyChangeListener
* @see #getPropertyChangeListeners
*
* @since 1.6
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
if (changeSupport != null) {
changeSupport.removePropertyChangeListener(listener);
}
}
/**
* Returns an array of all the property change listeners
* registered on this component.
*
* @return all of this component's <code>PropertyChangeListener</code>s
* or an empty array if no property change
* listeners are currently registered
*
* @see #addPropertyChangeListener
* @see #removePropertyChangeListener
* @see java.beans.PropertyChangeSupport#getPropertyChangeListeners
*
* @since 1.6
*/
public PropertyChangeListener[] getPropertyChangeListeners() {
if (changeSupport == null) {
return new PropertyChangeListener[0];
}
return changeSupport.getPropertyChangeListeners();
}
/**
* Support for reporting bound property changes for boolean properties.
* This method can be called when a bound property has changed and it will
* send the appropriate PropertyChangeEvent to any registered
* PropertyChangeListeners.
*
* @param propertyName the property whose value has changed
* @param oldValue the property's previous value
* @param newValue the property's new value
*
* @since 1.6
*/
protected void firePropertyChange(String propertyName,
Object oldValue, Object newValue) {
if (changeSupport != null) {
changeSupport.firePropertyChange(propertyName,
oldValue, newValue);
}
}
/**
* Set the busy state for the model. The model is considered
* busy when it is running a separate (interruptible)
* thread in order to load the contents of a directory.
*/
private synchronized void setBusy(final boolean busy, int fid) {
if (fid == fetchID.get()) {
boolean oldValue = this.busy;
this.busy = busy;
if (changeSupport != null && busy != oldValue) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
firePropertyChange("busy", !busy, busy);
}
});
}
}
}
private final class DoChangeContents implements Runnable {
private final List<File> addFiles;
private final List<File> remFiles;
private boolean doFire = true;
private final int fid;
private int addStart = 0;
private int remStart = 0;
DoChangeContents(List<File> addFiles, int addStart, List<File> remFiles,
int remStart, int fid) {
this.addFiles = addFiles;
this.addStart = addStart;
this.remFiles = remFiles;
this.remStart = remStart;
this.fid = fid;
}
synchronized void cancel() {
doFire = false;
}
public synchronized void run() {
if (fetchID.get() == fid && doFire) {
int remSize = (remFiles == null) ? 0 : remFiles.size();
int addSize = (addFiles == null) ? 0 : addFiles.size();
synchronized(fileCache) {
if (remSize > 0) {
fileCache.removeAll(remFiles);
}
if (addSize > 0) {
fileCache.addAll(addStart, addFiles);
}
files = null;
directories = null;
}
if (remSize > 0 && addSize == 0) {
fireIntervalRemoved(BasicDirectoryModel.this, remStart, remStart + remSize - 1);
} else if (addSize > 0 && remSize == 0 && addStart + addSize <= fileCache.size()) {
fireIntervalAdded(BasicDirectoryModel.this, addStart, addStart + addSize - 1);
} else {
fireContentsChanged();
}
}
}
}
}