From ca3b771cb00cc7b051dcd1080ef5e8ba9a2df140 Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Thu, 3 Dec 2015 20:42:22 -0800 Subject: [PATCH] Changed MouseEvent handling from EventStreams (not overridable) to EventHandlerTemplates (overridable) --- .../richtext/StyledTextAreaBehavior.java | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java index b9ae1c92c..3539d3233 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/StyledTextAreaBehavior.java @@ -45,6 +45,11 @@ class StyledTextAreaBehavior implements Behavior { private static final EventHandlerTemplate KEY_PRESSED_TEMPLATE; private static final EventHandlerTemplate KEY_TYPED_TEMPLATE; + private static final EventHandlerTemplate MOUSE_PRESSED_TEMPLATE; + private static final EventHandlerTemplate MOUSE_DRAGGED_TEMPLATE; + private static final EventHandlerTemplate DRAG_DETECTED_TEMPLATE; + private static final EventHandlerTemplate MOUSE_RELEASED_TEMPLATE; + static { SelectionPolicy selPolicy = isMac ? SelectionPolicy.EXTEND @@ -163,6 +168,26 @@ class StyledTextAreaBehavior implements Behavior { .create() .onlyWhen(b -> b.area.isEditable()); + + MOUSE_PRESSED_TEMPLATE = EventHandlerTemplate + .on(MouseEvent.MOUSE_PRESSED) + .act(StyledTextAreaBehavior::mousePressed) + .create(); + + MOUSE_DRAGGED_TEMPLATE = EventHandlerTemplate + .on(MouseEvent.MOUSE_DRAGGED) + .act(StyledTextAreaBehavior::mouseDragged) + .create(); + + DRAG_DETECTED_TEMPLATE = EventHandlerTemplate + .on(MouseEvent.DRAG_DETECTED) + .act(StyledTextAreaBehavior::dragDetected) + .create(); + + MOUSE_RELEASED_TEMPLATE = EventHandlerTemplate + .on(MouseEvent.MOUSE_RELEASED) + .act(StyledTextAreaBehavior::mouseReleased) + .create(); } /** @@ -218,18 +243,28 @@ private CaretOffsetX getTargetCaretOffset() { EventHandler keyPressedHandler = KEY_PRESSED_TEMPLATE.bind(this); EventHandler keyTypedHandler = KEY_TYPED_TEMPLATE.bind(this); + EventHandler mousePressedHandler = MOUSE_PRESSED_TEMPLATE.bind(this); + EventHandler mouseDraggedHandler = MOUSE_DRAGGED_TEMPLATE.bind(this); + EventHandler dragDetectedHandler = DRAG_DETECTED_TEMPLATE.bind(this); + EventHandler mouseReleasedHandler = MOUSE_RELEASED_TEMPLATE.bind(this); + EventHandlerHelper.installAfter(area.onKeyPressedProperty(), keyPressedHandler); EventHandlerHelper.installAfter(area.onKeyTypedProperty(), keyTypedHandler); - subscription = Subscription.multi( - eventsOf(area, MouseEvent.MOUSE_PRESSED).subscribe(this::mousePressed), - eventsOf(area, MouseEvent.MOUSE_DRAGGED).subscribe(this::mouseDragged), - eventsOf(area, MouseEvent.DRAG_DETECTED).subscribe(this::dragDetected), - eventsOf(area, MouseEvent.MOUSE_RELEASED).subscribe(this::mouseReleased), - () -> { + EventHandlerHelper.installAfter(area.onMousePressedProperty(), mousePressedHandler); + EventHandlerHelper.installAfter(area.onMouseDraggedProperty(), mouseDraggedHandler); + EventHandlerHelper.installAfter(area.onDragDetectedProperty(), dragDetectedHandler); + EventHandlerHelper.installAfter(area.onMouseReleasedProperty(), mouseReleasedHandler); + + subscription = () -> { EventHandlerHelper.remove(area.onKeyPressedProperty(), keyPressedHandler); EventHandlerHelper.remove(area.onKeyTypedProperty(), keyTypedHandler); - }); + + EventHandlerHelper.remove(area.onMousePressedProperty(), mousePressedHandler); + EventHandlerHelper.remove(area.onMouseDraggedProperty(), mouseDraggedHandler); + EventHandlerHelper.remove(area.onDragDetectedProperty(), dragDetectedHandler); + EventHandlerHelper.remove(area.onMouseReleasedProperty(), mouseReleasedHandler); + }; // setup auto-scroll Val projection = Val.combine(