Skip to content

Commit 20f2ce6

Browse files
Merge pull request #600 from JordanMartinez/paragraphIndexMapping
Feature: map visible paragraph index to all paragraph index & vice versa
2 parents d4fd9ac + 201a808 commit 20f2ce6

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,64 @@ CharacterHit hit(ParagraphBox.CaretOffsetX x, double y) {
740740
}
741741
}
742742

743+
/**
744+
* Maps a paragraph index from {@link #getParagraphs()} into the index system of {@link #getVisibleParagraphs()}.
745+
*/
746+
public final Optional<Integer> allParToVisibleParIndex(int allParIndex) {
747+
if (allParIndex < 0) {
748+
throw new IllegalArgumentException("Visible paragraph index cannot be negative but was " + allParIndex);
749+
}
750+
if (allParIndex >= getVisibleParagraphs().size()) {
751+
throw new IllegalArgumentException(String.format(
752+
"Paragraphs' last index is [%s] but allParIndex was [%s]",
753+
getParagraphs().size() - 1, allParIndex)
754+
);
755+
}
756+
Paragraph<PS, SEG, S> p = getParagraph(allParIndex);
757+
for (int index = 0; index < getVisibleParagraphs().size(); index++) {
758+
if (getVisibleParagraphs().get(index) == p) {
759+
return Optional.of(index);
760+
}
761+
}
762+
return Optional.empty();
763+
}
764+
765+
/**
766+
* Maps a paragraph index from {@link #getVisibleParagraphs()} into the index system of {@link #getParagraphs()}.
767+
*/
768+
public final int visibleParToAllParIndex(int visibleParIndex) {
769+
if (visibleParIndex < 0) {
770+
throw new IllegalArgumentException("Visible paragraph index cannot be negative but was " + visibleParIndex);
771+
}
772+
if (visibleParIndex >= getVisibleParagraphs().size()) {
773+
throw new IllegalArgumentException(String.format(
774+
"Visible paragraphs' last index is [%s] but visibleParIndex was [%s]",
775+
getVisibleParagraphs().size() - 1, visibleParIndex)
776+
);
777+
}
778+
Paragraph<PS, SEG, S> visibleP = getVisibleParagraphs().get(visibleParIndex);
779+
for (int index = 0; index < getParagraphs().size(); index++) {
780+
if (getParagraph(index) == visibleP) {
781+
return index;
782+
}
783+
}
784+
throw new AssertionError("Unreachable code");
785+
}
786+
787+
/**
788+
* Returns the index of the first visible paragraph in the index system of {@link #getParagraphs()}.
789+
*/
790+
public final int firstVisibleParToAllParIndex() {
791+
return visibleParToAllParIndex(0);
792+
}
793+
794+
/**
795+
* Returns the index of the last visible paragraph in the index system of {@link #getParagraphs()}.
796+
*/
797+
public final int lastVisibleParToAllParIndex() {
798+
return visibleParToAllParIndex(visibleParagraphs.size() - 1);
799+
}
800+
743801
@Override
744802
public CharacterHit hit(double x, double y) {
745803
// mouse position used, so account for padding

0 commit comments

Comments
 (0)