Skip to content

Commit 6edfa2c

Browse files
Merge pull request #542 from JordanMartinez/cleanup
Rename STABehavior; simplify EditableStyledDocument#plainTextChanges default method
2 parents 14c284a + d3c6ea4 commit 6edfa2c

4 files changed

Lines changed: 43 additions & 39 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ public GenericStyledArea(
688688
: EventStreams.never())
689689
.subscribe(evt -> Event.fireEvent(this, evt));
690690

691-
new StyledTextAreaBehavior(this);
691+
new GenericStyledAreaBehavior(this);
692692
}
693693

694694
/* ********************************************************************** *

richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java renamed to richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledAreaBehavior.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Controller for GenericStyledArea.
3434
*/
35-
class StyledTextAreaBehavior {
35+
class GenericStyledAreaBehavior {
3636

3737
private static final boolean isMac;
3838
private static final boolean isWindows;
@@ -42,19 +42,19 @@ class StyledTextAreaBehavior {
4242
isWindows = os.startsWith("Windows");
4343
}
4444

45-
private static final InputMapTemplate<StyledTextAreaBehavior, ? super Event> EVENT_TEMPLATE;
45+
private static final InputMapTemplate<GenericStyledAreaBehavior, ? super Event> EVENT_TEMPLATE;
4646

4747
static {
4848
SelectionPolicy selPolicy = isMac
4949
? SelectionPolicy.EXTEND
5050
: SelectionPolicy.ADJUST;
5151

52-
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> editsBase = sequence(
52+
InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> editsBase = sequence(
5353
// deletion
54-
consume(keyPressed(DELETE), StyledTextAreaBehavior::deleteForward),
55-
consume(keyPressed(BACK_SPACE), StyledTextAreaBehavior::deleteBackward),
56-
consume(keyPressed(DELETE, SHORTCUT_DOWN), StyledTextAreaBehavior::deleteNextWord),
57-
consume(keyPressed(BACK_SPACE, SHORTCUT_DOWN), StyledTextAreaBehavior::deletePrevWord),
54+
consume(keyPressed(DELETE), GenericStyledAreaBehavior::deleteForward),
55+
consume(keyPressed(BACK_SPACE), GenericStyledAreaBehavior::deleteBackward),
56+
consume(keyPressed(DELETE, SHORTCUT_DOWN), GenericStyledAreaBehavior::deleteNextWord),
57+
consume(keyPressed(BACK_SPACE, SHORTCUT_DOWN), GenericStyledAreaBehavior::deletePrevWord),
5858
// cut
5959
consume(
6060
anyOf(keyPressed(CUT), keyPressed(X, SHORTCUT_DOWN), keyPressed(DELETE, SHIFT_DOWN)),
@@ -72,9 +72,9 @@ class StyledTextAreaBehavior {
7272
anyOf(keyPressed(Y, SHORTCUT_DOWN), keyPressed(Z, SHORTCUT_DOWN, SHIFT_DOWN)),
7373
(b, e) -> b.view.redo())
7474
);
75-
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> edits = when(b -> b.view.isEditable(), editsBase);
75+
InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> edits = when(b -> b.view.isEditable(), editsBase);
7676

77-
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> verticalNavigation = sequence(
77+
InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> verticalNavigation = sequence(
7878
// vertical caret movement
7979
consume(
8080
anyOf(keyPressed(UP), keyPressed(KP_UP)),
@@ -95,10 +95,10 @@ class StyledTextAreaBehavior {
9595
consume(keyPressed(PAGE_DOWN, SHIFT_DOWN), (b, e) -> b.view.nextPage(SelectionPolicy.ADJUST))
9696
);
9797

98-
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> otherNavigation = sequence(
98+
InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> otherNavigation = sequence(
9999
// caret movement
100-
consume(anyOf(keyPressed(RIGHT), keyPressed(KP_RIGHT)), StyledTextAreaBehavior::right),
101-
consume(anyOf(keyPressed(LEFT), keyPressed(KP_LEFT)), StyledTextAreaBehavior::left),
100+
consume(anyOf(keyPressed(RIGHT), keyPressed(KP_RIGHT)), GenericStyledAreaBehavior::right),
101+
consume(anyOf(keyPressed(LEFT), keyPressed(KP_LEFT)), GenericStyledAreaBehavior::left),
102102
consume(keyPressed(HOME), (b, e) -> b.view.lineStart(SelectionPolicy.CLEAR)),
103103
consume(keyPressed(END), (b, e) -> b.view.lineEnd(SelectionPolicy.CLEAR)),
104104
consume(
@@ -118,12 +118,12 @@ class StyledTextAreaBehavior {
118118
anyOf(
119119
keyPressed(RIGHT, SHIFT_DOWN),
120120
keyPressed(KP_RIGHT, SHIFT_DOWN)
121-
), StyledTextAreaBehavior::selectRight),
121+
), GenericStyledAreaBehavior::selectRight),
122122
consume(
123123
anyOf(
124124
keyPressed(LEFT, SHIFT_DOWN),
125125
keyPressed(KP_LEFT, SHIFT_DOWN)
126-
), StyledTextAreaBehavior::selectLeft),
126+
), GenericStyledAreaBehavior::selectLeft),
127127
consume(keyPressed(HOME, SHIFT_DOWN), (b, e) -> b.view.lineStart(selPolicy)),
128128
consume(keyPressed(END, SHIFT_DOWN), (b, e) -> b.view.lineEnd(selPolicy)),
129129
consume(keyPressed(HOME, SHIFT_DOWN, SHORTCUT_DOWN), (b, e) -> b.view.start(selPolicy)),
@@ -141,7 +141,7 @@ class StyledTextAreaBehavior {
141141
consume(keyPressed(A, SHORTCUT_DOWN), (b, e) -> b.view.selectAll())
142142
);
143143

144-
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> copyAction = consume(
144+
InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> copyAction = consume(
145145
anyOf(
146146
keyPressed(COPY),
147147
keyPressed(C, SHORTCUT_DOWN),
@@ -160,9 +160,9 @@ class StyledTextAreaBehavior {
160160
e.getCode().isDigitKey() ||
161161
e.getCode().isWhitespaceKey();
162162

163-
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> charPressConsumer = consume(keyPressed().onlyIf(isChar.and(noControlKeys)));
163+
InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> charPressConsumer = consume(keyPressed().onlyIf(isChar.and(noControlKeys)));
164164

165-
InputMapTemplate<StyledTextAreaBehavior, ? super KeyEvent> keyPressedTemplate = edits
165+
InputMapTemplate<GenericStyledAreaBehavior, ? super KeyEvent> keyPressedTemplate = edits
166166
.orElse(otherNavigation).ifConsumed((b, e) -> b.view.clearTargetCaretOffset())
167167
.orElse(verticalNavigation)
168168
.orElse(copyAction)
@@ -171,14 +171,14 @@ class StyledTextAreaBehavior {
171171
// requestFollowCaret is called in keyTypedTemplate
172172
.orElse(charPressConsumer);
173173

174-
InputMapTemplate<StyledTextAreaBehavior, KeyEvent> keyTypedBase = consume(
174+
InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> keyTypedBase = consume(
175175
// character input
176176
EventPattern.keyTyped().onlyIf(noControlKeys.and(e -> isLegal(e.getCharacter()))),
177-
StyledTextAreaBehavior::keyTyped
177+
GenericStyledAreaBehavior::keyTyped
178178
).ifConsumed((b, e) -> b.view.requestFollowCaret());
179-
InputMapTemplate<StyledTextAreaBehavior, ? super KeyEvent> keyTypedTemplate = when(b -> b.view.isEditable(), keyTypedBase);
179+
InputMapTemplate<GenericStyledAreaBehavior, ? super KeyEvent> keyTypedTemplate = when(b -> b.view.isEditable(), keyTypedBase);
180180

181-
InputMapTemplate<StyledTextAreaBehavior, ? super MouseEvent> mousePressedTemplate = sequence(
181+
InputMapTemplate<GenericStyledAreaBehavior, ? super MouseEvent> mousePressedTemplate = sequence(
182182
// ignore mouse pressed events if the view is disabled
183183
process(mousePressed(MouseButton.PRIMARY), (b, e) -> b.view.isDisabled() ? Result.IGNORE : Result.PROCEED),
184184

@@ -191,59 +191,59 @@ class StyledTextAreaBehavior {
191191
),
192192
consume(
193193
mousePressed(MouseButton.PRIMARY).onlyIf(MouseEvent::isShiftDown),
194-
StyledTextAreaBehavior::handleShiftPress
194+
GenericStyledAreaBehavior::handleShiftPress
195195
),
196196
consume(
197197
mousePressed(MouseButton.PRIMARY).onlyIf(e -> e.getClickCount() == 1),
198-
StyledTextAreaBehavior::handleFirstPrimaryPress
198+
GenericStyledAreaBehavior::handleFirstPrimaryPress
199199
),
200200
consume(
201201
mousePressed(MouseButton.PRIMARY).onlyIf(e -> e.getClickCount() == 2),
202-
StyledTextAreaBehavior::handleSecondPress
202+
GenericStyledAreaBehavior::handleSecondPress
203203
),
204204
consume(
205205
mousePressed(MouseButton.PRIMARY).onlyIf(e -> e.getClickCount() == 3),
206-
StyledTextAreaBehavior::handleThirdPress
206+
GenericStyledAreaBehavior::handleThirdPress
207207
)
208208
);
209209

210210
Predicate<MouseEvent> primaryOnlyButton = e -> e.getButton() == MouseButton.PRIMARY && !e.isMiddleButtonDown() && !e.isSecondaryButtonDown();
211211

212-
InputMapTemplate<StyledTextAreaBehavior, ? super MouseEvent> mouseDragDetectedTemplate = consume(
212+
InputMapTemplate<GenericStyledAreaBehavior, ? super MouseEvent> mouseDragDetectedTemplate = consume(
213213
eventType(MouseEvent.DRAG_DETECTED).onlyIf(primaryOnlyButton),
214214
(b, e) -> b.handlePrimaryOnlyDragDetected()
215215
);
216216

217-
InputMapTemplate<StyledTextAreaBehavior, ? super MouseEvent> mouseDragTemplate = sequence(
217+
InputMapTemplate<GenericStyledAreaBehavior, ? super MouseEvent> mouseDragTemplate = sequence(
218218
process(
219219
mouseDragged().onlyIf(primaryOnlyButton),
220-
StyledTextAreaBehavior::processPrimaryOnlyMouseDragged
220+
GenericStyledAreaBehavior::processPrimaryOnlyMouseDragged
221221
),
222222
consume(
223223
mouseDragged(),
224-
StyledTextAreaBehavior::continueOrStopAutoScroll
224+
GenericStyledAreaBehavior::continueOrStopAutoScroll
225225
)
226226
);
227227

228-
InputMapTemplate<StyledTextAreaBehavior, ? super MouseEvent> mouseReleasedTemplate = sequence(
228+
InputMapTemplate<GenericStyledAreaBehavior, ? super MouseEvent> mouseReleasedTemplate = sequence(
229229
process(
230230
EventPattern.mouseReleased().onlyIf(primaryOnlyButton),
231-
StyledTextAreaBehavior::processMouseReleased
231+
GenericStyledAreaBehavior::processMouseReleased
232232
),
233233
consume(
234234
mouseReleased(),
235235
(b, e) -> b.autoscrollTo.setValue(null) // stop auto scroll
236236
)
237237
);
238238

239-
InputMapTemplate<StyledTextAreaBehavior, ? super MouseEvent> mouseTemplate = sequence(
239+
InputMapTemplate<GenericStyledAreaBehavior, ? super MouseEvent> mouseTemplate = sequence(
240240
mousePressedTemplate, mouseDragDetectedTemplate, mouseDragTemplate, mouseReleasedTemplate
241241
);
242242

243-
InputMapTemplate<StyledTextAreaBehavior, ? super ContextMenuEvent> contextMenuEventTemplate = consumeWhen(
243+
InputMapTemplate<GenericStyledAreaBehavior, ? super ContextMenuEvent> contextMenuEventTemplate = consumeWhen(
244244
EventPattern.eventType(ContextMenuEvent.CONTEXT_MENU_REQUESTED),
245245
b -> !b.view.isDisabled() && b.view.isContextMenuPresent(),
246-
StyledTextAreaBehavior::showContextMenu
246+
GenericStyledAreaBehavior::showContextMenu
247247
);
248248

249249
EVENT_TEMPLATE = sequence(mouseTemplate, keyPressedTemplate, keyTypedTemplate, contextMenuEventTemplate);
@@ -280,7 +280,7 @@ private enum DragState {
280280
* Constructors *
281281
* ********************************************************************** */
282282

283-
StyledTextAreaBehavior(GenericStyledArea<?, ?, ?> area) {
283+
GenericStyledAreaBehavior(GenericStyledArea<?, ?, ?> area) {
284284
this.view = area;
285285

286286
InputMapTemplate.installFallback(EVENT_TEMPLATE, this, b -> b.view);
@@ -289,7 +289,7 @@ private enum DragState {
289289
Val<Point2D> projection = Val.combine(
290290
autoscrollTo,
291291
area.layoutBoundsProperty(),
292-
StyledTextAreaBehavior::project);
292+
GenericStyledAreaBehavior::project);
293293
Val<Point2D> distance = Val.combine(
294294
autoscrollTo,
295295
projection,

richtextfx/src/main/java/org/fxmisc/richtext/model/EditableStyledDocument.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public interface EditableStyledDocument<PS, SEG, S> extends StyledDocument<PS, S
4545

4646
default EventStream<PlainTextChange> plainChanges() {
4747
return richChanges()
48-
.map(c -> new PlainTextChange(c.position, c.removed.getText(), c.inserted.getText()))
48+
.map(RichTextChange::toPlainTextChange)
4949
// filter out rich changes where the style was changed but text wasn't added/removed
50-
.filter(pc -> !pc.removed.equals(pc.inserted));
50+
.filter(pc -> !pc.isIdentity());
5151
}
5252

5353
EventStream<RichTextChange<PS, SEG, S>> richChanges();

richtextfx/src/main/java/org/fxmisc/richtext/model/RichTextChange.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ protected final StyledDocument<PS, SEG, S> sub(StyledDocument<PS, SEG, S> doc, i
3030
protected final RichTextChange<PS, SEG, S> create(int position, StyledDocument<PS, SEG, S> removed, StyledDocument<PS, SEG, S> inserted) {
3131
return new RichTextChange<>(position, removed, inserted);
3232
}
33+
34+
public final PlainTextChange toPlainTextChange() {
35+
return new PlainTextChange(position, removed.getText(), inserted.getText());
36+
}
3337
}

0 commit comments

Comments
 (0)