Example usage for javax.swing.text Utilities getTabbedTextOffset

List of usage examples for javax.swing.text Utilities getTabbedTextOffset

Introduction

In this page you can find the example usage for javax.swing.text Utilities getTabbedTextOffset.

Prototype

@Deprecated(since = "9")
public static final int getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e,
        int startOffset) 

Source Link

Document

Determines the relative offset into the given text that best represents the given span in the view coordinate system.

Usage

From source file:FormattedTextFieldExample.java

public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
    a = adjustAllocation(a);/*from  w ww . j  ava  2s.  c  om*/
    bias[0] = Position.Bias.Forward;

    int x = (int) fx;
    int y = (int) fy;
    Rectangle r = a.getBounds();
    int startOffset = element.getStartOffset();
    int endOffset = element.getEndOffset();

    if (y < r.y || x < r.x) {
        return startOffset;
    } else if (y > r.y + r.height || x > r.x + r.width) {
        return endOffset - 1;
    }

    // The given position is within the bounds of the view.
    int offset = Utilities.getTabbedTextOffset(contentBuff, getFontMetrics(), r.x, x, this, startOffset);
    // The offset includes characters not in the model,
    // so get rid of them to return a true model offset.
    for (int i = 0; i < offsets.length; i++) {
        if (offset <= offsets[i]) {
            offset = i;
            break;
        }
    }

    // Don't return an offset beyond the data
    // actually in the model.
    if (offset > endOffset - 1) {
        offset = endOffset - 1;
    }
    return offset;
}