Java Code Examples for java.awt.font.TextHitInfo#getInsertionIndex()

The following examples show how to use java.awt.font.TextHitInfo#getInsertionIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextBoxView.java    From SwingBox with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public int viewToModel(float x, float y, Shape a, Bias[] biasReturn)
{
    Rectangle alloc = toRect(a);
    // Move the y co-ord of the hit onto the baseline. This is because
    // TextLayout supports
    // italic carets and we do not.
    TextLayout layout = getTextLayout();
    TextHitInfo hit = layout.hitTestChar(x - (float) alloc.getX(), 0f);
    // TextHitInfo hit = layout.hitTestChar(x - box.getAbsoluteContentX(),
    // 0f);
    int pos = hit.getInsertionIndex();
    biasReturn[0] = hit.isLeadingEdge() ? Position.Bias.Forward
            : Position.Bias.Backward;
    return pos + getStartOffset();
}
 
Example 2
Source File: JTextComponent.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 3
Source File: JTextComponent.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 4
Source File: ZyCaret.java    From binnavi with Apache License 2.0 5 votes vote down vote up
private int calcHitPosition(final int caretPosition, final double x, final double y,
    final double zoomFactor) {
  boolean switched = false;

  int lp = m_mouse_pressed_y;
  int lr = m_mouse_released_y;

  if (lp > lr) {
    lp = m_mouse_released_y;
    lr = m_mouse_pressed_y;

    switched = true;
  }

  final int linecount = m_content.getLineCount();
  final double height = (float) m_content.getLineHeight();

  int maxIndex = caretPosition;
  for (int line = lp; line <= lr; line++) {
    double deltaY = 0;
    if (switched) {
      deltaY = height * zoomFactor * line;
    } else {
      deltaY = -(height * zoomFactor * (linecount - line));
    }

    final TextLayout textLayout = m_content.getLineContent(line).getTextLayout();
    final TextHitInfo hitInfo =
        textLayout.hitTestChar((float) x, (float) (y + deltaY), textLayout.getBounds());
    final int insertionIndex = hitInfo.getInsertionIndex();

    if ((caretPosition < insertionIndex) && (insertionIndex > maxIndex)) {
      maxIndex = insertionIndex;
    }
  }

  return maxIndex;
}
 
Example 5
Source File: ZyCaret.java    From binnavi with Apache License 2.0 5 votes vote down vote up
private int calcCaretPosition(final int hitLine, final double x, final double y) {
  final ZyLineContent content = m_content.getLineContent(hitLine);

  final TextLayout textlayout = m_content.getLineContent(hitLine).getTextLayout();
  final TextHitInfo hitInfo = textlayout.hitTestChar((float) x, (float) y, content.getBounds());
  return hitInfo.getInsertionIndex();
}
 
Example 6
Source File: JTextComponent.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 7
Source File: JTextComponent.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 8
Source File: JTextComponent.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 9
Source File: JTextComponent.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 10
Source File: DesktopEditorImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(@Nonnull InputMethodEvent e) {
  if (composedText != null) {
    int dot = composedTextRange.getStartOffset();

    TextHitInfo caretPos = e.getCaret();
    if (caretPos != null) {
      dot += caretPos.getInsertionIndex();
    }

    getCaretModel().moveToOffset(dot);
    getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
  }
}
 
Example 11
Source File: JTextComponent.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 12
Source File: JTextComponent.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 13
Source File: JTextComponent.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 14
Source File: JTextComponent.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 15
Source File: HighlightsViewUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static int viewToIndex(TextLayout textLayout, double x, Shape alloc, Position.Bias[] biasReturn) {
    Rectangle2D bounds = ViewUtils.shapeAsRect(alloc);
    TextHitInfo hitInfo = x2Index(textLayout, (float)(x - bounds.getX()));
    if (biasReturn != null) {
        biasReturn[0] = hitInfo.isLeadingEdge() ? Position.Bias.Forward : Position.Bias.Backward;
    }
    return hitInfo.getInsertionIndex();
}
 
Example 16
Source File: JTextComponent.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 17
Source File: JTextComponent.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 18
Source File: JTextComponent.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 19
Source File: JTextComponent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}
 
Example 20
Source File: JTextComponent.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setInputMethodCaretPosition(InputMethodEvent e) {
    int dot;

    if (composedTextExists()) {
        dot = composedTextStart.getOffset();
        if (!(caret instanceof ComposedTextCaret)) {
            if (composedTextCaret == null) {
                composedTextCaret = new ComposedTextCaret();
            }
            originalCaret = caret;
            // Sets composed text caret
            exchangeCaret(originalCaret, composedTextCaret);
        }

        TextHitInfo caretPos = e.getCaret();
        if (caretPos != null) {
            int index = caretPos.getInsertionIndex();
            dot += index;
            if (index == 0) {
                // Scroll the component if needed so that the composed text
                // becomes visible.
                try {
                    Rectangle d = modelToView(dot);
                    Rectangle end = modelToView(composedTextEnd.getOffset());
                    Rectangle b = getBounds();
                    d.x += Math.min(end.x - d.x, b.width);
                    scrollRectToVisible(d);
                } catch (BadLocationException ble) {}
            }
        }
        caret.setDot(dot);
    } else if (caret instanceof ComposedTextCaret) {
        dot = caret.getDot();
        // Restores original caret
        exchangeCaret(caret, originalCaret);
        caret.setDot(dot);
    }
}