📄 motiffilechooserui.java
字号:
/* * @(#)MotifFileChooserUI.java 1.52 07/06/20 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.motif;import javax.swing.*;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.IOException;import java.util.*;import sun.awt.shell.ShellFolder;import sun.swing.SwingUtilities2;/** * Motif FileChooserUI. * * @version 1.52 06/20/07 * @author Jeff Dinkins */public class MotifFileChooserUI extends BasicFileChooserUI { private FilterComboBoxModel filterComboBoxModel; protected JList directoryList = null; protected JList fileList = null; protected JTextField pathField = null; protected JComboBox filterComboBox = null; protected JTextField filenameTextField = null; private static final Dimension hstrut10 = new Dimension(10, 1); private static final Dimension vstrut10 = new Dimension(1, 10); private static final Insets insets = new Insets(10, 10, 10, 10); private static Dimension prefListSize = new Dimension(75, 150); private static Dimension WITH_ACCELERATOR_PREF_SIZE = new Dimension(650, 450); private static Dimension PREF_SIZE = new Dimension(350, 450); private static Dimension MIN_SIZE = new Dimension(200, 300); private static Dimension PREF_ACC_SIZE = new Dimension(10, 10); private static Dimension ZERO_ACC_SIZE = new Dimension(1, 1); private static Dimension MAX_SIZE = new Dimension(Short.MAX_VALUE, Short.MAX_VALUE); private static final Insets buttonMargin = new Insets(3, 3, 3, 3); private JPanel directoryPanel = new JPanel(); private JPanel bottomPanel; protected JButton approveButton; private String enterFileNameLabelText = null; private int enterFileNameLabelMnemonic = 0; private String filesLabelText = null; private int filesLabelMnemonic = 0; private String foldersLabelText = null; private int foldersLabelMnemonic = 0; private String pathLabelText = null; private int pathLabelMnemonic = 0; private String filterLabelText = null; private int filterLabelMnemonic = 0; 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 MotifFileChooserUI(JFileChooser filechooser) { super(filechooser); } public String getFileName() { if(filenameTextField != null) { return filenameTextField.getText(); } else { return null; } } public void setFileName(String filename) { if(filenameTextField != null) { filenameTextField.setText(filename); } } public String getDirectoryName() { return pathField.getText(); } public void setDirectoryName(String dirname) { pathField.setText(dirname); } public void ensureFileIsVisible(JFileChooser fc, File f) { // PENDING(jeff) } public void rescanCurrentDirectory(JFileChooser fc) { getModel().validateFileCache(); } public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) { return new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { String prop = e.getPropertyName(); if(prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { File f = (File) e.getNewValue(); if(f != null) { setFileName(getFileChooser().getName(f)); } } else if (prop.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) { File[] files = (File[]) e.getNewValue(); JFileChooser fc = getFileChooser(); if (files != null && files.length > 0 && (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) { setFileName(fileNameString(files)); } } else if (prop.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) { fileList.clearSelection(); } else if(prop.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) { directoryList.clearSelection(); ListSelectionModel sm = directoryList.getSelectionModel(); if (sm instanceof DefaultListSelectionModel) { ((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0); ((DefaultListSelectionModel)sm).setAnchorSelectionIndex(0); } fileList.clearSelection(); sm = fileList.getSelectionModel(); if (sm instanceof DefaultListSelectionModel) { ((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0); ((DefaultListSelectionModel)sm).setAnchorSelectionIndex(0); } File currentDirectory = getFileChooser().getCurrentDirectory(); if(currentDirectory != null) { try { setDirectoryName(ShellFolder.getNormalizedFile((File)e.getNewValue()).getPath()); } catch (IOException ioe) { setDirectoryName(((File)e.getNewValue()).getAbsolutePath()); } if ((getFileChooser().getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) && !getFileChooser().isMultiSelectionEnabled()) { setFileName(getDirectoryName()); } } } else if(prop.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) { directoryList.clearSelection(); } else if(prop == JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY) { if(getFileChooser().isMultiSelectionEnabled()) { fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } else { fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); fileList.clearSelection(); getFileChooser().setSelectedFiles(null); } } else if(prop == JFileChooser.ACCESSORY_CHANGED_PROPERTY) { if(getAccessoryPanel() != null) { if(e.getOldValue() != null) { getAccessoryPanel().remove((JComponent) e.getOldValue()); } JComponent accessory = (JComponent) e.getNewValue(); if(accessory != null) { getAccessoryPanel().add(accessory, BorderLayout.CENTER); getAccessoryPanel().setPreferredSize(PREF_ACC_SIZE); getAccessoryPanel().setMaximumSize(MAX_SIZE); } else { getAccessoryPanel().setPreferredSize(ZERO_ACC_SIZE); getAccessoryPanel().setMaximumSize(ZERO_ACC_SIZE); } } } else if (prop == JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY || prop == JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY || prop == JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY) { approveButton.setText(getApproveButtonText(getFileChooser())); approveButton.setToolTipText(getApproveButtonToolTipText(getFileChooser())); } else if (prop.equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) { doControlButtonsChanged(e); } else if (prop.equals("componentOrientation")) { ComponentOrientation o = (ComponentOrientation)e.getNewValue(); JFileChooser cc = (JFileChooser)e.getSource(); if (o != (ComponentOrientation)e.getOldValue()) { cc.applyComponentOrientation(o); } } } }; } // // ComponentUI Interface Implementation methods // public static ComponentUI createUI(JComponent c) { return new MotifFileChooserUI((JFileChooser)c); } public void installUI(JComponent c) { super.installUI(c); } public void uninstallUI(JComponent c) { c.removePropertyChangeListener(filterComboBoxModel); approveButton.removeActionListener(getApproveSelectionAction()); filenameTextField.removeActionListener(getApproveSelectionAction()); super.uninstallUI(c); } public void installComponents(JFileChooser fc) { fc.setLayout(new BorderLayout(10, 10)); fc.setAlignmentX(JComponent.CENTER_ALIGNMENT); JPanel interior = new JPanel() { public Insets getInsets() { return insets; } }; interior.setInheritsPopupMenu(true); align(interior); interior.setLayout(new BoxLayout(interior, BoxLayout.PAGE_AXIS)); fc.add(interior, BorderLayout.CENTER); // PENDING(jeff) - I18N JLabel l = new JLabel(pathLabelText); l.setDisplayedMnemonic(pathLabelMnemonic); align(l); interior.add(l); File currentDirectory = fc.getCurrentDirectory(); String curDirName = null; if(currentDirectory != null) { curDirName = currentDirectory.getPath(); } pathField = new JTextField(curDirName) { public Dimension getMaximumSize() { Dimension d = super.getMaximumSize(); d.height = getPreferredSize().height; return d; } }; pathField.setInheritsPopupMenu(true); l.setLabelFor(pathField); align(pathField); // Change to folder on return pathField.addActionListener(getUpdateAction()); interior.add(pathField); interior.add(Box.createRigidArea(vstrut10)); // CENTER: left, right accessory JPanel centerPanel = new JPanel(); centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.LINE_AXIS)); align(centerPanel); // left panel - Filter & folderList JPanel leftPanel = new JPanel(); leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS)); align(leftPanel); // add the filter PENDING(jeff) - I18N l = new JLabel(filterLabelText); l.setDisplayedMnemonic(filterLabelMnemonic); align(l); leftPanel.add(l); filterComboBox = new JComboBox() { public Dimension getMaximumSize() { Dimension d = super.getMaximumSize(); d.height = getPreferredSize().height; return d; } }; filterComboBox.setInheritsPopupMenu(true); l.setLabelFor(filterComboBox); filterComboBoxModel = createFilterComboBoxModel(); filterComboBox.setModel(filterComboBoxModel); filterComboBox.setRenderer(createFilterComboBoxRenderer()); fc.addPropertyChangeListener(filterComboBoxModel); align(filterComboBox); leftPanel.add(filterComboBox); // leftPanel.add(Box.createRigidArea(vstrut10)); // Add the Folder List PENDING(jeff) - I18N l = new JLabel(foldersLabelText); l.setDisplayedMnemonic(foldersLabelMnemonic); align(l); leftPanel.add(l); JScrollPane sp = createDirectoryList(); sp.getVerticalScrollBar().setFocusable(false); sp.getHorizontalScrollBar().setFocusable(false); sp.setInheritsPopupMenu(true); l.setLabelFor(sp.getViewport().getView()); leftPanel.add(sp); leftPanel.setInheritsPopupMenu(true); // create files list JPanel rightPanel = new JPanel(); align(rightPanel); rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS)); rightPanel.setInheritsPopupMenu(true); l = new JLabel(filesLabelText); l.setDisplayedMnemonic(filesLabelMnemonic); align(l); rightPanel.add(l); sp = createFilesList(); l.setLabelFor(sp.getViewport().getView()); rightPanel.add(sp); sp.setInheritsPopupMenu(true); centerPanel.add(leftPanel); centerPanel.add(Box.createRigidArea(hstrut10)); centerPanel.add(rightPanel); centerPanel.setInheritsPopupMenu(true); JComponent accessoryPanel = getAccessoryPanel(); JComponent accessory = fc.getAccessory(); if(accessoryPanel != null) { if(accessory == null) { accessoryPanel.setPreferredSize(ZERO_ACC_SIZE); accessoryPanel.setMaximumSize(ZERO_ACC_SIZE); } else { getAccessoryPanel().add(accessory, BorderLayout.CENTER); accessoryPanel.setPreferredSize(PREF_ACC_SIZE); accessoryPanel.setMaximumSize(MAX_SIZE); } align(accessoryPanel); centerPanel.add(accessoryPanel); accessoryPanel.setInheritsPopupMenu(true); } interior.add(centerPanel); interior.add(Box.createRigidArea(vstrut10)); // add the filename field PENDING(jeff) - I18N l = new JLabel(enterFileNameLabelText); l.setDisplayedMnemonic(enterFileNameLabelMnemonic); align(l); interior.add(l); filenameTextField = new JTextField() { public Dimension getMaximumSize() { Dimension d = super.getMaximumSize(); d.height = getPreferredSize().height; return d; } }; filenameTextField.setInheritsPopupMenu(true); l.setLabelFor(filenameTextField); filenameTextField.addActionListener(getApproveSelectionAction()); align(filenameTextField); filenameTextField.setAlignmentX(JComponent.LEFT_ALIGNMENT); interior.add(filenameTextField); bottomPanel = getBottomPanel(); bottomPanel.add(new JSeparator(), BorderLayout.NORTH); // Add buttons JPanel buttonPanel = new JPanel(); align(buttonPanel); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); buttonPanel.add(Box.createGlue()); approveButton = new JButton(getApproveButtonText(fc)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -