📄 myeditorkit.java
字号:
/**
*
*/
package flow.graph.gui.syntax;
import java.awt.*;
import java.awt.event.*;
import java.lang.StringBuffer;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class MyEditorKit extends StyledEditorKit{
public MyEditorKit(){
super();
}
public ViewFactory getViewFactory(){
return new MyViewFactory();
}
}
class MyViewFactory implements ViewFactory{
public MyViewFactory(){
}
public View create(Element element){
return new MyEditorView(element);
}
}
class MyEditorView extends PlainView{
public MyEditorView(Element element){
super(element);
}
protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException{
Document doc=getDocument();
Segment segment=new Segment(), token=new Segment();
int index=0, count=p1-p0;
char c='\u0000';
doc.getText(p0, count, segment);
for(int i=0; i<count; i++){
//System.out.println("ccc="+c);
if(Character.isLetter(c=segment.array[segment.offset+i])||c=='_'){ // :)
//System.out.println("1-------");
index=i;
while(++i<count&&(Character.isLetter((c=segment.array[segment.offset+i]))||c=='_'||(c>='0'&&c<='9')));
doc.getText(p0+index, (i--)-index, token);
if(KeyWord.isKeyWord(token)){
//g.setFont(KEYWORDFONT);
g.setColor(KEYWORDCOLOR);
}else{
int len = KeyWord.isOtherKeyWord(token);
if(len > 0){
g.setColor(KEYWORDCOLOR);
}
else{
//g.setFont(TEXTFONT);
g.setColor(TEXTCOLOR);
}
}
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
continue;
}else if(c>='0'&&c<='9'){
//System.out.println("2-------");
index=i;
while(++i<count&&(c=segment.array[segment.offset+i])>='0'&&c<='9');
doc.getText(p0+index, (i--)-index, token);
g.setColor(NUMCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
continue;
}else if(c == '-'){
//System.out.println("3-------");
index=i;
if((i+1)<count&&segment.array[segment.offset+(i+1)]=='-'){
doc.getText(p0+index, count-index, token);
//g.setFont(COMMENTFONT);
g.setColor(COMMENTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
break;
}
doc.getText(p0+index, 1, token);
//g.setFont(TEXTFONT);
g.setColor(TEXTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
//i--;
continue;
}else if(c=='/'){
//System.out.println("4-------");
index=i;
if((i+1)<count&&segment.array[segment.offset+(i+1)]=='/'){
c = segment.array[segment.offset+(i+1)];
//System.out.println("4.1.......");
doc.getText(p0+index, count-index, token);
//g.setFont(COMMENTFONT);
g.setColor(COMMENTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
break;
}
else if((i+1)<count&&segment.array[segment.offset+(i+1)]=='*'){
c = segment.array[segment.offset+(i+1)];
//System.out.println("4.2.......");
doc.getText(p0+index, count-index, token);
//g.setFont(COMMENTFONT);
g.setColor(COMMENTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
break;
}
else{
c = segment.array[segment.offset+(i+1)];
//System.out.println("4.3........");
doc.getText(p0+index, count-index, token);
g.setColor(TEXTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
break;
}
/*
doc.getText(p0+index, 1, token);
//g.setFont(TEXTFONT);
g.setColor(TEXTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
//i--;
continue;*/
}else if(c=='\''||c=='\"'){
//System.out.println("5-------");
index=i;
char ch='\u0000';
while(++i<count){
if((ch=segment.array[segment.offset+i])=='\\'){
i++;
continue;
}else if(ch==c) break;
}
if(i>=count) i=count-1;
doc.getText(p0+index, i-index+1, token);
//g.setFont(STRINGFONT);
g.setColor(STRINGCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
continue;
}
else{
//System.out.println("6-------");
index=i;
while(++i<count&&!Character.isLetter((c=segment.array[segment.offset+i]))&&c!='_'&&c!='/'&&c!='\''&&c!='\"'&&c==' '&&(c<'0'||c>'9'));
doc.getText(p0+index, (i--)-index, token);
//g.setFont(TEXTFONT);
g.setColor(TEXTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
}
}
return x;
}
protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException{
//g.setFont(TEXTFONT);
//g.setColor(TEXTCOLOR);
return super.drawSelectedText(g, x, y, p0, p1);
}
public static Font TEXTFONT=new Font("宋体", Font.PLAIN, 12);
public static Color TEXTCOLOR=Color.black;
public static Font NUMFONT=new Font("宋体", Font.PLAIN, 12);
public static Color NUMCOLOR=Color.red;
public static Font KEYWORDFONT=new Font(TEXTFONT.getFontName(), Font.BOLD, TEXTFONT.getSize());
public static Color KEYWORDCOLOR=Color.blue;//new Color(0, 0, 128);
public static Font COMMENTFONT=TEXTFONT;
//public static Color COMMENTCOLOR=new Color(192, 192, 192);
public static Color COMMENTCOLOR=new Color(71, 179, 40);
public static Font STRINGFONT=TEXTFONT;
public static Color STRINGCOLOR=Color.GRAY;//new Color(255, 0, 0);
}
class KeyWord{
public KeyWord(){
}
public static boolean isKeyWord(Segment seg){
boolean isKey=false;
for(int i=0; !isKey&&i<KEYWORDS.length; i++){
if(seg.count==KEYWORDS[i].length()){
isKey=true;
for(int j=0; isKey&&j<seg.count; j++)
if(seg.array[seg.offset+j]!=KEYWORDS[i].charAt(j))
isKey=false;
}
}
return isKey;
}
public static int isOtherKeyWord(Segment seg){
for(int i=0; i<KEYWORDS1.length; i++){
if(KEYWORDS1[i].startsWith(seg.toString())){
return KEYWORDS1[i].length()-1;
}
}
return 0;
}
public static final String[] KEYWORDS={
"if", "then", "else", "end", "elseif",
"while", "for",
"true", "false", "return", "local", "break", "function"
};
public static final String[] KEYWORDS1={
"regexp(", "gettimeout(", "exit(", "shortmsg.text)",
"shortmsg.serviceid)", "shortmsg.date)", "shortmsg.port)", "shortmsg.mobile)", "print("
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -