1113 lines · 39.6 KB · Java
RawDownload
/* * Copyright (c) 2003, 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 sun.swing.plaf.synth; import java.awt.*;import java.awt.event.*;import java.beans.*;import java.io.*;import java.util.*;import java.security.AccessController;import java.security.PrivilegedAction; import javax.swing.*;import javax.swing.event.*;import javax.swing.filechooser.*;import javax.swing.filechooser.FileFilter;import javax.swing.plaf.basic.*;import javax.swing.plaf.synth.*;import javax.swing.plaf.ActionMapUIResource; import sun.awt.shell.ShellFolder;import sun.swing.*; /** * Synth FileChooserUI implementation. * <p> * Note that the classes in the com.sun.java.swing.plaf.synth * package are not * part of the core Java APIs. They are a part of Sun's JDK and JRE * distributions. Although other licensees may choose to distribute * these classes, developers cannot depend on their availability in * non-Sun implementations. Additionally this API may change in * incompatible ways between releases. While this class is public, it * shoud be considered an implementation detail, and subject to change. * * @author Leif Samuelsson * @author Jeff Dinkins */public class SynthFileChooserUIImpl extends SynthFileChooserUI {    private JLabel lookInLabel;    private JComboBox directoryComboBox;    private DirectoryComboBoxModel directoryComboBoxModel;    private Action directoryComboBoxAction = new DirectoryComboBoxAction();     private FilterComboBoxModel filterComboBoxModel;     private JTextField fileNameTextField;     private FilePane filePane;    private JToggleButton listViewButton;    private JToggleButton detailsViewButton;     private boolean readOnly;     private JPanel buttonPanel;    private JPanel bottomPanel;     private JComboBox filterComboBox;     private static final Dimension hstrut5 = new Dimension(5, 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 Dimension LIST_PREF_SIZE = new Dimension(405, 135);     // 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    folderNameLabelMnemonic = 0;    private String folderNameLabelText = 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 AlignedLabel fileNameLabel;    private final PropertyChangeListener modeListener = new PropertyChangeListener() {        public void propertyChange(PropertyChangeEvent event) {            if (fileNameLabel != null) {                populateFileNameLabel();            }        }    };     private void populateFileNameLabel() {        if (getFileChooser().getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) {            fileNameLabel.setText(folderNameLabelText);            fileNameLabel.setDisplayedMnemonic(folderNameLabelMnemonic);        } else {            fileNameLabel.setText(fileNameLabelText);            fileNameLabel.setDisplayedMnemonic(fileNameLabelMnemonic);        }    }     public SynthFileChooserUIImpl(JFileChooser b) {        super(b);    }      private class SynthFileChooserUIAccessor implements FilePane.FileChooserUIAccessor {        public JFileChooser getFileChooser() {            return SynthFileChooserUIImpl.this.getFileChooser();        }         public BasicDirectoryModel getModel() {            return SynthFileChooserUIImpl.this.getModel();        }         public JPanel createList() {            return null;        }         public JPanel createDetailsView() {            return null;        }         public boolean isDirectorySelected() {            return SynthFileChooserUIImpl.this.isDirectorySelected();        }         public File getDirectory() {            return SynthFileChooserUIImpl.this.getDirectory();        }         public Action getChangeToParentDirectoryAction() {            return SynthFileChooserUIImpl.this.getChangeToParentDirectoryAction();        }         public Action getApproveSelectionAction() {            return SynthFileChooserUIImpl.this.getApproveSelectionAction();        }         public Action getNewFolderAction() {            return SynthFileChooserUIImpl.this.getNewFolderAction();        }         public MouseListener createDoubleClickListener(JList list) {            return SynthFileChooserUIImpl.this.createDoubleClickListener(getFileChooser(),                                                                     list);        }         public ListSelectionListener createListSelectionListener() {            return SynthFileChooserUIImpl.this.createListSelectionListener(getFileChooser());        }    }     protected void installDefaults(JFileChooser fc) {        super.installDefaults(fc);        readOnly = UIManager.getBoolean("FileChooser.readOnly");    }     public void installComponents(JFileChooser fc) {        super.installComponents(fc);         SynthContext context = getContext(fc, ENABLED);         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.getAccessibleContext().setAccessibleDescription(lookInLabelText);        directoryComboBox.putClientProperty( "JComboBox.isTableCellEditor", Boolean.TRUE );        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);         filePane = new FilePane(new SynthFileChooserUIAccessor());        fc.addPropertyChangeListener(filePane);         // Add 'Go Up' to context menu, plus 'Go Home' if on Unix        JPopupMenu contextMenu = filePane.getComponentPopupMenu();        if (contextMenu != null) {            contextMenu.insert(getChangeToParentDirectoryAction(), 0);            if (File.separatorChar == '/') {                contextMenu.insert(getGoHomeAction(), 1);            }        }     FileSystemView fsv = fc.getFileSystemView();     // Up Button    JButton upFolderButton = new JButton(getChangeToParentDirectoryAction());    upFolderButton.setText(null);    upFolderButton.setIcon(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.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    if (!readOnly) {        b = new JButton(filePane.getNewFolderAction());        b.setText(null);        b.setIcon(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();     // 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(filePane.getViewTypeAction(FilePane.VIEWTYPE_LIST));    topButtonPanel.add(listViewButton);    viewButtonGroup.add(listViewButton);     // 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(filePane.getViewTypeAction(FilePane.VIEWTYPE_DETAILS));    topButtonPanel.add(detailsViewButton);    viewButtonGroup.add(detailsViewButton);     filePane.addPropertyChangeListener(new PropertyChangeListener() {        public void propertyChange(PropertyChangeEvent e) {            if ("viewType".equals(e.getPropertyName())) {                int viewType = filePane.getViewType();                switch (viewType) {                    case FilePane.VIEWTYPE_LIST:                        listViewButton.setSelected(true);                        break;                    case FilePane.VIEWTYPE_DETAILS:                        detailsViewButton.setSelected(true);                        break;                }            }        }    });         // ************************************** //        // ******* Add the directory pane ******* //        // ************************************** //        fc.add(getAccessoryPanel(), BorderLayout.AFTER_LINE_ENDS);        JComponent accessory = fc.getAccessory();        if (accessory != null) {            getAccessoryPanel().add(accessory);        }        filePane.setPreferredSize(LIST_PREF_SIZE);        fc.add(filePane, BorderLayout.CENTER);          // ********************************** //        // **** Construct the bottom panel ** //        // ********************************** //        bottomPanel = new JPanel();        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(new Dimension(1, 5)));         fileNameLabel = new AlignedLabel();        populateFileNameLabel();        fileNamePanel.add(fileNameLabel);         fileNameTextField = new JTextField(35) {            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()) {                        filePane.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);        filterComboBox.getAccessibleContext().setAccessibleDescription(filesOfTypeLabelText);        filesOfTypeLabel.setLabelFor(filterComboBox);        filterComboBox.setRenderer(createFilterComboBoxRenderer());        filesOfTypePanel.add(filterComboBox);          // buttons        buttonPanel = new JPanel();        buttonPanel.setLayout(new ButtonAreaLayout());         buttonPanel.add(getApproveButton(fc));        buttonPanel.add(getCancelButton(fc));         if (fc.getControlButtonsAreShown()) {            addControlButtons();        }         groupLabels(new AlignedLabel[] { fileNameLabel, filesOfTypeLabel });    }     protected void installListeners(JFileChooser fc) {        super.installListeners(fc);        fc.addPropertyChangeListener(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY, modeListener);    }     protected void uninstallListeners(JFileChooser fc) {        fc.removePropertyChangeListener(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY, modeListener);        super.uninstallListeners(fc);    }     private String fileNameString(File file) {        if (file == null) {            return null;        } else {            JFileChooser fc = getFileChooser();            if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {                return file.getPath();            } else {                return file.getName();            }        }    }     private String fileNameString(File[] files) {        StringBuffer buf = new StringBuffer();        for (int i = 0; files != null && i < files.length; i++) {            if (i > 0) {                buf.append(" ");            }            if (files.length > 1) {                buf.append("\"");            }            buf.append(fileNameString(files[i]));            if (files.length > 1) {                buf.append("\"");            }        }        return buf.toString();    }     public void uninstallUI(JComponent c) {        // Remove listeners        c.removePropertyChangeListener(filterComboBoxModel);        c.removePropertyChangeListener(filePane);         if (filePane != null) {            filePane.uninstallUI();            filePane = null;        }         super.uninstallUI(c);    }     protected void installStrings(JFileChooser fc) {        super.installStrings(fc);         Locale l = fc.getLocale();         lookInLabelMnemonic = getMnemonic("FileChooser.lookInLabelMnemonic", l);        lookInLabelText = UIManager.getString("FileChooser.lookInLabelText", l);        saveInLabelText = UIManager.getString("FileChooser.saveInLabelText", l);         fileNameLabelMnemonic = getMnemonic("FileChooser.fileNameLabelMnemonic", l);        fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText", l);        folderNameLabelMnemonic = getMnemonic("FileChooser.folderNameLabelMnemonic", l);        folderNameLabelText = UIManager.getString("FileChooser.folderNameLabelText", l);         filesOfTypeLabelMnemonic = getMnemonic("FileChooser.filesOfTypeLabelMnemonic", l);        filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText", l);     upFolderToolTipText =  UIManager.getString("FileChooser.upFolderToolTipText",l);    upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName",l);     homeFolderToolTipText =  UIManager.getString("FileChooser.homeFolderToolTipText",l);    homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName",l);     newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText",l);    newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName",l);     listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText",l);    listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName",l);     detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText",l);

Lines 1–500 of 1113.