Set up eventbus system

This commit is contained in:
2021-02-19 22:11:33 +01:00
parent 132749eb34
commit fc055382d0
11 changed files with 184 additions and 60 deletions

View File

@@ -3,21 +3,38 @@ package app.controllers;
import java.net.URL;
import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.TwoDimensional.Bias;
import org.fxmisc.richtext.model.TwoDimensional.Position;
import app.events.EditorChangedEvent;
import app.model.Model;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.KeyEvent;
public class EditorController implements Initializable {
public class EditorController implements Initializable, Controller {
@FXML
private CodeArea editor;
private EventBus eventBus;
private Model model;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
editor.setParagraphGraphicFactory(LineNumberFactory.get(editor));
}
@Override
public void setEventBus(EventBus eventBus) {
this.eventBus = eventBus;
this.eventBus.register(this);
}
/**
* Links the controller to the global model
* @param model The model to be linked
@@ -26,21 +43,20 @@ public class EditorController implements Initializable {
this.model = model;
}
/**
* Initializes and customizes the properties of the javafx objects.
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
editor.setParagraphGraphicFactory(LineNumberFactory.get(editor));
}
/**
* Handles events from the editor, and reflects them in the model
* @param event The object containing metadata of the event
*/
@FXML
public void editorChanged(ActionEvent event) {
//
public void editorChanged(KeyEvent event) {
int offset = this.editor.getCaretPosition();
Position pos = this.editor.offsetToPosition(offset, Bias.Forward);
this.eventBus.post(
new EditorChangedEvent(
event.getCode(),
pos.getMajor() + 1,
pos.getMinor()
));
}
}