This fixes the problem with the combobox as described in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27605 and implements asynchronous loading of the directory contents.

2006-08-02  Roman Kennke  <[EMAIL PROTECTED]>

        PR 27605
        * javax/swing/JComboBox.java
        (setSelectedItem): Fire ActionEvent here.
        * javax/swing/plaf/basic/BasicDirectoryModel.java
        (directories): Changed to type Vector.
        (files): New field.
        (loadThread): New field.
        (DirectoryLoadThread): New inner class. This loads the contents
        of directories asynchronously.
        (getDirectories): Return cached Vector.
        (getFiles): Return cached Vector.
        (getSize): Return plain size of contents Vector.
        (propertyChange): Reread directory also for DIRECTORY_CHANGED,
        FILE_FILTER_CHANGED, FILE_HIDING_CHANGED and FILE_VIEW_CHANGED.
        (sort): Don't store sorted list in contents. This must be done
        asynchronously from the EventThread.
        (validateFileCache): Rewritten for asynchronous reading
        of directory contents.
        * javax/swing/plaf/basic/BasicFileChooserUI.java
        (installListeners): Install model as PropertyChangeListener.
        (uninstallListeners): Uninstall model as PropertyChangeListener.
        (createPropertyChangeListener): Return null just like the
        RI.

/Roman
Index: javax/swing/JComboBox.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JComboBox.java,v
retrieving revision 1.33
diff -u -1 -2 -r1.33 JComboBox.java
--- javax/swing/JComboBox.java	24 Jul 2006 15:04:05 -0000	1.33
+++ javax/swing/JComboBox.java	2 Aug 2006 15:19:38 -0000
@@ -462,24 +462,25 @@
   {
     return editor;
   }
 
   /**
    * Forces combo box to select given item
    *
    * @param item element in the combo box to select.
    */
   public void setSelectedItem(Object item)
   {
     dataModel.setSelectedItem(item);
+    fireActionEvent();
   }
 
   /**
    * Returns currently selected item in the combo box.
    * The result may be <code>null</code> to indicate that nothing is
    * currently selected.
    *
    * @return element that is currently selected in this combo box.
    */
   public Object getSelectedItem()
   {
     return dataModel.getSelectedItem();
Index: javax/swing/plaf/basic/BasicDirectoryModel.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicDirectoryModel.java,v
retrieving revision 1.4
diff -u -1 -2 -r1.4 BasicDirectoryModel.java
--- javax/swing/plaf/basic/BasicDirectoryModel.java	15 Jul 2006 21:37:54 -0000	1.4
+++ javax/swing/plaf/basic/BasicDirectoryModel.java	2 Aug 2006 15:19:38 -0000
@@ -31,57 +31,313 @@
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 package javax.swing.plaf.basic;
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.File;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Vector;
 import javax.swing.AbstractListModel;
 import javax.swing.JFileChooser;
+import javax.swing.SwingUtilities;
 import javax.swing.event.ListDataEvent;
 import javax.swing.filechooser.FileSystemView;
 
 
 /**
  * Implements an AbstractListModel for directories where the source
  * of the files is a JFileChooser object. 
  *
  * This class is used for sorting and ordering the file list in
  * a JFileChooser L&F object.
  */
 public class BasicDirectoryModel extends AbstractListModel
   implements PropertyChangeListener
 {
   /** The list of files itself */
   private Vector contents;
 
-  /** The number of directories in the list */
-  private int directories;
+  /**
+   * The directories in the list.
+   */
+  private Vector directories;
+
+  /**
+   * The files in the list.
+   */
+  private Vector files;
 
   /** The listing mode of the associated JFileChooser,
       either FILES_ONLY, DIRECTORIES_ONLY or FILES_AND_DIRECTORIES */
   private int listingMode;
 
   /** The JFileCooser associated with this model */
   private JFileChooser filechooser;
 
+  /**
+   * The thread that loads the file view.
+   */
+  private DirectoryLoadThread loadThread;
+
+  /**
+   * This thread is responsible for loading file lists from the
+   * current directory and updating the model.
+   */
+  private class DirectoryLoadThread extends Thread
+  {
+
+    /**
+     * Updates the Swing list model.
+     */
+    private class UpdateSwingRequest
+      implements Runnable
+    {
+
+      private List added;
+      private int addIndex;
+      private List removed;
+      private int removeIndex;
+      private boolean cancel;
+
+      UpdateSwingRequest(List add, int ai, List rem, int ri)
+      {
+        added = add;
+        addIndex = ai;
+        removed = rem;
+        removeIndex = ri;
+        cancel = false;
+      }
+
+      public void run()
+      {
+        if (! cancel)
+          {
+            int numRemoved = removed == null ? 0 : removed.size();
+            int numAdded = added == null ? 0 : added.size();
+            synchronized (contents)
+              {
+                if (numRemoved > 0)
+                  contents.removeAll(removed);
+                if (numAdded > 0)
+                  contents.addAll(added);
+
+                files = null;
+                directories = null;
+              }
+            if (numRemoved > 0 && numAdded == 0)
+              fireIntervalRemoved(BasicDirectoryModel.this, removeIndex,
+                                  removeIndex + numRemoved - 1);
+            else if (numRemoved == 0 && numAdded > 0)
+              fireIntervalAdded(BasicDirectoryModel.this, addIndex,
+                                addIndex + numAdded - 1);
+            else
+              fireContentsChanged();
+          }
+      }
+
+      void cancel()
+      {
+        cancel = true;
+      }
+    }
+
+    /**
+     * The directory beeing loaded.
+     */
+    File directory;
+
+    /**
+     * Stores all UpdateSwingRequests that are sent to the event queue.
+     */
+    private UpdateSwingRequest pending;
+
+    /**
+     * Creates a new DirectoryLoadThread that loads the specified
+     * directory.
+     *
+     * @param dir the directory to load
+     */
+    DirectoryLoadThread(File dir)
+    {
+      super("Basic L&F directory loader");
+      directory = dir;
+    }
+
+    public void run()
+    {
+      FileSystemView fsv = filechooser.getFileSystemView();
+      File[] files = fsv.getFiles(directory,
+                                  filechooser.isFileHidingEnabled());
+
+      // Occasional check if we have been interrupted.
+      if (isInterrupted())
+        return;
+
+      // Check list for accepted files.
+      Vector accepted = new Vector();
+      for (int i = 0; i < files.length; i++)
+        {
+          if (filechooser.accept(files[i]))
+            accepted.add(files[i]);
+        }
+      
+      // Occasional check if we have been interrupted.
+      if (isInterrupted())
+        return;
+
+      // Sort list.
+      sort(accepted);
+
+      // Now split up directories from files so that we get the directories
+      // listed before the files.
+      Vector newFiles = new Vector();
+      Vector newDirectories = new Vector();
+      for (Iterator i = accepted.iterator(); i.hasNext();)
+        {
+          File f = (File) i.next();
+          boolean traversable = filechooser.isTraversable(f);
+          if (traversable)
+            newDirectories.add(f);
+          else if (! traversable && filechooser.isFileSelectionEnabled())
+            newFiles.add(f);
+
+          // Occasional check if we have been interrupted.
+          if (isInterrupted())
+            return;
+
+        }
+
+      // Build up new file cache. Try to update only the changed elements.
+      // This will be important for actions like adding new files or
+      // directories inside a large file list.
+      Vector newCache = new Vector(newDirectories);
+      newCache.addAll(newFiles);
+
+      int newSize = newCache.size();
+      int oldSize = contents.size();
+      if (newSize < oldSize)
+        {
+          // Check for removed interval.
+          int start = -1;
+          int end = -1;
+          boolean found = false;
+          for (int i = 0; i < newSize && !found; i++)
+            {
+              if (! newCache.get(i).equals(contents.get(i)))
+                {
+                  start = i;
+                  end = i + oldSize - newSize;
+                  found = true;
+                }
+            }
+          if (start >= 0 && end > start
+              && contents.subList(end, oldSize)
+                                    .equals(newCache.subList(start, newSize)))
+            {
+              // Occasional check if we have been interrupted.
+              if (isInterrupted())
+                return;
+
+              Vector removed = new Vector(contents.subList(start, end));
+              UpdateSwingRequest r = new UpdateSwingRequest(null, 0,
+                                                            removed, start);
+              invokeLater(r);
+              newCache = null;
+            }
+        }
+      else if (newSize > oldSize)
+        {
+          // Check for inserted interval.
+          int start = oldSize;
+          int end = newSize;
+          boolean found = false;
+          for (int i = 0; i < oldSize && ! found; i++)
+            {
+              if (! newCache.get(i).equals(contents.get(i)))
+                {
+                  start = i;
+                  boolean foundEnd = false;
+                  for (int j = i; j < newSize && ! foundEnd; j++)
+                    {
+                      if (newCache.get(j).equals(contents.get(i)))
+                        {
+                          end = j;
+                          foundEnd = true;
+                        }
+                    }
+                  end = i + oldSize - newSize;
+                }
+            }
+          if (start >= 0 && end > start
+              && newCache.subList(end, newSize)
+                                    .equals(contents.subList(start, oldSize)))
+            {
+              // Occasional check if we have been interrupted.
+              if (isInterrupted())
+                return;
+
+              List added = newCache.subList(start, end);
+              UpdateSwingRequest r = new UpdateSwingRequest(added, start,
+                                                            null, 0); 
+              invokeLater(r);
+              newCache = null;
+            }
+        }
+
+      // Handle complete list changes (newCache != null).
+      if (newCache != null && ! contents.equals(newCache))
+        {
+          // Occasional check if we have been interrupted.
+          if (isInterrupted())
+            return;
+          UpdateSwingRequest r = new UpdateSwingRequest(newCache, 0,
+                                                        contents, 0);
+          invokeLater(r);
+        }
+    }
+
+    /**
+     * Wraps SwingUtilities.invokeLater() and stores the request in
+     * a Vector so that we can still cancel it later.
+     *
+     * @param update the request to invoke
+     */
+    private void invokeLater(UpdateSwingRequest update)
+    {
+      pending = update;
+      SwingUtilities.invokeLater(update);
+    }
+
+    /**
+     * Cancels all pending update requests that might be in the AWT
+     * event queue.
+     */
+    void cancelPending()
+    {
+      if (pending != null)
+        pending.cancel();
+    }
+  }
+
   /** A Comparator class/object for sorting the file list. */
   private Comparator comparator = new Comparator()
     {
       public int compare(Object o1, Object o2)
       {
 	if (lt((File) o1, (File) o2))
 	  return -1;
 	else
 	  return 1;
       }
     };
 
@@ -118,70 +374,93 @@
   {
     fireContentsChanged(this, 0, getSize() - 1);
   }
 
   /**
    * Returns a Vector of (java.io.File) objects containing
    * the directories in this list.
    *
    * @return a Vector
    */
   public Vector getDirectories()
   {
-    Vector tmp = new Vector();
-    for (int i = 0; i < directories; i++)
-      tmp.add(contents.get(i));
-    return tmp;
+    // Synchronize this with the UpdateSwingRequest for the case when
+    // contents is modified.
+    synchronized (contents)
+      {
+        Vector dirs = directories;
+        if (dirs == null)
+          {
+            // Initializes this in getFiles().
+            getFiles();
+            dirs = directories;
+          }
+        return dirs;
+      }
   }
 
   /**
    * Returns the (java.io.File) object at 
    * an index in the list.
    *
    * @param index The list index
    * @return a File object
    */
   public Object getElementAt(int index)
   {
     if (index > getSize() - 1)
       return null;
     return contents.elementAt(index);
   }
 
   /**
    * Returns a Vector of (java.io.File) objects containing
    * the files in this list.
    *
    * @return a Vector
    */
   public Vector getFiles()
   {
-    Vector tmp = new Vector();
-    for (int i = directories; i < getSize(); i++)
-      tmp.add(contents.get(i));
-    return tmp;
+    synchronized (contents)
+      {
+        Vector f = files;
+        if (f == null)
+          {
+            f = new Vector();
+            Vector d = new Vector(); // Directories;
+            for (Iterator i = contents.iterator(); i.hasNext();)
+              {
+                File file = (File) i.next();
+                if (filechooser.isTraversable(file))
+                  d.add(file);
+                else
+                  f.add(file);
+              }
+            files = f;
+            directories = d;
+          }
+        return f;
+      }
   }
 
   /**
    * Returns the size of the list, which only includes directories 
    * if the JFileChooser is set to DIRECTORIES_ONLY.
    *
    * Otherwise, both directories and files are included in the count.
    *
    * @return The size of the list.
    */
   public int getSize()
   {
-    if (listingMode == JFileChooser.DIRECTORIES_ONLY)
-      return directories;
     return contents.size();
   }
 
   /**
    * Returns the index of an (java.io.File) object in the list.
    *
    * @param o The object - normally a File.
    *
    * @return the index of that object, or -1 if it is not in the list.
    */
   public int indexOf(Object o)
   {
@@ -243,27 +522,32 @@
           return false;
       }
   }
 
   /**
    * Listens for a property change; the change in file selection mode of the
    * associated JFileChooser. Reloads the file cache on that event.
    *
    * @param e - A PropertyChangeEvent.
    */
   public void propertyChange(PropertyChangeEvent e)
   {
-    if (e.getPropertyName().equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY))
+    String property = e.getPropertyName();
+    if (property.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)
+        || property.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)
+        || property.equals(JFileChooser.FILE_HIDING_CHANGED_PROPERTY)
+        || property.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)
+        || property.equals(JFileChooser.FILE_VIEW_CHANGED_PROPERTY)
+        )
       {
-	listingMode = filechooser.getFileSelectionMode();
 	validateFileCache();
       }
   }
 
   /**
    * Renames a file - However, does <I>not</I> re-sort the list 
    * or replace the old file with the new one in the list.
    *
    * @param oldFile The old file
    * @param newFile The new file name
    *
    * @return <code>true</code> if the rename succeeded
@@ -272,57 +556,34 @@
   {
     return oldFile.renameTo( newFile );
   }
 
   /**
    * Sorts a Vector of File objects.
    *
    * @param v The Vector to sort.
    */
   protected void sort(Vector v)
   {
     Collections.sort(v, comparator);
-    Enumeration e = Collections.enumeration(v);
-    Vector tmp = new Vector();
-    for (; e.hasMoreElements();)
-      tmp.add(e.nextElement());
-
-    contents = tmp;
   }
 
   /**
    * Re-loads the list of files
    */
   public void validateFileCache()
   {
-    // FIXME: Get the files and sort them in a seperate thread and deliver
-    // them a few at a time to be filtered, so that the file selector is
-    // responsive even with long file lists.
-    contents.clear();
-    directories = 0;
-    FileSystemView fsv = filechooser.getFileSystemView();
-    File[] list = fsv.getFiles(filechooser.getCurrentDirectory(),
-                               filechooser.isFileHidingEnabled());
-
-    if (list == null)
-      return;
-
-    for (int i = 0; i < list.length; i++)
+    File dir = filechooser.getCurrentDirectory();
+    if (dir != null)
       {
-	if (list[i] == null)
-	  continue;
-	boolean isDir = filechooser.isTraversable(list[i]);
-
-	if( listingMode != JFileChooser.DIRECTORIES_ONLY || isDir )
-	  if (filechooser.accept(list[i]))
-	    {
-	      contents.add(list[i]);
-	      if (isDir)
-		directories++;
-	    }
+        // Cancel all pending requests.
+        if (loadThread != null)
+          {
+            loadThread.interrupt();
+            loadThread.cancelPending();
+          }
+        loadThread = new DirectoryLoadThread(dir);
+        loadThread.start();
       }
-    sort(contents);
-    filechooser.revalidate();
-    filechooser.repaint();
   }
 }
 
Index: javax/swing/plaf/basic/BasicFileChooserUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicFileChooserUI.java,v
retrieving revision 1.28
diff -u -1 -2 -r1.28 BasicFileChooserUI.java
--- javax/swing/plaf/basic/BasicFileChooserUI.java	2 Aug 2006 11:31:42 -0000	1.28
+++ javax/swing/plaf/basic/BasicFileChooserUI.java	2 Aug 2006 15:19:39 -0000
@@ -888,36 +888,42 @@
   public void uninstallComponents(JFileChooser fc)
   {
   }
 
   /**
    * Installs the listeners required by this UI delegate.
    *
    * @param fc  the file chooser.
    */
   protected void installListeners(JFileChooser fc)
   {
     propertyChangeListener = createPropertyChangeListener(filechooser);
-    filechooser.addPropertyChangeListener(propertyChangeListener);
+    if (propertyChangeListener != null)
+      filechooser.addPropertyChangeListener(propertyChangeListener);
+    fc.addPropertyChangeListener(getModel());
   }
 
   /**
    * Uninstalls the listeners previously installed by this UI delegate.
    *
    * @param fc  the file chooser.
    */
   protected void uninstallListeners(JFileChooser fc)
   {
-    filechooser.removePropertyChangeListener(propertyChangeListener);
-    propertyChangeListener = null;
+    if (propertyChangeListener != null)
+      {
+        filechooser.removePropertyChangeListener(propertyChangeListener);
+        propertyChangeListener = null;
+      }
+    fc.removePropertyChangeListener(getModel());
   }
 
   /**
    * Installs the defaults for this UI delegate.
    *
    * @param fc  the file chooser.
    */
   protected void installDefaults(JFileChooser fc)
   {
     installIcons(fc);
     installStrings(fc);
   }
@@ -1060,30 +1066,26 @@
   }
 
   /**
    * Creates a listener to handle changes to the properties of the given
    * file chooser component.
    * 
    * @param fc  the file chooser component.
    * 
    * @return A new listener.
    */
   public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
   {
-    return new PropertyChangeListener()
-    {
-      public void propertyChange(PropertyChangeEvent e)
-      {
-      }
-    };
+    // The RI returns null here, so do we.
+    return null;
   }
 
   /**
    * Returns the current file name.
    * 
    * @return The current file name.
    */
   public String getFileName()
   {
     return entry.getText();
   }
 

Reply via email to