📄 basicfilechooserui.java
字号:
/* * @(#)BasicFileChooserUI.java 1.74 07/08/22 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.basic;import javax.swing.*;import javax.swing.filechooser.*;import javax.swing.filechooser.FileFilter;import javax.swing.event.*;import javax.swing.plaf.*;import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.beans.*;import java.io.*;import java.util.*;import java.util.regex.*;import sun.awt.shell.ShellFolder;import sun.swing.*;import sun.swing.SwingUtilities2;/** * Basic L&F implementation of a FileChooser. * * @version %i% %g% * @author Jeff Dinkins */public class BasicFileChooserUI extends FileChooserUI { /* FileView icons */ protected Icon directoryIcon = null; protected Icon fileIcon = null; protected Icon computerIcon = null; protected Icon hardDriveIcon = null; protected Icon floppyDriveIcon = null; protected Icon newFolderIcon = null; protected Icon upFolderIcon = null; protected Icon homeFolderIcon = null; protected Icon listViewIcon = null; protected Icon detailsViewIcon = null; protected int saveButtonMnemonic = 0; protected int openButtonMnemonic = 0; protected int cancelButtonMnemonic = 0; protected int updateButtonMnemonic = 0; protected int helpButtonMnemonic = 0; /** * The mnemonic keycode used for the approve button when a directory * is selected and the current selection mode is FILES_ONLY. * * @since 1.4 */ protected int directoryOpenButtonMnemonic = 0; protected String saveButtonText = null; protected String openButtonText = null; protected String cancelButtonText = null; protected String updateButtonText = null; protected String helpButtonText = null; /** * The label text displayed on the approve button when a directory * is selected and the current selection mode is FILES_ONLY. * * @since 1.4 */ protected String directoryOpenButtonText = null; private String openDialogTitleText = null; private String saveDialogTitleText = null; protected String saveButtonToolTipText = null; protected String openButtonToolTipText = null; protected String cancelButtonToolTipText = null; protected String updateButtonToolTipText = null; protected String helpButtonToolTipText = null; /** * The tooltip text displayed on the approve button when a directory * is selected and the current selection mode is FILES_ONLY. * * @since 1.4 */ protected String directoryOpenButtonToolTipText = null; // Some generic FileChooser functions private Action approveSelectionAction = new ApproveSelectionAction(); private Action cancelSelectionAction = new CancelSelectionAction(); private Action updateAction = new UpdateAction(); private Action newFolderAction; private Action goHomeAction = new GoHomeAction(); private Action changeToParentDirectoryAction = new ChangeToParentDirectoryAction(); private String newFolderErrorSeparator = null; private String newFolderErrorText = null; private String fileDescriptionText = null; private String directoryDescriptionText = null; private JFileChooser filechooser = null; private boolean directorySelected = false; private File directory = null; private PropertyChangeListener propertyChangeListener = null; private AcceptAllFileFilter acceptAllFileFilter = new AcceptAllFileFilter(); private FileFilter actualFileFilter = null; private GlobFilter globFilter = null; private BasicDirectoryModel model = null; private BasicFileView fileView = new BasicFileView(); private boolean usesSingleFilePane; private boolean readOnly; // The accessoryPanel is a container to place the JFileChooser accessory component private JPanel accessoryPanel = null; private Handler handler; public BasicFileChooserUI(JFileChooser b) { } public void installUI(JComponent c) { accessoryPanel = new JPanel(new BorderLayout()); filechooser = (JFileChooser) c; createModel(); clearIconCache(); installDefaults(filechooser); installComponents(filechooser); installListeners(filechooser); filechooser.applyComponentOrientation(filechooser.getComponentOrientation()); } public void uninstallUI(JComponent c) { uninstallListeners((JFileChooser) filechooser); uninstallComponents((JFileChooser) filechooser); uninstallDefaults((JFileChooser) filechooser); if(accessoryPanel != null) { accessoryPanel.removeAll(); } accessoryPanel = null; getFileChooser().removeAll(); handler = null; } public void installComponents(JFileChooser fc) { } public void uninstallComponents(JFileChooser fc) { } protected void installListeners(JFileChooser fc) { propertyChangeListener = createPropertyChangeListener(fc); if(propertyChangeListener != null) { fc.addPropertyChangeListener(propertyChangeListener); } fc.addPropertyChangeListener(getModel()); InputMap inputMap = getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(fc, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); ActionMap actionMap = getActionMap(); SwingUtilities.replaceUIActionMap(fc, actionMap); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { return (InputMap)DefaultLookup.get(getFileChooser(), this, "FileChooser.ancestorInputMap"); } return null; } ActionMap getActionMap() { return createActionMap(); } ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); Action refreshAction = new UIAction(FilePane.ACTION_REFRESH) { public void actionPerformed(ActionEvent evt) { getFileChooser().rescanCurrentDirectory(); } }; map.put(FilePane.ACTION_APPROVE_SELECTION, getApproveSelectionAction()); map.put(FilePane.ACTION_CANCEL, getCancelSelectionAction()); map.put(FilePane.ACTION_REFRESH, refreshAction); map.put(FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY, getChangeToParentDirectoryAction()); return map; } protected void uninstallListeners(JFileChooser fc) { if(propertyChangeListener != null) { fc.removePropertyChangeListener(propertyChangeListener); } fc.removePropertyChangeListener(getModel()); SwingUtilities.replaceUIInputMap(fc, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); SwingUtilities.replaceUIActionMap(fc, null); } protected void installDefaults(JFileChooser fc) { installIcons(fc); installStrings(fc); usesSingleFilePane = UIManager.getBoolean("FileChooser.usesSingleFilePane"); readOnly = UIManager.getBoolean("FileChooser.readOnly"); TransferHandler th = fc.getTransferHandler(); if (th == null || th instanceof UIResource) { fc.setTransferHandler(defaultTransferHandler); } LookAndFeel.installProperty(fc, "opaque", Boolean.FALSE); } protected void installIcons(JFileChooser fc) { directoryIcon = UIManager.getIcon("FileView.directoryIcon"); fileIcon = UIManager.getIcon("FileView.fileIcon"); computerIcon = UIManager.getIcon("FileView.computerIcon"); hardDriveIcon = UIManager.getIcon("FileView.hardDriveIcon"); floppyDriveIcon = UIManager.getIcon("FileView.floppyDriveIcon"); newFolderIcon = UIManager.getIcon("FileChooser.newFolderIcon"); upFolderIcon = UIManager.getIcon("FileChooser.upFolderIcon"); homeFolderIcon = UIManager.getIcon("FileChooser.homeFolderIcon"); detailsViewIcon = UIManager.getIcon("FileChooser.detailsViewIcon"); listViewIcon = UIManager.getIcon("FileChooser.listViewIcon"); } protected void installStrings(JFileChooser fc) { Locale l = fc.getLocale(); newFolderErrorText = UIManager.getString("FileChooser.newFolderErrorText",l); newFolderErrorSeparator = UIManager.getString("FileChooser.newFolderErrorSeparator",l); fileDescriptionText = UIManager.getString("FileChooser.fileDescriptionText",l); directoryDescriptionText = UIManager.getString("FileChooser.directoryDescriptionText",l); saveButtonText = UIManager.getString("FileChooser.saveButtonText",l); openButtonText = UIManager.getString("FileChooser.openButtonText",l); saveDialogTitleText = UIManager.getString("FileChooser.saveDialogTitleText",l); openDialogTitleText = UIManager.getString("FileChooser.openDialogTitleText",l); cancelButtonText = UIManager.getString("FileChooser.cancelButtonText",l); updateButtonText = UIManager.getString("FileChooser.updateButtonText",l); helpButtonText = UIManager.getString("FileChooser.helpButtonText",l); directoryOpenButtonText = UIManager.getString("FileChooser.directoryOpenButtonText",l); saveButtonMnemonic = getMnemonic("FileChooser.saveButtonMnemonic", l); openButtonMnemonic = getMnemonic("FileChooser.openButtonMnemonic", l); cancelButtonMnemonic = getMnemonic("FileChooser.cancelButtonMnemonic", l); updateButtonMnemonic = getMnemonic("FileChooser.updateButtonMnemonic", l); helpButtonMnemonic = getMnemonic("FileChooser.helpButtonMnemonic", l); directoryOpenButtonMnemonic = getMnemonic("FileChooser.directoryOpenButtonMnemonic", l); saveButtonToolTipText = UIManager.getString("FileChooser.saveButtonToolTipText",l); openButtonToolTipText = UIManager.getString("FileChooser.openButtonToolTipText",l); cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText",l); updateButtonToolTipText = UIManager.getString("FileChooser.updateButtonToolTipText",l); helpButtonToolTipText = UIManager.getString("FileChooser.helpButtonToolTipText",l); directoryOpenButtonToolTipText = UIManager.getString("FileChooser.directoryOpenButtonToolTipText",l); } protected void uninstallDefaults(JFileChooser fc) { uninstallIcons(fc); uninstallStrings(fc); if (fc.getTransferHandler() instanceof UIResource) { fc.setTransferHandler(null); } } protected void uninstallIcons(JFileChooser fc) { directoryIcon = null; fileIcon = null; computerIcon = null; hardDriveIcon = null; floppyDriveIcon = null; newFolderIcon = null; upFolderIcon = null; homeFolderIcon = null; detailsViewIcon = null; listViewIcon = null; } protected void uninstallStrings(JFileChooser fc) { saveButtonText = null; openButtonText = null; cancelButtonText = null; updateButtonText = null; helpButtonText = null; directoryOpenButtonText = null; saveButtonToolTipText = null; openButtonToolTipText = null; cancelButtonToolTipText = null; updateButtonToolTipText = null; helpButtonToolTipText = null; directoryOpenButtonToolTipText = null; } protected void createModel() { if (model != null) { model.invalidateFileCache(); } model = new BasicDirectoryModel(getFileChooser()); } public BasicDirectoryModel getModel() { return model; } public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) { return null; } public String getFileName() { return null; } public String getDirectoryName() { return null; } public void setFileName(String filename) { } public void setDirectoryName(String dirname) { } public void rescanCurrentDirectory(JFileChooser fc) { } public void ensureFileIsVisible(JFileChooser fc, File f) { } public JFileChooser getFileChooser() { return filechooser; } public JPanel getAccessoryPanel() { return accessoryPanel; } protected JButton getApproveButton(JFileChooser fc) { return null; } public String getApproveButtonToolTipText(JFileChooser fc) { String tooltipText = fc.getApproveButtonToolTipText(); if(tooltipText != null) { return tooltipText; } if(fc.getDialogType() == JFileChooser.OPEN_DIALOG) { return openButtonToolTipText; } else if(fc.getDialogType() == JFileChooser.SAVE_DIALOG) { return saveButtonToolTipText; } return null; } public void clearIconCache() { fileView.clearIconCache(); } // ******************************************** // ************ Create Listeners ************** // ******************************************** private Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } protected MouseListener createDoubleClickListener(JFileChooser fc, JList list) { return new Handler(list); } public ListSelectionListener createListSelectionListener(JFileChooser fc) { return getHandler(); } private class Handler implements MouseListener, ListSelectionListener { JList list; Handler() { } Handler(JList list) { this.list = list; } public void mouseClicked(MouseEvent evt) { // Note: we can't depend on evt.getSource() because of backward // compatability if (list != null && SwingUtilities.isLeftMouseButton(evt) && (evt.getClickCount()%2 == 0)) { int index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint()); if (index >= 0) { File f = (File)list.getModel().getElementAt(index); try { // Strip trailing ".." f = ShellFolder.getNormalizedFile(f); } catch (IOException ex) { // That's ok, we'll use f as is } if(getFileChooser().isTraversable(f)) { list.clearSelection(); changeDirectory(f); } else { getFileChooser().approveSelection(); } } } } public void mouseEntered(MouseEvent evt) { if (list != null) { TransferHandler th1 = getFileChooser().getTransferHandler();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -