?? textfileeditor.java
字號:
package org.jr.jzj.editor;
/**
* <p>Copyright: Copyright (c) 2002-2003</p>
* <p>Company: JavaResearch(http://www.javaresearch.org)</p>
* <p>最后更新日期:2003年3月20日
* @author Barney,Cherami,Brain
* @version 0.8
*/
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.rtf.*;
import javax.swing.undo.*;
import org.jr.jzj.*;
/**
* java文件加亮的查看器,默認是java文件類型,支持顯示行號,undo等
* 支持以XML格式的語法加亮設置文件
*/
public class TextFileEditor
extends JPanel
implements UndoableEditListener, DocumentListener, CaretListener {
private static int UNDODEPTH = 100;
private static JZJLogger logger = new JZJLogger(TextFileEditor.class);
private JTextPane editor;
private Object highlight = null;
private boolean dirty;
private File file;
private UndoManager undo;
private String searchString = "";
private String replaceString = "";
private boolean ignoreCase = false;
private LineNumber lineNumber;
private JScrollPane scrollPane;
//
private HashMap typeHandlers;
private File syntaxFile = null;
private SyntaxParser parser = null;
/**
* 默認構造函數
*/
public TextFileEditor() {
super(new BorderLayout(), true);
setLayout(new BorderLayout());
editor = new JTextPane();
editor.addCaretListener(this);
scrollPane = new JScrollPane(editor);
add(scrollPane, BorderLayout.CENTER);
undo = new UndoManager();
undo.setLimit(this.UNDODEPTH);
editor.getDocument().addUndoableEditListener(this);
editor.getDocument().addDocumentListener(this);
setLineNumberVisible(false);
}
/**
* 直接打開一個文件的:構造函數
* @param f
*/
public TextFileEditor(File f) {
this();
doOpen(f);
}
/**
* 導入語法加亮,屬性文件改過之后也可用這個函數更新
* @param syntaxFile
*/
public void loadSyntax(File syntaxFile) {
this.syntaxFile = syntaxFile;
if (syntaxFile != null) {
parser = new SyntaxParser(syntaxFile);
if (typeHandlers == null) {
typeHandlers = new HashMap();
}
typeHandlers.clear();
}
}
/**
* 是否顯示行號
* @param bVisibale
*/
public void setLineNumberVisible(boolean bVisible) {
if (lineNumber == null) {
lineNumber = new LineNumber(editor);
scrollPane.setRowHeaderView(lineNumber);
}
scrollPane.getRowHeader().setVisible(bVisible);
}
public boolean isLineNumberVisible() {
return scrollPane.getRowHeader().isVisible();
}
/**
*
* @param type
* @return
*/
private EditorKit getEditorKitForContentType(String type) {
if (typeHandlers == null) {
typeHandlers = new HashMap();
}
EditorKit k = (EditorKit) typeHandlers.get(type);
if (k == null) {
k = createEditorKitForContentType(type);
if (k != null) {
typeHandlers.put(type, k);
editor.setEditorKitForContentType(type, k);
}
}
editor.setContentType(type);
return k;
}
/**
*
* @param type
* @return
*/
protected EditorKit createEditorKitForContentType(String type) {
if (parser != null) {
return createEditorKitForContentTypeX(type);
}
if (type.equals("text/java")) {
return (new SyntaxEditorKit());
}
else if (type.equals("text/plain")) {
return (new StyledEditorKit());
}
else if (type.equals("text/html")) {
return (new HTMLEditorKit());
}
else if (type.equals("text/rtf")) {
return (new RTFEditorKit());
}
else if (type.equals("text/cpp")) {
return (new SyntaxEditorKit());
}
else if (type.equals("text/unknown")) {
return (new StyledEditorKit());
}
else {
return editor.getEditorKitForContentType(type);
}
}
/**
*
* @param type
* @return
*/
protected EditorKit createEditorKitForContentTypeX(String type) {
if (type.equals("text/plain")) {
return (new StyledEditorKit());
}
else if (type.equals("text/html")) {
return (new HTMLEditorKit());
}
else if (type.equals("text/rtf")) {
return (new RTFEditorKit());
}
else {
EditorKit et = SyntaxEditorKit.newInstance(type, parser);
if (et != null) {
return et;
}
else {
return editor.getEditorKitForContentType(type);
}
}
}
protected void selectEditorKit(String filename) {
if (filename.endsWith(".java")) {
getEditorKitForContentType("text/java");
}
else if (filename.endsWith(".cpp") || filename.endsWith(".c")
|| filename.endsWith(".hpp") || filename.endsWith(".h")) {
getEditorKitForContentType("text/cpp");
}
else if (filename.endsWith(".html") || filename.endsWith(".htm")) {
getEditorKitForContentType("text/html");
}
else if (filename.endsWith(".rtf")) {
getEditorKitForContentType("text/rtf");
}
else {
getEditorKitForContentType("text/unknown");
}
}
/**
*
* @param f
*/
public void doOpen(File f) {
file = f;
if (f.getName().endsWith(".html") || f.getName().endsWith(".htm")) {
try {
editor.setPage(org.jr.io.FileUtil.getURL(f));
return;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
selectEditorKit(f.getName());
doOpen();
}
private void doOpen() {
try {
FileReader reader = new FileReader(file);
editor.read(reader, file.getAbsolutePath());
reader.close();
dirty = false;
}
catch (IOException ex) {
ex.printStackTrace();
logger.error(ex.toString());
}
}
public void doSave() {
if (file == null) {
return;
}
/**
* 保存
*/
try {
FileWriter writer = new FileWriter(file);
editor.write(writer);
writer.close();
dirty = false;
}
catch (IOException ex) {
logger.error(ex.toString());
}
}
public File getFile() {
return file;
}
public final JTextPane getEditor() {
return editor;
}
public void removeUpdate(DocumentEvent ev) {
dirty = true;
}
public void changedUpdate(DocumentEvent ev) {
}
public void insertUpdate(DocumentEvent ev) {
dirty = true;
}
public boolean getDirty() {
return dirty;
}
public void doUndo() {
if (undo.canUndo()) {
undo.undo();
}
}
public void doRedo() {
if (undo.canRedo()) {
undo.redo();
}
}
public void caretUpdate(CaretEvent ev) {
Highlighter highlighter = editor.getHighlighter();
if (highlight != null) {
highlighter.removeHighlight(highlight);
}
try {
int offset = findMatchingBracket(editor.getDocument(), ev.getDot() - 1);
if (offset != -1) {
highlight = highlighter.addHighlight(offset, offset + 1,
new DefaultHighlighter.
DefaultHighlightPainter(Color.cyan));
}
}
catch (BadLocationException ex) {
}
}
public void undoableEditHappened(UndoableEditEvent ev) {
undo.addEdit(ev.getEdit());
}
public String getSearchString() {
return searchString;
}
public void setSearchString(String s) {
searchString = s;
}
public String getReplaceString() {
return replaceString;
}
public void setReplaceString(String s) {
replaceString = s;
}
public boolean getIgnoreCase() {
return ignoreCase;
}
public void setIgnoreCase(boolean b) {
ignoreCase = b;
}
public void doSearch(String search, boolean ignore) {
if (search.length() > 0) {
setSearchString(search);
setIgnoreCase(ignore);
int length = editor.getDocument().getLength();
int position = editor.getCaretPosition();
try {
String contents = editor.getText(0, length);
if (ignoreCase) {
contents = contents.toLowerCase();
}
int found = contents.indexOf(search, position);
if (found != -1) {
editor.setCaretPosition(found);
editor.setSelectionStart(found);
editor.setSelectionEnd(found + search.length());
}
else {
Toolkit.getDefaultToolkit().beep();
}
}
catch (BadLocationException ex) {
}
}
}
public void doReplace(String replace) {
if (replace.length() > 0) {
setReplaceString(replace);
int start = editor.getSelectionStart();
int end = editor.getSelectionEnd();
if ( (start == 0 && end == 0) || (searchString.length() == 0) ||
(replaceString.length() == 0)) {
getToolkit().beep();
}
else {
try {
editor.getDocument().remove(start, end - start);
editor.getDocument().insertString(start, replaceString, null);
}
catch (BadLocationException ex) {
// MessageFrame.setErrorText(ex.toString());
}
}
}
}
public int findMatchingBracket(Document doc, int offset) throws
BadLocationException {
int toReturn = -1;
if (doc.getLength() == 0) {
return toReturn;
}
char c = doc.getText(offset, 1).charAt(0);
switch (c) {
case '(':
toReturn = searchForward(c, ')', offset);
break;
case '[':
toReturn = searchForward(c, ']', offset);
break;
case '{':
toReturn = searchForward(c, '}', offset);
break;
case ')':
toReturn = searchBackward(c, '(', offset);
break;
case ']':
toReturn = searchBackward(c, '[', offset);
break;
case '}':
toReturn = searchBackward(c, '{', offset);
break;
}
return toReturn;
}
private int searchForward(char c, char d, int offset) throws
BadLocationException {
int count = 1;
offset++;
int len = editor.getDocument().getLength() - offset;
String text = editor.getDocument().getText(offset, len);
for (int i = 0; i < len; i++) {
char x = text.charAt(i);
if (x == c) {
count++;
}
else if (x == d) {
if (--count == 0) {
return i + offset;
}
}
}
return -1;
}
private int searchBackward(char c, char d, int offset) throws
BadLocationException {
int count = 1;
String text = editor.getDocument().getText(0, offset);
for (int i = offset - 1; i >= 0; i--) {
char x = text.charAt(i);
if (x == c) {
count++;
}
else if (x == d) {
if (--count == 0) {
return i;
}
}
}
return -1;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -