aboutsummaryrefslogtreecommitdiff
path: root/src/macosx/classes/com/apple/laf/AquaFileSystemModel.java
blob: 31c249349ea481520ff6ed18ebe22ad0ef111b1d (plain)
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
/*
 * Copyright (c) 2011, 2012, 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 com.apple.laf;

import java.beans.*;
import java.io.File;
import java.util.*;

import javax.swing.*;
import javax.swing.event.ListDataEvent;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.AbstractTableModel;

/**
 * NavServices-like implementation of a file Table
 *
 * Some of it came from BasicDirectoryModel
 */
class AquaFileSystemModel extends AbstractTableModel implements PropertyChangeListener {
    private final JTable fFileList;
    private LoadFilesThread loadThread = null;
    private Vector<File> files = null;

    JFileChooser filechooser = null;
    Vector<SortableFile> fileCache = null;
    Object fileCacheLock;

    Vector<File> directories = null;
    int fetchID = 0;

    private final boolean fSortAscending[] = {true, true};
    // private boolean fSortAscending = true;
    private boolean fSortNames = true;
    private final String[] fColumnNames;
    public final static String SORT_BY_CHANGED = "sortByChanged";
    public final static String SORT_ASCENDING_CHANGED = "sortAscendingChanged";

    public AquaFileSystemModel(final JFileChooser filechooser, final JTable filelist, final String[] colNames) {
        fileCacheLock = new Object();
        this.filechooser = filechooser;
        fFileList = filelist;
        fColumnNames = colNames;
        validateFileCache();
        updateSelectionMode();
    }

    void updateSelectionMode() {
        // Save dialog lists can't be multi select, because all we're selecting is the next folder to open
        final boolean b = filechooser.isMultiSelectionEnabled() && filechooser.getDialogType() != JFileChooser.SAVE_DIALOG;
        fFileList.setSelectionMode(b ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION);
    }

    public void propertyChange(final PropertyChangeEvent e) {
        final 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) {
            invalidateFileCache();
            validateFileCache();
        } else if (prop.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) {
            updateSelectionMode();
        } else if (prop == JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY) {
            invalidateFileCache();
            validateFileCache();
        }
        if (prop == SORT_BY_CHANGED) {// $ Ought to just resort
            fSortNames = (((Integer)e.getNewValue()).intValue() == 0);
            invalidateFileCache();
            validateFileCache();
            fFileList.repaint();
        }
        if (prop == SORT_ASCENDING_CHANGED) {
            final int sortColumn = (fSortNames ? 0 : 1);
            fSortAscending[sortColumn] = ((Boolean)e.getNewValue()).booleanValue();
            invalidateFileCache();
            validateFileCache();
            fFileList.repaint();
        }
    }

    public void invalidateFileCache() {
        files = null;
        directories = null;

        synchronized(fileCacheLock) {
            if (fileCache != null) {
                final int lastRow = fileCache.size();
                fileCache = null;
                fireTableRowsDeleted(0, lastRow);
            }
        }
    }

    public Vector<File> getDirectories() {
        if (directories != null) { return directories; }
        return directories;
    }

    public Vector<File> getFiles() {
        if (files != null) { return files; }
        files = new Vector<File>();
        directories = new Vector<File>();
        directories.addElement(filechooser.getFileSystemView().createFileObject(filechooser.getCurrentDirectory(), ".."));

        synchronized(fileCacheLock) {
            for (int i = 0; i < fileCache.size(); i++) {
                final SortableFile sf = fileCache.elementAt(i);
                final File f = sf.fFile;
                if (filechooser.isTraversable(f)) {
                    directories.addElement(f);
                } else {
                    files.addElement(f);
                }
            }
        }

        return files;
    }

    public void runWhenDone(final Runnable runnable){
         synchronized (fileCacheLock) {
             if (loadThread != null) {
                 if (loadThread.isAlive()) {
                     loadThread.queuedTasks.add(runnable);
                     return;
                 }
             }

             SwingUtilities.invokeLater(runnable);
         }
     }

    public void validateFileCache() {
        final File currentDirectory = filechooser.getCurrentDirectory();

        if (currentDirectory == null) {
            invalidateFileCache();
            return;
        }

        if (loadThread != null) {
            // interrupt
            loadThread.interrupt();
        }

        fetchID++;

        // PENDING(jeff) pick the size more sensibly
        invalidateFileCache();
        synchronized(fileCacheLock) {
            fileCache = new Vector<SortableFile>(50);
        }

        loadThread = new LoadFilesThread(currentDirectory, fetchID);
        loadThread.start();
    }

    public int getColumnCount() {
        return 2;
    }

    public String getColumnName(final int col) {
        return fColumnNames[col];
    }

    public Class<? extends Object> getColumnClass(final int col) {
        if (col == 0) return File.class;
        return Date.class;
    }

    public int getRowCount() {
        synchronized(fileCacheLock) {
            if (fileCache != null) {
                return fileCache.size();
            }
            return 0;
        }
    }

    // SAK: Part of fix for 3168263. The fileCache contains
    // SortableFiles, so when finding a file in the list we need to
    // first create a sortable file.
    public boolean contains(final File o) {
        synchronized(fileCacheLock) {
            if (fileCache != null) {
                return fileCache.contains(new SortableFile(o));
            }
            return false;
        }
    }

    public int indexOf(final File o) {
        synchronized(fileCacheLock) {
            if (fileCache != null) {
                final boolean isAscending = fSortNames ? fSortAscending[0] : fSortAscending[1];
                final int row = fileCache.indexOf(new SortableFile(o));
                return isAscending ? row : fileCache.size() - row - 1;
            }
            return 0;
        }
    }

    // AbstractListModel interface
    public Object getElementAt(final int row) {
        return getValueAt(row, 0);
    }

    // AbstractTableModel interface

    public Object getValueAt(int row, final int col) {
        if (row < 0 || col < 0) return null;
        final boolean isAscending = fSortNames ? fSortAscending[0] : fSortAscending[1];
        synchronized(fileCacheLock) {
            if (fileCache != null) {
                if (!isAscending) row = fileCache.size() - row - 1;
                return fileCache.elementAt(row).getValueAt(col);
            }
            return null;
        }
    }

    // PENDING(jeff) - implement
    public void intervalAdded(final ListDataEvent e) {
    }

    // PENDING(jeff) - implement
    public void intervalRemoved(final ListDataEvent e) {
    }

    protected void sort(final Vector<Object> v) {
        if (fSortNames) sSortNames.quickSort(v, 0, v.size() - 1);
        else sSortDates.quickSort(v, 0, v.size() - 1);
    }

    // Liberated from the 1.1 SortDemo
    //
    // This is a generic version of C.A.R Hoare's Quick Sort
    // algorithm. This will handle arrays that are already
    // sorted, and arrays with duplicate keys.<BR>
    //
    // If you think of a one dimensional array as going from
    // the lowest index on the left to the highest index on the right
    // then the parameters to this function are lowest index or
    // left and highest index or right. The first time you call
    // this function it will be with the parameters 0, a.length - 1.
    //
    // @param a an integer array
    // @param lo0 left boundary of array partition
    // @param hi0 right boundary of array partition
    abstract class QuickSort {
        final void quickSort(final Vector<Object> v, final int lo0, final int hi0) {
            int lo = lo0;
            int hi = hi0;
            SortableFile mid;

            if (hi0 > lo0) {
                // Arbitrarily establishing partition element as the midpoint of
                // the array.
                mid = (SortableFile)v.elementAt((lo0 + hi0) / 2);

                // loop through the array until indices cross
                while (lo <= hi) {
                    // find the first element that is greater than or equal to
                    // the partition element starting from the left Index.
                    //
                    // Nasty to have to cast here. Would it be quicker
                    // to copy the vectors into arrays and sort the arrays?
                    while ((lo < hi0) && lt((SortableFile)v.elementAt(lo), mid)) {
                        ++lo;
                    }

                    // find an element that is smaller than or equal to
                    // the partition element starting from the right Index.
                    while ((hi > lo0) && lt(mid, (SortableFile)v.elementAt(hi))) {
                        --hi;
                    }

                    // if the indexes have not crossed, swap
                    if (lo <= hi) {
                        swap(v, lo, hi);
                        ++lo;
                        --hi;
                    }
                }

                // If the right index has not reached the left side of array
                // must now sort the left partition.
                if (lo0 < hi) {
                    quickSort(v, lo0, hi);
                }

                // If the left index has not reached the right side of array
                // must now sort the right partition.
                if (lo < hi0) {
                    quickSort(v, lo, hi0);
                }

            }
        }

        private final void swap(final Vector<Object> a, final int i, final int j) {
            final Object T = a.elementAt(i);
            a.setElementAt(a.elementAt(j), i);
            a.setElementAt(T, j);
        }

        protected abstract boolean lt(SortableFile a, SortableFile b);
    }

    class QuickSortNames extends QuickSort {
        protected boolean lt(final SortableFile a, final SortableFile b) {
            final String aLower = a.fName.toLowerCase();
            final String bLower = b.fName.toLowerCase();
            return aLower.compareTo(bLower) < 0;
        }
    }

    class QuickSortDates extends QuickSort {
        protected boolean lt(final SortableFile a, final SortableFile b) {
            return a.fDateValue < b.fDateValue;
        }
    }

    // for speed in sorting, displaying
    class SortableFile /* extends FileView */{
        File fFile;
        String fName;
        long fDateValue;
        Date fDate;

        SortableFile(final File f) {
            fFile = f;
            fName = fFile.getName();
            fDateValue = fFile.lastModified();
            fDate = new Date(fDateValue);
        }

        public Object getValueAt(final int col) {
            if (col == 0) return fFile;
            return fDate;
        }

        public boolean equals(final Object other) {
            final SortableFile otherFile = (SortableFile)other;
            return otherFile.fFile.equals(fFile);
        }
    }

    class LoadFilesThread extends Thread {
        Vector<Runnable> queuedTasks = new Vector<Runnable>();
        File currentDirectory = null;
        int fid;

        public LoadFilesThread(final File currentDirectory, final int fid) {
            super("Aqua L&F File Loading Thread");
            this.currentDirectory = currentDirectory;
            this.fid = fid;
        }

        public void run() {
            final Vector<DoChangeContents> runnables = new Vector<DoChangeContents>(10);
            final FileSystemView fileSystem = filechooser.getFileSystemView();

            final File[] list = fileSystem.getFiles(currentDirectory, filechooser.isFileHidingEnabled());

            final Vector<Object> acceptsList = new Vector<Object>();

            for (final File element : list) {
                // Return all files to the file chooser. The UI will disable or enable
                // the file name if the current filter approves.
                acceptsList.addElement(new SortableFile(element));
            }

            // Sort based on settings.
            sort(acceptsList);

            // Don't separate directories from files
            Vector<SortableFile> chunk = new Vector<SortableFile>(10);
            final int listSize = acceptsList.size();
            // run through list grabbing file/dirs in chunks of ten
            for (int i = 0; i < listSize;) {
                SortableFile f;
                for (int j = 0; j < 10 && i < listSize; j++, i++) {
                    f = (SortableFile)acceptsList.elementAt(i);
                    chunk.addElement(f);
                }
                final DoChangeContents runnable = new DoChangeContents(chunk, fid);
                runnables.addElement(runnable);
                SwingUtilities.invokeLater(runnable);
                chunk = new Vector<SortableFile>(10);
                if (isInterrupted()) {
                    // interrupted, cancel all runnables
                    cancelRunnables(runnables);
                    return;
                }
            }

            synchronized (fileCacheLock) {
                for (final Runnable r : queuedTasks) {
                    SwingUtilities.invokeLater(r);
                }
            }
        }

        public void cancelRunnables(final Vector<DoChangeContents> runnables) {
            for (int i = 0; i < runnables.size(); i++) {
                runnables.elementAt(i).cancel();
            }
        }
    }

    class DoChangeContents implements Runnable {
        private Vector<SortableFile> contentFiles;
        private boolean doFire = true;
        private final Object lock = new Object();
        private final int fid;

        public DoChangeContents(final Vector<SortableFile> files, final int fid) {
            this.contentFiles = files;
            this.fid = fid;
        }

        synchronized void cancel() {
            synchronized(lock) {
                doFire = false;
            }
        }

        public void run() {
            if (fetchID == fid) {
                synchronized(lock) {
                    if (doFire) {
                        synchronized(fileCacheLock) {
                            if (fileCache != null) {
                                for (int i = 0; i < contentFiles.size(); i++) {
                                    fileCache.addElement(contentFiles.elementAt(i));
                                    fireTableRowsInserted(i, i);
                                }
                            }
                        }
                    }
                    contentFiles = null;
                    directories = null;
                }
            }
        }
    }

    final QuickSortNames sSortNames = new QuickSortNames();
    final QuickSortDates sSortDates = new QuickSortDates();
}