Example usage for javax.swing.text View modelToView

List of usage examples for javax.swing.text View modelToView

Introduction

In this page you can find the example usage for javax.swing.text View modelToView.

Prototype

public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a)
        throws BadLocationException 

Source Link

Document

Provides a mapping, for a given region, from the document model coordinate space to the view coordinate space.

Usage

From source file:Main.java

@Override
public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
    g.setColor(color == null ? c.getSelectionColor() : color);
    Rectangle rect = null;//from  w  w  w .  ja  va2s.c om
    if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset()) {
        if (bounds instanceof Rectangle) {
            rect = (Rectangle) bounds;
        } else {
            rect = bounds.getBounds();
        }
    } else {
        try {
            Shape shape = view.modelToView(offs0, Position.Bias.Forward, offs1, Position.Bias.Backward, bounds);
            rect = (shape instanceof Rectangle) ? (Rectangle) shape : shape.getBounds();
        } catch (BadLocationException e) {
            return null;
        }
    }
    FontMetrics fm = c.getFontMetrics(c.getFont());
    int baseline = rect.y + rect.height - fm.getDescent() + 1;
    g.drawLine(rect.x, baseline, rect.x + rect.width, baseline);
    g.drawLine(rect.x, baseline + 1, rect.x + rect.width, baseline + 1);
    return rect;
}