⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpfilechooserui.java

📁 Swing Windows XP 外观和感觉 BeanSoft 修改版, 2003年 原始的作者: XP 外观和感觉 by Stefan Krause - http://www.stefan
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// Beta
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	XP Look and Feel                                                       *
*                                                                              *
*  (C) Copyright 2002, by Stefan Krause                                        *
*                                                                              *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


/*
 * 
 * This is an almost unchanged version of MetalFileChooserUI.
 * 
 * 
 * @(#)BasicFileChooserUI.java  1.45 02/04/11
 *
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
 
package com.stefankrause.xplookandfeel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.EventObject;
import java.util.Locale;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.AbstractListModel;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
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.KeyStroke;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.LookAndFeel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;
import javax.swing.plaf.ActionMapUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicFileChooserUI;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.text.Position;

import sun.awt.shell.ShellFolder;

public class XPFileChooserUI extends BasicFileChooserUI {

	// Much of the Metal UI for JFilechooser is just a copy of
	// the windows implementation, but using Metal themed buttons, lists,
	// icons, etc. We are planning a complete rewrite, and hence we've
	// made most things in this class private.
	private JPanel centerPanel;

	private JLabel lookInLabel;
	private JComboBox directoryComboBox;
	private DirectoryComboBoxModel directoryComboBoxModel;
	private Action directoryComboBoxAction = new DirectoryComboBoxAction();

	private FilterComboBoxModel filterComboBoxModel;

	private JTextField fileNameTextField;

	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 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 hstrut5 = new Dimension(5, 1);
	private static final Dimension hstrut11 = new Dimension(11, 1);

	private static final Dimension vstrut5 = new Dimension(1, 5);

	private static final Insets shrinkwrap = new Insets(2, 2, 2, 2);

	// Preferred and Minimum sizes for the dialog box
	private static int PREF_WIDTH = 500;
	private static int PREF_HEIGHT = 326;
	private static Dimension PREF_SIZE = new Dimension(PREF_WIDTH, PREF_HEIGHT);

	private static int MIN_WIDTH = 500;
	private static int MIN_HEIGHT = 326;
	private static Dimension MIN_SIZE = new Dimension(MIN_WIDTH, MIN_HEIGHT);

	private static int LIST_PREF_WIDTH = 405;
	private static int LIST_PREF_HEIGHT = 135;
	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;

	//
	// ComponentUI Interface Implementation methods
	//
	public static ComponentUI createUI(JComponent c) {
		return new XPFileChooserUI((JFileChooser) c);
	}

	public XPFileChooserUI(JFileChooser filechooser) {
		super(filechooser);
	}

	public void installUI(JComponent c) {
		super.installUI(c);
	}

	public void uninstallComponents(JFileChooser fc) {
		fc.removeAll();
		bottomPanel = null;
		buttonPanel = null;
	}

	public void installComponents(JFileChooser fc) {
		FileSystemView fsv = fc.getFileSystemView();

		fc.setBorder(new EmptyBorder(12, 12, 11, 11));
		fc.setLayout(new BorderLayout(0, 11));

		// ********************************* //
		// **** Construct the top panel **** //
		// ********************************* //

		// Directory manipulation buttons
		JPanel topPanel = new JPanel(new BorderLayout(11, 0));
		JPanel topButtonPanel = new JPanel();
		topButtonPanel.setLayout(new BoxLayout(topButtonPanel, BoxLayout.LINE_AXIS));
		topPanel.add(topButtonPanel, BorderLayout.AFTER_LINE_ENDS);

		// Add the top panel to the fileChooser
		fc.add(topPanel, BorderLayout.NORTH);

		// ComboBox Label
		lookInLabel = new JLabel(lookInLabelText);
		lookInLabel.setDisplayedMnemonic(lookInLabelMnemonic);
		topPanel.add(lookInLabel, BorderLayout.BEFORE_LINE_BEGINS);

		// 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.TOP_ALIGNMENT);
		directoryComboBox.setMaximumRowCount(8);

		topPanel.add(directoryComboBox, BorderLayout.CENTER);

		// Up Button
		JButton upFolderButton = new JButton(getChangeToParentDirectoryAction());
		upFolderButton.putClientProperty("JToolBar.isToolbarButton",Boolean.TRUE);
		upFolderButton.setOpaque(false);
		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(new Insets(5,5,5,5));

		topButtonPanel.add(upFolderButton);
		topButtonPanel.add(Box.createRigidArea(hstrut5));

		// Home Button
		File homeDir = fsv.getHomeDirectory();
		String toolTipText = homeFolderToolTipText;
		if (fsv.isRoot(homeDir)) {
			toolTipText = getFileView(fc).getName(homeDir); // Probably "Desktop".
		}

		JButton b = new JButton(homeFolderIcon);
		b.putClientProperty("JToolBar.isToolbarButton",Boolean.TRUE);
		b.setToolTipText(toolTipText);
		b.getAccessibleContext().setAccessibleName(homeFolderAccessibleName);
		b.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		b.setAlignmentY(JComponent.CENTER_ALIGNMENT);
		b.setMargin(new Insets(5,5,5,5));

		b.addActionListener(getGoHomeAction());
		topButtonPanel.add(b);
		topButtonPanel.add(Box.createRigidArea(hstrut5));

		// New Directory Button
		b = new JButton(getNewFolderAction());
		b.putClientProperty("JToolBar.isToolbarButton",Boolean.TRUE);
		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(new Insets(5,5,5,5));

		topButtonPanel.add(b);
		topButtonPanel.add(Box.createRigidArea(hstrut5));

		// 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.putClientProperty("JToolBar.isToolbarButton",Boolean.TRUE);

		listViewButton.setToolTipText(listViewButtonToolTipText);
		listViewButton.getAccessibleContext().setAccessibleName(listViewButtonAccessibleName);
		listViewButton.setSelected(true);
		listViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		listViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
		listViewButton.setMargin(shrinkwrap);
		listViewButton.addActionListener(viewButtonListener);
		topButtonPanel.add(listViewButton);
		viewButtonGroup.add(listViewButton);

		// Details Button
		detailsViewButton = new JToggleButton(detailsViewIcon);
		detailsViewButton.putClientProperty("JToolBar.isToolbarButton",Boolean.TRUE);
		detailsViewButton.setToolTipText(detailsViewButtonToolTipText);
		detailsViewButton.getAccessibleContext().setAccessibleName(detailsViewButtonAccessibleName);
		detailsViewButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		detailsViewButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
		detailsViewButton.setMargin(shrinkwrap);
		detailsViewButton.addActionListener(viewButtonListener);
		topButtonPanel.add(detailsViewButton);
		viewButtonGroup.add(detailsViewButton);

		// Use ShellFolder class to populate combobox only if
		// FileSystemView.getRoots() returns one folder and that is
		// the same as the first item in the ShellFolder combobox list.
		{
			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;
				}
			}
		}

		// ************************************** //
		// ******* 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;

⌨️ 快捷键说明

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