windowsfilechooserui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,088 行 · 第 1/5 页
JAVA
2,088 行
/* * @(#)WindowsFileChooserUI.java 1.78 03/02/17 * * Copyright 2003 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 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;/** * Windows L&F implementation of a FileChooser. * * @version 1.78 02/17/03 * @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_NAMES = new String[] { "Windows 3.1", "Windows 95", "Windows NT", "Windows 98", "Windows 2000", "Windows Me", "Windows XP" }; private static int WIN_31 = 0; private static int WIN_95 = 1; private static int WIN_NT = 2; private static int WIN_98 = 3; private static int WIN_2k = 4; private static int WIN_Me = 5; private static int WIN_XP = 6; private static String osName = System.getProperty("os.name"); private static String osVersion = System.getProperty("os.version"); private static final String OS_NAME = ((osName.equals(OS_NAMES[WIN_98]) && osVersion.startsWith("4.9")) ? "Windows Me" : osName); private static final int OS_LEVEL = Arrays.asList(OS_NAMES).indexOf(OS_NAME); private JPanel centerPanel; private JLabel lookInLabel; private JComboBox directoryComboBox; private DirectoryComboBoxModel directoryComboBoxModel; private ActionListener directoryComboBoxAction = new DirectoryComboBoxAction(); private FilterComboBoxModel filterComboBoxModel; private JTextField filenameTextField; private ShortCutPanel shortCutPanel; 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 smallIconsView = false; private Border listViewBorder; 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 hstrut10 = new Dimension(10, 1); private static final Dimension hstrut25 = new Dimension(25, 1); private static final Dimension vstrut1 = new Dimension(1, 1); private static final Dimension vstrut4 = new Dimension(1, 4); private static final Dimension vstrut5 = new Dimension(1, 5); 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); 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; private Action newFolderAction = new WindowsNewFolderAction(); private File newFolderFile; 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(); } public void installComponents(JFileChooser fc) { FileSystemView fsv = fc.getFileSystemView(); XPStyle xp = XPStyle.getXP(); if (xp != null) { listViewBorder = xp.getBorder("listview"); } else { listViewBorder = new BevelBorder(BevelBorder.LOWERED, UIManager.getColor("ToolBar.highlight"), UIManager.getColor("ToolBar.background"), UIManager.getColor("ToolBar.darkShadow"), UIManager.getColor("ToolBar.shadow")); } fc.setBorder(new EmptyBorder(4, 10, 10, 10)); fc.setLayout(new BorderLayout(8, 8)); // ********************************* // // **** Construct the top panel **** // // ********************************* // // Directory manipulation buttons JToolBar topPanel = new JToolBar(); topPanel.setFloatable(false); if (OS_LEVEL >= WIN_2k) { topPanel.putClientProperty("JToolBar.isRollover", Boolean.TRUE); } // Add the top panel to the fileChooser fc.add(topPanel, BorderLayout.NORTH); // ComboBox Label lookInLabel = new JLabel(lookInLabelText); lookInLabel.setDisplayedMnemonic(lookInLabelMnemonic); lookInLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT); lookInLabel.setAlignmentY(JComponent.CENTER_ALIGNMENT); topPanel.add(Box.createRigidArea(new Dimension(14,0))); topPanel.add(lookInLabel); topPanel.add(Box.createRigidArea(new Dimension(29,0))); // 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.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.getAccessibleContext().setAccessibleName(upFolderAccessibleName); upFolderButton.setAlignmentX(JComponent.LEFT_ALIGNMENT); upFolderButton.setAlignmentY(JComponent.CENTER_ALIGNMENT); upFolderButton.setMargin(shrinkwrap); upFolderButton.setFocusPainted(false); topPanel.add(upFolderButton); if (OS_LEVEL < WIN_2k) { topPanel.add(Box.createRigidArea(hstrut10)); } JButton b; if (OS_LEVEL == WIN_98) { // 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.getAccessibleContext().setAccessibleName(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 b = new JButton(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); b.setFocusPainted(false); topPanel.add(b); if (OS_LEVEL < WIN_2k) { topPanel.add(Box.createRigidArea(hstrut10)); } // 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.setFocusPainted(false); listViewButton.setSelected(true); listViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT); listViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT); listViewButton.setMargin(shrinkwrap); listViewButton.addActionListener(viewButtonListener); topPanel.add(listViewButton); viewButtonGroup.add(listViewButton); // Details Button detailsViewButton = new JToggleButton(detailsViewIcon); detailsViewButton.setToolTipText(detailsViewButtonToolTipText); detailsViewButton.getAccessibleContext().setAccessibleName(detailsViewButtonAccessibleName); detailsViewButton.setFocusPainted(false); detailsViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT); detailsViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT); detailsViewButton.setMargin(shrinkwrap); detailsViewButton.addActionListener(viewButtonListener); topPanel.add(detailsViewButton); viewButtonGroup.add(detailsViewButton); // Use ShellFolder class to populate shortcut panel and combobox // only if FileSystemView.getRoots() returns the desktop folder, // i.e. the normal Windows hierarchy. { 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; } } } if (OS_LEVEL >= WIN_2k && useShellFolder) { shortCutPanel = new ShortCutPanel(); fc.add(shortCutPanel, BorderLayout.BEFORE_LINE_BEGINS); } // ************************************** // // ******* 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 ** // // ********************************** // 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); } };
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?