📄 javatexteditor.java
字号:
/**
* (c) 2007 UCS_2008
*
* Project UCS (Ultra Corba Simulator)
* Subproject CorbaMNQ
* File JavaTextEditor.java
* Created on Sep 13, 2007 by ucs_2008
*
* History:
* Date(Y.M.D) User Reason (plus CR, LM, Fault number)
*
*/
package com.corba.mnq.ui.text.java;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JEditorPane;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.Element;
import javax.swing.text.JTextComponent;
import javax.swing.text.Keymap;
import java.util.StringTokenizer;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* name: "JavaTextEditor"
*
* @author ucs_2008
*/
public class JavaTextEditor extends JEditorPane {
static final long serialVersionUID = -5737922218642574020L;
public static final String CHAR_LITERAL_COLOR = "ucs.char_literal_color";
public static final String COMMON_WORD_COLOR = "ucs.common_word_color";
public static final String INDENT_ON_INSERT_BREAK = "indentOnInsertBreak";
public static final String KEYWORD_COLOR = "ucs.keyword_color";
/** used for java.util.logging */
private static final Logger LOG = Logger.getLogger(JavaTextEditor.class.getName());
public static final String MULTI_LINE_COMMENT_COLOR = "ucs.multi_line_color";
public static final String NUMERIC_LITERAL_COLOR = "ucs.numeric_literal_color";
public static final String OPERATOR_COLOR = "ucs.operator_color";
public static final String SINGLE_LINE_COMMENT_COLOR = "ucs.single_line_color";
public static final String STRING_LITERAL_COLOR = "ucs.string_literal_color";
public static final String TAB_SIZE = " ";
private Document doc = null;
protected KeyListener keyListener;
/**
* This is one class constructor
*/
public JavaTextEditor() {
try {
doc = getDocument();
setupKeymap();
setOpaque(true);
setBackground(new Color(231, 224, 241));
// setForeground(Color.darkGray);
setEditable(true);
setIndentOnBreak(true);
setKeywordColor(Color.blue);
setCommonWordColor(Color.black);
setCharLiteralColor(new Color(83, 114, 238));
setMultiLineCommentColor(new Color(0, 200, 0));
setNumericLiteralColor(new Color(255, 128, 64));
setSingleLineCommentColor(new Color(0, 200, 0));
setStringLiteralColor(new Color(83, 114, 238));
setOperatorColor(new Color(223, 72, 227));
setDragEnabled(true);
} catch (Exception ex) {
LOG.severe(ex.getMessage());
}
setOpaque(true);
keyListener = new KeyListener();
addKeyListener(keyListener);
}
public void clear() {
try {
doc.remove(0, doc.getLength());
} catch (Exception e) {
LOG.severe(e.getMessage());
}
}
protected EditorKit createDefaultEditorKit() {
return new JavaEditorKit();
}
public Color getCharLiteralColor() {
return (Color) getClientProperty(CHAR_LITERAL_COLOR);
}
public Color getCommonWordColor() {
return (Color) getClientProperty(COMMON_WORD_COLOR);
}
private Element getCurrentRow() {
return getRowAt(getCaretPosition());
}
public boolean getIndentOnBreak() {
Boolean b = (Boolean) getClientProperty(INDENT_ON_INSERT_BREAK);
if (b != null)
return false;
else
return b.booleanValue();
}
public Color getKeywordColor() {
return (Color) getClientProperty(KEYWORD_COLOR);
}
private String getLastToken(String rowSegment) throws BadLocationException {
int slashSlashIndex = rowSegment.indexOf("//");
int slashStarIndex = rowSegment.indexOf("/*");
int starSlashIndex = rowSegment.indexOf("*/");
if (slashSlashIndex != -1) {
rowSegment = rowSegment.substring(0, slashSlashIndex);
} else if (slashStarIndex != -1) {
try {
rowSegment = rowSegment.substring(0, slashStarIndex)
+ rowSegment.substring(starSlashIndex, rowSegment.length());
} catch (StringIndexOutOfBoundsException exc) {
rowSegment = rowSegment.substring(0, slashStarIndex);
}
}
StringTokenizer st = new StringTokenizer(rowSegment, "; ", false);
String lastToken = "";
if (st.hasMoreTokens()) {
lastToken = st.nextToken();
}
System.out.println("last token=" + lastToken);
return lastToken;
}
public Color getMultiLineCommentColor() {
return (Color) getClientProperty(MULTI_LINE_COMMENT_COLOR);
}
public Color getNumericLiteralColor() {
return (Color) getClientProperty(NUMERIC_LITERAL_COLOR);
}
public Color getOperatorColor() {
return (Color) getClientProperty(OPERATOR_COLOR);
}
private Element getRowAt(int offset) {
Element element = getDocument().getDefaultRootElement();
int rowNumber = element.getElementIndex(offset);
return element.getElement(rowNumber);
}
public Color getSingleLineCommentColor() {
return (Color) getClientProperty(SINGLE_LINE_COMMENT_COLOR);
}
public Color getStringLiteralColor() {
return (Color) getClientProperty(STRING_LITERAL_COLOR);
}
public int getWindowsReturnGap(String content, int end) {
String part = content.substring(0, end);
Pattern partPattern = Pattern.compile("\n");
Matcher partMatcher = partPattern.matcher(part);
int count = 0;
while (partMatcher.find()) {
count++;
}
return count;
}
public void setCharLiteralColor(Color c) {
putClientProperty(CHAR_LITERAL_COLOR, c);
}
public void setCommonWordColor(Color c) {
putClientProperty(COMMON_WORD_COLOR, c);
}
public void setIndentOnBreak(boolean b) {
putClientProperty(INDENT_ON_INSERT_BREAK, new Boolean(b));
}
public void setKeywordColor(Color c) {
putClientProperty(KEYWORD_COLOR, c);
}
public void setMultiLineCommentColor(Color c) {
putClientProperty(MULTI_LINE_COMMENT_COLOR, c);
}
public void setNumericLiteralColor(Color c) {
putClientProperty(NUMERIC_LITERAL_COLOR, c);
}
public void setOperatorColor(Color c) {
putClientProperty(OPERATOR_COLOR, c);
}
public void setSingleLineCommentColor(Color c) {
putClientProperty(SINGLE_LINE_COMMENT_COLOR, c);
}
public void setStringLiteralColor(Color c) {
putClientProperty(STRING_LITERAL_COLOR, c);
}
public void showJava(String idl) {
try {
doc.remove(0, doc.getLength());
doc.insertString(0, idl, null);
} catch (Exception e) {
LOG.severe(e.getMessage());
}
int pos = idl.indexOf(" // ------------------End----------------------");
if (pos < 1)
pos = 1;
setCaretPosition(pos - 1);
setCaretColor(Color.RED);
getCaret().setVisible(true);
requestFocus();
}
private void setupKeymap() {
Keymap map = JTextComponent.getKeymap("UCSKeyMap");
if (map == null) {
Keymap parent = getKeymap();
map = JTextComponent.addKeymap("UCSKeyMap", parent);
KeyStroke insertBreakKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
map.addActionForKeyStroke(insertBreakKeyStroke, new JavaEditorKit.InsertBreakAction());
}
setKeymap(map);
}
class KeyListener extends KeyAdapter {
private int caretPosition;
public void keyPressed(KeyEvent e) {
char keyChar = e.getKeyChar();
switch (keyChar) {
case '.':
// fireDotPressed(e);
break;
case '\n':
// fireReturnPressed(e);
break;
case '\t':
e.consume();
fireTabPressed();
default:
// nothing to do
}
}
private void fireTabPressed() {
try {
Document doc = JavaTextEditor.this.getDocument();
int caretPosition = JavaTextEditor.this.getCaretPosition();
doc.insertString(caretPosition, JavaTextEditor.TAB_SIZE, null);
} catch (BadLocationException exc) {
System.out.println(exc.offsetRequested());
exc.printStackTrace();
}
}
}
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -