Set up eventbus system

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

View File

@ -2,8 +2,6 @@ package app;
import java.io.IOException; import java.io.IOException;
import com.google.common.eventbus.EventBus;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
@ -13,8 +11,6 @@ import javafx.stage.Stage;
public class Main extends Application { public class Main extends Application {
EventBus eventBus;
/** /**
* Boilerplate function to launch the application * Boilerplate function to launch the application
*/ */
@ -38,7 +34,11 @@ public class Main extends Application {
try { try {
document = fxmlLoader.load(); document = fxmlLoader.load();
} catch (IOException e) { } 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); Scene scene = new Scene(document);
@ -46,7 +46,4 @@ public class Main extends Application {
window.show(); window.show();
} }
public EventBus getEventBus() {
return this.eventBus;
}
} }

View File

@ -1,15 +1,49 @@
package app; package app;
import java.net.URL; import java.net.URL;
import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
import app.controllers.*;
import app.model.Model; import app.model.Model;
import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
public class MainController implements 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; 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 * Links the controller to the global model
* @param model The model to be linked * @param model The model to be linked
@ -18,11 +52,4 @@ public class MainController implements Initializable {
this.model = model; this.model = model;
} }
/**
* Initializes and customizes the properties of the javafx objects.
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//
}
} }

View 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);
}

View File

@ -3,21 +3,38 @@ package app.controllers;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory; 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 app.model.Model;
import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.input.KeyEvent;
public class EditorController implements Initializable { public class EditorController implements Initializable, Controller {
@FXML @FXML
private CodeArea editor; private CodeArea editor;
private EventBus eventBus;
private Model model; 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 * Links the controller to the global model
* @param model The model to be linked * @param model The model to be linked
@ -26,21 +43,20 @@ public class EditorController implements Initializable {
this.model = model; 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 * Handles events from the editor, and reflects them in the model
* @param event The object containing metadata of the event * @param event The object containing metadata of the event
*/ */
@FXML @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()
));
} }
} }

View File

@ -6,15 +6,27 @@ import javafx.scene.control.TreeView;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
import app.model.Model; import app.model.Model;
import javafx.fxml.Initializable; 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; 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 * Links the controller to the global model
* @param model The model to be linked * @param model The model to be linked
@ -23,12 +35,4 @@ public class FiletreeController implements Initializable {
this.model = model; this.model = model;
} }
/**
* Initializes and customizes the properties of the javafx objects.
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// TODO: implement
}
} }

View File

@ -3,15 +3,27 @@ package app.controllers;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
import app.model.Model; import app.model.Model;
import javafx.fxml.Initializable; 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; 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 * Links the controller to the global model
* @param model The model to be linked * @param model The model to be linked
@ -20,12 +32,5 @@ public class MenubarController implements Initializable {
this.model = model; this.model = model;
} }
/**
* Initializes and customizes the properties of the javafx objects.
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// TODO: implement
}
} }

View File

@ -3,15 +3,37 @@ package app.controllers;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; 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 app.model.Model;
import javafx.fxml.FXML;
import javafx.fxml.Initializable; 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; 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 * Links the controller to the global model
* @param model The model to be linked * @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 setColumnRow(int column, int row) {
public void initialize(URL url, ResourceBundle resourceBundle) { this.columnrow.setText(String.format("[%d:%d]", row, column));
// TODO: implement
} }
@Subscribe
public void handle(EditorChangedEvent event) {
System.out.println("Key pressed: " + event.getKeyCode());
this.setColumnRow(event.getColumn(), event.getLineNumber());
}
} }

View 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;
}
}

View File

@ -0,0 +1,3 @@
package app.events;
interface Event {}

View File

@ -2,6 +2,7 @@
<?import javafx.scene.control.SplitPane?> <?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.TextFlow?> <?import javafx.scene.text.TextFlow?>
<BorderPane <BorderPane
@ -13,7 +14,7 @@
<top> <top>
<!-- Menubar --> <!-- Menubar -->
<fx:include source="components/Menubar.fxml" fx:id="MenubarRoot"/> <fx:include source="components/Menubar.fxml" fx:id="menubar"/>
</top> </top>
<center> <center>
@ -23,10 +24,10 @@
<items> <items>
<!-- File tree --> <!-- File tree -->
<fx:include source="components/Filetree.fxml" fx:id="FiletreeRoot"/> <fx:include source="components/Filetree.fxml" fx:id="filetree"/>
<!-- Editor --> <!-- Editor -->
<fx:include source="components/Editor.fxml" fx:id="EditorRoot"/> <fx:include source="components/Editor.fxml" fx:id="editor"/>
</items> </items>
</SplitPane> </SplitPane>
@ -34,7 +35,7 @@
<bottom> <bottom>
<!-- Modeline --> <!-- Modeline -->
<fx:include source="components/Modeline.fxml" fx:id="ModelineRoot"/> <fx:include source="components/Modeline.fxml" fx:id="modeline"/>
</bottom> </bottom>
</BorderPane> </BorderPane>

View File

@ -9,6 +9,6 @@
xmlns:fx="http://javafx.com/fxml/1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="app.controllers.EditorController"> fx:controller="app.controllers.EditorController">
<content> <content>
<CodeArea fx:id="editor"/> <CodeArea fx:id="editor" onKeyPressed="#editorChanged"/>
</content> </content>
</VirtualizedScrollPane> </VirtualizedScrollPane>