All pastes #296058 Raw Edit

Miscellany

public java v1 · immutable
#296058 ·published 2006-12-29 22:43 UTC
rendered paste body
/* * SyntaxDocument2.java * * Created on December 29, 2006, 5:19 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package net.sf.xpontus.view.editor;import java.io.StringReader;import java.util.List;import java.util.Vector;import javax.swing.event.DocumentEvent;import javax.swing.text.AttributeSet;import javax.swing.text.Element;import javax.swing.text.GapContent;import javax.swing.text.MutableAttributeSet;import javax.swing.text.PlainDocument;import javax.swing.text.SimpleAttributeSet;import net.sf.xpontus.view.editor.syntax.FastCharStream;import net.sf.xpontus.view.editor.syntax.Token;import net.sf.xpontus.view.editor.syntax.java.JavaParser;/** *  * @author mrcheeks */public class SyntaxDocument2 extends PlainDocument {	private ILexer lexer = new JavaParser();	/** Creates a new instance of SyntaxDocument2 */	public SyntaxDocument2() {		super(new GapContent());	}	public void relex(DocumentEvent e) {		int offset = e.getOffset();		Element root = this.getDefaultRootElement();		int lineIndex = root.getElementIndex(offset);		Element lineElement = root.getElement(lineIndex);		List tokenList = new Vector();		AttributeSet object = lineElement.getAttributes();		MutableAttributeSet lineAttributes = null;		if (object instanceof MutableAttributeSet) {			lineAttributes = (MutableAttributeSet) object;		} else {			lineAttributes = new SimpleAttributeSet(object);		}		try {			int start = lineElement.getStartOffset();			int end = lineElement.getEndOffset();			String lineText = getText(start, end);			lexer.ReInit(new FastCharStream(new StringReader(lineText)));			Token token = null;			while ((token = lexer.getNextToken()) != null && token.kind != 0) {				tokenList.add(token);			}		} catch (Exception e1) {			e1.printStackTrace();		}		lineAttributes.addAttribute("tokenList", tokenList);	}	protected void fireInsertUpdate(DocumentEvent e) {		super.fireChangedUpdate(e);		relex(e);	}	protected void fireRemoveUpdate(DocumentEvent e) {		super.fireRemoveUpdate(e);		relex(e);	}}