📄 oyoahafilechooserui.java
字号:
/* ==================================================================== * Copyright (c) 2001-2003 OYOAHA. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. The names "OYOAHA" must not be used to endorse or promote products * derived from this software without prior written permission. * For written permission, please contact email@oyoaha.com. * * 3. Products derived from this software may not be called "OYOAHA", * nor may "OYOAHA" appear in their name, without prior written * permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL OYOAHA OR ITS CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package com.oyoaha.swing.plaf.oyoaha.ui;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 java.text.*;import com.oyoaha.swing.plaf.oyoaha.*;import com.oyoaha.swing.plaf.oyoaha.icon.*;import com.oyoaha.swing.plaf.oyoaha.filechooser.*;public class OyoahaFileChooserUI extends BasicFileChooserUI implements ActionListener{ public OyoahaFileChooserUI(JFileChooser b) { super(b); } public static ComponentUI createUI(JComponent c) { return new OyoahaFileChooserUI((JFileChooser)c); } protected JPanel centerPanel; protected JComboBox directoryComboBox; protected OyoahaDirectoryComboBoxModel directoryComboBoxModel; protected Action directoryComboBoxAction = new DirectoryComboBoxAction(); protected FilterComboBoxModel filterComboBoxModel; protected JComboBox filterComboBox; protected JTextField filenameTextField; //use table instead? protected JList list; protected JButton approveButton; protected JButton cancelButton; protected JButton upFolderButton; protected JPanel bottomPanel; //- - - - - - - - - - - - - - - - - - - - - - - - - protected static final Dimension hstrut10 = new Dimension(6, 1); protected static final Dimension vstrut10 = new Dimension(1, 6); protected static final Insets insets = new Insets(0,3,0,3); protected static final Insets shrinkwrap = new Insets(0,0,0,0); //- - - - - - - - - - - - - - - - - - - - - - - - - protected int lookInLabelMnemonic = 0; protected String lookInLabelText = null; protected int fileNameLabelMnemonic = 0; protected String fileNameLabelText = null; protected int filesOfTypeLabelMnemonic = 0; protected String filesOfTypeLabelText = null; protected String upFolderToolTipText = null; protected String upFolderAccessibleName = null; protected String homeFolderToolTipText = null; protected String homeFolderAccessibleName = null; protected String newFolderToolTipText = null; protected String newFolderAccessibleName = null; protected String listViewButtonToolTipText = null; protected String listViewButtonAccessibleName = null; protected String detailsViewButtonToolTipText = null; protected String detailsViewButtonAccessibleName = null; //- - - - - - - - - - - - - - - - - - - - - - - - - protected PropertyChangeListener propertyChangeListener = null; protected AncestorListener ancestorListener = null; protected AcceptAllFileFilter acceptAllFileFilter = new AcceptAllFileFilter(); protected OyoahaDirectoryModel model = null; protected FileView fileView; protected Action newFolderAction; protected Icon sortIcon; protected String sortButtonText; protected int organizedMode; protected int sortedMode; protected JPopupMenu popup; //- - - - - - - - - - - - - - - - - - - - - - - - - public void uninstallComponents(JFileChooser fc) { fc.removeAll(); popup = null; } protected void installIcons(JFileChooser fc) { super.installIcons(fc); sortIcon = UIManager.getIcon("FileChooser.sortIcon"); } protected void installStrings(JFileChooser fc) { super.installStrings(fc); sortButtonText = UIManager.getString("FileChooser.sortButtonText"); //- - - - - - - - lookInLabelMnemonic = UIManager.getInt("FileChooser.lookInLabelMnemonic"); lookInLabelText = UIManager.getString("FileChooser.lookInLabelText"); fileNameLabelMnemonic = UIManager.getInt("FileChooser.fileNameLabelMnemonic"); fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText"); filesOfTypeLabelMnemonic = UIManager.getInt("FileChooser.filesOfTypeLabelMnemonic"); filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText"); upFolderToolTipText = UIManager.getString("FileChooser.upFolderToolTipText"); upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName"); newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText"); newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName"); } protected void uninstallIcons(JFileChooser fc) { super.uninstallIcons(fc); sortIcon = null; } protected void uninstallStrings(JFileChooser fc) { super.uninstallStrings(fc); sortButtonText = null; } protected void uninstallListeners(JFileChooser fc) { if(propertyChangeListener != null) { fc.removePropertyChangeListener(propertyChangeListener); } fc.removePropertyChangeListener(model);//SwingUtilities.replaceUIInputMap(fc, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);//SwingUtilities.replaceUIActionMap(fc, null); fc.removeAncestorListener(ancestorListener); ancestorListener = null; } protected boolean getControlButtonsAreShown() { //return fc.getControlButtonsAreShown(); return true; } public void installComponents(JFileChooser fc) { OyoahaFileView fview = getOyoahaFileView(fc); Object od = fview.getFileChooserPreference("organize"); if(od==null) { organizedMode = OyoahaDirectoryModel.USE_EXTENTION; } else { organizedMode = OyoahaDirectoryModel.getModeFromString(od.toString()); } od = fview.getFileChooserPreference("sort"); if(od==null) { sortedMode = OyoahaDirectoryModel.BY_NAME; } else { sortedMode = OyoahaDirectoryModel.geSortFromString(od.toString()); } //- - - - - - - - - - - - - - - - - - - - fc.setLayout(new BoxLayout(fc, BoxLayout.Y_AXIS)); fc.add(Box.createRigidArea(vstrut10)); //- - - - - - - - - - - - - - - - - - - - JPanel topPanel = new JPanel(); topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS)); fc.add(topPanel); fc.add(Box.createRigidArea(vstrut10)); JButton sortButton = new JButton(sortIcon); sortButton.setToolTipText("Sort options"); sortButton.getAccessibleContext().setAccessibleName("Sort options"); sortButton.setAlignmentX(JComponent.LEFT_ALIGNMENT); sortButton.setAlignmentY(JComponent.CENTER_ALIGNMENT); sortButton.setMargin(shrinkwrap); sortButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPopupMenu pop = getSortPopupMenu(); JButton button = (JButton)e.getSource(); pop.show(button,0,button.getHeight()); } }); topPanel.add(Box.createRigidArea(hstrut10)); topPanel.add(sortButton); topPanel.add(Box.createRigidArea(hstrut10)); /* JLabel l = new JLabel(lookInLabelText); l.setDisplayedMnemonic(lookInLabelMnemonic); l.setAlignmentX(JComponent.LEFT_ALIGNMENT); l.setAlignmentY(JComponent.CENTER_ALIGNMENT); topPanel.add(Box.createRigidArea(hstrut10)); topPanel.add(l); topPanel.add(Box.createRigidArea(hstrut10)); */ directoryComboBox = new JComboBox() { public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); } public Dimension getPreferredSize() { return new Dimension(180, super.getPreferredSize().height); } public Dimension getMinimumSize() { return getPreferredSize(); } }; directoryComboBox.putClientProperty( "JComboBox.lightweightKeyboardNavigation", "Lightweight" ); //l.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); topPanel.add(directoryComboBox); topPanel.add(Box.createRigidArea(hstrut10)); upFolderButton = new JButton(upFolderIcon); upFolderButton.setToolTipText(upFolderToolTipText); upFolderButton.getAccessibleContext().setAccessibleName(upFolderAccessibleName); upFolderButton.setAlignmentX(JComponent.LEFT_ALIGNMENT); upFolderButton.setAlignmentY(JComponent.CENTER_ALIGNMENT); upFolderButton.setMargin(shrinkwrap); upFolderButton.addActionListener(getChangeToParentDirectoryAction()); topPanel.add(upFolderButton); topPanel.add(Box.createRigidArea(hstrut10)); JButton b = new JButton(newFolderIcon); b.setToolTipText(newFolderToolTipText); b.getAccessibleContext().setAccessibleName(newFolderAccessibleName); b.setAlignmentX(JComponent.LEFT_ALIGNMENT); b.setAlignmentY(JComponent.CENTER_ALIGNMENT); b.setMargin(shrinkwrap); b.addActionListener(getNewFolderAction()); topPanel.add(b); topPanel.add(Box.createRigidArea(hstrut10)); /*b = new JButton(sortIcon); b.setToolTipText("Sort options"); b.getAccessibleContext().setAccessibleName("Sort options"); b.setAlignmentX(JComponent.LEFT_ALIGNMENT); b.setAlignmentY(JComponent.CENTER_ALIGNMENT); b.setMargin(shrinkwrap); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPopupMenu pop = getSortPopupMenu(); JButton button = (JButton)e.getSource(); pop.show(button,0,button.getHeight()); } }); topPanel.add(b); topPanel.add(Box.createRigidArea(hstrut10));*/ //- - - - - - - - - - - - - - - - - - - - centerPanel = new JPanel(new BorderLayout()); JComponent p = createList(fc); centerPanel.add(p, BorderLayout.CENTER); centerPanel.add(getAccessoryPanel(), BorderLayout.EAST); JComponent accessory = fc.getAccessory(); if(accessory != null) { getAccessoryPanel().add(accessory); } fc.add(centerPanel); //- - - - - - - - - - - - - - - - - - - - JPanel p1 = getBottomPanel(); p1.setLayout(new GridBagLayout()); //-------------------------------------------------------------------------- JLabel fnl = new JLabel(fileNameLabelText); fnl.setDisplayedMnemonic(fileNameLabelMnemonic); filenameTextField = new JTextField() { public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height); } }; filenameTextField.setEditable(true); fnl.setLabelFor(filenameTextField); filenameTextField.addActionListener(getApproveSelectionAction()); filenameTextField.addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent e) { list.clearSelection(); } } ); File f = fc.getSelectedFile(); if(f != null) { setFileName(fc.getName(f)); } JLabel ftl = new JLabel(filesOfTypeLabelText); ftl.setDisplayedMnemonic(filesOfTypeLabelMnemonic); filterComboBoxModel = createFilterComboBoxModel(); fc.addPropertyChangeListener(filterComboBoxModel); filterComboBox = new JComboBox(filterComboBoxModel); ftl.setLabelFor(filterComboBox); filterComboBox.setRenderer(createFilterComboBoxRenderer()); approveButton = new JButton(getApproveButtonText(fc)) { public Dimension getMaximumSize() { return approveButton.getPreferredSize().width > cancelButton.getPreferredSize().width ? approveButton.getPreferredSize() : cancelButton.getPreferredSize(); } }; approveButton.setMnemonic(getApproveButtonMnemonic(fc)); approveButton.addActionListener(getApproveSelectionAction()); approveButton.setToolTipText(getApproveButtonToolTipText(fc)); cancelButton = new JButton(cancelButtonText) { public Dimension getMaximumSize() { return approveButton.getPreferredSize().width > cancelButton.getPreferredSize().width ? approveButton.getPreferredSize() : cancelButton.getPreferredSize(); } }; cancelButton.setMnemonic(cancelButtonMnemonic); cancelButton.setToolTipText(cancelButtonToolTipText); cancelButton.addActionListener(getCancelSelectionAction()); //-------------------------------------------------------------------------- installComponent(p1, fnl, GridBagConstraints.WEST, GridBagConstraints.BOTH, insets, 0, 0, 1, 1, 0.0D, 0.0D); installComponent(p1, filenameTextField, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 1, 0, 1, 1, 1.0D, 0.0D); installComponent(p1, approveButton, GridBagConstraints.EAST, GridBagConstraints.BOTH, insets, 2, 0, 1, 1, 0.0D, 0.0D); installComponent(p1, new JPanel(), GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 0, 1, 2, 1, 1.0D, 0.0D); installComponent(p1, ftl, GridBagConstraints.WEST, GridBagConstraints.BOTH, insets, 0, 2, 1, 1, 0.0D, 0.0D); installComponent(p1, filterComboBox, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, insets, 1, 2, 1, 1, 1.0D, 0.0D); installComponent(p1, cancelButton, GridBagConstraints.EAST, GridBagConstraints.BOTH, insets, 2, 2, 1, 1, 0.0D, 0.0D); //-------------------------------------------------------------------------- fc.add(Box.createRigidArea(vstrut10)); Dimension dd = p1.getPreferredSize(); p1.setMaximumSize(new Dimension(Short.MAX_VALUE, dd.height)); fc.add(p1); fc.add(Box.createRigidArea(vstrut10)); if(getControlButtonsAreShown()) { addControlButtons(); } else { removeControlButtons(); } } public final static void installComponent(Container container, Component obj, int anchor, int constraints, Insets insets, int i, int j, int k, int l, double d, double d1) { GridBagLayout gridbaglayout = (GridBagLayout)container.getLayout(); GridBagConstraints gridbagconstraints = new GridBagConstraints(); gridbagconstraints.gridx = i; gridbagconstraints.gridy = j; gridbagconstraints.gridwidth = k; gridbagconstraints.gridheight = l; gridbagconstraints.weightx = d; gridbagconstraints.weighty = d1; gridbagconstraints.fill = constraints; gridbagconstraints.insets = insets; gridbagconstraints.anchor = anchor; container.add(obj); gridbaglayout.setConstraints(obj, gridbagconstraints); } public void actionPerformed(ActionEvent e) { String name = ((JComponent)e.getSource()).getName(); if(name==null) { return; } if(name.startsWith("use_")) { if(name.equals("use_extention")) { organizedMode = OyoahaDirectoryModel.USE_EXTENTION; } else if(name.equals("use_csextention")) { organizedMode = OyoahaDirectoryModel.USE_CASESENSITIVE_EXTENTION; } else if(name.equals("use_type")) { organizedMode = OyoahaDirectoryModel.USE_TYPE; } else if(name.equals("use_none")) { organizedMode = OyoahaDirectoryModel.USE_NONE; } OyoahaDirectoryModel m = getOyoahaModel(); if(m!=null) { list.clearSelection(); m.setUse(organizedMode); } } else if(name.startsWith("by_")) { list.clearSelection(); if(name.equals("by_name")) { sortedMode = OyoahaDirectoryModel.BY_NAME; } else if(name.equals("by_csname")) { sortedMode = OyoahaDirectoryModel.BY_CASESENSITIVE_NAME;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -