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;
}
}
相关文章推荐
- JEditorPane/JTextPane常见操作(字体,颜色,字号,加粗,斜线,下划线,保存,行间距 等操作)以及HTML空格处理
- label中显示不同颜色的字以及不同字体,字体高亮,DIY label
- HTML5之placeholder属性以及如何更改placeholder属性中文字颜色
- Ubuntu——修正LibreOffice中文乱码以及eclipse提示框颜色
- Cygwin中文乱码vim语法高亮ls颜色显示
- Ubuntu——修正LibreOffice中文乱码以及eclipse提示框颜色
- (七)lucene之中文检索和高亮显示以及摘要
- php将字符串转为ASCII,php将中文汉字字符串转为ASCII,以及互转
- xcode 代码颜色高亮,智能提示的问题以及解决方法
- label中显示不同颜色的字以及不同字体,字体高亮
- Lucene 3.6.1:中文分词、创建索引库、排序、多字段分页查询以及高亮显示
- Lucene中文分词以及关键字的高亮
- label中显示不同颜色的字以及不同字体,字体高亮,DIY label
- Eclipse 变量点击高亮显示以及自定义高亮显示颜色
- jsp向mysql数据库插入中文字段 乱码以及中文空白的解决办法
- Ruby on Rails实现文件的下载以及中文文件名乱码问题
- Linux下不同颜色命de文件类型 以及重要de配置文件
- Php发送请求参数丢失问题以及中文乱码问题
- 不要再吹水地球人听不懂的技术,咱来点干货!--中文前端UI框架Kit(四)给你真正的原生的drag以及drop事件,而不是像市面上主流的Js框架jQuery,Ext等,利用mousemove事件,判断位置来模拟
- vim 解决中文乱码,设置高亮,共享粘贴板