--- a/jdk/src/share/demo/jfc/FileChooserDemo/ExampleFileView.java Fri Mar 25 13:24:39 2011 +0100
+++ b/jdk/src/share/demo/jfc/FileChooserDemo/ExampleFileView.java Fri Mar 25 13:27:21 2011 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -29,14 +29,13 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-/*
- */
import javax.swing.*;
import javax.swing.filechooser.*;
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
-import java.io.File;
-import java.util.Hashtable;
/**
* A convenience implementation of the FileView interface that
@@ -61,15 +60,19 @@
* @author Jeff Dinkins
*/
public class ExampleFileView extends FileView {
- private final Hashtable<String, Icon> icons = new Hashtable<String, Icon>();
- private final Hashtable<File, String> fileDescriptions = new Hashtable<File, String>();
- private final Hashtable<String, String> typeDescriptions = new Hashtable<String, String>();
+
+ private final Map<String, Icon> icons = new HashMap<String, Icon>();
+ private final Map<File, String> fileDescriptions =
+ new HashMap<File, String>();
+ private final Map<String, String> typeDescriptions =
+ new HashMap<String, String>();
/**
* The name of the file. Do nothing special here. Let
* the system file view handle this.
* @see FileView#getName
*/
+ @Override
public String getName(File f) {
return null;
}
@@ -86,6 +89,7 @@
*
* @see FileView#getDescription
*/
+ @Override
public String getDescription(File f) {
return fileDescriptions.get(f);
}
@@ -112,6 +116,7 @@
*
* @see FileView#getTypeDescription
*/
+ @Override
public String getTypeDescription(File f) {
return typeDescriptions.get(getExtension(f));
}
@@ -122,12 +127,12 @@
*/
private String getExtension(File f) {
String name = f.getName();
- if(name != null) {
+ if (name != null) {
int extensionIndex = name.lastIndexOf('.');
- if(extensionIndex < 0) {
+ if (extensionIndex < 0) {
return null;
}
- return name.substring(extensionIndex+1).toLowerCase();
+ return name.substring(extensionIndex + 1).toLowerCase();
}
return null;
}
@@ -147,10 +152,11 @@
*
* @see FileView#getIcon
*/
+ @Override
public Icon getIcon(File f) {
Icon icon = null;
String extension = getExtension(f);
- if(extension != null) {
+ if (extension != null) {
icon = icons.get(extension);
}
return icon;
@@ -168,11 +174,11 @@
*
* @see FileView#isTraversable
*/
+ @Override
public Boolean isTraversable(File f) {
// if (some_reason) {
// return Boolean.FALSE;
// }
return null; // Use default from FileSystemView
}
-
}
--- a/jdk/src/share/demo/jfc/FileChooserDemo/FileChooserDemo.java Fri Mar 25 13:24:39 2011 +0100
+++ b/jdk/src/share/demo/jfc/FileChooserDemo/FileChooserDemo.java Fri Mar 25 13:27:21 2011 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -29,21 +29,56 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-/*
- */
-import javax.swing.*;
-import javax.swing.filechooser.*;
+import java.lang.reflect.InvocationTargetException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.UIManager.LookAndFeelInfo;
+import java.awt.BorderLayout;
+import java.awt.CardLayout;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.Insets;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.List;
+import javax.swing.BorderFactory;
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+import javax.swing.ButtonGroup;
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JComboBox;
+import javax.swing.JComponent;
+import javax.swing.JDialog;
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JRadioButton;
+import javax.swing.JTextField;
+import javax.swing.JToggleButton;
+import javax.swing.LookAndFeel;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+import javax.swing.WindowConstants;
+import javax.swing.filechooser.FileFilter;
+import javax.swing.filechooser.FileNameExtensionFilter;
+import javax.swing.filechooser.FileSystemView;
+import java.util.ArrayList;
import javax.swing.plaf.FileChooserUI;
import javax.swing.plaf.basic.BasicFileChooserUI;
+import java.io.File;
+import static javax.swing.JFileChooser.*;
-import java.awt.*;
-import java.io.File;
-import java.awt.event.*;
-import java.beans.*;
-import java.util.Vector;
-
-import static javax.swing.JFileChooser.*;
/**
*
@@ -51,12 +86,18 @@
*
* @author Jeff Dinkins
*/
+@SuppressWarnings("serial")
public class FileChooserDemo extends JPanel implements ActionListener {
+
+ public static final String NIMBUS_LAF_NAME = "Nimbus";
private static JFrame frame;
+ private final List<SupportedLaF> supportedLaFs =
+ new ArrayList<SupportedLaF>();
+ private static SupportedLaF nimbusLaF;
- private final Vector<SupportedLaF> supportedLaFs = new Vector();
private static class SupportedLaF {
+
private final String name;
private final LookAndFeel laf;
@@ -65,18 +106,15 @@
this.laf = laf;
}
+ @Override
public String toString() {
return name;
}
}
-
-
private JButton showButton;
-
private JCheckBox showAllFilesFilterCheckBox;
private JCheckBox showImageFilesFilterCheckBox;
private JCheckBox showFullDescriptionCheckBox;
-
private JCheckBox useFileViewCheckBox;
private JCheckBox useFileSystemViewCheckBox;
private JCheckBox accessoryCheckBox;
@@ -84,46 +122,44 @@
private JCheckBox useEmbedInWizardCheckBox;
private JCheckBox useControlsCheckBox;
private JCheckBox enableDragCheckBox;
-
private JRadioButton singleSelectionRadioButton;
private JRadioButton multiSelectionRadioButton;
-
private JRadioButton openRadioButton;
private JRadioButton saveRadioButton;
private JRadioButton customButton;
-
private JComboBox lafComboBox;
-
private JRadioButton justFilesRadioButton;
private JRadioButton justDirectoriesRadioButton;
private JRadioButton bothFilesAndDirectoriesRadioButton;
-
private JTextField customField;
-
private final ExampleFileView fileView;
-
private final ExampleFileSystemView fileSystemView;
-
- private final static Dimension hpad10 = new Dimension(10,1);
- private final static Dimension vpad20 = new Dimension(1,20);
+ private final static Dimension hpad10 = new Dimension(10, 1);
+ private final static Dimension vpad20 = new Dimension(1, 20);
private final static Dimension vpad7 = new Dimension(1, 7);
private final static Dimension vpad4 = new Dimension(1, 4);
private final static Insets insets = new Insets(5, 10, 0, 10);
-
private final FilePreviewer previewer;
private final JFileChooser chooser;
+ @SuppressWarnings("LeakingThisInConstructor")
public FileChooserDemo() {
- UIManager.LookAndFeelInfo[] installedLafs = UIManager.getInstalledLookAndFeels();
+ UIManager.LookAndFeelInfo[] installedLafs = UIManager.
+ getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo lafInfo : installedLafs) {
try {
- Class lnfClass = Class.forName(lafInfo.getClassName());
- LookAndFeel laf = (LookAndFeel)(lnfClass.newInstance());
+ Class<?> lnfClass = Class.forName(lafInfo.getClassName());
+ LookAndFeel laf = (LookAndFeel) (lnfClass.newInstance());
if (laf.isSupportedLookAndFeel()) {
String name = lafInfo.getName();
- supportedLaFs.add(new SupportedLaF(name, laf));
+ SupportedLaF supportedLaF = new SupportedLaF(name, laf);
+ supportedLaFs.add(supportedLaF);
+ if (NIMBUS_LAF_NAME.equals(name)) {
+ nimbusLaF = supportedLaF;
+ }
}
- } catch (Exception e) { // If ANYTHING weird happens, don't add it
+ } catch (Exception ignored) {
+ // If ANYTHING weird happens, don't add this L&F
}
}
@@ -134,8 +170,10 @@
// Create Custom FileView
fileView = new ExampleFileView();
- fileView.putIcon("jpg", new ImageIcon(getClass().getResource("/resources/images/jpgIcon.jpg")));
- fileView.putIcon("gif", new ImageIcon(getClass().getResource("/resources/images/gifIcon.gif")));
+ fileView.putIcon("jpg", new ImageIcon(getClass().getResource(
+ "/resources/images/jpgIcon.jpg")));
+ fileView.putIcon("gif", new ImageIcon(getClass().getResource(
+ "/resources/images/gifIcon.gif")));
// Create Custom FileSystemView
fileSystemView = new ExampleFileSystemView();
@@ -155,8 +193,11 @@
customButton.addActionListener(optionListener);
customField = new JTextField(8) {
+
+ @Override
public Dimension getMaximumSize() {
- return new Dimension(getPreferredSize().width, getPreferredSize().height);
+ return new Dimension(getPreferredSize().width,
+ getPreferredSize().height);
}
};
customField.setText("Doit");
@@ -220,7 +261,8 @@
group3.add(justDirectoriesRadioButton);
justDirectoriesRadioButton.addActionListener(optionListener);
- bothFilesAndDirectoriesRadioButton = new JRadioButton("Select Files or Directories");
+ bothFilesAndDirectoriesRadioButton = new JRadioButton(
+ "Select Files or Directories");
group3.add(bothFilesAndDirectoriesRadioButton);
bothFilesAndDirectoriesRadioButton.addActionListener(optionListener);
@@ -241,7 +283,8 @@
showButton.setMnemonic('s');
// Create laf combo box
- lafComboBox = new JComboBox(supportedLaFs);
+ lafComboBox = new JComboBox(supportedLaFs.toArray());
+ lafComboBox.setSelectedItem(nimbusLaF);
lafComboBox.setEditable(false);
lafComboBox.addActionListener(optionListener);
@@ -317,7 +360,8 @@
// ************* File & Directory Options *****************
// ********************************************************
JPanel control4 = new InsetPanel(insets);
- control4.setBorder(BorderFactory.createTitledBorder("File and Directory Options"));
+ control4.setBorder(BorderFactory.createTitledBorder(
+ "File and Directory Options"));
control4.setLayout(new BoxLayout(control4, BoxLayout.Y_AXIS));
control4.add(Box.createRigidArea(vpad20));
control4.add(justFilesRadioButton);
@@ -379,7 +423,7 @@
// clear the preview from the previous display of the chooser
JComponent accessory = chooser.getAccessory();
if (accessory != null) {
- ((FilePreviewer)accessory).loadImage(null);
+ ((FilePreviewer) accessory).loadImage(null);
}
if (useEmbedInWizardCheckBox.isSelected()) {
@@ -393,25 +437,28 @@
if (retval == APPROVE_OPTION) {
JOptionPane.showMessageDialog(frame, getResultString());
} else if (retval == CANCEL_OPTION) {
- JOptionPane.showMessageDialog(frame, "User cancelled operation. No file was chosen.");
+ JOptionPane.showMessageDialog(frame,
+ "User cancelled operation. No file was chosen.");
} else if (retval == ERROR_OPTION) {
- JOptionPane.showMessageDialog(frame, "An error occured. No file was chosen.");
+ JOptionPane.showMessageDialog(frame,
+ "An error occured. No file was chosen.");
} else {
JOptionPane.showMessageDialog(frame, "Unknown operation occured.");
}
}
private void resetFileFilters(boolean enableFilters,
- boolean showExtensionInDescription) {
+ boolean showExtensionInDescription) {
chooser.resetChoosableFileFilters();
if (enableFilters) {
- FileFilter jpgFilter = createFileFilter("JPEG Compressed Image Files",
- showExtensionInDescription, "jpg");
+ FileFilter jpgFilter = createFileFilter(
+ "JPEG Compressed Image Files",
+ showExtensionInDescription, "jpg");
FileFilter gifFilter = createFileFilter("GIF Image Files",
- showExtensionInDescription, "gif");
+ showExtensionInDescription, "gif");
FileFilter bothFilter = createFileFilter("JPEG and GIF Image Files",
- showExtensionInDescription, "jpg",
- "gif");
+ showExtensionInDescription, "jpg",
+ "gif");
chooser.addChoosableFileFilter(bothFilter);
chooser.addChoosableFileFilter(jpgFilter);
chooser.addChoosableFileFilter(gifFilter);
@@ -419,7 +466,7 @@
}
private FileFilter createFileFilter(String description,
- boolean showExtensionInDescription, String...extensions) {
+ boolean showExtensionInDescription, String... extensions) {
if (showExtensionInDescription) {
description = createFileNameFilterDescriptionFromExtensions(
description, extensions);
@@ -429,8 +476,8 @@
private String createFileNameFilterDescriptionFromExtensions(
String description, String[] extensions) {
- String fullDescription = (description == null) ?
- "(" : description + " (";
+ String fullDescription = (description == null) ? "(" : description
+ + " (";
// build the description from the extension list
fullDescription += "." + extensions[0];
for (int i = 1; i < extensions.length; i++) {
@@ -441,12 +488,15 @@
return fullDescription;
}
+
private class WizardDialog extends JDialog implements ActionListener {
+
CardLayout cardLayout;
JPanel cardPanel;
JLabel messageLabel;
JButton backButton, nextButton, closeButton;
+ @SuppressWarnings("LeakingThisInConstructor")
WizardDialog(JFrame frame, boolean modal) {
super(frame, "Embedded JFileChooser Demo", modal);
@@ -494,15 +544,16 @@
// Workaround for bug 4528663. This is necessary to
// pick up the contents of the file chooser text field.
// This will trigger an APPROVE_SELECTION action.
- ((BasicFileChooserUI)ui).getApproveSelectionAction().actionPerformed(null);
+ ((BasicFileChooserUI) ui).getApproveSelectionAction().
+ actionPerformed(null);
} else {
next();
}
} else if (src == closeButton) {
close();
- } else if (cmd == APPROVE_SELECTION) {
+ } else if (APPROVE_SELECTION.equals(cmd)) {
next();
- } else if (cmd == CANCEL_SELECTION) {
+ } else if (CANCEL_SELECTION.equals(cmd)) {
close();
}
}
@@ -528,6 +579,7 @@
setVisible(false);
}
+ @Override
public void dispose() {
chooser.removeActionListener(this);
@@ -542,13 +594,18 @@
private String getResultString() {
String resultString;
- String filter = chooser.getFileFilter().getDescription();
+ String filter;
+ if (chooser.getFileFilter() == null) {
+ filter = "";
+ } else {
+ filter = chooser.getFileFilter().getDescription();
+ }
String path = null;
boolean isDirMode = (chooser.getFileSelectionMode() == DIRECTORIES_ONLY);
boolean isMulti = chooser.isMultiSelectionEnabled();
if (isMulti) {
- File [] files = chooser.getSelectedFiles();
+ File[] files = chooser.getSelectedFiles();
if (files != null && files.length > 0) {
path = "";
for (File file : files) {
@@ -565,11 +622,10 @@
path = path.replace(" ", " ");
filter = filter.replace(" ", " ");
resultString =
- "<html>You chose " + (isMulti ? "these" : "this") + " " +
- (isDirMode ? (isMulti ? "directories" : "directory")
- : (isMulti ? "files" : "file")) +
- ": <code>" + path +
- "</code><br><br>with filter: <br><code>" + filter;
+ "<html>You chose " + (isMulti ? "these" : "this") + " " + (isDirMode ? (isMulti
+ ? "directories" : "directory")
+ : (isMulti ? "files" : "file")) + ": <code>" + path
+ + "</code><br><br>with filter: <br><code>" + filter;
} else {
resultString = "Nothing was chosen";
}
@@ -577,15 +633,14 @@
}
-
-
/** An ActionListener that listens to the radio buttons. */
private class OptionListener implements ActionListener {
+
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent) e.getSource();
boolean selected = false;
if (c instanceof JToggleButton) {
- selected = ((JToggleButton)c).isSelected();
+ selected = ((JToggleButton) c).isSelected();
}
if (c == openRadioButton) {
@@ -612,7 +667,7 @@
chooser.setAcceptAllFileFilterUsed(selected);
} else if (c == showImageFilesFilterCheckBox) {
resetFileFilters(selected,
- showFullDescriptionCheckBox.isSelected());
+ showFullDescriptionCheckBox.isSelected());
showFullDescriptionCheckBox.setEnabled(selected);
} else if (c == setHiddenCheckBox) {
chooser.setFileHidingEnabled(!selected);
@@ -637,7 +692,7 @@
}
} else if (c == showFullDescriptionCheckBox) {
resetFileFilters(showImageFilesFilterCheckBox.isSelected(),
- selected);
+ selected);
} else if (c == justFilesRadioButton) {
chooser.setFileSelectionMode(FILES_ONLY);
} else if (c == justDirectoriesRadioButton) {
@@ -653,27 +708,33 @@
chooser.setMultiSelectionEnabled(true);
}
} else if (c == lafComboBox) {
- SupportedLaF supportedLaF = ((SupportedLaF)lafComboBox.getSelectedItem());
+ SupportedLaF supportedLaF = ((SupportedLaF) lafComboBox.
+ getSelectedItem());
LookAndFeel laf = supportedLaF.laf;
try {
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(frame);
- if(chooser != null) {
+ if (chooser != null) {
SwingUtilities.updateComponentTreeUI(chooser);
}
frame.pack();
} catch (UnsupportedLookAndFeelException exc) {
// This should not happen because we already checked
- ((DefaultComboBoxModel)lafComboBox.getModel()).removeElement(supportedLaF);
+ ((DefaultComboBoxModel) lafComboBox.getModel()).
+ removeElement(supportedLaF);
}
}
}
}
- private class FilePreviewer extends JComponent implements PropertyChangeListener {
+
+ private class FilePreviewer extends JComponent implements
+ PropertyChangeListener {
+
ImageIcon thumbnail = null;
+ @SuppressWarnings("LeakingThisInConstructor")
public FilePreviewer(JFileChooser fc) {
setPreferredSize(new Dimension(100, 50));
fc.addPropertyChangeListener(this);
@@ -684,9 +745,10 @@
thumbnail = null;
} else {
ImageIcon tmpIcon = new ImageIcon(f.getPath());
- if(tmpIcon.getIconWidth() > 90) {
+ if (tmpIcon.getIconWidth() > 90) {
thumbnail = new ImageIcon(
- tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
+ tmpIcon.getImage().getScaledInstance(90, -1,
+ Image.SCALE_DEFAULT));
} else {
thumbnail = tmpIcon;
}
@@ -695,23 +757,24 @@
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
- if (prop == SELECTED_FILE_CHANGED_PROPERTY) {
- if(isShowing()) {
+ if (SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
+ if (isShowing()) {
loadImage((File) e.getNewValue());
repaint();
}
}
}
+ @Override
public void paint(Graphics g) {
- if(thumbnail != null) {
- int x = getWidth()/2 - thumbnail.getIconWidth()/2;
- int y = getHeight()/2 - thumbnail.getIconHeight()/2;
- if(y < 0) {
+ if (thumbnail != null) {
+ int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
+ int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;
+ if (y < 0) {
y = 0;
}
- if(x < 5) {
+ if (x < 5) {
x = 5;
}
thumbnail.paintIcon(this, g, x, y);
@@ -720,46 +783,57 @@
}
public static void main(String s[]) {
- /*
- NOTE: By default, the look and feel will be set to the
- Cross Platform Look and Feel (which is currently Metal).
- The user may someday be able to override the default
- via a system property. If you as the developer want to
- be sure that a particular L&F is set, you can do so
- by calling UIManager.setLookAndFeel(). For example, the
- first code snippet below forcibly sets the UI to be the
- System Look and Feel. The second code snippet forcibly
- sets the look and feel to the Cross Platform L&F.
+ try {
+ SwingUtilities.invokeAndWait(new Runnable() {
- Snippet 1:
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception exc) {
- System.err.println("Error loading L&F: " + exc);
- }
+ public void run() {
+ /*
+ * NOTE: By default, the look and feel will be set to the
+ * Cross Platform Look and Feel (which is currently Metal).
+ * The following code tries to set the Look and Feel to Nimbus.
+ * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html
+ */
+ try {
+ for (LookAndFeelInfo info : UIManager.
+ getInstalledLookAndFeels()) {
+ if (NIMBUS_LAF_NAME.equals(info.getName())) {
+ UIManager.setLookAndFeel(info.getClassName());
+ break;
+ }
+ }
+ } catch (Exception ignored) {
+ }
- Snippet 2:
- try {
- UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
- } catch (Exception exc) {
- System.err.println("Error loading L&F: " + exc);
- }
- */
+ FileChooserDemo panel = new FileChooserDemo();
- FileChooserDemo panel = new FileChooserDemo();
-
- frame = new JFrame("FileChooserDemo");
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.getContentPane().add("Center", panel);
- frame.pack();
- frame.setVisible(true);
+ frame = new JFrame("FileChooserDemo");
+ frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+ frame.getContentPane().add("Center", panel);
+ frame.pack();
+ frame.setVisible(true);
+ }
+ });
+ } catch (InterruptedException ex) {
+ Logger.getLogger(FileChooserDemo.class.getName()).log(Level.SEVERE,
+ null,
+ ex);
+ } catch (InvocationTargetException ex) {
+ Logger.getLogger(FileChooserDemo.class.getName()).log(Level.SEVERE,
+ null,
+ ex);
+ }
}
+
private static class InsetPanel extends JPanel {
+
Insets i;
+
InsetPanel(Insets i) {
this.i = i;
}
+
+ @Override
public Insets getInsets() {
return i;
}