windowsfilechooserui.java

来自「JAVA 所有包」· Java 代码 · 共 1,257 行 · 第 1/3 页

JAVA
1,257
字号
/* * @(#)WindowsFileChooserUI.java	1.101 06/04/27 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.windows;import javax.swing.*;import javax.swing.border.*;import javax.swing.filechooser.*;import javax.swing.event.*;import javax.swing.plaf.*;import javax.swing.plaf.basic.*;import java.awt.*;import java.awt.event.*;import java.beans.*;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.util.*;import sun.awt.shell.ShellFolder;import sun.swing.*;import javax.accessibility.*;/** * Windows L&F implementation of a FileChooser. * * @version 1.101 04/27/06 * @author Jeff Dinkins */public class WindowsFileChooserUI extends BasicFileChooserUI {    // The following are private because the implementation of the    // Windows FileChooser L&F is not complete yet.    private static final String OS_VERSION = System.getProperty("os.version");    private JPanel centerPanel;    private JLabel lookInLabel;    private JComboBox directoryComboBox;    private DirectoryComboBoxModel directoryComboBoxModel;    private ActionListener directoryComboBoxAction = new DirectoryComboBoxAction();    private FilterComboBoxModel filterComboBoxModel;    private JTextField filenameTextField;    private JToggleButton listViewButton;    private JToggleButton detailsViewButton;    private FilePane filePane;    private WindowsPlacesBar placesBar;    private boolean useShellFolder;    private JButton approveButton;    private JButton cancelButton;    private JPanel buttonPanel;    private JPanel bottomPanel;    private JComboBox filterComboBox;    private static final Dimension hstrut10 = new Dimension(10, 1);    private static final Dimension vstrut4  = new Dimension(1, 4);    private static final Dimension vstrut6  = new Dimension(1, 6);    private static final Dimension vstrut8  = new Dimension(1, 8);    private static final Insets shrinkwrap = new Insets(0,0,0,0);    // Preferred and Minimum sizes for the dialog box    private static int PREF_WIDTH = 425;    private static int PREF_HEIGHT = 245;    private static Dimension PREF_SIZE = new Dimension(PREF_WIDTH, PREF_HEIGHT);    private static int MIN_WIDTH = 425;    private static int MIN_HEIGHT = 245;    private static Dimension MIN_SIZE = new Dimension(MIN_WIDTH, MIN_HEIGHT);    private static int LIST_PREF_WIDTH = 444;    private static int LIST_PREF_HEIGHT = 138;    private static Dimension LIST_PREF_SIZE = new Dimension(LIST_PREF_WIDTH, LIST_PREF_HEIGHT);    // 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 BasicFileView fileView = new WindowsFileView();    //    // ComponentUI Interface Implementation methods    //    public static ComponentUI createUI(JComponent c) {        return new WindowsFileChooserUI((JFileChooser) c);    }    public WindowsFileChooserUI(JFileChooser filechooser) {	super(filechooser);    }    public void installUI(JComponent c) {	super.installUI(c);    }    public void uninstallComponents(JFileChooser fc) {	fc.removeAll();    }    private class WindowsFileChooserUIAccessor implements FilePane.FileChooserUIAccessor {	public JFileChooser getFileChooser() {	    return WindowsFileChooserUI.this.getFileChooser();	}	public BasicDirectoryModel getModel() {	    return WindowsFileChooserUI.this.getModel();	}	public JPanel createList() {	    return WindowsFileChooserUI.this.createList(getFileChooser());	}	public JPanel createDetailsView() {	    return WindowsFileChooserUI.this.createDetailsView(getFileChooser());	}	public boolean isDirectorySelected() {	    return WindowsFileChooserUI.this.isDirectorySelected();	}	public File getDirectory() {	    return WindowsFileChooserUI.this.getDirectory();	}	public Action getChangeToParentDirectoryAction() {	    return WindowsFileChooserUI.this.getChangeToParentDirectoryAction();	}	public Action getApproveSelectionAction() {	    return WindowsFileChooserUI.this.getApproveSelectionAction();	}	public Action getNewFolderAction() {	    return WindowsFileChooserUI.this.getNewFolderAction();	}	public MouseListener createDoubleClickListener(JList list) {	    return WindowsFileChooserUI.this.createDoubleClickListener(getFileChooser(),								       list);	}	public ListSelectionListener createListSelectionListener() {	    return WindowsFileChooserUI.this.createListSelectionListener(getFileChooser());	}    }    public void installComponents(JFileChooser fc) {	filePane = new FilePane(new WindowsFileChooserUIAccessor());	fc.addPropertyChangeListener(filePane);	FileSystemView fsv = fc.getFileSystemView();	fc.setBorder(new EmptyBorder(4, 10, 10, 10));	fc.setLayout(new BorderLayout(8, 8));	updateUseShellFolder();	// ********************************* //	// **** Construct the top panel **** //	// ********************************* //	// Directory manipulation buttons	JToolBar topPanel = new JToolBar();	topPanel.setFloatable(false);	if (OS_VERSION.compareTo("4.9") >= 0) {	// Windows Me/2000 and later (4.90/5.0)	    topPanel.putClientProperty("JToolBar.isRollover", Boolean.TRUE);	}	// Add the top panel to the fileChooser	fc.add(topPanel, BorderLayout.NORTH);	// ComboBox Label     	lookInLabel = new JLabel(lookInLabelText, JLabel.TRAILING) {	    public Dimension getPreferredSize() {		return getMinimumSize();	    }	    public Dimension getMinimumSize() {		Dimension d = super.getPreferredSize();		if (placesBar != null) {		    d.width = Math.max(d.width, placesBar.getWidth());		}		return d;	    }	};     	lookInLabel.setDisplayedMnemonic(lookInLabelMnemonic);	lookInLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT);	lookInLabel.setAlignmentY(JComponent.CENTER_ALIGNMENT);	topPanel.add(lookInLabel);	topPanel.add(Box.createRigidArea(new Dimension(8,0)));	// CurrentDir ComboBox	directoryComboBox = new JComboBox() {	    public Dimension getMinimumSize() {		Dimension d = super.getMinimumSize();		d.width = 60;		return d;	    }	    public Dimension getPreferredSize() {		Dimension d = super.getPreferredSize();		// Must be small enough to not affect total width.		d.width = 150;		return d;	    }	};	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.CENTER_ALIGNMENT);	directoryComboBox.setMaximumRowCount(8);	topPanel.add(directoryComboBox);	topPanel.add(Box.createRigidArea(hstrut10));	// Up Button	JButton upFolderButton = new JButton(getChangeToParentDirectoryAction());	upFolderButton.setText(null);	upFolderButton.setIcon(upFolderIcon);     	upFolderButton.setToolTipText(upFolderToolTipText);        upFolderButton.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY,                                         upFolderAccessibleName);        upFolderButton.putClientProperty(WindowsLookAndFeel.HI_RES_DISABLED_ICON_CLIENT_KEY,                                         Boolean.TRUE);	upFolderButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);	upFolderButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);	upFolderButton.setMargin(shrinkwrap);	upFolderButton.setFocusPainted(false);	topPanel.add(upFolderButton);	if (OS_VERSION.compareTo("4.9") < 0) {	// Before Windows Me/2000 (4.90/5.0)	    topPanel.add(Box.createRigidArea(hstrut10));	}	JButton b;	if (OS_VERSION.startsWith("4.1")) {		// Windows 98 (4.10)	    // Desktop Button	    File homeDir = fsv.getHomeDirectory();	    String toolTipText = homeFolderToolTipText;	    if (fsv.isRoot(homeDir)) {		toolTipText = getFileView(fc).getName(homeDir); // Probably "Desktop".	    }	    b = new JButton(getFileView(fc).getIcon(homeDir));	    b.setToolTipText(toolTipText);            b.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY,                                 toolTipText);	    b.setAlignmentX(JComponent.LEFT_ALIGNMENT);	    b.setAlignmentY(JComponent.CENTER_ALIGNMENT);	    b.setMargin(shrinkwrap);	    b.setFocusPainted(false);	    b.addActionListener(getGoHomeAction());	    topPanel.add(b);	    topPanel.add(Box.createRigidArea(hstrut10));	}	// New Directory Button	if (!UIManager.getBoolean("FileChooser.readOnly")) {	    b = new JButton(filePane.getNewFolderAction());	    b.setText(null);	    b.setIcon(newFolderIcon);	    b.setToolTipText(newFolderToolTipText);            b.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY,                                newFolderAccessibleName);            b.putClientProperty(WindowsLookAndFeel.HI_RES_DISABLED_ICON_CLIENT_KEY,                                Boolean.TRUE);	    b.setAlignmentX(JComponent.LEFT_ALIGNMENT);	    b.setAlignmentY(JComponent.CENTER_ALIGNMENT);	    b.setMargin(shrinkwrap);	    b.setFocusPainted(false);	    topPanel.add(b);	}	if (OS_VERSION.compareTo("4.9") < 0) {	// Before Windows Me/2000 (4.90/5.0)	    topPanel.add(Box.createRigidArea(hstrut10));	}	// View button group	ButtonGroup viewButtonGroup = new ButtonGroup();	// List Button	listViewButton = new JToggleButton(listViewIcon);     	listViewButton.setToolTipText(listViewButtonToolTipText);        listViewButton.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY,                                         listViewButtonAccessibleName);        listViewButton.putClientProperty(WindowsLookAndFeel.HI_RES_DISABLED_ICON_CLIENT_KEY,                                         Boolean.TRUE);	listViewButton.setFocusPainted(false);	listViewButton.setSelected(true);	listViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);	listViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);	listViewButton.setMargin(shrinkwrap);	listViewButton.addActionListener(filePane.getViewTypeAction(FilePane.VIEWTYPE_LIST));	topPanel.add(listViewButton);	viewButtonGroup.add(listViewButton);	// Details Button	detailsViewButton = new JToggleButton(detailsViewIcon);     	detailsViewButton.setToolTipText(detailsViewButtonToolTipText);        detailsViewButton.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY,                                            detailsViewButtonAccessibleName);        detailsViewButton.putClientProperty(WindowsLookAndFeel.HI_RES_DISABLED_ICON_CLIENT_KEY,                                            Boolean.TRUE);	detailsViewButton.setFocusPainted(false);	detailsViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);	detailsViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);	detailsViewButton.setMargin(shrinkwrap);	detailsViewButton.addActionListener(filePane.getViewTypeAction(FilePane.VIEWTYPE_DETAILS));	topPanel.add(detailsViewButton);	viewButtonGroup.add(detailsViewButton);	topPanel.add(Box.createRigidArea(new Dimension(60, 0)));	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 ******* //	// ************************************** //	centerPanel = new JPanel(new BorderLayout());	centerPanel.add(getAccessoryPanel(), BorderLayout.AFTER_LINE_ENDS);	JComponent accessory = fc.getAccessory();	if(accessory != null) {	    getAccessoryPanel().add(accessory);	}	filePane.setPreferredSize(LIST_PREF_SIZE);	centerPanel.add(filePane, BorderLayout.CENTER);	fc.add(centerPanel, BorderLayout.CENTER);	// ********************************** //	// **** Construct the bottom panel ** //	// ********************************** //	getBottomPanel().setLayout(new BoxLayout(getBottomPanel(), BoxLayout.LINE_AXIS));	// Add the bottom panel to file chooser	centerPanel.add(getBottomPanel(), BorderLayout.SOUTH);	// labels	JPanel labelPanel = new JPanel();	labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS));        labelPanel.add(Box.createRigidArea(vstrut4));     	JLabel fnl = new JLabel(fileNameLabelText);     	fnl.setDisplayedMnemonic(fileNameLabelMnemonic);	fnl.setAlignmentY(0);	labelPanel.add(fnl);	labelPanel.add(Box.createRigidArea(new Dimension(1,12)));     	JLabel ftl = new JLabel(filesOfTypeLabelText);     	ftl.setDisplayedMnemonic(filesOfTypeLabelMnemonic);	labelPanel.add(ftl);	getBottomPanel().add(labelPanel);	getBottomPanel().add(Box.createRigidArea(new Dimension(15, 0)));	// file entry and filters	JPanel fileAndFilterPanel = new JPanel();        fileAndFilterPanel.add(Box.createRigidArea(vstrut8));	fileAndFilterPanel.setLayout(new BoxLayout(fileAndFilterPanel, BoxLayout.Y_AXIS));	filenameTextField = new JTextField(35) {	    public Dimension getMaximumSize() {		return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height);	    }	};	fnl.setLabelFor(filenameTextField);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?