myeditorview.java

来自「一个C语言子集的编译器」· Java 代码 · 共 63 行

JAVA
63
字号
package com.king4solomon.homework.compiler.gui;

import java.awt.*;
import javax.swing.text.*;

import com.king4solomon.homework.compiler.core.*;

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;

		doc.getText(p0, count, segment);
		for (int i = 0; i < count; i++) {
			if (Character.isLetter(segment.array[segment.offset + i])) {
				index = i;
				while (++i < count && Character.isLetter(segment.array[segment.offset + i]))
					;
				doc.getText(p0 + index, (i--) - index, token);
				if (Keyword.isKeyword1(token)) {
					g.setFont(KEYWORD1FONT);
					g.setColor(KEYWORD1COLOR);
				} else if(Keyword.isKeyword2(token)){
					g.setFont(KEYWORD2FONT);
					g.setColor(KEYWORD2COLOR);
				} else {
					g.setFont(TEXTFONT);
					g.setColor(TEXTCOLOR);
				}
				x = Utilities.drawTabbedText(token, x, y, g, this, p0 + index);
				continue;
			} else {
				index = i;
				while (++i < count && !Character.isLetter(segment.array[segment.offset + i]))
					;
				doc.getText(p0 + index, (i--) - index, token);
				g.setFont(TEXTFONT);
				g.setColor(TEXTCOLOR);
				x = Utilities.drawTabbedText(token, x, y, g, this, p0 + index);
				continue;
			}
		}
		return 0;
	}

	public static Font TEXTFONT = new Font("DialogInput", Font.HANGING_BASELINE, 13);

	public static Color TEXTCOLOR = Color.BLACK;

	public static Font KEYWORD1FONT = new Font("Courier   New1", Font.BOLD, 13);

	public static Color KEYWORD1COLOR = Color.BLUE;
	
	public static Font KEYWORD2FONT = new Font("Courier   New2", Font.BOLD, 13);

	public static Color KEYWORD2COLOR = Color.GRAY;
}

⌨️ 快捷键说明

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