All pastes #297180 Raw Edit

Unnamed

public java v1 · immutable
#297180 ·published 2006-12-30 22:19 UTC
rendered paste body
package net.sf.xpontus.view.editor;import net.sf.xpontus.view.editor.syntax.GappedIntArray;import net.sf.xpontus.view.editor.syntax.StyleInfo;import net.sf.xpontus.view.editor.syntax.SyntaxSupport;import net.sf.xpontus.view.editor.syntax.Token;import net.sf.xpontus.view.editor.syntax.TokenMgrError;import net.sf.xpontus.view.editor.syntax.java.JavaColorProvider;import net.sf.xpontus.view.editor.syntax.java.JavaParser;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.util.ArrayList;import java.util.List;import java.util.Map;import javax.swing.text.BadLocationException;import javax.swing.text.Element;import javax.swing.text.Element;import javax.swing.text.JTextComponent;import javax.swing.text.PlainView;import javax.swing.text.PlainView;import javax.swing.text.Segment;import javax.swing.text.Utilities;/** * A Swing view implementation that colorizes lines of a SyntaxDocument using a * TokenMarker. * * This class should not be used directly; a SyntaxEditorKit should be used * instead. * * * @author Yves Zoundi * * @version $Id: SyntaxView.java,v 1.1.1.1 2005/12/08 01:34:34 yveszoundi Exp $ */public class SyntaxView2 extends PlainView {    private Segment line;    private Segment seg = new Segment();    private ILexer lexer;    private Color defaultColor = Color.BLACK;    private Map styleMap;    /**     * @param e     * @param provider     * @param lexer     */    public SyntaxView2(Element e, SyntaxSupport syntaxSupport) {        super(e);        line = new Segment();        // this.styleMap = syntaxSupport.getColorProvider().getStyles();        this.styleMap = new JavaColorProvider().getStyles();        this.lexer = new JavaParser();    }    private int drawElement(Element elem, Graphics g, int x, int y)        throws BadLocationException {        int start = elem.getStartOffset();        int end = elem.getEndOffset();        SyntaxDocument2 document = (SyntaxDocument2) getDocument();        GappedIntArray styleBuffer = document.getStyleBuffer();        document.getText(start, end - start, line);        if (!document.isTokenizing()) {            JTextComponent jtc = (JTextComponent) getContainer();            g.setFont(jtc.getFont());            g.setColor(jtc.getForeground());            return Utilities.drawTabbedText(line, x, y, g, this, start);        } else {            return drawSyntaxLine(start, end, g, x, y, document, styleBuffer);        }    }    private int drawSyntaxLine(int start, int end, Graphics g, int x, int y,        SyntaxDocument2 doc, GappedIntArray styleBuffer) {        if (start >= end) {            return x;        }        synchronized (styleBuffer) {            int styleBuffLen = styleBuffer.length();            int[] buffArray;            if (end > styleBuffLen) {                int diff = end - styleBuffLen;                int tmpLen = styleBuffLen - start;                buffArray = new int[diff + tmpLen];                int[] tmpArray = styleBuffer.get(start, tmpLen);                System.arraycopy(tmpArray, 0, buffArray, 0, tmpLen);                for (int i = 0; i < diff; i++) {                    buffArray[i + tmpLen] = SyntaxStyle.DEFAULT;                }            } else {                buffArray = styleBuffer.get(start, end - start);            }            if ((buffArray == null) || (buffArray.length == 0)) {                return x;            }            int id1 = StyleBufferUtilities.getStyle(buffArray[0]);            int offset = 0;            int buffLen = buffArray.length;            int lastElem = buffLen - 1;            for (int i = 0; i < buffLen; i++) {                int id2 = StyleBufferUtilities.getStyle(buffArray[i]);                if ((id2 != id1) || (i == lastElem)) {                    SyntaxStyle s = styleSet.getStyle(id1);                    setStyle(g, s);                    line.count = offset;                    x = Utilities.drawTabbedText(line, x, y, g, this, start +                            i);                    line.offset += offset;                    id1 = id2;                    offset = 0;                }                offset++;            }        }        return x;    }    public void applyStyle(Graphics g, int val) {        Graphics2D g2d = (Graphics2D) g;        g2d.setFont(g2d.getFont().deriveFont(Font.PLAIN));        if (styleMap.containsKey(new Integer(val))) {            StyleInfo style = (StyleInfo) styleMap.get(new Integer(val));            g2d.setColor(style.color);            if (style.bold && style.italic) {                g2d.setFont(g2d.getFont().deriveFont(Font.BOLD | Font.ITALIC));            } else if (style.bold) {                g2d.setFont(g2d.getFont().deriveFont(Font.BOLD));            } else if (style.italic) {                g2d.setFont(g2d.getFont().deriveFont(Font.ITALIC));            }        } else {            g2d.setColor(defaultColor);        }    }    /*     * (non-Javadoc)     *     * @see javax.swing.text.PlainView#drawLine(int, java.awt.Graphics, int,     *      int)     */    protected void drawLine(int lineIndex, Graphics g, int x, int y) {        Element elem = getElement().getElement(lineIndex);        try {            drawElement(elem, g, x, y);        } catch (Exception ballocks) {            return;        }    }    private int drawSyntaxLine(int start, int end, Graphics g, int x, int y,        SyntaxDocument2 doc) {        return 1;    }    /**     * @param g2d     * @param val     */    private void setFont(Graphics2D g2d, int val) {        g2d.setFont(g2d.getFont().deriveFont(Font.PLAIN));        if (styleMap.containsKey(new Integer(val))) {            StyleInfo style = (StyleInfo) styleMap.get(new Integer(val));            g2d.setColor(style.color);            if (style.bold && style.italic) {                g2d.setFont(g2d.getFont().deriveFont(Font.BOLD | Font.ITALIC));            } else if (style.bold) {                g2d.setFont(g2d.getFont().deriveFont(Font.BOLD));            } else if (style.italic) {                g2d.setFont(g2d.getFont().deriveFont(Font.ITALIC));            }        } else {            g2d.setColor(defaultColor);        }    }}