Example usage for javax.swing.text BadLocationException offsetRequested

List of usage examples for javax.swing.text BadLocationException offsetRequested

Introduction

In this page you can find the example usage for javax.swing.text BadLocationException offsetRequested.

Prototype

public int offsetRequested() 

Source Link

Document

Returns the offset into the document that was not legal.

Usage

From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxView.java

@Override
protected int drawUnselectedText(Graphics graphics, int x, int y, int p0, int p1) {
    Font saveFont = graphics.getFont();
    Color saveColor = graphics.getColor();
    SyntaxDocument doc = (SyntaxDocument) getDocument();
    Segment segment = getLineBuffer();
    try {/*from  www .jav  a 2s . c o  m*/
        // Colour the parts
        Iterator<Token> tokens = doc.getTokens(p0, p1);
        int start = p0;
        while (tokens.hasNext()) {
            Token t = tokens.next();
            // if there is a gap between the next token start and where we
            // should be starting (spaces not returned in tokens), then draw
            // it in the default type
            if (start < t.start) {
                SyntaxStyles.getInstance().setGraphicsStyle(graphics, TokenType.DEFAULT);
                doc.getText(start, t.start - start, segment);
                x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
            }
            // t and s are the actual start and length of what we should
            // put on the screen.  assume these are the whole token....
            int l = t.length;
            int s = t.start;
            // ... unless the token starts before p0:
            if (s < p0) {
                // token is before what is requested. adgust the length and s
                l -= (p0 - s);
                s = p0;
            }
            // if token end (s + l is still the token end pos) is greater 
            // than p1, then just put up to p1
            if (s + l > p1) {
                l = p1 - s;
            }
            doc.getText(s, l, segment);
            x = SyntaxStyles.getInstance().drawText(segment, x, y, graphics, this, t);
            start = t.start + t.length;
        }
        // now for any remaining text not tokenized:
        if (start < p1) {
            SyntaxStyles.getInstance().setGraphicsStyle(graphics, TokenType.DEFAULT);
            doc.getText(start, p1 - start, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
        }
    } catch (BadLocationException e) {
        System.err.println("Requested: " + e.offsetRequested());
        LOG.error(e.getMessage(), e);
    } finally {
        graphics.setFont(saveFont);
        graphics.setColor(saveColor);
    }

    return x;
}