From 47c10bbc90f1e57fa7c90850401540357d4120ba Mon Sep 17 00:00:00 2001 From: h7x4 Date: Wed, 24 Feb 2021 14:58:27 +0100 Subject: [PATCH] Add events for default editing actions --- .../app/controllers/EditorController.java | 35 +++++++++++++++++++ .../app/controllers/MenubarController.java | 30 ++++++++++++++++ src/main/java/app/events/CopyEvent.java | 5 +++ src/main/java/app/events/CutEvent.java | 5 +++ src/main/java/app/events/PasteEvent.java | 5 +++ src/main/java/app/events/RedoEvent.java | 5 +++ src/main/java/app/events/UndoEvent.java | 5 +++ .../resources/fxml/components/Menubar.fxml | 10 +++--- 8 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 src/main/java/app/events/CopyEvent.java create mode 100644 src/main/java/app/events/CutEvent.java create mode 100644 src/main/java/app/events/PasteEvent.java create mode 100644 src/main/java/app/events/RedoEvent.java create mode 100644 src/main/java/app/events/UndoEvent.java diff --git a/src/main/java/app/controllers/EditorController.java b/src/main/java/app/controllers/EditorController.java index 5ee19ce..0ab4f7a 100644 --- a/src/main/java/app/controllers/EditorController.java +++ b/src/main/java/app/controllers/EditorController.java @@ -13,10 +13,15 @@ import org.fxmisc.richtext.model.StyleSpans; import org.fxmisc.richtext.model.TwoDimensional.Bias; import org.fxmisc.richtext.model.TwoDimensional.Position; +import app.events.CopyEvent; +import app.events.CutEvent; import app.events.EditorChangedEvent; import app.events.LanguageChangedEvent; +import app.events.PasteEvent; +import app.events.RedoEvent; import app.events.ToggleCommentEvent; import app.events.ToggleWrapTextEvent; +import app.events.UndoEvent; import app.events.FileSaveStateChangedEvent; import app.model.Model; import app.service.LanguageOperations; @@ -141,4 +146,34 @@ public class EditorController implements Initializable, Controller { this.setWrapText(event.getIsWrapped()); } + @Subscribe + private void handle(UndoEvent event) { + if (this.editor.isFocused()) + this.editor.undo(); + } + + @Subscribe + private void handle(RedoEvent event) { + if (this.editor.isFocused()) + this.editor.redo(); + } + + @Subscribe + private void handle(CopyEvent event) { + if (this.editor.isFocused()) + this.editor.copy(); + } + + @Subscribe + private void handle(CutEvent event) { + if (this.editor.isFocused()) + this.editor.cut(); + } + + @Subscribe + private void handle(PasteEvent event) { + if (this.editor.isFocused()) + this.editor.paste(); + } + } diff --git a/src/main/java/app/controllers/MenubarController.java b/src/main/java/app/controllers/MenubarController.java index a7ce617..7fa391f 100644 --- a/src/main/java/app/controllers/MenubarController.java +++ b/src/main/java/app/controllers/MenubarController.java @@ -7,12 +7,17 @@ import java.util.ResourceBundle; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; +import app.events.CopyEvent; +import app.events.CutEvent; import app.events.ExitApplicationEvent; import app.events.LanguageChangedEvent; import app.events.OpenLinkInBrowserEvent; +import app.events.PasteEvent; +import app.events.RedoEvent; import app.events.ThemeChangedEvent; import app.events.ToggleCommentEvent; import app.events.ToggleWrapTextEvent; +import app.events.UndoEvent; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -128,6 +133,31 @@ public class MenubarController implements Initializable, Controller { /* EDIT */ /* ------------------------------------------------------------------------ */ + @FXML + private void handleUndo(ActionEvent event) { + this.eventBus.post(new UndoEvent()); + } + + @FXML + private void handleRedo(ActionEvent event) { + this.eventBus.post(new RedoEvent()); + } + + @FXML + private void handleCopy(ActionEvent event) { + this.eventBus.post(new CopyEvent()); + } + + @FXML + private void handleCut(ActionEvent event) { + this.eventBus.post(new CutEvent()); + } + + @FXML + private void handlePaste(ActionEvent event) { + this.eventBus.post(new PasteEvent()); + } + @FXML private void handleToggleComment(ActionEvent event) { this.eventBus.post(new ToggleCommentEvent()); diff --git a/src/main/java/app/events/CopyEvent.java b/src/main/java/app/events/CopyEvent.java new file mode 100644 index 0000000..1a33b4e --- /dev/null +++ b/src/main/java/app/events/CopyEvent.java @@ -0,0 +1,5 @@ +package app.events; + +public class CopyEvent extends Event { + +} diff --git a/src/main/java/app/events/CutEvent.java b/src/main/java/app/events/CutEvent.java new file mode 100644 index 0000000..b2c6309 --- /dev/null +++ b/src/main/java/app/events/CutEvent.java @@ -0,0 +1,5 @@ +package app.events; + +public class CutEvent extends Event { + +} diff --git a/src/main/java/app/events/PasteEvent.java b/src/main/java/app/events/PasteEvent.java new file mode 100644 index 0000000..ae904b0 --- /dev/null +++ b/src/main/java/app/events/PasteEvent.java @@ -0,0 +1,5 @@ +package app.events; + +public class PasteEvent extends Event { + +} diff --git a/src/main/java/app/events/RedoEvent.java b/src/main/java/app/events/RedoEvent.java new file mode 100644 index 0000000..90f15c8 --- /dev/null +++ b/src/main/java/app/events/RedoEvent.java @@ -0,0 +1,5 @@ +package app.events; + +public class RedoEvent extends Event { + +} diff --git a/src/main/java/app/events/UndoEvent.java b/src/main/java/app/events/UndoEvent.java new file mode 100644 index 0000000..e7fab08 --- /dev/null +++ b/src/main/java/app/events/UndoEvent.java @@ -0,0 +1,5 @@ +package app.events; + +public class UndoEvent extends Event { + +} diff --git a/src/main/resources/fxml/components/Menubar.fxml b/src/main/resources/fxml/components/Menubar.fxml index b5ed590..f5cc2da 100644 --- a/src/main/resources/fxml/components/Menubar.fxml +++ b/src/main/resources/fxml/components/Menubar.fxml @@ -73,12 +73,12 @@ - - + + - - - + + +