-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathEditableStyledDocument.java
487 lines (407 loc) · 20.4 KB
/
EditableStyledDocument.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
package org.fxmisc.richtext;
import static org.fxmisc.richtext.ReadOnlyStyledDocument.ParagraphsPolicy.*;
import static org.fxmisc.richtext.TwoDimensional.Bias.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.fxmisc.richtext.ReadOnlyStyledDocument.ParagraphsPolicy;
import org.reactfx.EventSource;
import org.reactfx.EventStream;
import org.reactfx.EventStreams;
import org.reactfx.Guard;
import org.reactfx.SuspendableNo;
import org.reactfx.util.Lists;
import org.reactfx.value.SuspendableVar;
import org.reactfx.value.Val;
import org.reactfx.value.Var;
/**
* Content model for {@link StyledTextArea}. Implements edit operations
* on styled text, but not worrying about additional aspects such as
* caret or selection.
*/
final class EditableStyledDocument<S, PS> extends StyledDocumentBase<S, PS, ObservableList<Paragraph<S, PS>>> {
/* ********************************************************************** *
* *
* Observables *
* *
* Observables are "dynamic" (i.e. changing) characteristics of an object.*
* They are not directly settable by the client code, but change in *
* response to user input and/or API actions. *
* *
* ********************************************************************** */
/**
* Content of this {@code StyledDocument}.
*/
private final StringBinding text = Bindings.createStringBinding(() -> getText(0, length()));
@Override
public String getText() { return text.getValue(); }
public ObservableValue<String> textProperty() { return text; }
/**
* Length of this {@code StyledDocument}.
*/
private final SuspendableVar<Integer> length = Var.newSimpleVar(0).suspendable();
public int getLength() { return length.getValue(); }
public Val<Integer> lengthProperty() { return length; }
@Override
public int length() { return length.getValue(); }
/**
* Unmodifiable observable list of styled paragraphs of this document.
*/
@Override
public ObservableList<Paragraph<S, PS>> getParagraphs() {
return FXCollections.unmodifiableObservableList(paragraphs);
}
/**
* Read-only snapshot of the current state of this document.
*/
public ReadOnlyStyledDocument<S, PS> snapshot() {
return new ReadOnlyStyledDocument<>(paragraphs, ParagraphsPolicy.COPY);
}
/* ********************************************************************** *
* *
* Event streams *
* *
* ********************************************************************** */
// To publish a text change:
// 1. push to textChangePosition,
// 2. push to textRemovalEnd,
// 3. push to insertedText.
//
// To publish a rich change:
// a)
// 1. push to textChangePosition,
// 2. push to textRemovalEnd,
// 3. push to insertedDocument;
// b)
// 1. push to styleChangePosition
// 2. push to styleChangeEnd
// 3. push to styleChangeDone.
private final EventSource<Integer> textChangePosition = new EventSource<>();
private final EventSource<Integer> styleChangePosition = new EventSource<>();
private final EventSource<Integer> textRemovalEnd = new EventSource<>();
private final EventSource<Integer> styleChangeEnd = new EventSource<>();
private final EventSource<String> insertedText = new EventSource<>();
private final EventSource<StyledDocument<S, PS>> insertedDocument = new EventSource<>();
private final EventSource<Void> styleChangeDone = new EventSource<>();
private final EventStream<PlainTextChange> plainTextChanges;
public EventStream<PlainTextChange> plainTextChanges() { return plainTextChanges; }
private final EventStream<RichTextChange<S, PS>> richChanges;
public EventStream<RichTextChange<S, PS>> richChanges() { return richChanges; }
{
EventStream<String> removedText = EventStreams.zip(textChangePosition, textRemovalEnd).map(t2 -> t2.map((a, b) -> getText(a, b)));
EventStream<Integer> changePosition = EventStreams.merge(textChangePosition, styleChangePosition);
EventStream<Integer> removalEnd = EventStreams.merge(textRemovalEnd, styleChangeEnd);
EventStream<StyledDocument<S, PS>> removedDocument = EventStreams.zip(changePosition, removalEnd).map(t2 -> t2.map((a, b) -> subSequence(a, b)));
EventStream<Integer> insertionEnd = styleChangeEnd.emitOn(styleChangeDone);
EventStream<StyledDocument<S, PS>> insertedDocument = EventStreams.merge(
this.insertedDocument,
changePosition.emitBothOnEach(insertionEnd).map(t2 -> t2.map((a, b) -> subSequence(a, b))));
plainTextChanges = EventStreams.zip(textChangePosition, removedText, insertedText)
.filter(t3 -> t3.map((pos, removed, inserted) -> !removed.equals(inserted)))
.map(t3 -> t3.map((pos, removed, inserted) -> new PlainTextChange(pos, removed, inserted)));
richChanges = EventStreams.zip(changePosition, removedDocument, insertedDocument)
.filter(t3 -> t3.map((pos, removed, inserted) -> !removed.equals(inserted)))
.map(t3 -> t3.map((pos, removed, inserted) -> new RichTextChange<>(pos, removed, inserted)));
}
/* ********************************************************************** *
* *
* Properties *
* *
* ********************************************************************** */
final BooleanProperty useInitialStyleForInsertion = new SimpleBooleanProperty();
/* ********************************************************************** *
* *
* Fields *
* *
* ********************************************************************** */
private final S initialStyle;
private final PS initialParagraphStyle;
/* ********************************************************************** *
* *
* Constructors *
* *
* ********************************************************************** */
@SuppressWarnings("unchecked")
EditableStyledDocument(S initialStyle, PS initialParagraphStyle) {
super(FXCollections.observableArrayList(new Paragraph<>(initialParagraphStyle, "", initialStyle)));
this.initialStyle = initialStyle;
this.initialParagraphStyle = initialParagraphStyle;
}
/* ********************************************************************** *
* *
* Actions *
* *
* Actions change the state of the object. They typically cause a change *
* of one or more observables and/or produce an event. *
* *
* ********************************************************************** */
public void replaceText(int start, int end, String text) {
StyledDocument<S, PS> doc = ReadOnlyStyledDocument.fromString(
text, getStyleForInsertionAt(start), getParagraphStyleForInsertionAt(start));
replace(start, end, doc);
}
public void replace(int start, int end, StyledDocument<S, PS> replacement) {
ensureValidRange(start, end);
textChangePosition.push(start);
textRemovalEnd.push(end);
Position start2D = navigator.offsetToPosition(start, Forward);
Position end2D = start2D.offsetBy(end - start, Forward);
int firstParIdx = start2D.getMajor();
int firstParFrom = start2D.getMinor();
int lastParIdx = end2D.getMajor();
int lastParTo = end2D.getMinor();
// Get the leftovers after cutting out the deletion
Paragraph<S, PS> firstPar = paragraphs.get(firstParIdx).trim(firstParFrom);
Paragraph<S, PS> lastPar = paragraphs.get(lastParIdx).subSequence(lastParTo);
List<Paragraph<S, PS>> replacementPars = replacement.getParagraphs();
List<Paragraph<S, PS>> newPars = join(firstPar, replacementPars, lastPar);
setAll(firstParIdx, lastParIdx + 1, newPars);
// update length, invalidate text
int replacementLength =
replacementPars.stream().mapToInt(Paragraph::length).sum() +
replacementPars.size() - 1;
int newLength = length.getValue() - (end - start) + replacementLength;
length.suspendWhile(() -> { // don't publish length change until text is invalidated
length.setValue(newLength);
text.invalidate();
});
// complete the change events
insertedText.push(replacement.toString());
StyledDocument<S, PS> doc =
replacement instanceof ReadOnlyStyledDocument
? replacement
: new ReadOnlyStyledDocument<>(replacement.getParagraphs(), COPY);
insertedDocument.push(doc);
}
public void setStyle(int from, int to, S style) {
ensureValidRange(from, to);
try(Guard commitOnClose = beginStyleChange(from, to)) {
Position start = navigator.offsetToPosition(from, Forward);
Position end = to == from
? start
: start.offsetBy(to - from, Backward);
int firstParIdx = start.getMajor();
int firstParFrom = start.getMinor();
int lastParIdx = end.getMajor();
int lastParTo = end.getMinor();
if(firstParIdx == lastParIdx) {
Paragraph<S, PS> p = paragraphs.get(firstParIdx);
p = p.restyle(firstParFrom, lastParTo, style);
paragraphs.set(firstParIdx, p);
} else {
int affectedPars = lastParIdx - firstParIdx + 1;
List<Paragraph<S, PS>> restyledPars = new ArrayList<>(affectedPars);
Paragraph<S, PS> firstPar = paragraphs.get(firstParIdx);
restyledPars.add(firstPar.restyle(firstParFrom, firstPar.length(), style));
for(int i = firstParIdx + 1; i < lastParIdx; ++i) {
Paragraph<S, PS> p = paragraphs.get(i);
restyledPars.add(p.restyle(style));
}
Paragraph<S, PS> lastPar = paragraphs.get(lastParIdx);
restyledPars.add(lastPar.restyle(0, lastParTo, style));
setAll(firstParIdx, lastParIdx + 1, restyledPars);
}
}
}
public void setStyle(int paragraph, S style) {
Paragraph<S, PS> p = paragraphs.get(paragraph);
int start = position(paragraph, 0).toOffset();
int end = start + p.length();
try(Guard commitOnClose = beginStyleChange(start, end)) {
p = p.restyle(style);
paragraphs.set(paragraph, p);
}
}
public void setStyle(int paragraph, int fromCol, int toCol, S style) {
ensureValidParagraphRange(paragraph, fromCol, toCol);
int parOffset = position(paragraph, 0).toOffset();
int start = parOffset + fromCol;
int end = parOffset + toCol;
try(Guard commitOnClose = beginStyleChange(start, end)) {
Paragraph<S, PS> p = paragraphs.get(paragraph);
p = p.restyle(fromCol, toCol, style);
paragraphs.set(paragraph, p);
}
}
public void setStyleSpans(int from, StyleSpans<? extends S> styleSpans) {
int len = styleSpans.length();
ensureValidRange(from, from + len);
Position start = offsetToPosition(from, Forward);
Position end = start.offsetBy(len, Backward);
int skip = terminatorLengthToSkip(start);
int trim = terminatorLengthToTrim(end);
if(skip + trim >= len) {
return;
} else if(skip + trim > 0) {
styleSpans = styleSpans.subView(skip, len - trim);
len -= skip + trim;
from += skip;
start = start.offsetBy(skip, Forward);
end = end.offsetBy(-trim, Backward);
}
try(Guard commitOnClose = beginStyleChange(from, from + len)) {
int firstParIdx = start.getMajor();
int firstParFrom = start.getMinor();
int lastParIdx = end.getMajor();
int lastParTo = end.getMinor();
if(firstParIdx == lastParIdx) {
Paragraph<S, PS> p = paragraphs.get(firstParIdx);
Paragraph<S, PS> q = p.restyle(firstParFrom, styleSpans);
if(q != p) {
paragraphs.set(firstParIdx, q);
}
} else {
Paragraph<S, PS> firstPar = paragraphs.get(firstParIdx);
Position spansFrom = styleSpans.position(0, 0);
Position spansTo = spansFrom.offsetBy(firstPar.length() - firstParFrom, Backward);
Paragraph<S, PS> q = firstPar.restyle(firstParFrom, styleSpans.subView(spansFrom, spansTo));
if(q != firstPar) {
paragraphs.set(firstParIdx, q);
}
spansFrom = spansTo.offsetBy(1, Forward); // skip the newline
for(int i = firstParIdx + 1; i < lastParIdx; ++i) {
Paragraph<S, PS> par = paragraphs.get(i);
spansTo = spansFrom.offsetBy(par.length(), Backward);
q = par.restyle(0, styleSpans.subView(spansFrom, spansTo));
if(q != par) {
paragraphs.set(i, q);
}
spansFrom = spansTo.offsetBy(1, Forward); // skip the newline
}
Paragraph<S, PS> lastPar = paragraphs.get(lastParIdx);
spansTo = spansFrom.offsetBy(lastParTo, Backward);
q = lastPar.restyle(0, styleSpans.subView(spansFrom, spansTo));
if(q != lastPar) {
paragraphs.set(lastParIdx, q);
}
}
}
}
public void setStyleSpans(int paragraph, int from, StyleSpans<? extends S> styleSpans) {
int len = styleSpans.length();
ensureValidParagraphRange(paragraph, from, len);
int parOffset = position(paragraph, 0).toOffset();
int start = parOffset + from;
int end = start + len;
try(Guard commitOnClose = beginStyleChange(start, end)) {
Paragraph<S, PS> p = paragraphs.get(paragraph);
Paragraph<S, PS> q = p.restyle(from, styleSpans);
if(q != p) {
paragraphs.set(paragraph, q);
}
}
}
public void setParagraphStyle(int parIdx, PS style) {
ensureValidParagraphIndex(parIdx);
Paragraph<S, PS> par = paragraphs.get(parIdx);
int len = par.length();
int start = position(parIdx, 0).toOffset();
int end = start + len;
try(Guard commitOnClose = beginStyleChange(start, end)) {
Paragraph<S, PS> q = par.setParagraphStyle(style);
paragraphs.set(parIdx, q);
}
}
/* ********************************************************************** *
* *
* Private and package private methods *
* *
* ********************************************************************** */
private final SuspendableNo beingUpdated = new SuspendableNo();
SuspendableNo beingUpdatedProperty() { return beingUpdated; }
final boolean isBeingUpdated() { return beingUpdated.get(); }
private void ensureValidParagraphIndex(int parIdx) {
Lists.checkIndex(parIdx, paragraphs.size());
}
private void ensureValidRange(int start, int end) {
Lists.checkRange(start, end, length());
}
private void ensureValidParagraphRange(int par, int start, int end) {
ensureValidParagraphIndex(par);
Lists.checkRange(start, end, fullLength(par));
}
private int fullLength(int par) {
int n = paragraphs.size();
return paragraphs.get(par).length() + (par == n-1 ? 0 : 1);
}
private int terminatorLengthToSkip(Position pos) {
Paragraph<S, PS> par = paragraphs.get(pos.getMajor());
int skipSum = 0;
while(pos.getMinor() == par.length() && pos.getMajor() < paragraphs.size() - 1) {
skipSum += 1;
pos = pos.offsetBy(1, Forward); // will jump to the next paragraph
par = paragraphs.get(pos.getMajor());
}
return skipSum;
}
private int terminatorLengthToTrim(Position pos) {
int parLen = paragraphs.get(pos.getMajor()).length();
int trimSum = 0;
while(pos.getMinor() > parLen) {
assert pos.getMinor() - parLen == 1;
trimSum += 1;
pos = pos.offsetBy(-1, Backward); // may jump to the end of previous paragraph, if parLen was 0
parLen = paragraphs.get(pos.getMajor()).length();
}
return trimSum;
}
private Guard beginStyleChange(int start, int end) {
styleChangePosition.push(start);
styleChangeEnd.push(end);
return () -> styleChangeDone.push(null);
}
private List<Paragraph<S, PS>> join(Paragraph<S, PS> first, List<Paragraph<S, PS>> middle, Paragraph<S, PS> last) {
int m = middle.size();
if(m == 0) {
return Arrays.asList(first.concat(last));
} else if(m == 1) {
return Arrays.asList(first.concat(middle.get(0)).concat(last));
} else {
List<Paragraph<S, PS>> res = new ArrayList<>(middle.size());
res.add(first.concat(middle.get(0)));
res.addAll(middle.subList(1, m - 1));
res.add(middle.get(m-1).concat(last));
return res;
}
}
// TODO: Replace with ObservableList.setAll(from, to, col) when implemented.
// See https://javafx-jira.kenai.com/browse/RT-32655.
private void setAll(int startIdx, int endIdx, Collection<Paragraph<S, PS>> pars) {
if(startIdx > 0 || endIdx < paragraphs.size()) {
paragraphs.subList(startIdx, endIdx).clear(); // note that paragraphs remains non-empty at all times
paragraphs.addAll(startIdx, pars);
} else {
paragraphs.setAll(pars);
}
}
S getStyleForInsertionAt(int pos) {
return getStyleForInsertionAt(navigator.offsetToPosition(pos, Forward));
}
S getStyleForInsertionAt(Position insertionPos) {
if(useInitialStyleForInsertion.get()) {
return initialStyle;
} else {
Paragraph<S, PS> par = paragraphs.get(insertionPos.getMajor());
return par.getStyleAtPosition(insertionPos.getMinor());
}
}
PS getParagraphStyleForInsertionAt(int pos) {
return getParagraphStyleForInsertionAt(navigator.offsetToPosition(pos, Forward));
}
PS getParagraphStyleForInsertionAt(Position insertionPos) {
if(useInitialStyleForInsertion.get()) {
return initialParagraphStyle;
} else {
Paragraph<S, PS> par = paragraphs.get(insertionPos.getMajor());
return par.getParagraphStyle();
}
}
}