Set up eventbus system
This commit is contained in:
parent
132749eb34
commit
fc055382d0
@ -2,8 +2,6 @@ package app;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
@ -13,8 +11,6 @@ import javafx.stage.Stage;
|
||||
|
||||
public class Main extends Application {
|
||||
|
||||
EventBus eventBus;
|
||||
|
||||
/**
|
||||
* Boilerplate function to launch the application
|
||||
*/
|
||||
@ -38,7 +34,11 @@ public class Main extends Application {
|
||||
try {
|
||||
document = fxmlLoader.load();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
System.err.println(e);
|
||||
throw new RuntimeException(
|
||||
"Couldn\'t find or missing permissions to load "
|
||||
+ getClass().getResource("/fxml/Main.fxml")
|
||||
);
|
||||
}
|
||||
|
||||
Scene scene = new Scene(document);
|
||||
@ -46,7 +46,4 @@ public class Main extends Application {
|
||||
window.show();
|
||||
}
|
||||
|
||||
public EventBus getEventBus() {
|
||||
return this.eventBus;
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,49 @@
|
||||
package app;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
import app.controllers.*;
|
||||
import app.model.Model;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
|
||||
public class MainController implements Initializable {
|
||||
|
||||
@FXML
|
||||
private EditorController editorController;
|
||||
|
||||
@FXML
|
||||
private FiletreeController filetreeController;
|
||||
|
||||
@FXML
|
||||
private ModelineController modelineController;
|
||||
|
||||
@FXML
|
||||
private MenubarController menubarController;
|
||||
|
||||
private EventBus eventBus;
|
||||
private Model model;
|
||||
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
this.eventBus = new EventBus();
|
||||
|
||||
List.of(
|
||||
editorController,
|
||||
filetreeController,
|
||||
modelineController,
|
||||
menubarController
|
||||
).forEach(c -> c.setEventBus(this.eventBus));
|
||||
}
|
||||
|
||||
public EventBus getEventBus() {
|
||||
return this.eventBus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Links the controller to the global model
|
||||
* @param model The model to be linked
|
||||
@ -18,11 +52,4 @@ public class MainController implements Initializable {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes and customizes the properties of the javafx objects.
|
||||
*/
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
//
|
||||
}
|
||||
}
|
14
src/main/java/app/controllers/Controller.java
Normal file
14
src/main/java/app/controllers/Controller.java
Normal file
@ -0,0 +1,14 @@
|
||||
package app.controllers;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
/**
|
||||
* Interface to make a controller that contains an EventBus
|
||||
*/
|
||||
public interface Controller {
|
||||
/**
|
||||
* Registers the main EventBus into the controller.
|
||||
* @param eventBus the main EventBus
|
||||
*/
|
||||
public void setEventBus(EventBus eventBus);
|
||||
}
|
@ -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()
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,15 +6,27 @@ import javafx.scene.control.TreeView;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
import app.model.Model;
|
||||
import javafx.fxml.Initializable;
|
||||
|
||||
public class FiletreeController implements Initializable {
|
||||
public class FiletreeController implements Initializable, Controller {
|
||||
|
||||
// TODO: Add component references, and event handlers
|
||||
|
||||
private EventBus eventBus;
|
||||
private Model model;
|
||||
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
@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
|
||||
@ -23,12 +35,4 @@ public class FiletreeController implements Initializable {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes and customizes the properties of the javafx objects.
|
||||
*/
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,15 +3,27 @@ package app.controllers;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
import app.model.Model;
|
||||
import javafx.fxml.Initializable;
|
||||
|
||||
public class MenubarController implements Initializable {
|
||||
public class MenubarController implements Initializable, Controller {
|
||||
|
||||
// TODO: Add component references, and event handlers
|
||||
|
||||
private EventBus eventBus;
|
||||
private Model model;
|
||||
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
@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
|
||||
@ -20,12 +32,5 @@ public class MenubarController implements Initializable {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes and customizes the properties of the javafx objects.
|
||||
*/
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,15 +3,37 @@ package app.controllers;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
|
||||
import app.events.EditorChangedEvent;
|
||||
import app.model.Model;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.Label;
|
||||
|
||||
public class ModelineController implements Initializable {
|
||||
public class ModelineController implements Initializable, Controller {
|
||||
|
||||
// TODO: Add component references, and event handlers
|
||||
@FXML
|
||||
private Label columnrow;
|
||||
|
||||
@FXML
|
||||
private Label language;
|
||||
|
||||
private EventBus eventBus;
|
||||
private Model model;
|
||||
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
setColumnRow(0, 1);
|
||||
}
|
||||
|
||||
@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
|
||||
@ -21,11 +43,17 @@ public class ModelineController implements Initializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes and customizes the properties of the javafx objects.
|
||||
* Update the colum row counter
|
||||
* @param column The column number
|
||||
* @param row The row number
|
||||
*/
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
// TODO: implement
|
||||
public void setColumnRow(int column, int row) {
|
||||
this.columnrow.setText(String.format("[%d:%d]", row, column));
|
||||
}
|
||||
|
||||
}
|
||||
@Subscribe
|
||||
public void handle(EditorChangedEvent event) {
|
||||
System.out.println("Key pressed: " + event.getKeyCode());
|
||||
this.setColumnRow(event.getColumn(), event.getLineNumber());
|
||||
}
|
||||
}
|
29
src/main/java/app/events/EditorChangedEvent.java
Normal file
29
src/main/java/app/events/EditorChangedEvent.java
Normal file
@ -0,0 +1,29 @@
|
||||
package app.events;
|
||||
|
||||
import javafx.scene.input.KeyCode;
|
||||
|
||||
public class EditorChangedEvent implements Event {
|
||||
|
||||
private KeyCode keyCode;
|
||||
private int lineNumber;
|
||||
private int column;
|
||||
|
||||
public EditorChangedEvent(KeyCode keyCode, int lineNumber, int column) {
|
||||
this.keyCode = keyCode;
|
||||
this.lineNumber = lineNumber;
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public KeyCode getKeyCode() {
|
||||
return this.keyCode;
|
||||
}
|
||||
|
||||
public int getLineNumber() {
|
||||
return this.lineNumber;
|
||||
}
|
||||
|
||||
public int getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
}
|
3
src/main/java/app/events/Event.java
Normal file
3
src/main/java/app/events/Event.java
Normal file
@ -0,0 +1,3 @@
|
||||
package app.events;
|
||||
|
||||
interface Event {}
|
@ -2,6 +2,7 @@
|
||||
|
||||
<?import javafx.scene.control.SplitPane?>
|
||||
<?import javafx.scene.layout.BorderPane?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.text.TextFlow?>
|
||||
|
||||
<BorderPane
|
||||
@ -13,7 +14,7 @@
|
||||
|
||||
<top>
|
||||
<!-- Menubar -->
|
||||
<fx:include source="components/Menubar.fxml" fx:id="MenubarRoot"/>
|
||||
<fx:include source="components/Menubar.fxml" fx:id="menubar"/>
|
||||
</top>
|
||||
|
||||
<center>
|
||||
@ -23,10 +24,10 @@
|
||||
<items>
|
||||
|
||||
<!-- File tree -->
|
||||
<fx:include source="components/Filetree.fxml" fx:id="FiletreeRoot"/>
|
||||
<fx:include source="components/Filetree.fxml" fx:id="filetree"/>
|
||||
|
||||
<!-- Editor -->
|
||||
<fx:include source="components/Editor.fxml" fx:id="EditorRoot"/>
|
||||
<fx:include source="components/Editor.fxml" fx:id="editor"/>
|
||||
|
||||
</items>
|
||||
</SplitPane>
|
||||
@ -34,7 +35,7 @@
|
||||
|
||||
<bottom>
|
||||
<!-- Modeline -->
|
||||
<fx:include source="components/Modeline.fxml" fx:id="ModelineRoot"/>
|
||||
<fx:include source="components/Modeline.fxml" fx:id="modeline"/>
|
||||
</bottom>
|
||||
|
||||
</BorderPane>
|
||||
|
@ -9,6 +9,6 @@
|
||||
xmlns:fx="http://javafx.com/fxml/1"
|
||||
fx:controller="app.controllers.EditorController">
|
||||
<content>
|
||||
<CodeArea fx:id="editor"/>
|
||||
<CodeArea fx:id="editor" onKeyPressed="#editorChanged"/>
|
||||
</content>
|
||||
</VirtualizedScrollPane>
|
Loading…
Reference in New Issue
Block a user