Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
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
/*
 * @(#)BasicDirectoryModel.java    1.31 04/05/05
 *
 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
 
//package javax.swing.plaf.basic;
 
import java.io.File;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import java.beans.*;
 
import sun.awt.shell.ShellFolder;
 
/**
 * Basic implementation of a file list.
 *
 * @version %i% %g%
 * @author Jeff Dinkins
 */
public class cTreeDirectoryModel extends DefaultTreeModel implements PropertyChangeListener {
 
    private JFileChooser filechooser = null;
    // PENDING(jeff) pick the size more sensibly
    private Vector fileCache = new Vector(50);
    private LoadFilesThread loadThread = null;
    private Vector files = null;
    private Vector directories = null;
    private int fetchID = 0;
 
    public cTreeDirectoryModel(JFileChooser filechooser) {
        super(null);
   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();
   }
    }
 
    /**
     * Obsolete - not used.
     */
    public void invalidateFileCache() {
    }
 
    public Vector<File> getDirectories() {
   synchronized(fileCache) {
       if (directories != null) {
       return directories;
       }
       Vector fls = getFiles();
       return directories;
   }
    }
 
    public Vector<File> getFiles() {
   synchronized(fileCache) {
       if (files != null) {
       return files;
       }
       files = new Vector();
       directories = new Vector();
       directories.addElement(filechooser.getFileSystemView().createFileObject(
       filechooser.getCurrentDirectory(), "..")
       );
 
       for (int i = 0; i < getSize(); i++) {
       File f = (File)fileCache.get(i);
       if (filechooser.isTraversable(f)) {
           directories.add(f);
       } else {
           files.add(f);
       }
       }
       return files;
   }
    }
 
    public void validateFileCache() {
   File currentDirectory = filechooser.getCurrentDirectory();
   if (currentDirectory == null) {
       return;
   }
   if (loadThread != null) {
       loadThread.interrupt();
            loadThread.cancelRunnables();
   }
   fetchID++;
   loadThread = new LoadFilesThread(currentDirectory, fetchID);
   loadThread.start();
    }
 
    /**
     * 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;
   }
    }
 
 
    public void fireContentsChanged() {
   // System.out.println("BasicDirectoryModel: firecontentschanged");
   //fireContentsChanged(this, 0, getSize()-1);
    }
 
    public int getSize() {
   return fileCache.size();
    }
 
    public boolean contains(Object o) {
   return fileCache.contains(o);
    }
 
    public int indexOf(Object o) {
   return fileCache.indexOf(o);
    }
 
    public Object getElementAt(int index) {
   return fileCache.get(index);
    }
 
    /**
     * Obsolete - not used.
     */
    public void intervalAdded(ListDataEvent e) {
    }
 
    /**
     * Obsolete - not used.
     */
    public void intervalRemoved(ListDataEvent e) {
    }
 
    protected void sort(Vector<? extends File> v){
   ShellFolder.sortFiles(v);
    }
 
    // Obsolete - not used
    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;
   }
    }
 
 
    class LoadFilesThread extends Thread {
   File currentDirectory = null;
   int fid;
   Vector runnables = new Vector(10);
   
   public LoadFilesThread(File currentDirectory, int fid) {
       super("Basic L&F File Loading Thread");
       this.currentDirectory = currentDirectory;
       this.fid = fid;
   }
   
   private void invokeLater(Runnable runnable) {
       runnables.addElement(runnable);
       SwingUtilities.invokeLater(runnable);
   }
 
   public void run() {
       FileSystemView fileSystem = filechooser.getFileSystemView();
 
       File[] list = fileSystem.getFiles(currentDirectory, filechooser.isFileHidingEnabled());
 
       Vector<File> acceptsList = new Vector<File>();
 
       if (isInterrupted()) {
       return;
       }
 
       // run through the file list, add directories and selectable files to fileCache
       for (int i = 0; i < list.length; i++) {
       if(filechooser.accept(list[i])) {
           acceptsList.addElement(list[i]);
       }
       }
 
       if (isInterrupted()) {
       return;
       }
 
       // First sort alphabetically by filename
       sort(acceptsList);
 
       Vector newDirectories = new Vector(50);
       Vector newFiles = new Vector();
       // run through list grabbing directories in chunks of ten
       for(int i = 0; i < acceptsList.size(); i++) {
       File f = (File) acceptsList.elementAt(i);
       boolean isTraversable = filechooser.isTraversable(f);
       if (isTraversable) {
           newDirectories.addElement(f);
       } else if (!isTraversable && filechooser.isFileSelectionEnabled()) {
           newFiles.addElement(f);
       }
       if(isInterrupted()) {
           return;
       }
       }
 
       Vector newFileCache = new Vector(newDirectories);
       newFileCache.addAll(newFiles);
 
       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
           && newFileCache.subList(end, newSize).equals(fileCache.subList(start, oldSize))) {
           if(isInterrupted()) {
               return;
           }
           invokeLater(new DoChangeContents(newFileCache.subList(start, end), start, null, 0, fid));
           newFileCache = null;
       }
       } 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
           && fileCache.subList(end, oldSize).equals(newFileCache.subList(start, newSize))) {
           if(isInterrupted()) {
               return;
           }
           invokeLater(new DoChangeContents(null, 0, new Vector(fileCache.subList(start, end)),
                            start, fid));
           newFileCache = null;
       }
       }
       if (newFileCache != null && !fileCache.equals(newFileCache)) {
           if (isInterrupted()) {
           cancelRunnables(runnables);
           }
       invokeLater(new DoChangeContents(newFileCache, 0, fileCache, 0, fid));
       }
   }
 
 
   public void cancelRunnables(Vector runnables) {
       for(int i = 0; i < runnables.size(); i++) {
       ((DoChangeContents)runnables.elementAt(i)).cancel();
       }
   }
 
     public void cancelRunnables() {
            cancelRunnables(runnables);
   }
   }
 
    class DoChangeContents implements Runnable {
   private List addFiles;
   private List remFiles;
   private boolean doFire = true;
   private int fid;
   private int addStart = 0;
   private int remStart = 0;
   private int change;
   
   public DoChangeContents(List addFiles, int addStart, List 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 == 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 && fileCache.size() > addSize) {
           //fireIntervalAdded(BasicDirectoryModel.this, addStart, addStart + addSize - 1);
       } else {
           fireContentsChanged();
       }
       }
   }
    }
}