/** * Check if the text node intersects with the start and end locations * * @param start * the start * @param end * the end * @return <code>true</code> if intersects */ public boolean intersectOffset(int start, int end) { if (getStartOffset() > end) { return false; } else if (getEndOffset() < start) { return false; } return true; }
/** * Find the position of a the caret at a given index * * @param index * the index * @return the location relative to this node or <code>null</code> */ public @Nullable Point2D getCareLocation(int index) { for (StyledTextNode t : this.textNodes) { if (t.getStartOffset() <= index && t.getEndOffset() >= index) { double x = t.getCharLocation(index); return sceneToLocal(t.localToScene(x, 0)); } } return null; }
private void handleSelectionChange(Observable o, TextSelection oldSelection, TextSelection newSelection) { if (newSelection.length == 0) { this.selectionMarker.setVisible(false); this.selectionMarker.resize(0, 0); } else { this.selectionMarker.setVisible(true); int start = newSelection.offset; int end = newSelection.offset + newSelection.length; this.selectionStartNode = null; this.selectionEndNode = null; for (StyledTextNode t : this.textNodes) { if (t.intersectOffset(start, end)) { if (this.selectionStartNode == null) { this.selectionStartNode = t; } this.selectionEndNode = t; } } if (this.selectionStartNode != null && this.selectionEndNode != null) { int charIndex = start - this.selectionStartNode.getStartOffset(); this.selectionStartX = this.selectionStartNode.getCharLocation(charIndex); charIndex = end - this.selectionEndNode.getStartOffset(); this.selectionEndX = this.selectionEndNode.getCharLocation(charIndex); requestLayout(); } } }
/** * Find the caret index at the give point * * @param point * the point relative to coordinate system of this node * @return the index or <code>-1</code> if not found */ public int getCaretIndexAtPoint(Point2D point) { Point2D scenePoint = localToScene(point); for (StyledTextNode t : this.textNodes) { if (t.localToScene(t.getBoundsInLocal()).contains(scenePoint)) { return t.getCaretIndexAtPoint(t.sceneToLocal(scenePoint)) + t.getStartOffset(); } } return -1; }
for (StyledTextNode t : this.textNodes) { t.applyCss(); if (t.getStartOffset() <= this.caretIndex && (t.getEndOffset() > this.caretIndex || this.textNodes.get(this.textNodes.size() - 1) == t)) { double caretX = t.getCharLocation(this.caretIndex - t.getStartOffset()); double x = this.textLayoutNode.localToParent(t.getBoundsInParent().getMinX(), 0).getX() + caretX; double h = t.prefHeight(-1);