📄 rtext.java~2~
字号:
/*
* 11/14/2003
*
* RText.java - A syntax highlighting programmer's text editor written in Java.
* Copyright (C) 2003 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;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.prefs.Preferences;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Element;
import org.fife.RUtilities;
import org.fife.help.HelpDialog;
import org.fife.io.UnicodeReader;
import org.fife.ui.CustomizableToolBar;
import org.fife.ui.OptionsDialog;
import org.fife.ui.SplashScreen;
import org.fife.ui.app.AbstractGUIApplication;
import org.fife.ui.app.AbstractPluggableGUIApplication;
import org.fife.ui.app.ExtendedLookAndFeelInfo;
import org.fife.ui.app.GUIApplicationPreferences;
import org.fife.ui.app.Plugin;
import org.fife.ui.app.ThirdPartyLookAndFeelManager;
import org.fife.ui.rsyntaxtextarea.SyntaxHighlightingColorScheme;
import org.fife.ui.rtextarea.IconGroup;
import org.fife.ui.rtextarea.RTextAreaEditorKit;
import org.fife.ui.rtextfilechooser.RTextFileChooser;
import org.fife.ui.search.FindDialog;
/**
* An instance of the rtext text editor. <code>RText</code> is a fully featured
* programmer's text editor with the following features:
*
* <ul>
* <li>Syntax highlighting for 15+ languages.</li>
* <li>Edit multiple documents simultaneously.</li>
* <li>Auto-indent.</li>
* <li>Find/Replace/Find in Files, with regular expression functionality.</li>
* <li>Printing and Print Preview.</li>
* <li>Online help.</li>
* <li>Intelligent source browsing via Exhuberant CTags.</li>
* <li>Macros.</li>
* <li>Code templates.</li>
* <li>Many other features.</li>
* </ul>
*
* At the heart of this program is
* {@link org.fife.ui.rsyntaxtextarea.RSyntaxTextArea}, a fully-featured,
* syntax highlighting text component. That's where most of the meat is.
* All text areas are contained in a subclass of
* {@link org.fife.rtext.AbstractMainView}, which keeps the state of all of the
* text areas in synch (fonts used, colors, etc.). This class (RText) contains
* an instance of a subclass of {@link org.fife.rtext.AbstractMainView} (which
* contains all of the text areas) as well as the menu, soure browser, and
* status bar.
*
* @author Robert Futrell
* @version 0.9.4.0
*/
public class RText
extends AbstractPluggableGUIApplication
implements ActionListener, DocumentListener,
CaretListener, KeyListener,
PropertyChangeListener,
RTextActionInfo {
// Constants specifying the current view style.
public static final int TABBED_VIEW = 0;
public static final int SPLIT_PANE_VIEW = 1;
public static final int MDI_VIEW = 2;
// Properties fired.
public static final String ICON_STYLE_PROPERTY = "RText.iconStyle";
public static final String MAIN_VIEW_STYLE_PROPERTY = "RText.mainViewStyle";
private Map iconGroupMap;
private RTextMenuBar menuBar; // Menu bar for rtext.
public OptionsDialog optionsDialog; // Dialog for displaying miscellaneous options.
private AbstractMainView mainView; // Area showing all open documents.
private int mainViewStyle; // Either TABBED_VIEW or SPLIT_PANE_VIEW.
private RTextFileChooser chooser; // The Open/Save/Save As dialog.
private HelpDialog helpDialog; // The Help dialog.
private boolean aboutDialogCreated;
private SyntaxHighlightingColorScheme colorScheme; // Syntax highlighting colors.
private IconGroup iconGroup;
private String workingDirectory; // The directory in which new empty files are placed.
private String newFileName; // The name for new empty text files.
private SearchToolBar searchBar;
private static final String VERSION_STRING = "0.9.4.0.20060113";
/*****************************************************************************/
/**
* Creates an instance of the <code>RText</code> editor.
*
* @param filesToOpen Array of <code>java.lang.String</code>s containing
* the files we want to open initially.
*/
public RText(String[] filesToOpen) {
super("rtext", "RText.jar");
openFiles(filesToOpen);
}
/*****************************************************************************/
/**
* Creates an instance of the <code>RText</code> editor.
*
* @param filesToOpen Array of <code>java.lang.String</code>s containing
* the files we want to open initially.
* @param preferences The preferences with which to initialize this RText.
*/
public RText(String[] filesToOpen, RTextPreferences preferences) {
super("rtext", "RText.jar", preferences);
openFiles(filesToOpen);
}
/*****************************************************************************/
// What to do when user does something.
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
// If they choose "Exit" from the File menu.
if (actionCommand.equals("Exit")) {
doExit();
}
else if (actionCommand.equals("TileVertically")) {
( (RTextMDIView) mainView).tileWindowsVertically();
}
else if (actionCommand.equals("TileHorizontally")) {
( (RTextMDIView) mainView).tileWindowsHorizontally();
}
else if (actionCommand.equals("Cascade")) {
( (RTextMDIView) mainView).cascadeWindows();
}
// Any other event, we don't know how to deal with yet.
else {
JOptionPane.showMessageDialog(this,
"Unknown actionCommand: '" + actionCommand +
"'",
getResourceBundle().getString(
"ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
}
}
/*****************************************************************************/
/**
* Returns whether or not tabs are emulated with spaces (i.e. "soft" tabs).
* This simply calls <code>mainView.areTabsEmulated</code>.
*
* @return <code>true</code> if tabs are emulated with spaces;
* <code>false</code> if they aren't.
*/
public boolean areTabsEmulated() {
return mainView.areTabsEmulated();
}
/*****************************************************************************/
/**
* Called when cursor in text editor changes position.
*
* @param e The caret event.
*/
public void caretUpdate(CaretEvent e) {
// Update row/column information in status field.
RTextEditorPane currentTextArea = mainView.currentTextArea;
int caretPosition = currentTextArea.getCaretPosition();
Element map = currentTextArea.getDocument().getDefaultRootElement();
int lineNumber = map.getElementIndex(caretPosition);
int lineStartOffset = map.getElement(lineNumber).getStartOffset();
( (StatusBar) getStatusBar()).setRowAndColumn(
lineNumber + 1, caretPosition - lineStartOffset + 1);
}
/*****************************************************************************/
// This function is called whenever a property of the text is changed.
public void changedUpdate(DocumentEvent e) {
}
/*****************************************************************************/
/**
* Converts all instances of a number of spaces equal to a tab in all open documents
* into tabs.
*
* @see #convertOpenFilesTabsToSpaces
*/
public void convertOpenFilesSpacesToTabs() {
mainView.convertOpenFilesSpacesToTabs();
}
/*****************************************************************************/
/**
* Converts all tabs in all open documents into an equivalent number of
* spaces.
*
* @see #convertOpenFilesSpacesToTabs
*/
public void convertOpenFilesTabsToSpaces() {
mainView.convertOpenFilesTabsToSpaces();
}
/*****************************************************************************/
/**
* Returns the About dialog for this application.
*
* @return The About dialog.
*/
protected org.fife.ui.AboutDialog createAboutDialog() {
aboutDialogCreated = true;
return new org.fife.rtext.AboutDialog(this);
}
/*****************************************************************************/
/**
* Creates the array of actions used by this RText.
*
* @param prefs The RText properties for this RText instance.
*/
protected void createActions(GUIApplicationPreferences prefs) {
// We use a different resource bundle so we don't needlessly keep
// all of this stuff in memory in the main RText bundle.
ResourceBundle msg = ResourceBundle.getBundle(
"org.fife.rtext.RTextActions");
URLClassLoader cl = (URLClassLoader)this.getClass().getClassLoader();
String commonIconPath = "org/fife/rtext/graphics/common_icons/";
this.setIconImage(new ImageIcon(cl.findResource(
"org/fife/rtext/graphics/rtexticon.gif")).getImage());
String temp = msg.getString("NewActionName");
addAction(NEW_ACTION, new NewAction(temp, null, temp,
new Integer(msg.getString(
"NewActionMnemonic").charAt(0)),
prefs.getAccelerator(NEW_ACTION), this));
temp = msg.getString("OpenActionName");
addAction(OPEN_ACTION, new OpenAction(temp, null, temp,
new Integer(msg.getString(
"OpenActionMnemonic").charAt(0)),
prefs.getAccelerator(OPEN_ACTION), this));
temp = msg.getString("ONWActionName");
addAction(OPEN_NEWWIN_ACTION, new OpenInNewWindowAction(temp, null,
temp,
new Integer(msg.getString("ONWActionMnemonic").charAt(0)),
prefs.getAccelerator(OPEN_NEWWIN_ACTION), this));
temp = msg.getString("OpenRemoteActionName");
addAction(OPEN_REMOTE_ACTION, new OpenRemoteAction(temp, null, temp,
new Integer(msg.getString("OpenRemoteActionMnemonic").charAt(0)),
null, this));
temp = msg.getString("SaveActionName");
addAction(SAVE_ACTION, new SaveAction(temp, null, temp,
new Integer(msg.getString(
"SaveActionMnemonic").charAt(0)),
prefs.getAccelerator(SAVE_ACTION), this));
temp = msg.getString("SaveAsActionName");
addAction(SAVE_AS_ACTION, new SaveAsAction(temp, null, temp,
new Integer(msg.getString(
"SaveAsActionMnemonic").charAt(0)),
prefs.getAccelerator(
SAVE_AS_ACTION), this));
temp = msg.getString("SAWPActionName");
addAction(SAVE_WEBPAGE_ACTION, new SaveAsWebPageAction(temp, null, temp,
new Integer(msg.getString("SAWPActionMnemonic").charAt(0)),
prefs.getAccelerator(SAVE_WEBPAGE_ACTION), this));
temp = msg.getString("SaveAllActionName");
addAction(SAVE_ALL_ACTION, new SaveAllAction(temp, null, temp, null,
prefs.getAccelerator(
SAVE_ALL_ACTION), this));
temp = msg.getString("TimeActionName");
addAction(TIME_DATE_ACTION, new RTextAreaEditorKit.TimeDateAction(temp,
new ImageIcon(cl.findResource(commonIconPath + "timedate16.gif")),
temp,
new Integer(msg.getString("TimeActionMnemonic").charAt(0)),
prefs.getAccelerator(TIME_DATE_ACTION)));
temp = msg.getString("LNActionName");
addAction(LINE_NUMBER_ACTION, new LineNumberAction(temp, null, temp,
new Integer(msg.getString("LNActionMnemonic").charAt(0)),
prefs.getAccelerator(LINE_NUMBER_ACTION), this));
temp = msg.getString("HelpActionName");
addAction(HELP_ACTION, new HelpAction(temp, null, temp,
new Integer(msg.getString(
"HelpActionMnemonic").charAt(0)),
prefs.getAccelerator(HELP_ACTION), this));
temp = msg.getString("AboutActionName");
addAction(ABOUT_ACTION, new AboutAction(temp, null, temp,
new Integer(msg.getString(
"AboutActionMnemonic").charAt(0)),
prefs.getAccelerator(ABOUT_ACTION), this));
temp = msg.getString("OptionsActionName");
addAction(OPTIONS_ACTION, new OptionsAction(temp, null, temp,
new Integer(msg.getString(
"OptionsActionMnemonic").charAt(0)),
prefs.getAccelerator(
OPTIONS_ACTION), this));
msg = null; // May help with GC.
}
/*****************************************************************************/
/**
* Creates and returns the menu bar used in this application.
*
* @param prefs This GUI application's preferences.
* @return The menu bar.
*/
protected JMenuBar createMenuBar(
GUIApplicationPreferences prefs) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -