90
90
import org .reactfx .value .Var ;
91
91
92
92
/**
93
- * Text editing control. Accepts user input (keyboard, mouse) and
94
- * provides API to assign style to text ranges. It is suitable for
93
+ * Text editing control that renders and edits a {@link EditableStyledDocument}.
94
+ *
95
+ * Accepts user input (keyboard, mouse) and provides API to assign style to text ranges. It is suitable for
95
96
* syntax highlighting and rich-text editors.
96
97
*
97
98
* <h3>Adding Scrollbars to the Area</h3>
98
99
*
99
- * <p>The scroll bars no longer appear when the content spans outside
100
- * of the viewport. To add scroll bars, the area needs to be wrapped in
101
- * a {@link VirtualizedScrollPane}. For example, </p>
102
- * <pre>
103
- * {@code
100
+ * <p>By default, scroll bars do not appear when the content spans outside of the viewport.
101
+ * To add scroll bars, the area needs to be wrapped in a {@link VirtualizedScrollPane}. For example, </p>
102
+ * <pre><code>
104
103
* // shows area without scroll bars
105
104
* InlineCssTextArea area = new InlineCssTextArea();
106
105
*
107
106
* // add scroll bars that will display as needed
108
- * VirtualizedScrollPane< InlineCssTextArea> vsPane = new VirtualizedScrollPane(area);
107
+ * VirtualizedScrollPane< InlineCssTextArea> vsPane = new VirtualizedScrollPane<> (area);
109
108
*
110
- * Parent parent = //;
109
+ * Parent parent = // creation code
111
110
* parent.getChildren().add(vsPane)
112
- * }
113
- * </pre>
111
+ * </code></pre>
114
112
*
115
113
* <h3>Auto-Scrolling to the Caret</h3>
116
114
*
117
115
* <p>Every time the underlying {@link EditableStyledDocument} changes via user interaction (e.g. typing) through
118
- * the {@code StyledTextArea }, the area will scroll to insure the caret is kept in view. However, this does not
116
+ * the {@code GenericStyledArea }, the area will scroll to insure the caret is kept in view. However, this does not
119
117
* occur if changes are done programmatically. For example, let's say the area is displaying the bottom part
120
118
* of the area's {@link EditableStyledDocument} and some code changes something in the top part of the document
121
119
* that is not currently visible. If there is no call to {@link #requestFollowCaret()} at the end of that code,
122
120
* the area will not auto-scroll to that section of the document. The change will occur, and the user will continue
123
121
* to see the bottom part of the document as before. If such a call is there, then the area will scroll
124
122
* to the top of the document and no longer display the bottom part of it.</p>
123
+ * <p>For example...</p>
124
+ * <pre><code>
125
+ * // assuming the user is currently seeing the top of the area
126
+ *
127
+ * // then changing the bottom, currently not visible part of the area...
128
+ * int startParIdx = 40;
129
+ * int startColPosition = 2;
130
+ * int endParIdx = 42;
131
+ * int endColPosition = 10;
132
+ *
133
+ * // ...by itself will not scroll the viewport to where the change occurs
134
+ * area.replaceText(startParIdx, startColPosition, endParIdx, endColPosition, "replacement text");
135
+ *
136
+ * // adding this line after the last modification to the area will cause the viewport to scroll to that change
137
+ * // leaving the following line out will leave the viewport unaffected and the user will not notice any difference
138
+ * area.requestFollowCaret();
139
+ * </code></pre>
125
140
*
126
141
* <p>Additionally, when overriding the default user-interaction behavior, remember to include a call
127
142
* to {@link #requestFollowCaret()}.</p>
136
151
*
137
152
* <h3>Overriding default keyboard behavior</h3>
138
153
*
139
- * {@code StyledTextArea } uses {@code KEY_TYPED} handler to handle ordinary
140
- * character input and {@code KEY_PRESSED} handler to handle control key
154
+ * {@code GenericStyledArea } uses {@link javafx.scene.input.KeyEvent# KEY_TYPED KEY_TYPED} to handle ordinary
155
+ * character input and {@link javafx.scene.input.KeyEvent# KEY_PRESSED KEY_PRESSED} to handle control key
141
156
* combinations (including Enter and Tab). To add or override some keyboard
142
157
* shortcuts, while keeping the rest in place, you would combine the default
143
158
* event handler with a new one that adds or overrides some of the default
144
- * key combinations. This is how to bind {@code Ctrl+S} to the {@code save()}
145
- * operation:
146
- * <pre>
147
- * {@code
159
+ * key combinations.
160
+ * <p>
161
+ * For example, this is how to bind {@code Ctrl+S} to the {@code save()} operation:
162
+ * </p>
163
+ * <pre><code>
148
164
* import static javafx.scene.input.KeyCode.*;
149
165
* import static javafx.scene.input.KeyCombination.*;
150
166
* import static org.fxmisc.wellbehaved.event.EventPattern.*;
151
167
* import static org.fxmisc.wellbehaved.event.InputMap.*;
152
168
*
153
169
* import org.fxmisc.wellbehaved.event.Nodes;
154
170
*
155
- * Nodes.addInputMap(area, consume(keyPressed(S, CONTROL_DOWN), event -> save()));
156
- * }
157
- * </pre>
171
+ * // installs the following consume InputMap,
172
+ * // so that a CTRL+S event saves the document and consumes the event
173
+ * Nodes.addInputMap(area, consume(keyPressed(S, CONTROL_DOWN), event -> save()));
174
+ * </code></pre>
158
175
*
159
176
* <h3>Overriding default mouse behavior</h3>
160
177
*
161
178
* The area's default mouse behavior properly handles auto-scrolling and dragging the selected text to a new location.
162
179
* As such, some parts cannot be partially overridden without it affecting other behavior.
163
180
*
164
- * <p>The following lists either {@link org.fxmisc.wellbehaved.event.EventPattern}s that cannot be overridden without
165
- * negatively affecting the default mouse behavior or describe how to safely override things in a special way without
166
- * disrupting the auto scroll behavior.</p>
181
+ * <p>The following lists either {@link org.fxmisc.wellbehaved.event.EventPattern}s that cannot be
182
+ * overridden without negatively affecting the default mouse behavior or describe how to safely override things
183
+ * in a special way without disrupting the auto scroll behavior.</p>
167
184
* <ul>
168
185
* <li>
169
186
* <em>First (1 click count) Primary Button Mouse Pressed Events:</em>
227
244
* </li>
228
245
* </ul>
229
246
*
247
+ * <h3>CSS, Style Classes, and Pseudo Classes</h3>
248
+ * <p>
249
+ * Refer to the <a href="https://github.com/FXMisc/RichTextFX/wiki/RichTextFX-CSS-Reference-Guide">
250
+ * RichTextFX CSS Reference Guide
251
+ * </a>.
252
+ * </p>
253
+ *
254
+ * <h3>Area Actions and Other Operations</h3>
255
+ * <p>
256
+ * To distinguish the actual operations one can do on this area from the boilerplate methods
257
+ * within this area (e.g. properties and their getters/setters, etc.), look at the interfaces
258
+ * this area implements. Each lists and documents methods that fall under that category.
259
+ * </p>
260
+ *
261
+ * <h3>Calculating a Position Within the Area</h3>
262
+ * <p>
263
+ * To calculate a position or index within the area, read through the javadoc of
264
+ * {@link org.fxmisc.richtext.model.TwoDimensional} and {@link org.fxmisc.richtext.model.TwoDimensional.Bias}.
265
+ * Also, read the difference between "position" and "index" in
266
+ * {@link org.fxmisc.richtext.model.StyledDocument#getAbsolutePosition(int, int)}.
267
+ * </p>
268
+ *
269
+ * @see EditableStyledDocument
270
+ * @see TwoDimensional
271
+ * @see org.fxmisc.richtext.model.TwoDimensional.Bias
272
+ * @see VirtualFlow
273
+ * @see VirtualizedScrollPane
274
+ * @see Caret
275
+ * @see Selection
276
+ * @see CaretSelectionBind
230
277
*
231
278
* @param <PS> type of style that can be applied to paragraphs (e.g. {@link TextFlow}.
232
279
* @param <SEG> type of segment used in {@link Paragraph}. Can be only text (plain or styled) or
@@ -586,6 +633,20 @@ public GenericStyledArea(@NamedArg("initialParagraphStyle") PS initialParagraphS
586
633
this (initialParagraphStyle , applyParagraphStyle , initialTextStyle , segmentOps , true , nodeFactory );
587
634
}
588
635
636
+ /**
637
+ * Same as {@link #GenericStyledArea(Object, BiConsumer, Object, TextOps, Function)} but also allows one
638
+ * to specify whether the undo manager should be a plain or rich undo manager via {@code preserveStyle}.
639
+ *
640
+ * @param initialParagraphStyle style to use in places where no other style is specified (yet).
641
+ * @param applyParagraphStyle function that, given a {@link TextFlow} node and
642
+ * a style, applies the style to the paragraph node. This function is
643
+ * used by the default skin to apply style to paragraph nodes.
644
+ * @param initialTextStyle style to use in places where no other style is specified (yet).
645
+ * @param segmentOps The operations which are defined on the text segment objects.
646
+ * @param preserveStyle whether to use an undo manager that can undo/redo {@link RichTextChange}s or
647
+ * {@link PlainTextChange}s
648
+ * @param nodeFactory A function which is used to create the JavaFX scene node for a particular segment.
649
+ */
589
650
public GenericStyledArea (@ NamedArg ("initialParagraphStyle" ) PS initialParagraphStyle ,
590
651
@ NamedArg ("applyParagraphStyle" ) BiConsumer <TextFlow , PS > applyParagraphStyle ,
591
652
@ NamedArg ("initialTextStyle" ) S initialTextStyle ,
@@ -598,8 +659,8 @@ public GenericStyledArea(@NamedArg("initialParagraphStyle") PS initialParagraphS
598
659
599
660
/**
600
661
* The same as {@link #GenericStyledArea(Object, BiConsumer, Object, TextOps, Function)} except that
601
- * this constructor can be used to create another {@code GenericStyledArea} object that
602
- * shares the same {@link EditableStyledDocument}.
662
+ * this constructor can be used to create another {@code GenericStyledArea} that renders and edits the same
663
+ * {@link EditableStyledDocument} or when one wants to use a custom {@link EditableStyledDocument} implementation .
603
664
*/
604
665
public GenericStyledArea (
605
666
@ NamedArg ("initialParagraphStyle" ) PS initialParagraphStyle ,
@@ -612,6 +673,20 @@ public GenericStyledArea(
612
673
613
674
}
614
675
676
+ /**
677
+ * Creates an area with flexibility in all of its options.
678
+ *
679
+ * @param initialParagraphStyle style to use in places where no other style is specified (yet).
680
+ * @param applyParagraphStyle function that, given a {@link TextFlow} node and
681
+ * a style, applies the style to the paragraph node. This function is
682
+ * used by the default skin to apply style to paragraph nodes.
683
+ * @param initialTextStyle style to use in places where no other style is specified (yet).
684
+ * @param document the document to render and edit
685
+ * @param segmentOps The operations which are defined on the text segment objects.
686
+ * @param preserveStyle whether to use an undo manager that can undo/redo {@link RichTextChange}s or
687
+ * {@link PlainTextChange}s
688
+ * @param nodeFactory A function which is used to create the JavaFX scene node for a particular segment.
689
+ */
615
690
public GenericStyledArea (
616
691
@ NamedArg ("initialParagraphStyle" ) PS initialParagraphStyle ,
617
692
@ NamedArg ("applyParagraphStyle" ) BiConsumer <TextFlow , PS > applyParagraphStyle ,
@@ -835,9 +910,13 @@ TwoDimensional.Position currentLine() {
835
910
return _position (parIdx , lineIdx );
836
911
}
837
912
838
- public final int lineIndex (int paragraphIndex , int column ) {
913
+ /**
914
+ * Returns 0 if the given paragraph displays its content across only one line, or returns the index
915
+ * of the line on which the given column position appears if the paragraph spans multiple lines.
916
+ */
917
+ public final int lineIndex (int paragraphIndex , int columnPosition ) {
839
918
Cell <Paragraph <PS , SEG , S >, ParagraphBox <PS , SEG , S >> cell = virtualFlow .getCell (paragraphIndex );
840
- return cell .getNode ().getCurrentLineIndex (column );
919
+ return cell .getNode ().getCurrentLineIndex (columnPosition );
841
920
}
842
921
843
922
TwoDimensional .Position _position (int par , int line ) {
@@ -1211,6 +1290,9 @@ public void replace(int start, int end, StyledDocument<PS, SEG, S> replacement)
1211
1290
* *
1212
1291
* ********************************************************************** */
1213
1292
1293
+ /**
1294
+ * Disposes this area, preventing memory leaks.
1295
+ */
1214
1296
public void dispose () {
1215
1297
subscriptions .unsubscribe ();
1216
1298
virtualFlow .dispose ();
0 commit comments