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

📄 filesystemtreeplugin.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 06/13/2005
 *
 * FileSystemTreePlugin.java - A plugin that displays a tree of files on the
 * local filesystem, allowing for easy opening of files.
 * Copyright (C) 2005 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This file is a part of RText.
 *
 * RText 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.
 *
 * RText 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.rtext.plugins.filesystemtree;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.event.*;

import org.fife.io.UnicodeReader;
import org.fife.rtext.*;
import org.fife.ui.OptionsDialogPanel;
import org.fife.ui.RScrollPane;
import org.fife.ui.app.*;
import org.fife.ui.rtextfilechooser.FileSystemTree;


/**
 * A panel displaying all files on the local filesystem, allowing for quick
 * and easy opening of files without opening the file chooser.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class FileSystemTreePlugin extends GUIPlugin {

	private RText owner;
	private String name;
	private FileSystemTree tree;
	private Listener listener;
	private ResourceBundle msg;
	private FileSystemTreeOptionPanel optionPanel;
	private Icon pluginIcon;

	static final String BUNDLE_NAME			=
					"org/fife/rtext/plugins/filesystemtree/FileSystemTree";
	private static final String VERSION_STRING	= "0.9.4.0";


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


	/**
	 * Creates a new <code>FileSystemTreePlugin</code>.
	 *
	 * @param app The RText instance.
	 */
	public FileSystemTreePlugin(AbstractPluggableGUIApplication app) {

		this.owner = (RText)app;
		listener = new Listener();

		ClassLoader cl = this.getClass().getClassLoader();
		URL url = cl.getResource("org/fife/rtext/plugins/filesystemtree/filesystemtree.gif");
		if (url!=null)
			pluginIcon = new ImageIcon(url);

		msg = ResourceBundle.getBundle(BUNDLE_NAME, getLocale(), cl);
		this.name = msg.getString("Name");

		setLayout(new BorderLayout());

		tree = new FileSystemTree();
		tree.addMouseListener(listener);
		tree.addPropertyChangeListener(listener);
		RScrollPane scrollPane = new RScrollPane(tree);
		add(scrollPane);

		// Make the Enter key trigger opening a file on the JTree.
		InputMap inputMap = tree.getInputMap(JComponent.WHEN_FOCUSED);
		ActionMap actionMap = tree.getActionMap();
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "OnEnter");
		actionMap.put("OnEnter", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				doOpenFile();
			}
		});


		// Set any preferences saved from the last time this plugin was used.
		FileSystemTreePreferences sbp = (FileSystemTreePreferences)
									FileSystemTreePreferences.load();
		setActive(sbp.active);
		setPosition(sbp.position);

	}


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


	/**
	 * Creates a preferences instance for this GUI plugin based on its
	 * current properties.  Your GUI plugin should create a subclass of
	 * <code>GUIPluginPreferences</code> that loads and saves properties
	 * specific to your plugin, and return it from this method.
	 *
	 * @return A preferences instance.
	 * @see org.fife.ui.app.GUIPluginPreferences
	 */
	protected GUIPluginPreferences createPreferences() {
		return (GUIPluginPreferences)FileSystemTreePreferences.
										generatePreferences(this);
	}


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


	/**
	 * If a file is selected in the file system tree, it is opened in RText.
	 */
	protected void doOpenFile() {
		String fileName = tree.getSelectedFileName();
		if (fileName!=null) {
			File file = new File(fileName);
			// We'll make sure the file exists and is a regular file
			// (as opposed to a directory) before attempting to open it.
			if (file.isFile()) {
				try {
					// Do a quick 'n' dirty Unicode check.  If the
					// file is not Unicode, it will be opened with
					// the system's default encoding.
					UnicodeReader r = new UnicodeReader(file);
					String encoding = r.getEncoding();
					r.close();
					AbstractMainView mainView = owner.getMainView();
					mainView.addOldTextFile(fileName, encoding);
					mainView.currentTextArea.setCaretPosition(0);
				} catch (IOException ioe) {
					owner.displayException(ioe);
				}
			}
		}
	}


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


	/**
	 * Returns the author of the plugin.
	 *
	 * @return The plugin's author.
	 */
	public String getAuthor() {
		return "Robert Futrell";
	}


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


	/**
	 * Returns the icon for this plugin.
	 *
	 * @return The icon for this plugin.
	 */
	public Icon getIcon() {
		return pluginIcon;
	}


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


	/**
	 * Returns the menu items for this plugin.
	 *
	 * @return The menu for this plugin.
	 */
	public JMenu getMenu() {

		JMenu menu = new JMenu(getName());
		
		JCheckBoxMenuItem cbMenuItem =
					new JCheckBoxMenuItem(new ViewAction(msg));
		cbMenuItem.setSelected(isActive());
		menu.add(cbMenuItem);

		return menu;

	}


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


	/**
	 * Returns the name of this <code>GUIPlugin</code>.
	 *
	 * @return This plugin's name.
	 */
	public String getName() {
		return name;
	}


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


	/**
	 * Returns the options panel for this source browser.
	 *
	 * @return The options panel.
	 */
	public synchronized PluginOptionsDialogPanel getOptionsDialogPanel() {
		if (optionPanel==null) {
			optionPanel = new FileSystemTreeOptionPanel(owner, this);
		}
		return optionPanel;
	}


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


	/**
	 * Returns the plugin version.
	 */
	public String getVersion() {
		return VERSION_STRING;
	}


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


	/**
	 * Called just after a plugin is added to a GUI application.
	 *
	 * @param app The application to which this plugin was just added.
	 * @see #uninstall
	 */
	public void install(AbstractPluggableGUIApplication app) {
	}


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


	/**
	 * Called just before this <code>Plugin</code> is removed from an
	 * RText instance.  Here we uninstall any listeners we registered.
	 *
	 * @return Whether the uninstall went cleanly.
	 */
	public boolean uninstall() {
		return true;
	}


/*****************************************************************************/
/********************** INNER CLASSES ****************************************/
/*****************************************************************************/


	/**
	 * Listens for events in this plugin (specifically, in the JTree).
	 */
	private class Listener extends MouseAdapter implements
									PropertyChangeListener {

		public void mouseClicked(MouseEvent e) {
			if (e.getClickCount()==2) {
				doOpenFile();
			}
		}

		public void propertyChange(PropertyChangeEvent e) {
			String name = e.getPropertyName();
			if (name.equals(FileSystemTree.WILL_EXPAND_PROPERTY)) {
				owner.setCursor(Cursor.
							getPredefinedCursor(Cursor.WAIT_CURSOR));
			}
			else if (name.equals(FileSystemTree.EXPANDED_PROPERTY)) {
				owner.setCursor(Cursor.
							getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			}
		}

	}


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


	/**
	 * Toggles the visibility of this file system tree.
	 */
	private class ViewAction extends AbstractAction {

		public ViewAction(ResourceBundle msg) {
			putValue(NAME, msg.getString("MenuItem.View"));
			putValue(MNEMONIC_KEY, new Integer(
					msg.getString("MenuItem.View.Mnemonic").charAt(0)));
			putValue(LONG_DESCRIPTION, msg.getString("MenuItem.View.Desc"));
		}

		public void actionPerformed(ActionEvent e) {
			setActive(!isActive());
		}

	}


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

}

⌨️ 快捷键说明

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