This fixes the handling of special files like . and .. in JFileChooser
among a couple of minor design things that I noticed while fixing this.
2006-08-02 Roman Kennke <[EMAIL PROTECTED]>
PR 27604
* javax/swing/plaf/basic/BasicChooserUI.java
(BasicFileView.getName): Fetch the real name from the
file chooser's FileSystemView.
* javax/swing/plaf/metal/MetalChooserUI.java
(DirectoryComboBoxRenderer.getListCellRendererComponent):
Set the text fetched from the JFileChooser.getName().
* javax/swing/FileSystemView.java
(createFileObject): When file is a filesystem root,
create a filesystem root object first.
(getSystemDisplayName): Return the filename. Added specnote
about ShellFolder class that is mentioned in the spec.
* javax/swing/UnixFileSystemView.java
(getSystemDisplayName): Implemented to return the real name
of a file, special handling files like '.' or '..'.
/Roman
Index: javax/swing/plaf/basic/BasicFileChooserUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicFileChooserUI.java,v
retrieving revision 1.27
diff -u -1 -2 -r1.27 BasicFileChooserUI.java
--- javax/swing/plaf/basic/BasicFileChooserUI.java 15 Jul 2006 21:37:54 -0000 1.27
+++ javax/swing/plaf/basic/BasicFileChooserUI.java 2 Aug 2006 10:43:23 -0000
@@ -259,25 +259,32 @@
return val;
}
/**
* Returns the name for the given file/directory.
*
* @param f the file/directory.
*
* @return The name of the file/directory.
*/
public String getName(File f)
{
- return f.getName();
+ String name = null;
+ if (f != null)
+ {
+ JFileChooser c = getFileChooser();
+ FileSystemView v = c.getFileSystemView();
+ name = v.getSystemDisplayName(f);
+ }
+ return name;
}
/**
* Returns a localised description for the type of file/directory.
*
* @param f the file/directory.
*
* @return A type description for the given file/directory.
*/
public String getTypeDescription(File f)
{
if (filechooser.isTraversable(f))
Index: javax/swing/plaf/metal/MetalFileChooserUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalFileChooserUI.java,v
retrieving revision 1.24
diff -u -1 -2 -r1.24 MetalFileChooserUI.java
--- javax/swing/plaf/metal/MetalFileChooserUI.java 15 Jul 2006 21:37:54 -0000 1.24
+++ javax/swing/plaf/metal/MetalFileChooserUI.java 2 Aug 2006 10:43:24 -0000
@@ -583,26 +583,26 @@
* @param index the item index.
* @param isSelected is the item selected?
* @param cellHasFocus does the list cell have focus?
*
* @return The list cell renderer.
*/
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
FileView fileView = getFileView(getFileChooser());
File file = (File) value;
setIcon(fileView.getIcon(file));
- setText(fileView.getName(file));
-
+ setText(getFileChooser().getName(file));
+
if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
Index: javax/swing/filechooser/FileSystemView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/filechooser/FileSystemView.java,v
retrieving revision 1.10
diff -u -1 -2 -r1.10 FileSystemView.java
--- javax/swing/filechooser/FileSystemView.java 2 Nov 2005 10:31:18 -0000 1.10
+++ javax/swing/filechooser/FileSystemView.java 2 Aug 2006 10:43:24 -0000
@@ -67,25 +67,28 @@
return new File(dir, filename);
}
/**
* Creates a new file object from the specified path.
*
* @param path the path.
*
* @return A new file object.
*/
public File createFileObject(String path)
{
- return new File(path);
+ File f = new File(path);
+ if (isFileSystemRoot(f))
+ f = this.createFileSystemRoot(f);
+ return f;
}
/**
* DOCUMENT ME!
*
* @param f DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
protected File createFileSystemRoot(File f)
{
File[] roots = File.listRoots();
@@ -214,34 +217,42 @@
* override this method.
*
* @return An array containing the file system roots.
*/
public File[] getRoots()
{
// subclass
return null;
}
/**
* Returns the name of a file as it would be displayed by the underlying
- * system. This implementation returns <code>null</code>, subclasses must
- * override.
+ * system.
*
* @param f the file.
*
- * @return <code>null</code>.
+ * @return the name of a file as it would be displayed by the underlying
+ * system
+ *
+ * @specnote The specification suggests that the information here is
+ * fetched from a ShellFolder class. This seems to be a non public
+ * private file handling class. We simply return File.getName()
+ * here and leave special handling to subclasses.
*/
public String getSystemDisplayName(File f)
{
- return null;
+ String name = null;
+ if (f != null)
+ name = f.getName();
+ return name;
}
/**
* Returns the icon that would be displayed for the given file by the
* underlying system. This implementation returns <code>null</code>,
* subclasses must override.
*
* @param f the file.
*
* @return <code>null</code>.
*/
public Icon getSystemIcon(File f)
Index: javax/swing/filechooser/UnixFileSystemView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/filechooser/UnixFileSystemView.java,v
retrieving revision 1.4
diff -u -1 -2 -r1.4 UnixFileSystemView.java
--- javax/swing/filechooser/UnixFileSystemView.java 23 Mar 2006 10:17:31 -0000 1.4
+++ javax/swing/filechooser/UnixFileSystemView.java 2 Aug 2006 10:43:24 -0000
@@ -97,35 +97,52 @@
/**
* Returns an array containing the file system root.
*
* @return An array containing the file system root.
*/
public File[] getRoots()
{
return File.listRoots();
}
/**
* Returns the name of a file as it would be displayed by the underlying
- * system. This method is NOT YET IMPLEMENTED.
+ * system.
*
* @param f the file.
*
- * @return <code>null</code>.
+ * @return the name of a file as it would be displayed by the underlying
+ * system
*/
public String getSystemDisplayName(File f)
- throws NotImplementedException
{
- // FIXME: Implement;
- return null;
+ String name = null;
+ if (f != null)
+ {
+ if (isRoot(f))
+ name = f.getAbsolutePath();
+ else
+ {
+ try
+ {
+ String path = f.getCanonicalPath();
+ name = path.substring(path.lastIndexOf(File.separator) + 1);
+ }
+ catch (IOException e)
+ {
+ name = f.getName();
+ }
+ }
+ }
+ return name;
}
/**
* Returns the icon that would be displayed for the given file by the
* underlying system. This method is NOT YET IMPLEMENTED.
*
* @param f the file.
*
* @return <code>null</code>.
*/
public Icon getSystemIcon(File f)
throws NotImplementedException