7027694: /jfc/FileChooserDemo demo needs to be improved
Reviewed-by: rupashka
This commit is contained in:
parent
3d4529f30f
commit
12f9a0fc72
@ -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,13 +29,12 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.swing.filechooser.FileSystemView;
|
||||
|
||||
|
||||
/**
|
||||
* This is a simple example that uses the FileSystemView class.
|
||||
* You can provide a superclass of the FileSystemView class with your own functionality.
|
||||
@ -43,6 +42,7 @@ import javax.swing.filechooser.FileSystemView;
|
||||
* @author Pavel Porvatov
|
||||
*/
|
||||
public class ExampleFileSystemView extends FileSystemView {
|
||||
|
||||
/**
|
||||
* Creates a new folder with the default name "New folder". This method is invoked
|
||||
* when the user presses the "New folder" button.
|
||||
@ -65,8 +65,9 @@ public class ExampleFileSystemView extends FileSystemView {
|
||||
* Returns a list which appears in a drop-down list of the FileChooser component.
|
||||
* In this implementation only the home directory is returned.
|
||||
*/
|
||||
@Override
|
||||
public File[] getRoots() {
|
||||
return new File[]{getHomeDirectory()};
|
||||
return new File[] { getHomeDirectory() };
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,9 +75,11 @@ public class ExampleFileSystemView extends FileSystemView {
|
||||
* A string with all upper case letters is returned for a directory.
|
||||
* A string with all lower case letters is returned for a file.
|
||||
*/
|
||||
@Override
|
||||
public String getSystemDisplayName(File f) {
|
||||
String displayName = super.getSystemDisplayName(f);
|
||||
|
||||
return f.isDirectory() ? displayName.toUpperCase() : displayName.toLowerCase();
|
||||
return f.isDirectory() ? displayName.toUpperCase() : displayName.
|
||||
toLowerCase();
|
||||
}
|
||||
}
|
||||
|
@ -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.Hashtable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* A convenience implementation of the FileView interface that
|
||||
@ -61,15 +60,19 @@ import java.util.Hashtable;
|
||||
* @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 @@ public class ExampleFileView extends FileView {
|
||||
*
|
||||
* @see FileView#getDescription
|
||||
*/
|
||||
@Override
|
||||
public String getDescription(File f) {
|
||||
return fileDescriptions.get(f);
|
||||
}
|
||||
@ -112,6 +116,7 @@ public class ExampleFileView extends FileView {
|
||||
*
|
||||
* @see FileView#getTypeDescription
|
||||
*/
|
||||
@Override
|
||||
public String getTypeDescription(File f) {
|
||||
return typeDescriptions.get(getExtension(f));
|
||||
}
|
||||
@ -122,12 +127,12 @@ public class ExampleFileView extends FileView {
|
||||
*/
|
||||
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 @@ public class ExampleFileView extends FileView {
|
||||
*
|
||||
* @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 @@ public class ExampleFileView extends FileView {
|
||||
*
|
||||
* @see FileView#isTraversable
|
||||
*/
|
||||
@Override
|
||||
public Boolean isTraversable(File f) {
|
||||
// if (some_reason) {
|
||||
// return Boolean.FALSE;
|
||||
// }
|
||||
return null; // Use default from FileSystemView
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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,34 +29,75 @@
|
||||
* 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.awt.*;
|
||||
import java.io.File;
|
||||
import java.awt.event.*;
|
||||
import java.beans.*;
|
||||
import java.util.Vector;
|
||||
|
||||
import static javax.swing.JFileChooser.*;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* A demo which makes extensive use of the file chooser.
|
||||
*
|
||||
* @author Jeff Dinkins
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
private static JFrame frame;
|
||||
|
||||
private final Vector<SupportedLaF> supportedLaFs = new Vector();
|
||||
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 static class SupportedLaF {
|
||||
|
||||
private final String name;
|
||||
private final LookAndFeel laf;
|
||||
|
||||
@ -65,18 +106,15 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
|
||||
// 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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
// ************* 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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
// 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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
}
|
||||
|
||||
private FileFilter createFileFilter(String description,
|
||||
boolean showExtensionInDescription, String...extensions) {
|
||||
boolean showExtensionInDescription, String... extensions) {
|
||||
if (showExtensionInDescription) {
|
||||
description = createFileNameFilterDescriptionFromExtensions(
|
||||
description, extensions);
|
||||
@ -429,8 +476,8 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
// 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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
chooser.removeActionListener(this);
|
||||
|
||||
@ -542,13 +594,18 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/** 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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
}
|
||||
} 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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 @@ public class FileChooserDemo extends JPanel implements ActionListener {
|
||||
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 class FileChooserDemo extends JPanel implements ActionListener {
|
||||
|
||||
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 class FileChooserDemo extends JPanel implements ActionListener {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user