From 201a8085af8be46ac37c53a96b3123b364a28755 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Sat, 30 Sep 2017 10:34:42 -0700 Subject: [PATCH] Feature: map visible paragraph index to all paragraph index & vice versa --- .../fxmisc/richtext/GenericStyledArea.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java index 56344975f..e74490c5e 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java @@ -740,6 +740,64 @@ CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) { } } + /** + * Maps a paragraph index from {@link #getParagraphs()} into the index system of {@link #getVisibleParagraphs()}. + */ + public final Optional allParToVisibleParIndex(int allParIndex) { + if (allParIndex < 0) { + throw new IllegalArgumentException("Visible paragraph index cannot be negative but was " + allParIndex); + } + if (allParIndex >= getVisibleParagraphs().size()) { + throw new IllegalArgumentException(String.format( + "Paragraphs' last index is [%s] but allParIndex was [%s]", + getParagraphs().size() - 1, allParIndex) + ); + } + Paragraph p = getParagraph(allParIndex); + for (int index = 0; index < getVisibleParagraphs().size(); index++) { + if (getVisibleParagraphs().get(index) == p) { + return Optional.of(index); + } + } + return Optional.empty(); + } + + /** + * Maps a paragraph index from {@link #getVisibleParagraphs()} into the index system of {@link #getParagraphs()}. + */ + public final int visibleParToAllParIndex(int visibleParIndex) { + if (visibleParIndex < 0) { + throw new IllegalArgumentException("Visible paragraph index cannot be negative but was " + visibleParIndex); + } + if (visibleParIndex >= getVisibleParagraphs().size()) { + throw new IllegalArgumentException(String.format( + "Visible paragraphs' last index is [%s] but visibleParIndex was [%s]", + getVisibleParagraphs().size() - 1, visibleParIndex) + ); + } + Paragraph visibleP = getVisibleParagraphs().get(visibleParIndex); + for (int index = 0; index < getParagraphs().size(); index++) { + if (getParagraph(index) == visibleP) { + return index; + } + } + throw new AssertionError("Unreachable code"); + } + + /** + * Returns the index of the first visible paragraph in the index system of {@link #getParagraphs()}. + */ + public final int firstVisibleParToAllParIndex() { + return visibleParToAllParIndex(0); + } + + /** + * Returns the index of the last visible paragraph in the index system of {@link #getParagraphs()}. + */ + public final int lastVisibleParToAllParIndex() { + return visibleParToAllParIndex(visibleParagraphs.size() - 1); + } + @Override public CharacterHit hit(double x, double y) { // mouse position used, so account for padding