您的位置:首页 > 编程语言 > PHP开发

SWT JTextPane 中文以及ASCII以外的字颜色高亮

2018-01-09 12:20 489 查看
package com.hsbc.automation;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

protected Shell shell;

/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Test window = new Test();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(606, 372);
shell.setText("SWT Application");

Composite comp = new Composite(shell, SWT.EMBEDDED);
comp.setSize(590, 334);
comp.setLocation(0, 0);

final java.awt.Frame frame = SWT_AWT.new_Frame(comp);
frame.setLayout(null);
final MyTextPane editorPane = new MyTextPane();
editorPane.setSize(590, 334);
editorPane.setLocation(0, 0);

frame.add(editorPane);
}
}


package com.hsbc.automation;

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.rtf.RTFEditorKit;

public class MyTextPane extends JTextPane {

private static final long serialVersionUID = -8222291704834840091L;
protected StyleContext m_context;
protected DefaultStyledDocument m_doc;
private MutableAttributeSet keyAttr, normalAttr;
private MutableAttributeSet inputAttributes = new RTFEditorKit().getInputAttributes();

public MyTextPane() {
super();
m_context = new StyleContext();
m_doc = new DefaultStyledDocument(m_context);
this.setDocument(m_doc);

this.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
syntaxParse();
}
});

keyAttr = new SimpleAttributeSet();
StyleConstants.setForeground(keyAttr, Color.red);

normalAttr = new SimpleAttributeSet();
StyleConstants.setForeground(normalAttr, Color.black);
}

public void syntaxParse() {
try {
String s = null;
Element root = m_doc.getDefaultRootElement();

int cursorPos = this.getCaretPosition();
int line = root.getElementIndex(cursorPos);

Element para = root.getElement(line);
int start = para.getStartOffset();
int end = para.getEndOffset() - 1;
s = m_doc.getText(start, end - start);

m_doc.setCharacterAttributes(start, s.length(), normalAttr, false);
boolean flage = false;
boolean flage2 = false;
boolean firstFlage = true;
for (int j = 0; j < s.length(); j++) {
String simpleStr = s.substring(j, j+1);
if (!isASCII(simpleStr)) {
m_doc.setCharacterAttributes(start+j, 1, keyAttr, false);
}
if (simpleStr.equals("&")) {
flage = true;
continue;
}
if (flage) {
if (simpleStr.equals("#")) {
flage2 = true;
continue;
}
}

if (flage2) {
if (isNumber(simpleStr)) {
if (firstFlage) {
m_doc.setCharacterAttributes(start+j-2, 3, keyAttr, false);
firstFlage = false;
}else {
m_doc.setCharacterAttributes(start+j, 1, keyAttr, false);
}
continue;
}
}
flage = false;
flage2 = false;
firstFlage = true;
}

inputAttributes.addAttributes(normalAttr);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static boolean isASCII(int c) {
return c>= 0 && c < 128;
}

public static boolean isASCII(String str) {
if (str == null) {
return false;
}
if (str.length() == 1) {
return isASCII(str.charAt(0));
}
return false;
}

public static boolean isNumber(int c) {
return c>= 48 && c <= 57;
}

public static boolean isNumber(String str) {
if (str == null) {
return false;
}
if (str.length() == 1) {
return isNumber(str.charAt(0));
}
return false;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SWT JTextPane
相关文章推荐