metalfilechooserui.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 2,081 行 · 第 1/4 页
JAVA
2,081 行
/* MetalFileChooserUI.java -- Copyright (C) 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.plaf.metal;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.Insets;import java.awt.LayoutManager;import java.awt.Rectangle;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.text.NumberFormat;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.File;import javax.swing.AbstractAction;import javax.swing.AbstractListModel;import javax.swing.ActionMap;import javax.swing.BorderFactory;import javax.swing.ButtonGroup;import javax.swing.ComboBoxModel;import javax.swing.DefaultListCellRenderer;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.JToggleButton;import javax.swing.JViewport;import javax.swing.ListModel;import javax.swing.ListSelectionModel;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.filechooser.FileFilter;import javax.swing.filechooser.FileSystemView;import javax.swing.filechooser.FileView;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.basic.BasicFileChooserUI;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.DefaultTableModel;import java.sql.Date;import java.text.DateFormat;import java.util.List;/** * A UI delegate for the {@link JFileChooser} component. This class is only * partially implemented and is not usable yet. */public class MetalFileChooserUI extends BasicFileChooserUI{ /** * A renderer for the files and directories in the file chooser table. */ class TableFileRenderer extends DefaultTableCellRenderer { /** * Creates a new renderer. */ public TableFileRenderer() { super(); } /** * Returns a component that can render the specified value. * * @param table the table * @param value the string value of the cell * @param isSelected is the item selected? * @param hasFocus does the item have the focus? * @param row the row * @param column the column * * @return The renderer. */ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (column == 0) { FileView v = getFileView(getFileChooser()); ListModel lm = fileList.getModel(); if (row < lm.getSize()) setIcon(v.getIcon((File) lm.getElementAt(row))); } else setIcon(null); setText(value.toString()); setOpaque(true); setEnabled(table.isEnabled()); setFont(fileList.getFont()); if (startEditing && column == 0 || !isSelected) { setBackground(table.getBackground()); setForeground(table.getForeground()); } else { setBackground(table.getSelectionBackground()); setForeground(table.getSelectionForeground()); } if (hasFocus) setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); else setBorder(noFocusBorder); return this; } } /** * ActionListener for the list view. */ class ListViewActionListener implements ActionListener { /** * This method is invoked when an action occurs. * * @param e - * the <code>ActionEvent</code> that occurred */ public void actionPerformed(ActionEvent e) { if (!listView) { int[] index = fileTable.getSelectedRows(); listView = true; JFileChooser fc = getFileChooser(); fc.remove(fileTablePanel); createList(fc); fileList.getSelectionModel().clearSelection(); if (index.length > 0) for (int i = 0; i < index.length; i++) fileList.getSelectionModel().addSelectionInterval(index[i], index[i]); fc.add(fileListPanel, BorderLayout.CENTER); fc.revalidate(); fc.repaint(); } } } /** * ActionListener for the details view. */ class DetailViewActionListener implements ActionListener { /** * This method is invoked when an action occurs. * * @param e - * the <code>ActionEvent</code> that occurred */ public void actionPerformed(ActionEvent e) { if (listView) { int[] index = fileList.getSelectedIndices(); JFileChooser fc = getFileChooser(); listView = false; fc.remove(fileListPanel); if (fileTable == null) createDetailsView(fc); else updateTable(); fileTable.getSelectionModel().clearSelection(); if (index.length > 0) { for (int i = 0; i < index.length; i++) fileTable.getSelectionModel().addSelectionInterval(index[i], index[i]); } fc.add(fileTablePanel, BorderLayout.CENTER); fc.revalidate(); fc.repaint(); } } } /** * A property change listener. */ class MetalFileChooserPropertyChangeListener implements PropertyChangeListener { /** * Default constructor. */ public MetalFileChooserPropertyChangeListener() { } /** * Handles a property change event. * * @param e the event. */ public void propertyChange(PropertyChangeEvent e) { JFileChooser filechooser = getFileChooser(); String n = e.getPropertyName(); if (n.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) { int mode = -1; if (filechooser.isMultiSelectionEnabled()) mode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION; else mode = ListSelectionModel.SINGLE_SELECTION; if (listView) fileList.setSelectionMode(mode); else fileTable.setSelectionMode(mode); } else if (n.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { File file = filechooser.getSelectedFile(); if (file != null && filechooser.getDialogType() == JFileChooser.SAVE_DIALOG) { if (file.isDirectory() && filechooser.isTraversable(file)) { directoryLabel = look; dirLabel.setText(directoryLabel); filechooser.setApproveButtonText(openButtonText); filechooser.setApproveButtonToolTipText(openButtonToolTipText); } else if (file.isFile()) { directoryLabel = save; dirLabel.setText(directoryLabel); filechooser.setApproveButtonText(saveButtonText); filechooser.setApproveButtonToolTipText(saveButtonToolTipText); } } if (file == null) setFileName(null); else setFileName(file.getName()); int index = -1; index = getModel().indexOf(file); if (index >= 0) { if (listView) { fileList.setSelectedIndex(index); fileList.ensureIndexIsVisible(index); fileList.revalidate(); fileList.repaint(); } else { fileTable.getSelectionModel().addSelectionInterval(index, index); fileTable.scrollRectToVisible(fileTable.getCellRect(index, 0, true)); fileTable.revalidate(); fileTable.repaint(); } } } else if (n.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) { if (listView) { fileList.clearSelection(); fileList.revalidate(); fileList.repaint(); } else { fileTable.clearSelection(); fileTable.revalidate(); fileTable.repaint(); } setDirectorySelected(false); File currentDirectory = filechooser.getCurrentDirectory(); setDirectory(currentDirectory); boolean hasParent = (currentDirectory.getParentFile() != null); getChangeToParentDirectoryAction().setEnabled(hasParent); } else if (n.equals(JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY)) { filterModel.propertyChange(e); } else if (n.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) { filterModel.propertyChange(e); } else if (n.equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY) || n.equals(JFileChooser.DIALOG_TITLE_CHANGED_PROPERTY)) { Window owner = SwingUtilities.windowForComponent(filechooser); if (owner instanceof JDialog) ((JDialog) owner).setTitle(getDialogTitle(filechooser)); approveButton.setText(getApproveButtonText(filechooser)); approveButton.setToolTipText( getApproveButtonToolTipText(filechooser)); approveButton.setMnemonic(getApproveButtonMnemonic(filechooser)); } else if (n.equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY)) approveButton.setText(getApproveButtonText(filechooser)); else if (n.equals( JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY)) approveButton.setToolTipText(getApproveButtonToolTipText(filechooser)); else if (n.equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY)) approveButton.setMnemonic(getApproveButtonMnemonic(filechooser)); else if (n.equals( JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) { if (filechooser.getControlButtonsAreShown()) { topPanel.add(controls, BorderLayout.EAST); } else topPanel.remove(controls); topPanel.revalidate(); topPanel.repaint(); topPanel.doLayout(); } else if (n.equals( JFileChooser.ACCEPT_ALL_FILE_FILTER_USED_CHANGED_PROPERTY)) { if (filechooser.isAcceptAllFileFilterUsed()) filechooser.addChoosableFileFilter( getAcceptAllFileFilter(filechooser)); else filechooser.removeChoosableFileFilter( getAcceptAllFileFilter(filechooser)); } else if (n.equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY)) { JComponent old = (JComponent) e.getOldValue(); if (old != null) getAccessoryPanel().remove(old); JComponent newval = (JComponent) e.getNewValue(); if (newval != null) getAccessoryPanel().add(newval); } if (n.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY) || n.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY) || n.equals(JFileChooser.FILE_HIDING_CHANGED_PROPERTY)) { // Remove editing component if (fileTable != null) fileTable.removeAll(); if (fileList != null) fileList.removeAll(); startEditing = false; // Set text on button back to original. if (filechooser.getDialogType() == JFileChooser.SAVE_DIALOG) { directoryLabel = save; dirLabel.setText(directoryLabel); filechooser.setApproveButtonText(saveButtonText); filechooser.setApproveButtonToolTipText(saveButtonToolTipText); } rescanCurrentDirectory(filechooser); } filechooser.revalidate(); filechooser.repaint(); } }; /** * A combo box model containing the selected directory and all its parent * directories. */ protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel { /** Storage for the items in the model. */ private List items; /** The index of the selected item. */ private int selectedIndex; /** * Creates a new model. */ public DirectoryComboBoxModel() { items = new java.util.ArrayList(); selectedIndex = -1; } /** * Returns the number of items in the model. * * @return The number of items in the model. */ public int getSize() { return items.size(); } /** * Returns the item at the specified index. * * @param index the item index. * * @return The item. */ public Object getElementAt(int index) { return items.get(index); } /** * Returns the depth of the item at the given <code>index</code>. * * @param index the item index. * * @return The depth. */ public int getDepth(int index) { return Math.max(index, 0); } /** * Returns the selected item, or <code>null</code> if no item is selected. * * @return The selected item, or <code>null</code>. */ public Object getSelectedItem() { if (selectedIndex >= 0) return items.get(selectedIndex); else return null; } /** * Sets the selected item. This clears all the directories from the
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?