📄 filechooserui.java
字号:
package com.digitprop.tonic;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.table.*;
import javax.swing.text.Position;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.util.*;
import sun.awt.shell.ShellFolder;
/** The UI delegate for JFileChooser instances. This is basically
* identical to the Metal file chooser, except that it prevents
* the renaming of files at simple double clicks, and that is
* displays the OS specific icons for files types, instead of
* generic icons.
*
* @author Markus Fischer
*
* <p>This software is under the <a href="http://www.gnu.org/copyleft/lesser.html" target="_blank">GNU Lesser General Public License</a>
*/
/*
* ------------------------------------------------------------------------
* Copyright (C) 2004 Markus Fischer
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* You can contact the author at:
* Markus Fischer
* www.digitprop.com
* info@digitprop.com
* ------------------------------------------------------------------------
*/
public class FileChooserUI extends BasicFileChooserUI
{
/** Caches icons used for the file chooser */
private static final Hashtable iconCache=new Hashtable();
private JPanel centerPanel;
private JLabel lookInLabel;
private JComboBox directoryComboBox;
private DirectoryComboBoxModel directoryComboBoxModel;
private Action directoryComboBoxAction= new DirectoryComboBoxAction();
private FilterComboBoxModel filterComboBoxModel;
private JTextField fileNameTextField;
private JToggleButton listViewButton;
private JToggleButton detailsViewButton;
private JPanel listViewPanel;
private JPanel detailsViewPanel;
private JPanel currentViewPanel;
private FocusListener editorFocusListener= new FocusAdapter()
{
public void focusLost(FocusEvent e)
{
if (!e.isTemporary())
{
applyEdit();
}
}
};
private boolean useShellFolder;
private ListSelectionModel listSelectionModel;
private JList list;
private JTable detailsTable;
private JButton approveButton;
private JButton cancelButton;
private JPanel buttonPanel;
private JPanel bottomPanel;
private JComboBox filterComboBox;
private static final Dimension hstrut5= new Dimension(5, 1);
private static final Dimension hstrut11= new Dimension(11, 1);
private static final Dimension vstrut5= new Dimension(1, 5);
private static final Insets shrinkwrap= new Insets(0, 0, 0, 0);
// Preferred and Minimum sizes for the dialog box
private static int PREF_WIDTH= 500;
private static int PREF_HEIGHT= 326;
private static Dimension PREF_SIZE= new Dimension(PREF_WIDTH, PREF_HEIGHT);
private static int MIN_WIDTH= 500;
private static int MIN_HEIGHT= 326;
private static Dimension MIN_SIZE= new Dimension(MIN_WIDTH, MIN_HEIGHT);
private static int LIST_PREF_WIDTH= 405;
private static int LIST_PREF_HEIGHT= 135;
private static Dimension LIST_PREF_SIZE=
new Dimension(LIST_PREF_WIDTH, LIST_PREF_HEIGHT);
private static final int COLUMN_FILENAME= 0;
private static final int COLUMN_FILESIZE= 1;
private static final int COLUMN_FILETYPE= 2;
private static final int COLUMN_FILEDATE= 3;
private static final int COLUMN_FILEATTR= 4;
private static final int COLUMN_COLCOUNT= 5;
private int[] COLUMN_WIDTHS= { 150, 75, 130, 130, 40 };
// Labels, mnemonics, and tooltips (oh my!)
private int lookInLabelMnemonic= 0;
private String lookInLabelText= null;
private String saveInLabelText= null;
private int fileNameLabelMnemonic= 0;
private String fileNameLabelText= null;
private int filesOfTypeLabelMnemonic= 0;
private String filesOfTypeLabelText= null;
private String upFolderToolTipText= null;
private String upFolderAccessibleName= null;
private String homeFolderToolTipText= null;
private String homeFolderAccessibleName= null;
private String newFolderToolTipText= null;
private String newFolderAccessibleName= null;
private String listViewButtonToolTipText= null;
private String listViewButtonAccessibleName= null;
private String detailsViewButtonToolTipText= null;
private String detailsViewButtonAccessibleName= null;
private String fileNameHeaderText= null;
private String fileSizeHeaderText= null;
private String fileTypeHeaderText= null;
private String fileDateHeaderText= null;
private String fileAttrHeaderText= null;
/** Creates and returns a UI delegate for the
* specified component.
*/
public static ComponentUI createUI(JComponent c)
{
return new FileChooserUI((JFileChooser) c);
}
/** Creates an instance for the specified JFileChooser */
public FileChooserUI(JFileChooser filechooser)
{
super(filechooser);
}
/** Installs the UI delegate for the specified component */
public void installUI(JComponent c)
{
super.installUI(c);
}
/** Uninstalls the UI delegate for the specified JFileChooser */
public void uninstallComponents(JFileChooser fc)
{
fc.removeAll();
bottomPanel= null;
buttonPanel= null;
}
/** Installs the components for the specified JFileChooser */
public void installComponents(JFileChooser fc)
{
FileSystemView fsv= fc.getFileSystemView();
fc.setBorder(new EmptyBorder(12, 12, 11, 11));
fc.setLayout(new BorderLayout(0, 11));
// ********************************* //
// **** Construct the top panel **** //
// ********************************* //
// Directory manipulation buttons
JPanel topPanel= new JPanel(new BorderLayout(11, 0));
JPanel topButtonPanel= new JPanel();
topButtonPanel.setLayout(
new BoxLayout(topButtonPanel, BoxLayout.LINE_AXIS));
topPanel.add(topButtonPanel, BorderLayout.AFTER_LINE_ENDS);
// Add the top panel to the fileChooser
fc.add(topPanel, BorderLayout.NORTH);
// ComboBox Label
lookInLabel= new JLabel(lookInLabelText);
lookInLabel.setDisplayedMnemonic(lookInLabelMnemonic);
topPanel.add(lookInLabel, BorderLayout.BEFORE_LINE_BEGINS);
// CurrentDir ComboBox
directoryComboBox= new JComboBox();
directoryComboBox.putClientProperty(
"JComboBox.lightweightKeyboardNavigation",
"Lightweight");
lookInLabel.setLabelFor(directoryComboBox);
directoryComboBoxModel= createDirectoryComboBoxModel(fc);
directoryComboBox.setModel(directoryComboBoxModel);
directoryComboBox.addActionListener(directoryComboBoxAction);
directoryComboBox.setRenderer(createDirectoryComboBoxRenderer(fc));
directoryComboBox.setAlignmentX(JComponent.LEFT_ALIGNMENT);
directoryComboBox.setAlignmentY(JComponent.TOP_ALIGNMENT);
directoryComboBox.setMaximumRowCount(8);
topPanel.add(directoryComboBox, BorderLayout.CENTER);
// Up Button
JButton upFolderButton= new JButton(getChangeToParentDirectoryAction());
upFolderButton.setText(null);
upFolderButton.setIcon(upFolderIcon);
upFolderButton.setDisabledIcon(new GrayedIcon(upFolderIcon));
upFolderButton.setToolTipText(upFolderToolTipText);
upFolderButton.getAccessibleContext().setAccessibleName(
upFolderAccessibleName);
upFolderButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
upFolderButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
upFolderButton.setMargin(shrinkwrap);
topButtonPanel.add(upFolderButton);
topButtonPanel.add(Box.createRigidArea(hstrut5));
// Home Button
File homeDir= fsv.getHomeDirectory();
String toolTipText= homeFolderToolTipText;
if (fsv.isRoot(homeDir))
{
toolTipText= getFileView(fc).getName(homeDir); // Probably "Desktop".
}
JButton b= new JButton(homeFolderIcon);
b.setDisabledIcon(new GrayedIcon(homeFolderIcon));
b.setToolTipText(toolTipText);
b.getAccessibleContext().setAccessibleName(homeFolderAccessibleName);
b.setAlignmentX(JComponent.LEFT_ALIGNMENT);
b.setAlignmentY(JComponent.CENTER_ALIGNMENT);
b.setMargin(shrinkwrap);
b.addActionListener(getGoHomeAction());
topButtonPanel.add(b);
topButtonPanel.add(Box.createRigidArea(hstrut5));
// New Directory Button
b= new JButton(getNewFolderAction());
b.setText(null);
b.setIcon(newFolderIcon);
b.setDisabledIcon(new GrayedIcon(newFolderIcon));
b.setToolTipText(newFolderToolTipText);
b.getAccessibleContext().setAccessibleName(newFolderAccessibleName);
b.setAlignmentX(JComponent.LEFT_ALIGNMENT);
b.setAlignmentY(JComponent.CENTER_ALIGNMENT);
b.setMargin(shrinkwrap);
topButtonPanel.add(b);
topButtonPanel.add(Box.createRigidArea(hstrut5));
// View button group
ButtonGroup viewButtonGroup= new ButtonGroup();
class ViewButtonListener implements ActionListener
{
JFileChooser fc;
ViewButtonListener(JFileChooser fc)
{
this.fc= fc;
}
public void actionPerformed(ActionEvent e)
{
JToggleButton b= (JToggleButton) e.getSource();
JPanel oldViewPanel= currentViewPanel;
if (b == detailsViewButton)
{
if (detailsViewPanel == null)
{
detailsViewPanel= createDetailsView(fc);
detailsViewPanel.setPreferredSize(LIST_PREF_SIZE);
}
currentViewPanel= detailsViewPanel;
}
else
{
currentViewPanel= listViewPanel;
}
if (currentViewPanel != oldViewPanel)
{
centerPanel.remove(oldViewPanel);
centerPanel.add(currentViewPanel, BorderLayout.CENTER);
centerPanel.revalidate();
centerPanel.repaint();
}
}
}
ViewButtonListener viewButtonListener= new ViewButtonListener(fc);
// List Button
listViewButton= new JToggleButton(listViewIcon);
listViewButton.setToolTipText(listViewButtonToolTipText);
listViewButton.getAccessibleContext().setAccessibleName(
listViewButtonAccessibleName);
listViewButton.setSelected(true);
listViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
listViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
listViewButton.setMargin(shrinkwrap);
listViewButton.addActionListener(viewButtonListener);
topButtonPanel.add(listViewButton);
viewButtonGroup.add(listViewButton);
topButtonPanel.add(Box.createHorizontalStrut(2));
// Details Button
detailsViewButton= new JToggleButton(detailsViewIcon);
detailsViewButton.setToolTipText(detailsViewButtonToolTipText);
detailsViewButton.getAccessibleContext().setAccessibleName(
detailsViewButtonAccessibleName);
detailsViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
detailsViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
detailsViewButton.setMargin(shrinkwrap);
detailsViewButton.addActionListener(viewButtonListener);
topButtonPanel.add(detailsViewButton);
viewButtonGroup.add(detailsViewButton);
// Use ShellFolder class to populate combobox only if
// FileSystemView.getRoots() returns one folder and that is
// the same as the first item in the ShellFolder combobox list.
{
useShellFolder= false;
File[] roots= fsv.getRoots();
if (roots != null && roots.length == 1)
{
File[] cbFolders=
(File[]) ShellFolder.get("fileChooserComboBoxFolders");
if (cbFolders != null
&& cbFolders.length > 0
&& roots[0] == cbFolders[0])
{
useShellFolder= true;
}
}
}
// ************************************** //
// ******* Add the directory pane ******* //
// ************************************** //
centerPanel= new JPanel(new BorderLayout());
listViewPanel= createList(fc);
listSelectionModel= list.getSelectionModel();
listViewPanel.setPreferredSize(LIST_PREF_SIZE);
centerPanel.add(listViewPanel, BorderLayout.CENTER);
currentViewPanel= listViewPanel;
centerPanel.add(getAccessoryPanel(), BorderLayout.AFTER_LINE_ENDS);
JComponent accessory= fc.getAccessory();
if (accessory != null)
{
getAccessoryPanel().add(accessory);
}
fc.add(centerPanel, BorderLayout.CENTER);
// ********************************** //
// **** Construct the bottom panel ** //
// ********************************** //
JPanel bottomPanel= getBottomPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
fc.add(bottomPanel, BorderLayout.SOUTH);
// FileName label and textfield
JPanel fileNamePanel= new JPanel();
fileNamePanel.setLayout(
new BoxLayout(fileNamePanel, BoxLayout.LINE_AXIS));
bottomPanel.add(fileNamePanel);
bottomPanel.add(Box.createRigidArea(vstrut5));
AlignedLabel fileNameLabel= new AlignedLabel(fileNameLabelText);
fileNameLabel.setDisplayedMnemonic(fileNameLabelMnemonic);
fileNamePanel.add(fileNameLabel);
fileNameTextField= new JTextField()
{
public Dimension getMaximumSize()
{
return new Dimension(
Short.MAX_VALUE,
super.getPreferredSize().height);
}
};
fileNamePanel.add(fileNameTextField);
fileNameLabel.setLabelFor(fileNameTextField);
fileNameTextField.addFocusListener(new FocusAdapter()
{
public void focusGained(FocusEvent e)
{
if (!getFileChooser().isMultiSelectionEnabled())
{
listSelectionModel.clearSelection();
}
}
});
if (fc.isMultiSelectionEnabled())
{
setFileName(fileNameString(fc.getSelectedFiles()));
}
else
{
setFileName(fileNameString(fc.getSelectedFile()));
}
// Filetype label and combobox
JPanel filesOfTypePanel= new JPanel();
filesOfTypePanel.setLayout(
new BoxLayout(filesOfTypePanel, BoxLayout.LINE_AXIS));
bottomPanel.add(filesOfTypePanel);
AlignedLabel filesOfTypeLabel= new AlignedLabel(filesOfTypeLabelText);
filesOfTypeLabel.setDisplayedMnemonic(filesOfTypeLabelMnemonic);
filesOfTypePanel.add(filesOfTypeLabel);
filterComboBoxModel= createFilterComboBoxModel();
fc.addPropertyChangeListener(filterComboBoxModel);
filterComboBox= new JComboBox(filterComboBoxModel);
filesOfTypeLabel.setLabelFor(filterComboBox);
filterComboBox.setRenderer(createFilterComboBoxRenderer());
filesOfTypePanel.add(filterComboBox);
// buttons
getButtonPanel().setLayout(new ButtonAreaLayout());
approveButton= new JButton(getApproveButtonText(fc));
// Note: Metal does not use mnemonics for approve and cancel
approveButton.addActionListener(getApproveSelectionAction());
approveButton.setToolTipText(getApproveButtonToolTipText(fc));
getButtonPanel().add(approveButton);
cancelButton= new JButton(cancelButtonText);
cancelButton.setToolTipText(cancelButtonToolTipText);
cancelButton.addActionListener(getCancelSelectionAction());
getButtonPanel().add(cancelButton);
if (fc.getControlButtonsAreShown())
{
addControlButtons();
}
groupLabels(new AlignedLabel[] { fileNameLabel, filesOfTypeLabel });
}
protected ImageIcon getDisabledIcon(Icon icon, Component c)
{
Image img=c.createImage(icon.getIconWidth(), icon.getIconHeight());
Graphics g=img.getGraphics();
icon.paintIcon(c, g, 0, 0);
g.dispose();
ImageIcon disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(img));
return disabledIcon;
}
protected JPanel getButtonPanel()
{
if (buttonPanel == null)
{
buttonPanel= new JPanel();
}
return buttonPanel;
}
protected JPanel getBottomPanel()
{
if (bottomPanel == null)
{
bottomPanel= new JPanel();
}
return bottomPanel;
}
protected void installStrings(JFileChooser fc)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -