author | prr |
Sat, 19 Sep 2015 15:45:59 -0700 | |
changeset 32865 | f9cb6e427f9e |
parent 31653 | d88ff422c7fb |
child 35675 | 1c5ad1244124 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
31653
d88ff422c7fb
8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents:
29922
diff
changeset
|
2 |
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. |
12047 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.apple.laf; |
|
27 |
||
29922 | 28 |
|
12047 | 29 |
import java.beans.*; |
30 |
import java.io.File; |
|
31 |
import java.util.*; |
|
32 |
import javax.swing.*; |
|
33 |
import javax.swing.event.ListDataEvent; |
|
34 |
import javax.swing.filechooser.FileSystemView; |
|
35 |
import javax.swing.table.AbstractTableModel; |
|
36 |
||
29922 | 37 |
import sun.misc.ManagedLocalsThread; |
12047 | 38 |
/** |
39 |
* NavServices-like implementation of a file Table |
|
40 |
* |
|
41 |
* Some of it came from BasicDirectoryModel |
|
42 |
*/ |
|
23649
f4f882f0056b
8035692: Fix serial lint warnings in mac-specific code
darcy
parents:
23010
diff
changeset
|
43 |
@SuppressWarnings("serial") // Superclass is not serializable across versions |
12047 | 44 |
class AquaFileSystemModel extends AbstractTableModel implements PropertyChangeListener { |
45 |
private final JTable fFileList; |
|
29922 | 46 |
private FilesLoader filesLoader = null; |
12047 | 47 |
private Vector<File> files = null; |
48 |
||
49 |
JFileChooser filechooser = null; |
|
50 |
Vector<SortableFile> fileCache = null; |
|
51 |
Object fileCacheLock; |
|
52 |
||
53 |
Vector<File> directories = null; |
|
54 |
int fetchID = 0; |
|
55 |
||
56 |
private final boolean fSortAscending[] = {true, true}; |
|
57 |
// private boolean fSortAscending = true; |
|
58 |
private boolean fSortNames = true; |
|
59 |
private final String[] fColumnNames; |
|
32865
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
31653
diff
changeset
|
60 |
public static final String SORT_BY_CHANGED = "sortByChanged"; |
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
31653
diff
changeset
|
61 |
public static final String SORT_ASCENDING_CHANGED = "sortAscendingChanged"; |
12047 | 62 |
|
63 |
public AquaFileSystemModel(final JFileChooser filechooser, final JTable filelist, final String[] colNames) { |
|
64 |
fileCacheLock = new Object(); |
|
65 |
this.filechooser = filechooser; |
|
66 |
fFileList = filelist; |
|
67 |
fColumnNames = colNames; |
|
68 |
validateFileCache(); |
|
69 |
updateSelectionMode(); |
|
70 |
} |
|
71 |
||
72 |
void updateSelectionMode() { |
|
73 |
// Save dialog lists can't be multi select, because all we're selecting is the next folder to open |
|
74 |
final boolean b = filechooser.isMultiSelectionEnabled() && filechooser.getDialogType() != JFileChooser.SAVE_DIALOG; |
|
75 |
fFileList.setSelectionMode(b ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION); |
|
76 |
} |
|
77 |
||
78 |
public void propertyChange(final PropertyChangeEvent e) { |
|
79 |
final String prop = e.getPropertyName(); |
|
80 |
if (prop == JFileChooser.DIRECTORY_CHANGED_PROPERTY || prop == JFileChooser.FILE_VIEW_CHANGED_PROPERTY || prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY || prop == JFileChooser.FILE_HIDING_CHANGED_PROPERTY) { |
|
81 |
invalidateFileCache(); |
|
82 |
validateFileCache(); |
|
83 |
} else if (prop.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) { |
|
84 |
updateSelectionMode(); |
|
85 |
} else if (prop == JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY) { |
|
86 |
invalidateFileCache(); |
|
87 |
validateFileCache(); |
|
88 |
} |
|
89 |
if (prop == SORT_BY_CHANGED) {// $ Ought to just resort |
|
90 |
fSortNames = (((Integer)e.getNewValue()).intValue() == 0); |
|
91 |
invalidateFileCache(); |
|
92 |
validateFileCache(); |
|
93 |
fFileList.repaint(); |
|
94 |
} |
|
95 |
if (prop == SORT_ASCENDING_CHANGED) { |
|
96 |
final int sortColumn = (fSortNames ? 0 : 1); |
|
97 |
fSortAscending[sortColumn] = ((Boolean)e.getNewValue()).booleanValue(); |
|
98 |
invalidateFileCache(); |
|
99 |
validateFileCache(); |
|
100 |
fFileList.repaint(); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
public void invalidateFileCache() { |
|
105 |
files = null; |
|
106 |
directories = null; |
|
107 |
||
108 |
synchronized(fileCacheLock) { |
|
109 |
if (fileCache != null) { |
|
110 |
final int lastRow = fileCache.size(); |
|
111 |
fileCache = null; |
|
112 |
fireTableRowsDeleted(0, lastRow); |
|
113 |
} |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
public Vector<File> getDirectories() { |
|
118 |
if (directories != null) { return directories; } |
|
119 |
return directories; |
|
120 |
} |
|
121 |
||
122 |
public Vector<File> getFiles() { |
|
123 |
if (files != null) { return files; } |
|
124 |
files = new Vector<File>(); |
|
125 |
directories = new Vector<File>(); |
|
126 |
directories.addElement(filechooser.getFileSystemView().createFileObject(filechooser.getCurrentDirectory(), "..")); |
|
127 |
||
128 |
synchronized(fileCacheLock) { |
|
129 |
for (int i = 0; i < fileCache.size(); i++) { |
|
130 |
final SortableFile sf = fileCache.elementAt(i); |
|
131 |
final File f = sf.fFile; |
|
132 |
if (filechooser.isTraversable(f)) { |
|
133 |
directories.addElement(f); |
|
134 |
} else { |
|
135 |
files.addElement(f); |
|
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
return files; |
|
141 |
} |
|
142 |
||
143 |
public void runWhenDone(final Runnable runnable){ |
|
144 |
synchronized (fileCacheLock) { |
|
29922 | 145 |
if (filesLoader != null) { |
146 |
if (filesLoader.loadThread.isAlive()) { |
|
147 |
filesLoader.queuedTasks.add(runnable); |
|
12047 | 148 |
return; |
149 |
} |
|
150 |
} |
|
151 |
||
152 |
SwingUtilities.invokeLater(runnable); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
public void validateFileCache() { |
|
157 |
final File currentDirectory = filechooser.getCurrentDirectory(); |
|
158 |
||
159 |
if (currentDirectory == null) { |
|
160 |
invalidateFileCache(); |
|
161 |
return; |
|
162 |
} |
|
163 |
||
29922 | 164 |
if (filesLoader != null) { |
12047 | 165 |
// interrupt |
29922 | 166 |
filesLoader.loadThread.interrupt(); |
12047 | 167 |
} |
168 |
||
169 |
fetchID++; |
|
170 |
||
171 |
// PENDING(jeff) pick the size more sensibly |
|
172 |
invalidateFileCache(); |
|
173 |
synchronized(fileCacheLock) { |
|
174 |
fileCache = new Vector<SortableFile>(50); |
|
175 |
} |
|
176 |
||
29922 | 177 |
filesLoader = new FilesLoader(currentDirectory, fetchID); |
12047 | 178 |
} |
179 |
||
180 |
public int getColumnCount() { |
|
181 |
return 2; |
|
182 |
} |
|
183 |
||
184 |
public String getColumnName(final int col) { |
|
185 |
return fColumnNames[col]; |
|
186 |
} |
|
187 |
||
188 |
public Class<? extends Object> getColumnClass(final int col) { |
|
189 |
if (col == 0) return File.class; |
|
190 |
return Date.class; |
|
191 |
} |
|
192 |
||
193 |
public int getRowCount() { |
|
194 |
synchronized(fileCacheLock) { |
|
195 |
if (fileCache != null) { |
|
196 |
return fileCache.size(); |
|
197 |
} |
|
198 |
return 0; |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
// SAK: Part of fix for 3168263. The fileCache contains |
|
203 |
// SortableFiles, so when finding a file in the list we need to |
|
204 |
// first create a sortable file. |
|
205 |
public boolean contains(final File o) { |
|
206 |
synchronized(fileCacheLock) { |
|
207 |
if (fileCache != null) { |
|
208 |
return fileCache.contains(new SortableFile(o)); |
|
209 |
} |
|
210 |
return false; |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
public int indexOf(final File o) { |
|
215 |
synchronized(fileCacheLock) { |
|
216 |
if (fileCache != null) { |
|
217 |
final boolean isAscending = fSortNames ? fSortAscending[0] : fSortAscending[1]; |
|
218 |
final int row = fileCache.indexOf(new SortableFile(o)); |
|
219 |
return isAscending ? row : fileCache.size() - row - 1; |
|
220 |
} |
|
221 |
return 0; |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
// AbstractListModel interface |
|
226 |
public Object getElementAt(final int row) { |
|
227 |
return getValueAt(row, 0); |
|
228 |
} |
|
229 |
||
230 |
// AbstractTableModel interface |
|
231 |
||
232 |
public Object getValueAt(int row, final int col) { |
|
233 |
if (row < 0 || col < 0) return null; |
|
234 |
final boolean isAscending = fSortNames ? fSortAscending[0] : fSortAscending[1]; |
|
235 |
synchronized(fileCacheLock) { |
|
236 |
if (fileCache != null) { |
|
237 |
if (!isAscending) row = fileCache.size() - row - 1; |
|
238 |
return fileCache.elementAt(row).getValueAt(col); |
|
239 |
} |
|
240 |
return null; |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
// PENDING(jeff) - implement |
|
245 |
public void intervalAdded(final ListDataEvent e) { |
|
246 |
} |
|
247 |
||
248 |
// PENDING(jeff) - implement |
|
249 |
public void intervalRemoved(final ListDataEvent e) { |
|
250 |
} |
|
251 |
||
252 |
protected void sort(final Vector<Object> v) { |
|
253 |
if (fSortNames) sSortNames.quickSort(v, 0, v.size() - 1); |
|
254 |
else sSortDates.quickSort(v, 0, v.size() - 1); |
|
255 |
} |
|
256 |
||
257 |
// Liberated from the 1.1 SortDemo |
|
258 |
// |
|
259 |
// This is a generic version of C.A.R Hoare's Quick Sort |
|
260 |
// algorithm. This will handle arrays that are already |
|
261 |
// sorted, and arrays with duplicate keys.<BR> |
|
262 |
// |
|
263 |
// If you think of a one dimensional array as going from |
|
264 |
// the lowest index on the left to the highest index on the right |
|
265 |
// then the parameters to this function are lowest index or |
|
266 |
// left and highest index or right. The first time you call |
|
267 |
// this function it will be with the parameters 0, a.length - 1. |
|
268 |
// |
|
269 |
// @param a an integer array |
|
270 |
// @param lo0 left boundary of array partition |
|
271 |
// @param hi0 right boundary of array partition |
|
272 |
abstract class QuickSort { |
|
273 |
final void quickSort(final Vector<Object> v, final int lo0, final int hi0) { |
|
274 |
int lo = lo0; |
|
275 |
int hi = hi0; |
|
276 |
SortableFile mid; |
|
277 |
||
278 |
if (hi0 > lo0) { |
|
279 |
// Arbitrarily establishing partition element as the midpoint of |
|
280 |
// the array. |
|
281 |
mid = (SortableFile)v.elementAt((lo0 + hi0) / 2); |
|
282 |
||
283 |
// loop through the array until indices cross |
|
284 |
while (lo <= hi) { |
|
285 |
// find the first element that is greater than or equal to |
|
286 |
// the partition element starting from the left Index. |
|
287 |
// |
|
288 |
// Nasty to have to cast here. Would it be quicker |
|
289 |
// to copy the vectors into arrays and sort the arrays? |
|
290 |
while ((lo < hi0) && lt((SortableFile)v.elementAt(lo), mid)) { |
|
291 |
++lo; |
|
292 |
} |
|
293 |
||
294 |
// find an element that is smaller than or equal to |
|
295 |
// the partition element starting from the right Index. |
|
296 |
while ((hi > lo0) && lt(mid, (SortableFile)v.elementAt(hi))) { |
|
297 |
--hi; |
|
298 |
} |
|
299 |
||
300 |
// if the indexes have not crossed, swap |
|
301 |
if (lo <= hi) { |
|
302 |
swap(v, lo, hi); |
|
303 |
++lo; |
|
304 |
--hi; |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
// If the right index has not reached the left side of array |
|
309 |
// must now sort the left partition. |
|
310 |
if (lo0 < hi) { |
|
311 |
quickSort(v, lo0, hi); |
|
312 |
} |
|
313 |
||
314 |
// If the left index has not reached the right side of array |
|
315 |
// must now sort the right partition. |
|
316 |
if (lo < hi0) { |
|
317 |
quickSort(v, lo, hi0); |
|
318 |
} |
|
319 |
||
320 |
} |
|
321 |
} |
|
322 |
||
323 |
private final void swap(final Vector<Object> a, final int i, final int j) { |
|
324 |
final Object T = a.elementAt(i); |
|
325 |
a.setElementAt(a.elementAt(j), i); |
|
326 |
a.setElementAt(T, j); |
|
327 |
} |
|
328 |
||
329 |
protected abstract boolean lt(SortableFile a, SortableFile b); |
|
330 |
} |
|
331 |
||
332 |
class QuickSortNames extends QuickSort { |
|
333 |
protected boolean lt(final SortableFile a, final SortableFile b) { |
|
334 |
final String aLower = a.fName.toLowerCase(); |
|
335 |
final String bLower = b.fName.toLowerCase(); |
|
336 |
return aLower.compareTo(bLower) < 0; |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
class QuickSortDates extends QuickSort { |
|
341 |
protected boolean lt(final SortableFile a, final SortableFile b) { |
|
342 |
return a.fDateValue < b.fDateValue; |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
// for speed in sorting, displaying |
|
347 |
class SortableFile /* extends FileView */{ |
|
348 |
File fFile; |
|
349 |
String fName; |
|
350 |
long fDateValue; |
|
351 |
Date fDate; |
|
352 |
||
353 |
SortableFile(final File f) { |
|
354 |
fFile = f; |
|
355 |
fName = fFile.getName(); |
|
356 |
fDateValue = fFile.lastModified(); |
|
357 |
fDate = new Date(fDateValue); |
|
358 |
} |
|
359 |
||
360 |
public Object getValueAt(final int col) { |
|
361 |
if (col == 0) return fFile; |
|
362 |
return fDate; |
|
363 |
} |
|
364 |
||
365 |
public boolean equals(final Object other) { |
|
366 |
final SortableFile otherFile = (SortableFile)other; |
|
367 |
return otherFile.fFile.equals(fFile); |
|
368 |
} |
|
25142
5b781839611d
8046271: Fix overrides lint warnings in Apple laf code
darcy
parents:
23649
diff
changeset
|
369 |
|
5b781839611d
8046271: Fix overrides lint warnings in Apple laf code
darcy
parents:
23649
diff
changeset
|
370 |
@Override |
5b781839611d
8046271: Fix overrides lint warnings in Apple laf code
darcy
parents:
23649
diff
changeset
|
371 |
public int hashCode() { |
5b781839611d
8046271: Fix overrides lint warnings in Apple laf code
darcy
parents:
23649
diff
changeset
|
372 |
return Objects.hashCode(fFile); |
5b781839611d
8046271: Fix overrides lint warnings in Apple laf code
darcy
parents:
23649
diff
changeset
|
373 |
} |
12047 | 374 |
} |
375 |
||
29922 | 376 |
class FilesLoader implements Runnable { |
377 |
Vector<Runnable> queuedTasks = new Vector<>(); |
|
12047 | 378 |
File currentDirectory = null; |
379 |
int fid; |
|
29922 | 380 |
Thread loadThread; |
12047 | 381 |
|
29922 | 382 |
public FilesLoader(final File currentDirectory, final int fid) { |
12047 | 383 |
this.currentDirectory = currentDirectory; |
384 |
this.fid = fid; |
|
29922 | 385 |
String name = "Aqua L&F File Loading Thread"; |
31653
d88ff422c7fb
8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents:
29922
diff
changeset
|
386 |
this.loadThread = new ManagedLocalsThread(this, name); |
29922 | 387 |
this.loadThread.start(); |
12047 | 388 |
} |
389 |
||
29922 | 390 |
@Override |
12047 | 391 |
public void run() { |
392 |
final Vector<DoChangeContents> runnables = new Vector<DoChangeContents>(10); |
|
393 |
final FileSystemView fileSystem = filechooser.getFileSystemView(); |
|
394 |
||
395 |
final File[] list = fileSystem.getFiles(currentDirectory, filechooser.isFileHidingEnabled()); |
|
396 |
||
397 |
final Vector<Object> acceptsList = new Vector<Object>(); |
|
398 |
||
399 |
for (final File element : list) { |
|
400 |
// Return all files to the file chooser. The UI will disable or enable |
|
401 |
// the file name if the current filter approves. |
|
402 |
acceptsList.addElement(new SortableFile(element)); |
|
403 |
} |
|
404 |
||
405 |
// Sort based on settings. |
|
406 |
sort(acceptsList); |
|
407 |
||
408 |
// Don't separate directories from files |
|
409 |
Vector<SortableFile> chunk = new Vector<SortableFile>(10); |
|
410 |
final int listSize = acceptsList.size(); |
|
411 |
// run through list grabbing file/dirs in chunks of ten |
|
412 |
for (int i = 0; i < listSize;) { |
|
413 |
SortableFile f; |
|
414 |
for (int j = 0; j < 10 && i < listSize; j++, i++) { |
|
415 |
f = (SortableFile)acceptsList.elementAt(i); |
|
416 |
chunk.addElement(f); |
|
417 |
} |
|
418 |
final DoChangeContents runnable = new DoChangeContents(chunk, fid); |
|
419 |
runnables.addElement(runnable); |
|
420 |
SwingUtilities.invokeLater(runnable); |
|
421 |
chunk = new Vector<SortableFile>(10); |
|
29922 | 422 |
if (loadThread.isInterrupted()) { |
12047 | 423 |
// interrupted, cancel all runnables |
424 |
cancelRunnables(runnables); |
|
425 |
return; |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
synchronized (fileCacheLock) { |
|
430 |
for (final Runnable r : queuedTasks) { |
|
431 |
SwingUtilities.invokeLater(r); |
|
432 |
} |
|
433 |
} |
|
434 |
} |
|
435 |
||
436 |
public void cancelRunnables(final Vector<DoChangeContents> runnables) { |
|
437 |
for (int i = 0; i < runnables.size(); i++) { |
|
438 |
runnables.elementAt(i).cancel(); |
|
439 |
} |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
class DoChangeContents implements Runnable { |
|
444 |
private Vector<SortableFile> contentFiles; |
|
445 |
private boolean doFire = true; |
|
446 |
private final Object lock = new Object(); |
|
447 |
private final int fid; |
|
448 |
||
449 |
public DoChangeContents(final Vector<SortableFile> files, final int fid) { |
|
450 |
this.contentFiles = files; |
|
451 |
this.fid = fid; |
|
452 |
} |
|
453 |
||
454 |
synchronized void cancel() { |
|
455 |
synchronized(lock) { |
|
456 |
doFire = false; |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
public void run() { |
|
461 |
if (fetchID == fid) { |
|
462 |
synchronized(lock) { |
|
463 |
if (doFire) { |
|
464 |
synchronized(fileCacheLock) { |
|
465 |
if (fileCache != null) { |
|
466 |
for (int i = 0; i < contentFiles.size(); i++) { |
|
467 |
fileCache.addElement(contentFiles.elementAt(i)); |
|
468 |
fireTableRowsInserted(i, i); |
|
469 |
} |
|
470 |
} |
|
471 |
} |
|
472 |
} |
|
473 |
contentFiles = null; |
|
474 |
directories = null; |
|
475 |
} |
|
476 |
} |
|
477 |
} |
|
478 |
} |
|
479 |
||
480 |
final QuickSortNames sSortNames = new QuickSortNames(); |
|
481 |
final QuickSortDates sSortDates = new QuickSortDates(); |
|
482 |
} |