Add events for default editing actions

This commit is contained in:
Oystein Kristoffer Tveit 2021-02-24 14:58:27 +01:00
parent 8ddcb9743e
commit 47c10bbc90
8 changed files with 95 additions and 5 deletions

View File

@ -13,10 +13,15 @@ import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.TwoDimensional.Bias; import org.fxmisc.richtext.model.TwoDimensional.Bias;
import org.fxmisc.richtext.model.TwoDimensional.Position; import org.fxmisc.richtext.model.TwoDimensional.Position;
import app.events.CopyEvent;
import app.events.CutEvent;
import app.events.EditorChangedEvent; import app.events.EditorChangedEvent;
import app.events.LanguageChangedEvent; import app.events.LanguageChangedEvent;
import app.events.PasteEvent;
import app.events.RedoEvent;
import app.events.ToggleCommentEvent; import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent; import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent;
import app.events.FileSaveStateChangedEvent; import app.events.FileSaveStateChangedEvent;
import app.model.Model; import app.model.Model;
import app.service.LanguageOperations; import app.service.LanguageOperations;
@ -141,4 +146,34 @@ public class EditorController implements Initializable, Controller {
this.setWrapText(event.getIsWrapped()); 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();
}
} }

View File

@ -7,12 +7,17 @@ import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus; import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import app.events.CopyEvent;
import app.events.CutEvent;
import app.events.ExitApplicationEvent; import app.events.ExitApplicationEvent;
import app.events.LanguageChangedEvent; import app.events.LanguageChangedEvent;
import app.events.OpenLinkInBrowserEvent; import app.events.OpenLinkInBrowserEvent;
import app.events.PasteEvent;
import app.events.RedoEvent;
import app.events.ThemeChangedEvent; import app.events.ThemeChangedEvent;
import app.events.ToggleCommentEvent; import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent; import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
@ -128,6 +133,31 @@ public class MenubarController implements Initializable, Controller {
/* EDIT */ /* 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 @FXML
private void handleToggleComment(ActionEvent event) { private void handleToggleComment(ActionEvent event) {
this.eventBus.post(new ToggleCommentEvent()); this.eventBus.post(new ToggleCommentEvent());

View File

@ -0,0 +1,5 @@
package app.events;
public class CopyEvent extends Event {
}

View File

@ -0,0 +1,5 @@
package app.events;
public class CutEvent extends Event {
}

View File

@ -0,0 +1,5 @@
package app.events;
public class PasteEvent extends Event {
}

View File

@ -0,0 +1,5 @@
package app.events;
public class RedoEvent extends Event {
}

View File

@ -0,0 +1,5 @@
package app.events;
public class UndoEvent extends Event {
}

View File

@ -73,12 +73,12 @@
</Menu> </Menu>
<Menu mnemonicParsing="false" text="Edit"> <Menu mnemonicParsing="false" text="Edit">
<items> <items>
<MenuItem mnemonicParsing="false" text="Undo" accelerator="Shortcut+z"/> <MenuItem mnemonicParsing="false" text="Undo" accelerator="Shortcut+z" onAction="#handleUndo"/>
<MenuItem mnemonicParsing="false" text="Redo" accelerator="Shortcut+y"/> <MenuItem mnemonicParsing="false" text="Redo" accelerator="Shortcut+y" onAction="#handleRedo"/>
<SeparatorMenuItem/> <SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Cut" accelerator="Shortcut+x"/> <MenuItem mnemonicParsing="false" text="Cut" accelerator="Shortcut+x" onAction="#handleCut"/>
<MenuItem mnemonicParsing="false" text="Copy" accelerator="Shortcut+c"/> <MenuItem mnemonicParsing="false" text="Copy" accelerator="Shortcut+c" onAction="#handleCopy"/>
<MenuItem mnemonicParsing="false" text="Paste" accelerator="Shortcut+v"/> <MenuItem mnemonicParsing="false" text="Paste" accelerator="Shortcut+v" onAction="#handlePaste"/>
<SeparatorMenuItem/> <SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Find" accelerator="Shortcut+f"/> <MenuItem mnemonicParsing="false" text="Find" accelerator="Shortcut+f"/>
<MenuItem mnemonicParsing="false" text="Replace" accelerator="Shortcut+h"/> <MenuItem mnemonicParsing="false" text="Replace" accelerator="Shortcut+h"/>