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

📄 rtextfilechooser.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 * 06/30/2004
 *
 * RTextFileChooser - A Microsoft Windows-style file chooser for any
 *                    Look-and-Feel.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui.rtextfilechooser;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;

import org.fife.RUtilities;
import org.fife.io.UnicodeReader;
import org.fife.ui.FSATextField;
import org.fife.ui.MenuButton;
import org.fife.ui.RScrollPane;


/**
 * A powerful, flexible text file chooser.  Its UI is similar to the Windows LnF
 * <code>JFileChooser</code>, but it is not affected by Look-and-Feel changes.
 * Designed for opening text files, this file chooser has the following
 * features:
 * <ul>
 *   <li>List/Details/Icon views</li>
 *   <li>Select files only, directories only, or both files and directories</li>
 *   <li>Rename and delete files directly from the file chooser</li>
 *   <li>Select the file encoding when opening/saving files</li>
 *   <li>Set the text color used to identify different file types</li>
 * </ul>
 * This component has many of the features found in <code>JFileChooser</code>
 * and is designed to be a drop-in replacement for opening text files.
 *
 * @author Robert Futrell
 * @version 0.2
 */
public class RTextFileChooser extends JComponent implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 705535860637476890L;

	public static final int LIST_MODE				= 0;
	public static final int DETAILS_MODE			= 1;
	public static final int ICONS_MODE				= 3;

	public static final int APPROVE_OPTION			= JFileChooser.APPROVE_OPTION;
	public static final int CANCEL_OPTION			= JFileChooser.CANCEL_OPTION;
	public static final int ERROR_OPTION			= JFileChooser.ERROR_OPTION;

	public static final int FILES_ONLY				= JFileChooser.FILES_ONLY;
	public static final int DIRECTORIES_ONLY		= JFileChooser.DIRECTORIES_ONLY;
	public static final int FILES_AND_DIRECTORIES	= JFileChooser.FILES_AND_DIRECTORIES;

	private static final int OPEN					= 0;
	private static final int SAVE					= 1;

	static final boolean IGNORE_CASE				= RTextFileChooser.getIgnoreCase();

	private static final String separator			= System.getProperty("file.separator");

	private FileSystemView fileSystemView;
	private ItemListener itemListener;
	private JPanel topPanel;

	/*
	 * Labels for the various combo boxes.
	 */
	private JLabel lookInLabel;
	private JLabel fileNameLabel;
	private JLabel filterLabel;
	private JLabel encodingLabel;

	/*
	 * The combo boxes themselves.
	 */
	private JComboBox lookInComboBox;
	private FSATextField fileNameTextField;
	private JComboBox filterComboBox;
	private JComboBox encodingComboBox;

	/*
	 * Any renderers/listeners.
	 */
	private LookInComboCellRenderer lookInRenderer;
	private TextFieldListener textFieldListener;

	/*
	 * The buttons at the top of the dialog.
	 */
	private JButton upOneLevelButton;
	private JButton newFolderButton;
	private MenuButton viewButton;

	/*
	 * The menu items in the "view" menu button.
	 */
	private JRadioButtonMenuItem listMenuItem;
	private JRadioButtonMenuItem detailsMenuItem;
	private JRadioButtonMenuItem iconsMenuItem;

	/*
	 * The accept and cancel buttons.
	 */
	private JButton acceptButton;
	private JButton cancelButton;

	/*
	 * The main view (both list and table).
	 */
	protected RTextFileChooserView view;
	private RScrollPane viewScrollPane;

	/*
	 * The right-click popup menu.
	 */
	private JPopupMenu popupMenu;

	/*
	 * The "glob" (wildcard) filter.
	 */
	private WildcardFileFilter globFilter;

	/*
	 * Icons.
	 */
	private Icon upFolderIcon;
	private Icon newFolderIcon;
	private Icon detailsViewIcon;
	private Icon listViewIcon;
	private Icon iconsViewIcon;
	private FileChooserIconManager iconManager;

	/*
	 * Strings used by the file chooser.
	 */
	private String saveButtonText;
	private String saveDialogTitleText;
	private String saveButtonToolTipText;
	private int saveButtonMnemonic;
	private String openButtonText;
	private String openDialogTitleText;
	private String openButtonToolTipText;
	private int openButtonMnemonic;
	private String cancelButtonText;
	private String cancelButtonToolTipText;
	private int cancelButtonMnemonic;
	private String newFolderPrompt;
	private String errorNewDirPrompt;
	private String errorDialogTitle;
	private String newNamePrompt;
	private String renameFailText;
	private String renameErrorMessage;
	private String deleteConfirmPrompt;
	private String deleteMultipleConfirmPrompt;
	private String deleteFailText;
	private String invalidFileNameMessage;
	private String directoryText;
	private String fileText;
	private String nameString;
	private String sizeString;
	private String typeString;
	private String statusString;
	private String lastModifiedString;
	String readString;
	String writeString;
	String readWriteString;

	/*
	 * Internal stuff.
	 */
	private int mode					= -1; // So that we don't get an NPE initially.
	private boolean multiSelectionEnabled	= false;
	private int fileSelectionMode			= FILES_ONLY;
	private boolean fileSystemAware;
	private String encoding;
	private FileFilter filterToSelect;

	private FileTypeInfo tempInfo;	// Used internally.
	private Color defaultFileColor;	// Default color for filenames.
	private HashMap customColors;		// Mapping of extensions to colors.
	private boolean showHiddenFiles;
	private Color hiddenFileColor;

	File currentDirectory;
	private File[] selectedFiles;

	private Dimension lastSize;

	private Vector fileFilters = new Vector(5,1);
	private FileFilter currentFileFilter;

	private boolean isChangingDirectories;

	private URLClassLoader cl;

	private File[] filesToSelect;			// Any files to select (specified via setSelectedFiles).
	File[] underlinedFiles;				// Files whose names are underlined (i.e., they are "opened" in the application).

	private JDialog dialog;				// The dialog that displays this file chooser (which is actually a JComponent).

	private boolean guiInitialized;

	/**
	 * The return value given after showOpenDialog or showSaveDialog.
	 */
	private int retVal;


/*****************************************************************************/


	/**
	 * Creates a new <code>RTextFileChooser</code> defaulting to
	 * <code>LIST_MODE</code> and the user's home directory.
	 */
	public RTextFileChooser() {
		this(LIST_MODE);
	}


/*****************************************************************************/


	/**
	 * Creates a new <code>RTextFileChooser</code> defaulting to
	 * <code>LIST_MODE</code>.
	 *
	 * @param mode Either <code>LIST_MODE</code> or <code>DETAILS_MODE</code>.
	 */
	public RTextFileChooser(int mode) {
		this(mode, System.getProperty("user.dir"));
	}


/*****************************************************************************/


	/**
	 * Creates a new <code>RTextFileChooser</code> defaulting to
	 * <code>LIST_MODE</code>.
	 *
	 * @param mode Either <code>LIST_MODE</code> or <code>DETAILS_MODE</code>.
	 * @param startDirectory The directory for the file chooser to "start" in.
	 */
	public RTextFileChooser(int mode, String startDirectory) {
		this(mode, new File(startDirectory));
	}


/*****************************************************************************/


	/**
	 * Creates a new <code>RTextFileChooser</code>.
	 *
	 * @param mode Either <code>LIST_MODE</code> or <code>DETAILS_MODE</code>.
	 * @param startDirectory The directory for the file chooser to "start" in.
	 */
	public RTextFileChooser(int mode, File startDirectory) {

		super();

		fileSystemView = FileSystemView.getFileSystemView();
		iconManager = new FileChooserIconManager();

		itemListener = new RTextFileChooserItemListener();

		// Get the "current directory" for the file chooser.
		if (startDirectory.isDirectory())
			currentDirectory = startDirectory;
		else
			currentDirectory = new File(System.getProperty("user.dir"));

		// Read and set the user's preferences for the file chooser.
		// We need to do this after all components are added above.
		FileChooserPreferences prefs = FileChooserPreferences.load();
		tempInfo = new FileTypeInfo(null, null);
		setDefaultFileColor(prefs.defaultFileColor);
		customColors = prefs.customColors;
		setShowHiddenFiles(prefs.showHiddenFiles);
		setHiddenFileColor(prefs.hiddenFileColor);
		setFileSystemAware(prefs.fileSystemAware);
		prefs = null;

		// Do NOT call setViewMode() yet, as we can do without its overhead.
		this.mode = mode;
		guiInitialized = false;


	}


/*****************************************************************************/


	/**
	 * Initializes the GUI components for the file chooser.  This stuff
	 * isn't done in the constructor as it can be time-consuming (actually,
	 * Java checking for roots of the file system can be time-consuming).
	 * Instead, we put it in this method and defer it until the file chooser
	 * is actually displayed, making sure it is only done once.  This way
	 * the user can pull up the Options dialog and change properties of the
	 * file chooser without the GUI having to be initialized.
	 */
	protected synchronized void initializeGUIComponents() {

		// Only initialize the GUI once.
		if (guiInitialized)
			return;

		// Get our class loader.
		cl = (URLClassLoader)this.getClass().getClassLoader();

		// Get the icons for all stuff below.
		getIcons();

		setLayout(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		topPanel = new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));

		lookInLabel = new JLabel();
		topPanel.add(lookInLabel);

		int horizStrutSize = 4;
		Border empty3Border = BorderFactory.createEmptyBorder(3,3,3,3);

		lookInComboBox = new JComboBox();
		lookInRenderer = new LookInComboCellRenderer();
		lookInRenderer.createCachedData(); // Depends on rootManager.
		lookInComboBox.setRenderer(lookInRenderer);
		populateLookInComboBox();
		lookInComboBox.addItemListener(itemListener);
		topPanel.add(lookInComboBox);
		topPanel.add(Box.createHorizontalStrut(horizStrutSize));

		upOneLevelButton = new JButton(upFolderIcon);
		upOneLevelButton.setActionCommand("UpOneLevel");
		upOneLevelButton.addActionListener(this);
		upOneLevelButton.setBorder(empty3Border);
		topPanel.add(upOneLevelButton);
		topPanel.add(Box.createHorizontalStrut(horizStrutSize));

		newFolderButton = new JButton(newFolderIcon);
		newFolderButton.setActionCommand("CreateNewDirectory");
		newFolderButton.addActionListener(this);
		newFolderButton.setBorder(empty3Border);
		topPanel.add(newFolderButton);
		topPanel.add(Box.createHorizontalStrut(horizStrutSize));

		viewButton = new MenuButton(listViewIcon);
		viewButton.setBorder(empty3Border);
		listMenuItem = new JRadioButtonMenuItem("List", listViewIcon, mode==LIST_MODE);
		listMenuItem.setActionCommand("ListButton");
		listMenuItem.addActionListener(this);
		viewButton.addMenuItem(listMenuItem);
		ButtonGroup bg = new ButtonGroup();
		bg.add(listMenuItem);
		detailsMenuItem = new JRadioButtonMenuItem("Details", detailsViewIcon, mode==DETAILS_MODE);
		detailsMenuItem.setActionCommand("DetailsButton");
		detailsMenuItem.addActionListener(this);
		viewButton.addMenuItem(detailsMenuItem);
		bg.add(detailsMenuItem);
		iconsMenuItem = new JRadioButtonMenuItem("Icons", iconsViewIcon, mode==ICONS_MODE);
		iconsMenuItem.setActionCommand("IconsButton");
		iconsMenuItem.addActionListener(this);
		viewButton.addMenuItem(iconsMenuItem);
		bg.add(iconsMenuItem);
		topPanel.add(viewButton);

		add(topPanel, BorderLayout.NORTH);

		// Initialize listeners.
		textFieldListener = new TextFieldListener();

		// Initialize either the list view or the details (table) view.
		setViewModeImpl(mode);
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
		centerPanel.add(Box.createVerticalStrut(5));
		centerPanel.add(viewScrollPane);
		centerPanel.add(Box.createVerticalStrut(5));
		add(centerPanel);

		// Create a panel for the bottom stuff.
		JPanel bottomPanel = new JPanel(new SpringLayout());

		fileNameLabel = new JLabel();
		filterLabel = new JLabel();
		encodingLabel = new JLabel();

		Dimension fieldSize = new Dimension(300,20);
		Dimension maximumFieldSize = new Dimension(2000,20);

		fileNameTextField = new FSATextField(false, currentDirectory);
		fileNameTextField.setFileSystemAware(getFileSystemAware());
		fileNameTextField.setPreferredSize(fieldSize);
		fileNameTextField.setMaximumSize(maximumFieldSize);
		fileNameTextField.addFocusListener(textFieldListener);
		fileNameTextField.getDocument().addDocumentListener(textFieldListener);
		fileNameLabel.setLabelFor(fileNameTextField);

		filterComboBox = new JComboBox();

⌨️ 快捷键说明

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