From 71852ba35148318760bcb2ec8619a5d10ecb071a Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Thu, 3 Dec 2015 22:11:03 -0800 Subject: [PATCH 1/4] Open up access to CharacterHit for subclasses of StyledTextArea --- .../src/main/java/org/fxmisc/richtext/CharacterHit.java | 2 +- .../src/main/java/org/fxmisc/richtext/StyledTextArea.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/CharacterHit.java b/richtextfx/src/main/java/org/fxmisc/richtext/CharacterHit.java index b860a3e36..50a547da8 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/CharacterHit.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/CharacterHit.java @@ -2,7 +2,7 @@ import java.util.OptionalInt; -class CharacterHit { +public class CharacterHit { public static CharacterHit insertionAt(int insertionIndex) { return new CharacterHit(OptionalInt.empty(), insertionIndex); diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java index 0117caa14..887123fbb 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java @@ -654,14 +654,14 @@ ParagraphBox.CaretOffsetX getCaretOffsetX() { return virtualFlow.getHeight(); } - CharacterHit hit(ParagraphBox.CaretOffsetX x, TwoDimensional.Position targetLine) { + protected CharacterHit hit(ParagraphBox.CaretOffsetX x, TwoDimensional.Position targetLine) { int parIdx = targetLine.getMajor(); ParagraphBox cell = virtualFlow.getCell(parIdx).getNode(); CharacterHit parHit = cell.hitTextLine(x, targetLine.getMinor()); return parHit.offset(getParagraphOffset(parIdx)); } - CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) { + protected CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) { VirtualFlowHit, ParagraphBox>> hit = virtualFlow.hit(0.0, y); if(hit.isBeforeCells()) { return CharacterHit.insertionAt(0); @@ -677,7 +677,7 @@ CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) { } } - CharacterHit hit(double x, double y) { + protected CharacterHit hit(double x, double y) { VirtualFlowHit, ParagraphBox>> hit = virtualFlow.hit(x, y); if(hit.isBeforeCells()) { return CharacterHit.insertionAt(0); From b3f984b293e558a0c759cc7ffb62e9e5c96d02c6 Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Fri, 4 Dec 2015 12:01:49 -0800 Subject: [PATCH 2/4] Made ParagraphBox class package-private again: fixes small mistake originally made in commit "0ae84c25936bdb285434ecc96564f0b1f946221d" --- richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java index e6a9d596d..de822f95a 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java @@ -31,7 +31,7 @@ import org.reactfx.value.Val; import org.reactfx.value.Var; -public class ParagraphBox extends Region { +class ParagraphBox extends Region { /** * An opaque class representing horizontal caret offset. From 853a8e40f1af49f976195b0eac38a8759707738c Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Fri, 4 Dec 2015 12:05:23 -0800 Subject: [PATCH 3/4] Made ParagraphBox Constructor package-private: no longer needs to be public now that skin has been removed and this class was moved to richtext package. --- richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java index de822f95a..6e31a4395 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/ParagraphBox.java @@ -69,7 +69,7 @@ public ObjectProperty> graphicFactoryProperty() { public void setIndex(int index) { this.index.setValue(index); } public int getIndex() { return index.getValue(); } - public ParagraphBox(Paragraph par, BiConsumer applyStyle, BiConsumer applyParagraphStyle) { + ParagraphBox(Paragraph par, BiConsumer applyStyle, BiConsumer applyParagraphStyle) { this.getStyleClass().add("paragraph-box"); this.text = new ParagraphText<>(par, applyStyle); applyParagraphStyle.accept(this.text, par.getParagraphStyle()); From 86fcbdc6d5e5d3dcab22b54f64063e499eecfed2 Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Fri, 4 Dec 2015 12:52:23 -0800 Subject: [PATCH 4/4] Adjust hit() methods' accessibility: only hit(x, y) should be public. --- .../org/fxmisc/richtext/StyledTextArea.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java index 887123fbb..21465d4ec 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextArea.java @@ -654,14 +654,14 @@ ParagraphBox.CaretOffsetX getCaretOffsetX() { return virtualFlow.getHeight(); } - protected CharacterHit hit(ParagraphBox.CaretOffsetX x, TwoDimensional.Position targetLine) { + CharacterHit hit(ParagraphBox.CaretOffsetX x, TwoDimensional.Position targetLine) { int parIdx = targetLine.getMajor(); ParagraphBox cell = virtualFlow.getCell(parIdx).getNode(); CharacterHit parHit = cell.hitTextLine(x, targetLine.getMinor()); return parHit.offset(getParagraphOffset(parIdx)); } - protected CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) { + CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) { VirtualFlowHit, ParagraphBox>> hit = virtualFlow.hit(0.0, y); if(hit.isBeforeCells()) { return CharacterHit.insertionAt(0); @@ -677,7 +677,23 @@ protected CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) { } } - protected CharacterHit hit(double x, double y) { + /** + * Helpful for determining which letter is at point x, y: + *
+     *     {@code
+     *     StyledTextArea area = // creation code
+     *     area.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent e) -> {
+     *         CharacterHit hit = area.hit(e.getX(), e.getY());
+     *         int characterPosition = hit.getInsertionIndex();
+     *
+     *         // move the caret to that character's position
+     *         area.moveTo(characterPosition, SelectionPolicy.CLEAR);
+     *     }}
+     * 
+ * @param x + * @param y + */ + public CharacterHit hit(double x, double y) { VirtualFlowHit, ParagraphBox>> hit = virtualFlow.hit(x, y); if(hit.isBeforeCells()) { return CharacterHit.insertionAt(0);