1372 lines · 47.8 KB · Java
RawDownload
/* * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ 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.List;import java.util.regex.*;import sun.awt.shell.ShellFolder;import sun.swing.*;import sun.swing.SwingUtilities2; /** * Basic L&F implementation of a FileChooser. * * @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 Icon viewMenuIcon = 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 newFolderParentDoesntExistTitleText = null;    private String newFolderParentDoesntExistText = 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;     /**     * Creates a {@code BasicFileChooserUI} implementation     * for the specified component. By default     * the {@code BasicLookAndFeel} class uses     * {@code createUI} methods of all basic UIs classes     * to instantiate UIs.     *     * @param c the {@code JFileChooser} which needs a UI     * @return the {@code BasicFileChooserUI} object     *     * @see UIDefaults#getUI(JComponent)     * @since 1.7     */    public static ComponentUI createUI(JComponent c) {        return new BasicFileChooserUI((JFileChooser) c);    }     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(filechooser);        uninstallComponents(filechooser);        uninstallDefaults(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");        viewMenuIcon     = UIManager.getIcon("FileChooser.viewMenuIcon");    }     protected void installStrings(JFileChooser fc) {         Locale l = fc.getLocale();        newFolderErrorText = UIManager.getString("FileChooser.newFolderErrorText",l);        newFolderErrorSeparator = UIManager.getString("FileChooser.newFolderErrorSeparator",l);         newFolderParentDoesntExistTitleText = UIManager.getString("FileChooser.newFolderParentDoesntExistTitleText", l);        newFolderParentDoesntExistText = UIManager.getString("FileChooser.newFolderParentDoesntExistText", 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;        viewMenuIcon     = 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 JButton getDefaultButton(JFileChooser fc) {        return getApproveButton(fc);    }     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();                TransferHandler th2 = list.getTransferHandler();                if (th1 != th2) {                    list.setTransferHandler(th1);                }                if (getFileChooser().getDragEnabled() != list.getDragEnabled()) {                    list.setDragEnabled(getFileChooser().getDragEnabled());                }            }        }         public void mouseExited(MouseEvent evt) {        }         public void mousePressed(MouseEvent evt) {        }

Lines 1–500 of 1372.