This fixes some bugs:
-Default mode is FILES_ONLY.
-FILES_ONLY mode doesn't mean "show only files" it means "allow only
files to be selected". 
-Hitting enter or return doesn't cancel renaming of files.
-File-name textbox is not set to directory names when in FILES_ONLY,
(but you can still traverse them, requiring a new field for the selected
directory)

And completes BasicDirectoryModel, which I think is all right now.

Some remaining bugs:
- Changing to list view and back screws things up (the table header is
still there, for instance)
- Scrolling size is wrong.
- Sometimes you can select empty entries.

Let me know if there are any regressions.

/Sven

2006-07-14  Sven de Marothy  <[EMAIL PROTECTED]>

        * javax/swing/JFileChooser.java
        Change default selection mode to FILES_ONLY.
        * javax/swing/plaf/basic/BasicDirectoryModel.java
        Document, fix selection mode filtering.
        (renameFile): Implement
        * javax/swing/plaf/basic/BasicFileChooserUI.java
        (selectedDir): New field to handle selected directories,
        disallow selecting of directories in FILES_ONLY mode.
        * javax/swing/plaf/metal/MetalFileChooserUI.java:
        (EditingActionListener.actionPerformed):
        Stop editing on all actions (e.g. return-key press)


Index: javax/swing/JFileChooser.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/JFileChooser.java,v
retrieving revision 1.34
diff -U3 -r1.34 JFileChooser.java
--- javax/swing/JFileChooser.java	12 Jul 2006 21:22:22 -0000	1.34
+++ javax/swing/JFileChooser.java	14 Jul 2006 23:35:37 -0000
@@ -353,7 +353,7 @@
    * The file selection mode.
    * @see #setFileSelectionMode(int) 
    */
-  private int fileSelectionMode = FILES_AND_DIRECTORIES;
+  private int fileSelectionMode = FILES_ONLY;
 
   /** 
    * The file view.
Index: javax/swing/plaf/basic/BasicDirectoryModel.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/plaf/basic/BasicDirectoryModel.java,v
retrieving revision 1.3
diff -U3 -r1.3 BasicDirectoryModel.java
--- javax/swing/plaf/basic/BasicDirectoryModel.java	1 Jun 2006 05:17:02 -0000	1.3
+++ javax/swing/plaf/basic/BasicDirectoryModel.java	14 Jul 2006 23:35:37 -0000
@@ -1,5 +1,5 @@
 /* BasicDirectoryModel.java --
-   Copyright (C) 2005  Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -51,24 +51,29 @@
 
 
 /**
- * DOCUMENT ME!
+ * 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
 {
-  /** DOCUMENT ME! */
+  /** The list of files itself */
   private Vector contents;
 
-  /** DOCUMENT ME! */
+  /** The number of directories in the list */
   private int directories;
 
-  /** DOCUMENT ME! */
+  /** The listing mode of the associated JFileChooser,
+      either FILES_ONLY, DIRECTORIES_ONLY or FILES_AND_DIRECTORIES */
   private int listingMode;
 
-  /** DOCUMENT ME! */
+  /** The JFileCooser associated with this model */
   private JFileChooser filechooser;
 
-  /** DOCUMENT ME! */
+  /** A Comparator class/object for sorting the file list. */
   private Comparator comparator = new Comparator()
     {
       public int compare(Object o1, Object o2)
@@ -91,14 +96,15 @@
     filechooser.addPropertyChangeListener(this);
     listingMode = filechooser.getFileSelectionMode();
     contents = new Vector();
+    validateFileCache();
   }
 
   /**
-   * DOCUMENT ME!
+   * Returns whether a given (File) object is included in the list.
    *
-   * @param o DOCUMENT ME!
+   * @param o - The file object to test.
    *
-   * @return DOCUMENT ME!
+   * @return <code>true</code> if the list contains the given object.
    */
   public boolean contains(Object o)
   {
@@ -106,7 +112,7 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Fires a content change event. 
    */
   public void fireContentsChanged()
   {
@@ -114,9 +120,10 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Returns a Vector of (java.io.File) objects containing
+   * the directories in this list.
    *
-   * @return DOCUMENT ME!
+   * @return a Vector
    */
   public Vector getDirectories()
   {
@@ -127,26 +134,24 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Returns the (java.io.File) object at 
+   * an index in the list.
    *
-   * @param index DOCUMENT ME!
-   *
-   * @return DOCUMENT ME!
+   * @param index The list index
+   * @return a File object
    */
   public Object getElementAt(int index)
   {
     if (index > getSize() - 1)
       return null;
-    if (listingMode == JFileChooser.FILES_ONLY)
-      return contents.get(directories + index);
-    else
-      return contents.elementAt(index);
+    return contents.elementAt(index);
   }
 
   /**
-   * DOCUMENT ME!
+   * Returns a Vector of (java.io.File) objects containing
+   * the files in this list.
    *
-   * @return DOCUMENT ME!
+   * @return a Vector
    */
   public Vector getFiles()
   {
@@ -157,37 +162,34 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * 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 DOCUMENT ME!
+   * @return The size of the list.
    */
   public int getSize()
   {
     if (listingMode == JFileChooser.DIRECTORIES_ONLY)
       return directories;
-    else if (listingMode == JFileChooser.FILES_ONLY)
-      return contents.size() - directories;
     return contents.size();
   }
 
   /**
-   * DOCUMENT ME!
+   * Returns the index of an (java.io.File) object in the list.
    *
-   * @param o DOCUMENT ME!
+   * @param o The object - normally a File.
    *
-   * @return DOCUMENT ME!
+   * @return the index of that object, or -1 if it is not in the list.
    */
   public int indexOf(Object o)
   {
-    if (listingMode == JFileChooser.FILES_ONLY)
-      return contents.indexOf(o) - directories;
     return contents.indexOf(o);
   }
 
   /**
-   * DOCUMENT ME!
-   *
-   * @param e DOCUMENT ME!
+   * Obsoleted method which does nothing.
    */
   public void intervalAdded(ListDataEvent e)
   {
@@ -195,9 +197,7 @@
   }
 
   /**
-   * DOCUMENT ME!
-   *
-   * @param e DOCUMENT ME!
+   * Obsoleted method which does nothing.
    */
   public void intervalRemoved(ListDataEvent e)
   {
@@ -205,7 +205,7 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Obsoleted method which does nothing.
    */
   public void invalidateFileCache()
   {
@@ -213,12 +213,16 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Less than, determine the relative order in the list of two files
+   * for sorting purposes.
    *
-   * @param a DOCUMENT ME!
-   * @param b DOCUMENT ME!
+   * The order is: directories < files, and thereafter alphabetically,
+   * using the default locale collation.
    *
-   * @return DOCUMENT ME!
+   * @param a the first file
+   * @param b the second file
+   *
+   * @return <code>true</code> if a > b, <code>false</code> if a < b.
    */
   protected boolean lt(File a, File b)
   {
@@ -241,34 +245,38 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Listens for a property change; the change in file selection mode of the
+   * associated JFileChooser. Reloads the file cache on that event.
    *
-   * @param e DOCUMENT ME!
+   * @param e - A PropertyChangeEvent.
    */
   public void propertyChange(PropertyChangeEvent e)
   {
     if (e.getPropertyName().equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY))
-      listingMode = filechooser.getFileSelectionMode();
+      {
+	listingMode = filechooser.getFileSelectionMode();
+	validateFileCache();
+      }
   }
 
   /**
-   * DOCUMENT ME!
+   * 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 DOCUMENT ME!
-   * @param newFile DOCUMENT ME!
+   * @param oldFile The old file
+   * @param newFile The new file name
    *
-   * @return DOCUMENT ME!
+   * @return <code>true</code> if the rename succeeded
    */
   public boolean renameFile(File oldFile, File newFile)
   {
-    // FIXME: implement
-    return false;
+    return oldFile.renameTo( newFile );
   }
 
   /**
-   * DOCUMENT ME!
+   * Sorts a Vector of File objects.
    *
-   * @param v DOCUMENT ME!
+   * @param v The Vector to sort.
    */
   protected void sort(Vector v)
   {
@@ -282,10 +290,13 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * 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();
@@ -299,15 +310,19 @@
       {
 	if (list[i] == null)
 	  continue;
-	if (filechooser.accept(list[i]))
-	  {
-	    contents.add(list[i]);
-	    if (filechooser.isTraversable(list[i]))
-	      directories++;
-	  }
+	boolean isDir = filechooser.isTraversable(list[i]);
+
+	if( listingMode != JFileChooser.DIRECTORIES_ONLY || isDir )
+	  if (filechooser.accept(list[i]))
+	    {
+	      contents.add(list[i]);
+	      if (isDir)
+		directories++;
+	    }
       }
     sort(contents);
     filechooser.revalidate();
     filechooser.repaint();
   }
 }
+
Index: javax/swing/plaf/basic/BasicFileChooserUI.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/plaf/basic/BasicFileChooserUI.java,v
retrieving revision 1.26
diff -U3 -r1.26 BasicFileChooserUI.java
--- javax/swing/plaf/basic/BasicFileChooserUI.java	13 Jun 2006 09:28:57 -0000	1.26
+++ javax/swing/plaf/basic/BasicFileChooserUI.java	14 Jul 2006 23:35:37 -0000
@@ -160,6 +160,8 @@
       else
         {
           File f = new File(filechooser.getCurrentDirectory(), getFileName());
+	  if ( selectedDir != null )
+	    f = selectedDir;
           if (filechooser.isTraversable(f))
             {
               filechooser.setCurrentDirectory(f);
@@ -409,7 +411,7 @@
               closeDialog();
             }
         }
-      else
+      else // single click
         {
           String path = p.toString();
           File f = fsv.createFileObject(path);
@@ -436,10 +438,11 @@
             }
           lastSelected = path;
           parentPath = path.substring(0, path.lastIndexOf("/") + 1);
+	    
           if (f.isFile())
             setFileName(path.substring(path.lastIndexOf("/") + 1));
-          else if (filechooser.getFileSelectionMode() == 
-            JFileChooser.DIRECTORIES_ONLY)
+          else if (filechooser.getFileSelectionMode() != 
+		   JFileChooser.FILES_ONLY)
             setFileName(path);
         }
     }
@@ -538,7 +541,7 @@
     }
 
     /**
-     * DOCUMENT ME!
+     * Sets the JFileChooser to the selected file on an update
      *
      * @param e DOCUMENT ME!
      */
@@ -550,9 +553,15 @@
 	return;
       File file = filechooser.getFileSystemView().createFileObject(f.toString());
       if (! filechooser.isTraversable(file))
-	filechooser.setSelectedFile(file);
+	{
+	  selectedDir = null;
+	  filechooser.setSelectedFile(file);
+	}
       else
-	filechooser.setSelectedFile(null);
+	{
+	  selectedDir = file;
+	  filechooser.setSelectedFile(null);
+	}
     }
   }
 
@@ -752,6 +761,13 @@
    * @see #getUpdateAction()
    */
   private UpdateAction updateAction;
+
+  /**
+   * When in FILES_ONLY, mode a directory cannot be selected, so
+   * we save a reference to any it here. This is used to enter
+   * the directory on "Open" when in that mode.
+   */
+  private File selectedDir;
   
   // -- end private --
 
Index: javax/swing/plaf/metal/MetalFileChooserUI.java
===================================================================
RCS file: /sources/classpath/classpath/javax/swing/plaf/metal/MetalFileChooserUI.java,v
retrieving revision 1.23
diff -U3 -r1.23 MetalFileChooserUI.java
--- javax/swing/plaf/metal/MetalFileChooserUI.java	11 May 2006 17:05:55 -0000	1.23
+++ javax/swing/plaf/metal/MetalFileChooserUI.java	14 Jul 2006 23:35:38 -0000
@@ -303,8 +303,9 @@
             
           if (file == null)
             setFileName(null);
-          else
-            setFileName(file.getName());
+          else if (file.isFile() || filechooser.getFileSelectionMode() 
+		   != JFileChooser.FILES_ONLY)
+	    setFileName(file.getName());
           int index = -1;
           index = getModel().indexOf(file);
           if (index >= 0)
@@ -956,9 +957,12 @@
         {
           String text = editField.getText();
           if (text != null && text != "" && !text.equals(fc.getName(editFile)))
-              if (editFile.renameTo(fc.getFileSystemView().createFileObject(
-                  fc.getCurrentDirectory(), text)))
+	    {
+	      File f = fc.getFileSystemView().
+		createFileObject(fc.getCurrentDirectory(), text);
+              if ( editFile.renameTo(f) )
                 rescanCurrentDirectory(fc);
+	    }
           list.remove(editField);
         }
       startEditing = false;
@@ -982,17 +986,8 @@
        */
       public void actionPerformed(ActionEvent e)
       {
-        if (e.getActionCommand().equals("notify-field-accept"))
+	if (editField != null)
           completeEditing();
-        else if (editField != null)
-          {
-            list.remove(editField);
-            startEditing = false;
-            editFile = null;
-            lastSelected = null;
-            editField = null;
-            list.repaint();
-          }
       }
     }
   }
@@ -1101,7 +1096,7 @@
           lastSelected = selVal;
           if (f.isFile())
             setFileName(path.substring(path.lastIndexOf("/") + 1));
-          else if (fc.getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY)
+          else if (fc.getFileSelectionMode() != JFileChooser.FILES_ONLY)
             setFileName(path);
         }
       fileTable.repaint();
@@ -1171,16 +1166,8 @@
        */
       public void actionPerformed(ActionEvent e)
       {
-        if (e.getActionCommand().equals("notify-field-accept"))
+	if (editField != null)
           completeEditing();
-        else if (editField != null)
-          {
-            table.remove(editField);
-            startEditing = false;
-            editFile = null;
-            editField = null;
-            table.repaint();
-          }
       }
     }
     

Reply via email to