summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven de Marothy <sven@physto.se>2006-07-15 21:37:53 +0000
committerSven de Marothy <sven@physto.se>2006-07-15 21:37:53 +0000
commit6bd49e2fd076e2293b727056839b186d1b0f52aa (patch)
tree0783fd6a280aee36b28e614623f5d3b150f50007
parent101ff34066ccc14fb59489b1b22d848b0aa625b2 (diff)
downloadclasspath-6bd49e2fd076e2293b727056839b186d1b0f52aa.tar.gz
2006-07-15 Sven de Marothy <sven@physto.se>
* 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)
-rw-r--r--ChangeLog14
-rw-r--r--javax/swing/JFileChooser.java2
-rw-r--r--javax/swing/plaf/basic/BasicDirectoryModel.java137
-rw-r--r--javax/swing/plaf/basic/BasicFileChooserUI.java28
-rw-r--r--javax/swing/plaf/metal/MetalFileChooserUI.java35
5 files changed, 124 insertions, 92 deletions
diff --git a/ChangeLog b/ChangeLog
index fb9d249aa..cc7de827c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2006-07-15 Sven de Marothy <sven@physto.se>
+
+ * 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)
+
2006-07-15 Mark Wielaard <mark@klomp.org>
* doc/vmintegration.texinfo (gnu.java.lang.management): Change xref
diff --git a/javax/swing/JFileChooser.java b/javax/swing/JFileChooser.java
index 8b87f27f6..a508b8fcb 100644
--- a/javax/swing/JFileChooser.java
+++ b/javax/swing/JFileChooser.java
@@ -353,7 +353,7 @@ public class JFileChooser extends JComponent implements Accessible
* The file selection mode.
* @see #setFileSelectionMode(int)
*/
- private int fileSelectionMode = FILES_AND_DIRECTORIES;
+ private int fileSelectionMode = FILES_ONLY;
/**
* The file view.
diff --git a/javax/swing/plaf/basic/BasicDirectoryModel.java b/javax/swing/plaf/basic/BasicDirectoryModel.java
index ef7a880c2..ca7d20ea0 100644
--- a/javax/swing/plaf/basic/BasicDirectoryModel.java
+++ b/javax/swing/plaf/basic/BasicDirectoryModel.java
@@ -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 @@ import javax.swing.filechooser.FileSystemView;
/**
- * 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 @@ public class BasicDirectoryModel extends AbstractListModel
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 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * DOCUMENT ME!
+ * Fires a content change event.
*/
public void fireContentsChanged()
{
@@ -114,9 +120,10 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * 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 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * 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 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * 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 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * DOCUMENT ME!
- *
- * @param e DOCUMENT ME!
+ * Obsoleted method which does nothing.
*/
public void intervalRemoved(ListDataEvent e)
{
@@ -205,7 +205,7 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * DOCUMENT ME!
+ * Obsoleted method which does nothing.
*/
public void invalidateFileCache()
{
@@ -213,12 +213,16 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * 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 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * 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 @@ public class BasicDirectoryModel extends AbstractListModel
}
/**
- * 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 @@ public class BasicDirectoryModel extends AbstractListModel
{
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();
}
}
+
diff --git a/javax/swing/plaf/basic/BasicFileChooserUI.java b/javax/swing/plaf/basic/BasicFileChooserUI.java
index 1356db4ae..52b7f5720 100644
--- a/javax/swing/plaf/basic/BasicFileChooserUI.java
+++ b/javax/swing/plaf/basic/BasicFileChooserUI.java
@@ -160,6 +160,8 @@ public class BasicFileChooserUI extends FileChooserUI
else
{
File f = new File(filechooser.getCurrentDirectory(), getFileName());
+ if ( selectedDir != null )
+ f = selectedDir;
if (filechooser.isTraversable(f))
{
filechooser.setCurrentDirectory(f);
@@ -409,7 +411,7 @@ public class BasicFileChooserUI extends FileChooserUI
closeDialog();
}
}
- else
+ else // single click
{
String path = p.toString();
File f = fsv.createFileObject(path);
@@ -436,10 +438,11 @@ public class BasicFileChooserUI extends FileChooserUI
}
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 @@ public class BasicFileChooserUI extends FileChooserUI
}
/**
- * DOCUMENT ME!
+ * Sets the JFileChooser to the selected file on an update
*
* @param e DOCUMENT ME!
*/
@@ -550,9 +553,15 @@ public class BasicFileChooserUI extends FileChooserUI
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 @@ public class BasicFileChooserUI extends FileChooserUI
* @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 --
diff --git a/javax/swing/plaf/metal/MetalFileChooserUI.java b/javax/swing/plaf/metal/MetalFileChooserUI.java
index 1219ad9fd..fc8024d7b 100644
--- a/javax/swing/plaf/metal/MetalFileChooserUI.java
+++ b/javax/swing/plaf/metal/MetalFileChooserUI.java
@@ -303,8 +303,9 @@ public class MetalFileChooserUI
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 @@ public class MetalFileChooserUI
{
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 class MetalFileChooserUI
*/
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 @@ public class MetalFileChooserUI
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 class MetalFileChooserUI
*/
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();
- }
}
}