-
Notifications
You must be signed in to change notification settings - Fork 463
/
TextArea.java
690 lines (569 loc) · 24.9 KB
/
TextArea.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javafx.scene.control;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javafx.beans.InvalidationListener;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.css.CssMetaData;
import javafx.css.StyleConverter;
import javafx.css.StyleableBooleanProperty;
import javafx.css.StyleableIntegerProperty;
import javafx.css.StyleableProperty;
import com.sun.javafx.collections.ListListenerHelper;
import com.sun.javafx.collections.NonIterableChange;
import javafx.css.converter.SizeConverter;
import javafx.scene.control.skin.TextAreaSkin;
import javafx.css.Styleable;
import javafx.scene.AccessibleRole;
/**
* Text input component that allows a user to enter multiple lines of
* plain text. Unlike in previous releases of JavaFX, support for single line
* input is not available as part of the TextArea control, however this is
* the sole-purpose of the {@link TextField} control. Additionally, if you want
* a form of rich-text editing, there is also the
* {@link javafx.scene.web.HTMLEditor HTMLEditor} control.
*
* <p>TextArea supports the notion of showing {@link #promptTextProperty() prompt text}
* to the user when there is no {@link #textProperty() text} already in the
* TextArea (either via the user, or set programmatically). This is a useful
* way of informing the user as to what is expected in the text area, without
* having to resort to {@link Tooltip tooltips} or on-screen {@link Label labels}.
*
* <p>Example:
* <pre><code> var textArea = new TextArea("Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
* + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim "
* + "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip "
* + "ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
* + "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat "
* + "cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
* textArea.setWrapText(true);</code></pre>
*
* <img src="doc-files/TextArea.png" alt="Image of the TextArea control">
*
* @see TextField
* @since JavaFX 2.0
*/
public class TextArea extends TextInputControl {
// Text area content model
private static final class TextAreaContent extends ContentBase {
private final List<StringBuilder> paragraphs = new ArrayList<>();
private final ParagraphList paragraphList = new ParagraphList();
private int contentLength = 0;
private TextAreaContent() {
paragraphs.add(new StringBuilder(DEFAULT_PARAGRAPH_CAPACITY));
paragraphList.content = this;
}
@Override public String get(int start, int end) {
int length = end - start;
StringBuilder textBuilder = new StringBuilder(length);
int paragraphCount = paragraphs.size();
int paragraphIndex = 0;
int offset = start;
while (paragraphIndex < paragraphCount) {
StringBuilder paragraph = paragraphs.get(paragraphIndex);
int count = paragraph.length() + 1;
if (offset < count) {
break;
}
offset -= count;
paragraphIndex++;
}
// Read characters until end is reached, appending to text builder
// and moving to next paragraph as needed
StringBuilder paragraph = paragraphs.get(paragraphIndex);
int i = 0;
while (i < length) {
if (offset == paragraph.length()
&& i < contentLength) {
textBuilder.append('\n');
paragraph = paragraphs.get(++paragraphIndex);
offset = 0;
} else {
textBuilder.append(paragraph.charAt(offset++));
}
i++;
}
return textBuilder.toString();
}
@Override
@SuppressWarnings("unchecked")
public void insert(int index, String text, boolean notifyListeners) {
if (index < 0
|| index > contentLength) {
throw new IndexOutOfBoundsException();
}
if (text == null) {
throw new IllegalArgumentException();
}
text = TextInputControl.filterInput(text, false, false);
int length = text.length();
if (length > 0) {
// Split the text into lines
ArrayList<StringBuilder> lines = new ArrayList<>();
StringBuilder line = new StringBuilder(DEFAULT_PARAGRAPH_CAPACITY);
for (int i = 0; i < length; i++) {
char c = text.charAt(i);
if (c == '\n') {
lines.add(line);
line = new StringBuilder(DEFAULT_PARAGRAPH_CAPACITY);
} else {
line.append(c);
}
}
lines.add(line);
// Merge the text into the existing content
// Merge the text into the existing content
int paragraphIndex = paragraphs.size();
int offset = contentLength + 1;
StringBuilder paragraph = null;
do {
paragraph = paragraphs.get(--paragraphIndex);
offset -= paragraph.length() + 1;
} while (index < offset);
int start = index - offset;
int n = lines.size();
if (n == 1) {
// The text contains only a single line; insert it into the
// intersecting paragraph
paragraph.insert(start, line);
fireParagraphListChangeEvent(paragraphIndex, paragraphIndex + 1,
Collections.singletonList((CharSequence)paragraph));
} else {
// The text contains multiple line; split the intersecting
// paragraph
int end = paragraph.length();
CharSequence trailingText = paragraph.subSequence(start, end);
paragraph.delete(start, end);
// Append the first line to the intersecting paragraph and
// append the trailing text to the last line
StringBuilder first = lines.get(0);
paragraph.insert(start, first);
line.append(trailingText);
fireParagraphListChangeEvent(paragraphIndex, paragraphIndex + 1,
Collections.singletonList((CharSequence)paragraph));
// Insert the remaining lines into the paragraph list
paragraphs.addAll(paragraphIndex + 1, lines.subList(1, n));
fireParagraphListChangeEvent(paragraphIndex + 1, paragraphIndex + n,
Collections.EMPTY_LIST);
}
// Update content length
contentLength += length;
if (notifyListeners) {
fireValueChangedEvent();
}
}
}
@Override public void delete(int start, int end, boolean notifyListeners) {
if (start > end) {
throw new IllegalArgumentException();
}
if (start < 0
|| end > contentLength) {
throw new IndexOutOfBoundsException();
}
int length = end - start;
if (length > 0) {
// Identify the trailing paragraph index
int paragraphIndex = paragraphs.size();
int offset = contentLength + 1;
StringBuilder paragraph = null;
do {
paragraph = paragraphs.get(--paragraphIndex);
offset -= paragraph.length() + 1;
} while (end < offset);
int trailingParagraphIndex = paragraphIndex;
int trailingOffset = offset;
StringBuilder trailingParagraph = paragraph;
// Identify the leading paragraph index
paragraphIndex++;
offset += paragraph.length() + 1;
do {
paragraph = paragraphs.get(--paragraphIndex);
offset -= paragraph.length() + 1;
} while (start < offset);
int leadingParagraphIndex = paragraphIndex;
int leadingOffset = offset;
StringBuilder leadingParagraph = paragraph;
// Remove the text
if (leadingParagraphIndex == trailingParagraphIndex) {
// The removal affects only a single paragraph
leadingParagraph.delete(start - leadingOffset,
end - leadingOffset);
fireParagraphListChangeEvent(leadingParagraphIndex, leadingParagraphIndex + 1,
Collections.singletonList((CharSequence)leadingParagraph));
} else {
// The removal spans paragraphs; remove any intervening paragraphs and
// merge the leading and trailing segments
CharSequence leadingSegment = leadingParagraph.subSequence(0,
start - leadingOffset);
int trailingSegmentLength = (start + length) - trailingOffset;
trailingParagraph.delete(0, trailingSegmentLength);
fireParagraphListChangeEvent(trailingParagraphIndex, trailingParagraphIndex + 1,
Collections.singletonList((CharSequence)trailingParagraph));
if (trailingParagraphIndex - leadingParagraphIndex > 0) {
List<CharSequence> removed = new ArrayList<>(paragraphs.subList(leadingParagraphIndex,
trailingParagraphIndex));
paragraphs.subList(leadingParagraphIndex,
trailingParagraphIndex).clear();
fireParagraphListChangeEvent(leadingParagraphIndex, leadingParagraphIndex,
removed);
}
// Trailing paragraph is now at the former leading paragraph's index
trailingParagraph.insert(0, leadingSegment);
fireParagraphListChangeEvent(leadingParagraphIndex, leadingParagraphIndex + 1,
Collections.singletonList((CharSequence)leadingParagraph));
}
// Update content length
contentLength -= length;
if (notifyListeners) {
fireValueChangedEvent();
}
}
}
@Override public int length() {
return contentLength;
}
@Override public String get() {
return get(0, length());
}
@Override public String getValue() {
return get();
}
private void fireParagraphListChangeEvent(int from, int to, List<CharSequence> removed) {
ParagraphListChange change = new ParagraphListChange(paragraphList, from, to, removed);
ListListenerHelper.fireValueChangedEvent(paragraphList.listenerHelper, change);
}
}
// Observable list of paragraphs
private static final class ParagraphList extends AbstractList<CharSequence>
implements ObservableList<CharSequence> {
private TextAreaContent content;
private ListListenerHelper<CharSequence> listenerHelper;
@Override
public CharSequence get(int index) {
return content.paragraphs.get(index);
}
@Override
public boolean addAll(Collection<? extends CharSequence> paragraphs) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(CharSequence... paragraphs) {
throw new UnsupportedOperationException();
}
@Override
public boolean setAll(Collection<? extends CharSequence> paragraphs) {
throw new UnsupportedOperationException();
}
@Override
public boolean setAll(CharSequence... paragraphs) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
return content.paragraphs.size();
}
@Override
public void addListener(ListChangeListener<? super CharSequence> listener) {
listenerHelper = ListListenerHelper.addListener(listenerHelper, listener);
}
@Override
public void removeListener(ListChangeListener<? super CharSequence> listener) {
listenerHelper = ListListenerHelper.removeListener(listenerHelper, listener);
}
@Override
public boolean removeAll(CharSequence... elements) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(CharSequence... elements) {
throw new UnsupportedOperationException();
}
@Override
public void remove(int from, int to) {
throw new UnsupportedOperationException();
}
@Override
public void addListener(InvalidationListener listener) {
listenerHelper = ListListenerHelper.addListener(listenerHelper, listener);
}
@Override
public void removeListener(InvalidationListener listener) {
listenerHelper = ListListenerHelper.removeListener(listenerHelper, listener);
}
}
private static final class ParagraphListChange extends NonIterableChange<CharSequence> {
private List<CharSequence> removed;
protected ParagraphListChange(ObservableList<CharSequence> list, int from, int to,
List<CharSequence> removed) {
super(from, to, list);
this.removed = removed;
}
@Override
public List<CharSequence> getRemoved() {
return removed;
}
@Override
protected int[] getPermutation() {
return new int[0];
}
}
/**
* The default value for {@link #prefColumnCountProperty() prefColumnCount}.
*/
public static final int DEFAULT_PREF_COLUMN_COUNT = 40;
/**
* The default value for {@link #prefRowCountProperty() prefRowCount}.
*/
public static final int DEFAULT_PREF_ROW_COUNT = 10;
private static final int DEFAULT_PARAGRAPH_CAPACITY = 32;
/**
* Creates a {@code TextArea} with empty text content.
*/
public TextArea() {
this("");
}
/**
* Creates a {@code TextArea} with initial text content.
*
* @param text A string for text content.
*/
public TextArea(String text) {
super(new TextAreaContent());
getStyleClass().add("text-area");
setAccessibleRole(AccessibleRole.TEXT_AREA);
setText(text);
}
@Override final void textUpdated() {
setScrollTop(0);
setScrollLeft(0);
}
/**
* Returns an unmodifiable list of the character sequences that back the
* text area's content.
* @return an unmodifiable list of the character sequences that back the
* text area's content
*/
public ObservableList<CharSequence> getParagraphs() {
return ((TextAreaContent)getContent()).paragraphList;
}
/* *************************************************************************
* *
* Properties *
* *
**************************************************************************/
/**
* If a run of text exceeds the width of the {@code TextArea},
* then this variable indicates whether the text should wrap onto
* another line.
*/
private BooleanProperty wrapText = new StyleableBooleanProperty(false) {
@Override public Object getBean() {
return TextArea.this;
}
@Override public String getName() {
return "wrapText";
}
@Override public CssMetaData<TextArea,Boolean> getCssMetaData() {
return StyleableProperties.WRAP_TEXT;
}
};
public final BooleanProperty wrapTextProperty() { return wrapText; }
public final boolean isWrapText() { return wrapText.getValue(); }
public final void setWrapText(boolean value) { wrapText.setValue(value); }
/**
* The preferred number of text columns. This is used for
* calculating the {@code TextArea}'s preferred width.
*/
private IntegerProperty prefColumnCount = new StyleableIntegerProperty(DEFAULT_PREF_COLUMN_COUNT) {
private int oldValue = get();
@Override
protected void invalidated() {
int value = get();
if (value < 0) {
if (isBound()) {
unbind();
}
set(oldValue);
throw new IllegalArgumentException("value cannot be negative.");
}
oldValue = value;
}
@Override public CssMetaData<TextArea,Number> getCssMetaData() {
return StyleableProperties.PREF_COLUMN_COUNT;
}
@Override
public Object getBean() {
return TextArea.this;
}
@Override
public String getName() {
return "prefColumnCount";
}
};
public final IntegerProperty prefColumnCountProperty() { return prefColumnCount; }
public final int getPrefColumnCount() { return prefColumnCount.getValue(); }
public final void setPrefColumnCount(int value) { prefColumnCount.setValue(value); }
/**
* The preferred number of text rows. This is used for calculating
* the {@code TextArea}'s preferred height.
*/
private IntegerProperty prefRowCount = new StyleableIntegerProperty(DEFAULT_PREF_ROW_COUNT) {
private int oldValue = get();
@Override
protected void invalidated() {
int value = get();
if (value < 0) {
if (isBound()) {
unbind();
}
set(oldValue);
throw new IllegalArgumentException("value cannot be negative.");
}
oldValue = value;
}
@Override public CssMetaData<TextArea,Number> getCssMetaData() {
return StyleableProperties.PREF_ROW_COUNT;
}
@Override
public Object getBean() {
return TextArea.this;
}
@Override
public String getName() {
return "prefRowCount";
}
};
public final IntegerProperty prefRowCountProperty() { return prefRowCount; }
public final int getPrefRowCount() { return prefRowCount.getValue(); }
public final void setPrefRowCount(int value) { prefRowCount.setValue(value); }
/**
* The number of pixels by which the content is vertically
* scrolled.
*/
private DoubleProperty scrollTop = new SimpleDoubleProperty(this, "scrollTop", 0);
public final DoubleProperty scrollTopProperty() { return scrollTop; }
public final double getScrollTop() { return scrollTop.getValue(); }
public final void setScrollTop(double value) { scrollTop.setValue(value); }
/**
* The number of pixels by which the content is horizontally
* scrolled.
*/
private DoubleProperty scrollLeft = new SimpleDoubleProperty(this, "scrollLeft", 0);
public final DoubleProperty scrollLeftProperty() { return scrollLeft; }
public final double getScrollLeft() { return scrollLeft.getValue(); }
public final void setScrollLeft(double value) { scrollLeft.setValue(value); }
/* *************************************************************************
* *
* Methods *
* *
**************************************************************************/
/** {@inheritDoc} */
@Override protected Skin<?> createDefaultSkin() {
return new TextAreaSkin(this);
}
@Override
String filterInput(String text) {
return TextInputControl.filterInput(text, false, false);
}
/* *************************************************************************
* *
* Stylesheet Handling *
* *
**************************************************************************/
private static class StyleableProperties {
private static final CssMetaData<TextArea,Number> PREF_COLUMN_COUNT =
new CssMetaData<>("-fx-pref-column-count",
SizeConverter.getInstance(), DEFAULT_PREF_COLUMN_COUNT) {
@Override
public boolean isSettable(TextArea n) {
return !n.prefColumnCount.isBound();
}
@Override
public StyleableProperty<Number> getStyleableProperty(TextArea n) {
return (StyleableProperty<Number>)n.prefColumnCountProperty();
}
};
private static final CssMetaData<TextArea,Number> PREF_ROW_COUNT =
new CssMetaData<>("-fx-pref-row-count",
SizeConverter.getInstance(), DEFAULT_PREF_ROW_COUNT) {
@Override
public boolean isSettable(TextArea n) {
return !n.prefRowCount.isBound();
}
@Override
public StyleableProperty<Number> getStyleableProperty(TextArea n) {
return (StyleableProperty<Number>)n.prefRowCountProperty();
}
};
private static final CssMetaData<TextArea,Boolean> WRAP_TEXT =
new CssMetaData<>("-fx-wrap-text",
StyleConverter.getBooleanConverter(), false) {
@Override
public boolean isSettable(TextArea n) {
return !n.wrapText.isBound();
}
@Override
public StyleableProperty<Boolean> getStyleableProperty(TextArea n) {
return (StyleableProperty<Boolean>)n.wrapTextProperty();
}
};
private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
static {
final List<CssMetaData<? extends Styleable, ?>> styleables =
new ArrayList<>(TextInputControl.getClassCssMetaData());
styleables.add(PREF_COLUMN_COUNT);
styleables.add(PREF_ROW_COUNT);
styleables.add(WRAP_TEXT);
STYLEABLES = Collections.unmodifiableList(styleables);
}
}
/**
* Gets the {@code CssMetaData} associated with this class, which may include the
* {@code CssMetaData} of its superclasses.
* @return the {@code CssMetaData}
* @since JavaFX 8.0
*/
public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
return StyleableProperties.STYLEABLES;
}
/**
* {@inheritDoc}
* @since JavaFX 8.0
*/
@Override
public List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() {
return getClassCssMetaData();
}
}