|
| 1 | +package org.fxmisc.richtext; |
| 2 | + |
| 3 | +import javafx.beans.value.ObservableValue; |
| 4 | +import org.fxmisc.richtext.model.StyledDocument; |
| 5 | + |
| 6 | +/** |
| 7 | + * An object for encapsulating a selection in a given area that is bound to an underlying caret. In other words, |
| 8 | + * {@link #selectRange(int, int) selecting some range in the area} will move a caret in the same call. |
| 9 | + * |
| 10 | + * <p> |
| 11 | + * <b>"Position"</b> refers to the place in-between characters. In other words, every {@code "|"} in |
| 12 | + * {@code "|t|e|x|t|"} is a valid position. There are two kinds of positions used here:</p> |
| 13 | + * <ol> |
| 14 | + * <li> |
| 15 | + * {@link #getStartPosition()}/{@link #getEndPosition()}, which refers to a position somewhere in the |
| 16 | + * entire area's content. It's bounds are {@code 0 <= x < area.getLength()}. |
| 17 | + * </li> |
| 18 | + * <li> |
| 19 | + * {@link #getStartColumnPosition()}/{@link #getEndColumnPosition()}, which refers to a position |
| 20 | + * somewhere in the current paragraph. It's bounds are {@code 0 <= x < area.getParagraphLength(index)}. |
| 21 | + * </li> |
| 22 | + * </ol> |
| 23 | + * |
| 24 | + * Note: when parameter names are "position" without the "column" prefix, they refer to the position in the entire area. |
| 25 | + * |
| 26 | + * <p> |
| 27 | + * The selection is typically made using the {@link #getAnchorPosition() anchor's position} and |
| 28 | + * the underlying {@link Caret#getPosition() caret's position}. Hence, {@link #selectRange(int, int)} |
| 29 | + * is the typical method to use, although {@link #selectRange0(int, int)} can also be used. |
| 30 | + * </p> |
| 31 | + * <p> |
| 32 | + * Be careful about calling the underlying {@link Caret#moveTo(int)} method. This will displace the caret |
| 33 | + * from the selection bounds and may lead to undesirable/unexpected behavior. If this is done, a |
| 34 | + * {@link #selectRange(int, int)} call will reposition the caret, so that it is either the start or end |
| 35 | + * bound of this selection. |
| 36 | + * </p> |
| 37 | + * |
| 38 | + * <p> |
| 39 | + * For type safety, {@link #getSelectedDocument()} requires the same generic types from {@link StyledDocument}. |
| 40 | + * This means that one must write a lot of boilerplate for the generics: |
| 41 | + * {@code BoundedSelection<Collection<String>, StyledText<Collection<String>>, Collection<String>> selection}. |
| 42 | + * However, this is only necessary if one is using {@link #getSelectedDocument()} or |
| 43 | + * {@link #selectedDocumentProperty()}. <b>If you are not going to use the "selectedDocument" getter or property, |
| 44 | + * then just write the much simpler {@code BoundedSelection<?, ?, ?> selection}</b>. |
| 45 | + * </p> |
| 46 | + * |
| 47 | + * @param <PS> type for {@link StyledDocument}'s paragraph style; only necessary when using the "selectedDocument" |
| 48 | + * getter or property |
| 49 | + * @param <SEG> type for {@link StyledDocument}'s segment type; only necessary when using the "selectedDocument" |
| 50 | + * getter or property |
| 51 | + * @param <S> type for {@link StyledDocument}'s segment style; only necessary when using the "selectedDocument" |
| 52 | + * getter or property |
| 53 | + */ |
| 54 | +public interface BoundedSelection<PS, SEG, S> extends UnboundedSelection<PS, SEG, S> { |
| 55 | + |
| 56 | + @Override |
| 57 | + default boolean isBoundToCaret() { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + default BoundedSelection asBoundedSelection() { |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + int getAnchorPosition(); |
| 67 | + ObservableValue<Integer> anchorPositionProperty(); |
| 68 | + |
| 69 | + int getAnchorParIndex(); |
| 70 | + ObservableValue<Integer> anchorParIndexProperty(); |
| 71 | + |
| 72 | + int getAnchorColPosition(); |
| 73 | + ObservableValue<Integer> anchorColPositionProperty(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Positions the anchor and caretPosition explicitly, |
| 77 | + * effectively creating a selection. |
| 78 | + * |
| 79 | + * <p><b>Caution:</b> see {@link org.fxmisc.richtext.model.TextEditingArea#getAbsolutePosition(int, int)} |
| 80 | + * to know how the column index argument can affect the returned position.</p> |
| 81 | + */ |
| 82 | + void selectRange(int anchorParagraph, int anchorColumn, int caretParagraph, int caretColumn); |
| 83 | + |
| 84 | + /** |
| 85 | + * Positions the anchor and caretPosition explicitly, |
| 86 | + * effectively creating a selection. |
| 87 | + */ |
| 88 | + void selectRange(int anchorPosition, int caretPosition); |
| 89 | + |
| 90 | +} |
0 commit comments