FileDocCategorySizeDatePackage
BasicDirectoryModel.javaAPI DocJava SE 6 API16620Tue Jun 10 00:26:46 BST 2008javax.swing.plaf.basic

BasicDirectoryModel

public class BasicDirectoryModel extends AbstractListModel implements PropertyChangeListener
Basic implementation of a file list.
version
%i% %g%
author
Jeff Dinkins

Fields Summary
private JFileChooser
filechooser
private Vector
fileCache
private LoadFilesThread
loadThread
private Vector
files
private Vector
directories
private int
fetchID
private PropertyChangeSupport
changeSupport
private boolean
busy
Constructors Summary
public BasicDirectoryModel(JFileChooser filechooser)


       
        this.filechooser = filechooser;
        validateFileCache();
    
Methods Summary
public voidaddPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list. The listener is registered for all bound properties of this class.

If listener is null, 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

        if (changeSupport == null) {
            changeSupport = new PropertyChangeSupport(this);
        }
        changeSupport.addPropertyChangeListener(listener);
    
public booleancontains(java.lang.Object o)

        return fileCache.contains(o);
    
public voidfireContentsChanged()

        // System.out.println("BasicDirectoryModel: firecontentschanged"); 
        fireContentsChanged(this, 0, getSize()-1);
    
protected voidfirePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
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

        if (changeSupport != null) {
            changeSupport.firePropertyChange(propertyName,
                                             oldValue, newValue);
        }
    
public java.util.VectorgetDirectories()

        synchronized(fileCache) {
            if (directories != null) {
                return directories;
            }
            Vector fls = getFiles();
            return directories;
        }
    
public java.lang.ObjectgetElementAt(int index)

        return fileCache.get(index);
    
public java.util.VectorgetFiles()

        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 java.beans.PropertyChangeListener[]getPropertyChangeListeners()
Returns an array of all the property change listeners registered on this component.

return
all of this component's PropertyChangeListeners or an empty array if no property change listeners are currently registered
see
#addPropertyChangeListener
see
#removePropertyChangeListener
see
java.beans.PropertyChangeSupport#getPropertyChangeListeners
since
1.6

        if (changeSupport == null) {
            return new PropertyChangeListener[0];
        }
        return changeSupport.getPropertyChangeListeners();
    
public intgetSize()

        return fileCache.size();
    
public intindexOf(java.lang.Object o)

        return fileCache.indexOf(o);
    
public voidintervalAdded(javax.swing.event.ListDataEvent e)
Obsolete - not used.

    
public voidintervalRemoved(javax.swing.event.ListDataEvent e)
Obsolete - not used.

    
public voidinvalidateFileCache()
This method is used to interrupt file loading thread.

        if (loadThread != null) {
            loadThread.interrupt();
            loadThread.cancelRunnables();
            loadThread = null;
        }
    
protected booleanlt(java.io.File a, java.io.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;
        }
    
public voidpropertyChange(java.beans.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();
        }
    
public voidremovePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list.

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

        if (changeSupport != null) {
            changeSupport.removePropertyChangeListener(listener);
        }
    
public booleanrenameFile(java.io.File oldFile, java.io.File newFile)
Renames a file in the underlying file system.

param
oldFile a File object representing the existing file
param
newFile a File object representing the desired new file name
return
true if rename succeeded, otherwise false
since
1.4

        synchronized(fileCache) {
            if (oldFile.renameTo(newFile)) {
                validateFileCache();
                return true;
            }
            return false;
        }
    
private synchronized voidsetBusy(boolean busy, int fid)
Set the busy state for the model. The model is considered busy when it is running a separate (interruptable) thread in order to load the contents of a directory.

        if (fid == fetchID) {
            boolean oldValue = this.busy;
            this.busy = busy;

            if (changeSupport != null && busy != oldValue) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        firePropertyChange("busy", !busy, busy);
                    }
                });
            }
        }
    
protected voidsort(java.util.Vector v)

        ShellFolder.sortFiles(v);
    
public voidvalidateFileCache()

        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();