JTextPane关键字显示的另一种方式

另一种方式处理JTextPane中的关键字显示

mport java.awt.Color;
import java.util.StringTokenizer;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
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.rtf.RTFEditorKit;

/**
 *@author gao
 */
/**
 * @author Administrator
 *
 */
/**
 * @author Administrator
 *
 */
public class CustomizeStyledDocument extends DefaultStyledDocument {
	private int type = -1;// 数据连接类型

	AttributeSet myAttributeSet = null;
	private MutableAttributeSet keyAttr, normalAttr;
	private MutableAttributeSet inputAttributes = new RTFEditorKit()
			.getInputAttributes();
	private String[] keyWord = { "int", "float" };

	public CustomizeStyledDocument(int type) {
		this.type = type;
		// 定义关键字显示属性
		keyAttr = new SimpleAttributeSet();
		StyleConstants.setForeground(keyAttr, Color.red);

		// 定义一般文本显示属性
		normalAttr = new SimpleAttributeSet();
		StyleConstants.setForeground(normalAttr, Color.black);

	}

	/**
	 *插入字符串
	 */
	public void insertString(int offset, String str, AttributeSet a)
			throws BadLocationException {
		this.myAttributeSet = a;
		super.insertString(offset, str, a);
		setSyntaxColor(offset, str.length());
	}

	/**
	 *删除字符串
	 */
	public void remove(int offs, int len) throws BadLocationException {
		super.remove(offs, len);
		Element root = getDefaultRootElement();
		int linebeg = root.getElementIndex(offs);
		dealLine(root,linebeg);
	}


	/**
	 *根据一个范围,设置该范围内的的SyntaxColor
	 */
	private void setSyntaxColor(int offset, int len)
			throws BadLocationException {
		Element root = getDefaultRootElement();

		int linebeg = root.getElementIndex(offset);
		int lineEnd = root.getElementIndex(offset + len);

		
		for(int i=linebeg;i<lineEnd+1;i++){
				dealLine(root,i);
		}

	}

	/**
	 * 处理一行中的Word
	 */
	private void dealLine(Element root,int curLine) {
		Element para = root.getElement(curLine);
		int start = para.getStartOffset();
		int end = para.getEndOffset() - 1;
		String s=null;
		
		try {
			s = getText(start, end - start);
		} catch (BadLocationException e) {
			
			e.printStackTrace();
			return;
		}
		int i = 0;
		int xStart = 0;

		// 分析关键字---
		setCharacterAttributes(start, s.length(), normalAttr, false);
		MyStringTokenizer st = new MyStringTokenizer(s);
		while (st.hasMoreTokens()) {
			s = st.nextToken();
			if (s == null)
				return;
			for (i = 0; i < keyWord.length; i++) {
				if (s.equals(keyWord[i]))
					break;
			}
			if (i >= keyWord.length)
				continue;

			xStart = st.getCurrPosition();

			// 设置关键字显示属性
			setCharacterAttributes(start + xStart, s.length(), keyAttr, false);
		}
		inputAttributes.addAttributes(normalAttr);
	}

}

/**
 * 拆分一行中的word
 *
 */
class MyStringTokenizer extends StringTokenizer {
	String sval = " ";
	String oldStr, str;
	int m_currPosition = 0, m_beginPosition = 0;

	MyStringTokenizer(String str) {
		super(str, " ");
		this.oldStr = str;
		this.str = str;
	}

	public String nextToken() {
		try {
			String s = super.nextToken();
			int pos = -1;

			if (oldStr.equals(s)) {
				return s;
			}

			pos = str.indexOf(s + sval);
			if (pos == -1) {
				pos = str.indexOf(sval + s);
				if (pos == -1)
					return null;
				else
					pos += 1;
			}

			int xBegin = pos + s.length();
			str = str.substring(xBegin);

			m_currPosition = m_beginPosition + pos;
			m_beginPosition = m_beginPosition + xBegin;
			return s;
		} catch (java.util.NoSuchElementException ex) {
			ex.printStackTrace();
			return null;
		}
	}

	// 返回token在字符串中的位置
	public int getCurrPosition() {
		return m_currPosition;
	}
}

 

   同样,使用时作为JTextPane中的Document即可

   如果要可以识别“// “种类别的注释,可以加上以下内容

	int pos = s.indexOf("//");
		if(pos != -1){		
			setCharacterAttributes(pos+start, s.length()-pos, commentiAttr, true);
			if(pos==0)
				return;
			s=s.substring(0, pos);		
		}

   其中commentiAttr为自定义的注释的风格。

    

你可能感兴趣的:(swing)