-
Notifications
You must be signed in to change notification settings - Fork 236
/
UndoUtils.java
279 lines (247 loc) · 13 KB
/
UndoUtils.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
package org.fxmisc.richtext.util;
import org.fxmisc.richtext.GenericStyledArea;
import org.fxmisc.richtext.MultiChangeBuilder;
import org.fxmisc.richtext.model.PlainTextChange;
import org.fxmisc.richtext.model.RichTextChange;
import org.fxmisc.richtext.model.TextChange;
import org.fxmisc.undo.UndoManager;
import org.fxmisc.undo.UndoManagerFactory;
import org.fxmisc.undo.impl.MultiChangeUndoManagerImpl;
import org.fxmisc.undo.impl.UnlimitedChangeQueue;
import org.reactfx.SuspendableYes;
import org.reactfx.value.Val;
import javafx.beans.value.ObservableBooleanValue;
import java.time.Duration;
import java.util.List;
import java.util.function.Consumer;
/**
* A class filled with factory methods to help easily construct an {@link UndoManager} for a {@link GenericStyledArea}.
*/
public final class UndoUtils {
private UndoUtils() {
throw new IllegalStateException("UndoUtils cannot be instantiated");
}
public static final Duration DEFAULT_PREVENT_MERGE_DELAY = Duration.ofMillis(500);
/**
* Constructs an UndoManager with an unlimited history:
* if {@link GenericStyledArea#isPreserveStyle() the area's preserveStyle flag is true}, the returned UndoManager
* can undo/redo multiple {@link RichTextChange}s; otherwise, it can undo/redo multiple {@link PlainTextChange}s.
*/
public static <PS, SEG, S> UndoManager defaultUndoManager(GenericStyledArea<PS, SEG, S> area) {
return area.isPreserveStyle()
? richTextUndoManager(area)
: plainTextUndoManager(area);
}
/**
* Constructs an UndoManager with no history
*/
public static UndoManager noOpUndoManager() {
return new UndoManager() {
private final Val<Boolean> alwaysFalse = Val.constant(false);
@Override public boolean undo() { return false; }
@Override public boolean redo() { return false; }
@Override public Val<Boolean> undoAvailableProperty() { return alwaysFalse; }
@Override public boolean isUndoAvailable() { return false; }
@Override public Val<Boolean> redoAvailableProperty() { return alwaysFalse; }
@Override public boolean isRedoAvailable() { return false; }
@Override public boolean isPerformingAction() { return false; }
@Override public boolean isAtMarkedPosition() { return false; }
// not sure whether these may throw NPEs at some point
@Override public Val nextUndoProperty() { return null; }
@Override public Val nextRedoProperty() { return null; }
@Override public ObservableBooleanValue performingActionProperty() { return null; }
@Override public UndoPosition getCurrentPosition() { return null; }
@Override public ObservableBooleanValue atMarkedPositionProperty() { return null; }
// ignore these
@Override public void preventMerge() { }
@Override public void forgetHistory() { }
@Override public void close() { }
};
}
/* ********************************************************************** *
* *
* UndoManager Factory Methods *
* *
* Code that constructs different kinds of UndoManagers for an area *
* *
* ********************************************************************** */
/**
* Returns an UndoManager with an unlimited history that can undo/redo {@link RichTextChange}s. New changes
* emitted from the stream will not be merged with the previous change
* after {@link #DEFAULT_PREVENT_MERGE_DELAY}
*/
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextUndoManager(
GenericStyledArea<PS, SEG, S> area) {
return richTextUndoManager(area, UndoManagerFactory.unlimitedHistoryFactory());
}
/**
* Returns an UndoManager that can undo/redo {@link RichTextChange}s. New changes
* emitted from the stream will not be merged with the previous change
* after {@code preventMergeDelay}
*/
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextUndoManager(
GenericStyledArea<PS, SEG, S> area, Duration preventMergeDelay) {
return richTextUndoManager(area, UndoManagerFactory.unlimitedHistoryFactory(), preventMergeDelay);
};
/**
* Returns an UndoManager that can undo/redo {@link RichTextChange}s. New changes
* emitted from the stream will not be merged with the previous change
* after {@link #DEFAULT_PREVENT_MERGE_DELAY}
*/
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextUndoManager(
GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory) {
return richTextUndoManager(area, factory, DEFAULT_PREVENT_MERGE_DELAY);
};
/**
* Returns an UndoManager that can undo/redo {@link RichTextChange}s. New changes
* emitted from the stream will not be merged with the previous change
* after {@code preventMergeDelay}
*/
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextUndoManager(
GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory, Duration preventMergeDelay) {
return factory.createMultiChangeUM(area.multiRichChanges(),
TextChange::invert,
applyMultiRichTextChange(area),
TextChange::mergeWith,
TextChange::isIdentity,
preventMergeDelay);
};
/**
* Returns an UndoManager with an unlimited history that can undo/redo {@link RichTextChange}s. New changes
* emitted from the stream will not be merged with the previous change after {@link #DEFAULT_PREVENT_MERGE_DELAY}
* <p><b>Note</b>: that <u>only styling changes</u> may occur <u>during suspension</u> of the undo manager.
*/
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextSuspendableUndoManager(
GenericStyledArea<PS, SEG, S> area, SuspendableYes suspendUndo) {
return richTextSuspendableUndoManager(area, DEFAULT_PREVENT_MERGE_DELAY, suspendUndo);
}
/**
* Returns an UndoManager with an unlimited history that can undo/redo {@link RichTextChange}s. New changes
* emitted from the stream will not be merged with the previous change after {@code preventMergeDelay}.
* <p><b>Note</b>: that <u>only styling changes</u> may occur <u>during suspension</u> of the undo manager.
*/
public static <PS, SEG, S> UndoManager<List<RichTextChange<PS, SEG, S>>> richTextSuspendableUndoManager(
GenericStyledArea<PS, SEG, S> area, Duration preventMergeDelay, SuspendableYes suspendUndo) {
RichTextChange.skipStyleComparison( true );
return new MultiChangeUndoManagerImpl<>
(
new UnlimitedChangeQueue<>(),
TextChange::invert,
applyMultiRichTextChange(area),
TextChange::mergeWith,
TextChange::isIdentity,
area.multiRichChanges().conditionOn(suspendUndo),
preventMergeDelay
);
};
/**
* Returns an UndoManager with an unlimited history that can undo/redo {@link PlainTextChange}s. New changes
* emitted from the stream will not be merged with the previous change
* after {@link #DEFAULT_PREVENT_MERGE_DELAY}
*/
public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManager(
GenericStyledArea<PS, SEG, S> area) {
return plainTextUndoManager(area, DEFAULT_PREVENT_MERGE_DELAY);
}
/**
* Returns an UndoManager that can undo/redo {@link PlainTextChange}s. New changes
* emitted from the stream will not be merged with the previous change
* after {@code preventMergeDelay}
*/
public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManager(
GenericStyledArea<PS, SEG, S> area, Duration preventMergeDelay) {
return plainTextUndoManager(area, UndoManagerFactory.unlimitedHistoryFactory(), preventMergeDelay);
}
/**
* Returns an UndoManager that can undo/redo {@link PlainTextChange}s. New changes
* emitted from the stream will not be merged with the previous change
* after {@link #DEFAULT_PREVENT_MERGE_DELAY}
*/
public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManager(
GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory) {
return plainTextUndoManager(area, factory, DEFAULT_PREVENT_MERGE_DELAY);
}
/**
* Returns an UndoManager that can undo/redo {@link PlainTextChange}s. New changes
* emitted from the stream will not be merged with the previous change
* after {@code preventMergeDelay}
*/
public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManager(
GenericStyledArea<PS, SEG, S> area, UndoManagerFactory factory, Duration preventMergeDelay) {
return factory.createMultiChangeUM(area.multiPlainChanges(),
TextChange::invert,
applyMultiPlainTextChange(area),
TextChange::mergeWith,
TextChange::isIdentity,
preventMergeDelay);
}
/* ********************************************************************** *
* *
* Change Appliers *
* *
* Code that handles how a change should be applied to the area *
* *
* ********************************************************************** */
/**
* Applies a {@link PlainTextChange} to the given area when the {@link UndoManager}'s change stream emits an event
* by {@code area.replaceText(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
*/
public static <PS, SEG, S> Consumer<PlainTextChange> applyPlainTextChange(GenericStyledArea<PS, SEG, S> area) {
return change -> {
area.replaceText(change.getPosition(), change.getRemovalEnd(), change.getInserted());
moveToChange( area, change );
};
}
/**
* Applies a {@link RichTextChange} to the given area when the {@link UndoManager}'s change stream emits an event
* by {@code area.replace(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
*/
public static <PS, SEG, S> Consumer<RichTextChange<PS, SEG, S>> applyRichTextChange(
GenericStyledArea<PS, SEG, S> area) {
return change -> {
area.replace(change.getPosition(), change.getRemovalEnd(), change.getInserted());
moveToChange( area, change );
};
}
/**
* Applies a list of {@link PlainTextChange}s to the given area when the {@link UndoManager}'s change stream emits
* an event by {@code area.replaceAbsolutely(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
*/
public static <PS, SEG, S> Consumer<List<PlainTextChange>> applyMultiPlainTextChange(
GenericStyledArea<PS, SEG, S> area) {
return changeList -> {
MultiChangeBuilder<PS, SEG, S> builder = area.createMultiChange(changeList.size());
for (PlainTextChange c : changeList) {
builder.replaceTextAbsolutely(c.getPosition(), c.getRemovalEnd(), c.getInserted());
}
builder.commit();
moveToChange( area, changeList.get( changeList.size()-1 ) );
};
}
/**
* Applies a list of {@link RichTextChange} to the given area when the {@link UndoManager}'s change stream emits
* an event by {@code area.replaceAbsolutely(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
*/
public static <PS, SEG, S> Consumer<List<RichTextChange<PS, SEG, S>>> applyMultiRichTextChange(
GenericStyledArea<PS, SEG, S> area) {
return changeList -> {
MultiChangeBuilder<PS, SEG, S> builder = area.createMultiChange(changeList.size());
for (RichTextChange<PS, SEG, S> c : changeList) {
builder.replaceAbsolutely(c.getPosition(), c.getRemovalEnd(), c.getInserted());
}
builder.commit();
moveToChange( area, changeList.get( changeList.size()-1 ) );
};
}
/*
* Address #912 "After undo/redo, new text is inserted at the end".
* Without breaking PositionTests. (org.fxmisc.richtext.api.caret)
*/
private static void moveToChange( GenericStyledArea area, TextChange chg )
{
int pos = chg.getPosition();
int len = chg.getNetLength();
if ( len > 0 ) pos += len;
area.moveTo( Math.min( pos, area.getLength() ) );
}
}