Example usage for javax.swing.text Caret isSelectionVisible

List of usage examples for javax.swing.text Caret isSelectionVisible

Introduction

In this page you can find the example usage for javax.swing.text Caret isSelectionVisible.

Prototype

public boolean isSelectionVisible();

Source Link

Document

Determines if the selection is currently visible.

Usage

From source file:FormattedTextFieldExample.java

protected void drawLine(int line, Graphics g, int x, int y) {
    // Set the colors
    JTextComponent host = (JTextComponent) getContainer();
    unselected = (host.isEnabled()) ? host.getForeground() : host.getDisabledTextColor();
    Caret c = host.getCaret();
    selected = c.isSelectionVisible() ? host.getSelectedTextColor() : unselected;

    int p0 = element.getStartOffset();
    int p1 = element.getEndOffset() - 1;
    int sel0 = ((JTextComponent) getContainer()).getSelectionStart();
    int sel1 = ((JTextComponent) getContainer()).getSelectionEnd();

    try {/* w  ww .j  a  va 2  s .  c  o  m*/
        // If the element is empty or there is no selection
        // in this view, just draw the whole thing in one go.
        if (p0 == p1 || sel0 == sel1 || inView(p0, p1, sel0, sel1) == false) {
            drawUnselectedText(g, x, y, 0, contentBuff.count);
            return;
        }

        // There is a selection in this view. Draw up to three regions:
        //   (a) The unselected region before the selection.
        //   (b) The selected region.
        //   (c) The unselected region after the selection.
        // First, map the selected region offsets to be relative
        // to the start of the region and then map them to view
        // offsets so that they take into account characters not
        // present in the model.
        int mappedSel0 = mapOffset(Math.max(sel0 - p0, 0));
        int mappedSel1 = mapOffset(Math.min(sel1 - p0, p1 - p0));

        if (mappedSel0 > 0) {
            // Draw an initial unselected region
            x = drawUnselectedText(g, x, y, 0, mappedSel0);
        }
        x = drawSelectedText(g, x, y, mappedSel0, mappedSel1);

        if (mappedSel1 < contentBuff.count) {
            drawUnselectedText(g, x, y, mappedSel1, contentBuff.count);
        }
    } catch (BadLocationException e) {
        // Should not happen!
    }
}