6
6
import java .util .function .BiConsumer ;
7
7
import java .util .function .BiFunction ;
8
8
import java .util .function .Consumer ;
9
- import java .util .function .IntFunction ;
10
- import java .util .function .ToIntFunction ;
11
9
12
- import javafx .beans .binding .IntegerBinding ;
13
- import javafx .beans .binding .ObjectBinding ;
14
10
import javafx .beans .binding .StringBinding ;
15
11
import javafx .beans .property .BooleanProperty ;
16
12
import javafx .beans .property .ReadOnlyIntegerProperty ;
21
17
import javafx .beans .value .ObservableIntegerValue ;
22
18
import javafx .beans .value .ObservableStringValue ;
23
19
import javafx .beans .value .ObservableValue ;
24
- import javafx .collections .FXCollections ;
25
20
import javafx .collections .ObservableList ;
26
21
import javafx .css .CssMetaData ;
27
22
import javafx .css .Styleable ;
@@ -119,12 +114,12 @@ public void setUndoManager(UndoManagerProvider<TextChange> undoManagerProvider)
119
114
**************************************************************************/
120
115
121
116
// text
122
- @ Override public final String getText () { return content .get (); }
123
- @ Override public final ObservableStringValue textProperty () { return content ; }
117
+ @ Override public final String getText () { return content .getText (); }
118
+ @ Override public final ObservableStringValue textProperty () { return content . textProperty () ; }
124
119
125
120
// length
126
- @ Override public final int getLength () { return content .length (). get (); }
127
- @ Override public final ObservableIntegerValue lengthProperty () { return content .length (); }
121
+ @ Override public final int getLength () { return content .getLength (); }
122
+ @ Override public final ObservableIntegerValue lengthProperty () { return content .lengthProperty (); }
128
123
129
124
// caret position
130
125
private final ReadOnlyIntegerWrapper caretPosition = new ReadOnlyIntegerWrapper (this , "caretPosition" , 0 );
@@ -156,8 +151,9 @@ public void setUndoManager(UndoManagerProvider<TextChange> undoManagerProvider)
156
151
@ Override public final int getCaretColumn () { return caretColumn .get (); }
157
152
158
153
// paragraphs
154
+ private final ObservableList <Paragraph <S >> paragraphs ;
159
155
@ Override public ObservableList <Paragraph <S >> getParagraphs () {
160
- return FXCollections . unmodifiableObservableList ( content . paragraphs ) ;
156
+ return paragraphs ;
161
157
}
162
158
163
159
@@ -181,7 +177,7 @@ public void setUndoManager(UndoManagerProvider<TextChange> undoManagerProvider)
181
177
/**
182
178
* content model
183
179
*/
184
- private final StyledTextAreaContent <S > content ;
180
+ private final StyledTextDocument <S > content ;
185
181
186
182
/**
187
183
* Style used by default when no other style is provided.
@@ -212,27 +208,28 @@ public void setUndoManager(UndoManagerProvider<TextChange> undoManagerProvider)
212
208
public StyledTextArea (S initialStyle , BiConsumer <Text , S > applyStyle ) {
213
209
this .initialStyle = initialStyle ;
214
210
this .applyStyle = applyStyle ;
215
- content = new StyledTextAreaContent <>(initialStyle );
211
+ content = new StyledTextDocument <>(initialStyle );
212
+ paragraphs = content .getParagraphs ();
216
213
217
214
undoManager = createUndoManager (defaultUndoManagerFactory );
218
215
219
- ObservableValue <Position > caretPosition2D = createBinding (caretPosition , p -> content .offsetToPosition (p ));
220
- currentParagraph = createIntegerBinding (caretPosition2D , p -> p .getMajor ());
221
- caretColumn = createIntegerBinding (caretPosition2D , p -> p .getMinor ());
216
+ ObservableValue <Position > caretPosition2D = BindingFactories . createBinding (caretPosition , p -> content .offsetToPosition (p ));
217
+ currentParagraph = BindingFactories . createIntegerBinding (caretPosition2D , p -> p .getMajor ());
218
+ caretColumn = BindingFactories . createIntegerBinding (caretPosition2D , p -> p .getMinor ());
222
219
223
220
// Keep caret position in the current paragraph up to date.
224
221
caretPosition .addListener (obs -> {
225
222
// by the time this listener is called, both currentParagraph and
226
223
// caretColumn have been invalidated, so get()-ing them yields
227
224
// up-to-date values.
228
- Paragraph <S > par = content . paragraphs .get (currentParagraph .get ());
225
+ Paragraph <S > par = paragraphs .get (currentParagraph .get ());
229
226
par .setCaretPosition (caretColumn .get ());
230
227
});
231
228
232
229
selectedText = new StringBinding () {
233
230
{ bind (selection , textProperty ()); }
234
231
@ Override protected String computeValue () {
235
- return content .get (selection .get ());
232
+ return content .getText (selection .get ());
236
233
}
237
234
};
238
235
@@ -250,12 +247,12 @@ public StyledTextArea(S initialStyle, BiConsumer<Text, S> applyStyle) {
250
247
251
248
@ Override
252
249
public final String getText (int start , int end ) {
253
- return content .get (start , end );
250
+ return content .getText (start , end );
254
251
}
255
252
256
253
@ Override
257
254
public String getText (int paragraph ) {
258
- return content . paragraphs .get (paragraph ).toString ();
255
+ return paragraphs .get (paragraph ).toString ();
259
256
}
260
257
261
258
/**
@@ -358,7 +355,7 @@ public void selectRange(int anchor, int caretPosition) {
358
355
// update selection in paragraphs
359
356
int start = selection .getStart ();
360
357
int end = selection .getEnd ();
361
- for (Paragraph <S > par : content . paragraphs ) {
358
+ for (Paragraph <S > par : paragraphs ) {
362
359
int len = par .length ();
363
360
if (end > 0 && start < len ) {
364
361
par .setSelection (start , Math .min (end , len ));
@@ -408,26 +405,4 @@ private UndoManager createUndoManager(UndoManagerProvider<TextChange> factory) {
408
405
BiFunction <TextChange , TextChange , Optional <TextChange >> merge = (change1 , change2 ) -> change1 .mergeWith (change2 );
409
406
return factory .get (apply , undo , merge , textChanges ());
410
407
}
411
-
412
- private static <T > ObjectBinding <T > createBinding (ObservableIntegerValue dep , IntFunction <T > computeValue ) {
413
- return new ObjectBinding <T >() {
414
- { bind (dep ); }
415
-
416
- @ Override
417
- protected T computeValue () {
418
- return computeValue .apply (dep .get ());
419
- }
420
- };
421
- }
422
-
423
- private static <A > IntegerBinding createIntegerBinding (ObservableValue <A > dep , ToIntFunction <A > computeValue ) {
424
- return new IntegerBinding () {
425
- { bind (dep ); }
426
-
427
- @ Override
428
- protected int computeValue () {
429
- return computeValue .applyAsInt (dep .getValue ());
430
- }
431
- };
432
- }
433
408
}
0 commit comments