📄 basicdirectorymodel.java
字号:
/* * @(#)BasicDirectoryModel.java 1.37 07/08/29 * * Copyright 2006 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 java.util.concurrent.Callable;import javax.swing.*;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 BasicDirectoryModel extends AbstractListModel 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; private PropertyChangeSupport changeSupport; private boolean busy = false; 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 (loadThread != null) { loadThread.interrupt(); loadThread.cancelRunnables(); loadThread = null; } } 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(); } setBusy(true, ++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() { run0(); setBusy(false, fid); } public void run0() { ShellFolder.getInvoker().invoke(new Callable<Void>() { public Void call() throws Exception { FileSystemView fileSystem = filechooser.getFileSystemView(); File[] list = fileSystem.getFiles(currentDirectory, filechooser.isFileHidingEnabled()); Vector<File> acceptsList = new Vector<File>(); if (isInterrupted()) { return null; } // 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 null; } // First sort alphabetically by filename
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -