TDT4100-project/src/main/java/app/MainController.java

155 lines
4.1 KiB
Java
Raw Normal View History

2021-02-12 22:25:41 +01:00
package app;
2021-02-17 21:36:12 +01:00
import java.net.URL;
2021-02-19 22:11:33 +01:00
import java.util.List;
2021-02-17 21:36:12 +01:00
import java.util.ResourceBundle;
2021-02-12 22:25:41 +01:00
2021-04-12 10:51:53 +02:00
import javax.swing.JOptionPane;
2021-02-19 22:11:33 +01:00
import com.google.common.eventbus.EventBus;
2021-02-23 00:46:43 +01:00
import com.google.common.eventbus.Subscribe;
2021-02-19 22:11:33 +01:00
import app.controllers.*;
2021-02-23 15:10:09 +01:00
import app.events.ExitApplicationEvent;
2021-02-23 00:46:43 +01:00
import app.events.LanguageChangedEvent;
2021-02-24 11:36:04 +01:00
import app.events.OpenLinkInBrowserEvent;
2021-02-24 14:22:14 +01:00
import app.events.ThemeChangedEvent;
2021-02-17 21:36:12 +01:00
import app.model.Model;
2021-02-24 11:36:04 +01:00
import javafx.application.HostServices;
2021-02-23 15:10:09 +01:00
import javafx.application.Platform;
2021-02-19 22:11:33 +01:00
import javafx.fxml.FXML;
2021-02-17 21:36:12 +01:00
import javafx.fxml.Initializable;
2021-04-26 14:35:47 +02:00
/**
* An FXML controller that controls the application and all subcontrollers
*/
2021-02-17 21:36:12 +01:00
public class MainController implements Initializable {
2021-02-19 22:11:33 +01:00
@FXML
private EditorController editorController;
@FXML
private FiletreeController filetreeController;
@FXML
private ModelineController modelineController;
@FXML
private MenubarController menubarController;
private EventBus eventBus;
2021-02-24 11:36:04 +01:00
private HostServices hostServices;
2021-02-17 21:36:12 +01:00
2021-02-19 22:11:33 +01:00
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.eventBus = new EventBus();
2021-02-23 00:46:43 +01:00
this.eventBus.register(this);
2021-02-19 22:11:33 +01:00
2021-03-01 09:21:28 +01:00
List.of(editorController, filetreeController, modelineController, menubarController)
.forEach(c -> c.setEventBus(this.eventBus));
2021-02-23 00:46:43 +01:00
2021-02-19 22:11:33 +01:00
}
2021-02-23 00:46:43 +01:00
/**
2021-03-02 02:05:50 +01:00
* Get the global eventbus
2021-03-01 09:21:28 +01:00
*
2021-03-02 02:05:50 +01:00
* @return The EventBus object
2021-02-23 00:46:43 +01:00
*/
2021-02-19 22:11:33 +01:00
public EventBus getEventBus() {
return this.eventBus;
}
2021-03-02 02:05:50 +01:00
/**
* Get the global Host Services API
*
* @return The JavaFX HostServices object
* @see #setHostServices(HostServices)
*/
2021-02-24 11:36:04 +01:00
public HostServices getHostServices() {
return hostServices;
}
2021-04-26 16:52:12 +02:00
/**
* @return All subcontrollers of this controller
*/
2021-04-15 16:17:46 +02:00
public List<Controller> getInnerControllers() {
2021-04-20 17:37:50 +02:00
return List.of(editorController, filetreeController, modelineController, menubarController);
2021-04-15 16:17:46 +02:00
}
2021-03-02 02:05:50 +01:00
/**
* Set a reference to the global Host Services API
2021-04-12 10:51:53 +02:00
*
2021-03-02 02:05:50 +01:00
* @param hostServices The JavaFX HostServices object
* @see #getHostServices()
*/
2021-02-24 11:36:04 +01:00
public void setHostServices(HostServices hostServices) {
this.hostServices = hostServices;
}
2021-03-02 02:05:50 +01:00
/**
* Replace a CSS file in a specific location in the application CSS array
2021-04-12 10:51:53 +02:00
*
2021-03-02 02:05:50 +01:00
* @param position The position of the CSS file to replace
2021-04-12 10:51:53 +02:00
* @param cssPath The path in resources to the new CSS file
2021-03-02 02:05:50 +01:00
*/
2021-02-24 14:22:14 +01:00
private void setCSSAt(int position, String cssPath) {
2021-04-12 10:51:53 +02:00
// TODO: Error check that position in range 0 to 1
2021-03-01 09:21:28 +01:00
String nextStyleSheet = getClass().getResource(cssPath).toExternalForm();
2021-02-24 14:22:14 +01:00
Model.getScene().getStylesheets().set(position, nextStyleSheet);
}
2021-04-26 14:35:47 +02:00
/* ------------------------------------------------------------------------ */
/* EVENT BUS LISTENERS */
/* ------------------------------------------------------------------------ */
2021-02-17 21:36:12 +01:00
/**
2021-03-02 02:05:50 +01:00
* Change the CSS according to which language is being used
2021-04-12 10:51:53 +02:00
*
2021-02-23 17:10:33 +01:00
* @param event
2021-02-17 21:36:12 +01:00
*/
2021-02-23 00:46:43 +01:00
@Subscribe
2021-04-26 14:35:47 +02:00
public void handle(LanguageChangedEvent event) {
2021-02-24 14:22:14 +01:00
this.setCSSAt(1, "/styling/languages/" + event.getLanguage().toLowerCase() + ".css");
2021-02-17 21:36:12 +01:00
}
2021-03-02 02:05:50 +01:00
/**
* Change the CSS according to which theme the user chooses
2021-04-12 10:51:53 +02:00
*
2021-03-02 02:05:50 +01:00
* @param event
*/
2021-02-24 14:22:14 +01:00
@Subscribe
2021-04-26 14:35:47 +02:00
public void handle(ThemeChangedEvent event) {
2021-02-24 14:22:14 +01:00
this.setCSSAt(0, "/styling/themes/" + event.getTheme().toLowerCase().replace(" ", "-") + ".css");
}
2021-02-24 11:36:04 +01:00
2021-03-02 02:05:50 +01:00
/**
* Open a link in the browser.
2021-04-12 10:51:53 +02:00
*
2021-03-02 02:05:50 +01:00
* @param event
*/
2021-02-24 11:36:04 +01:00
@Subscribe
2021-04-26 14:35:47 +02:00
public void handle(OpenLinkInBrowserEvent event) {
2021-02-24 11:36:04 +01:00
this.getHostServices().showDocument(event.getLink());
}
2021-02-23 17:10:33 +01:00
/**
2021-04-12 15:51:57 +02:00
* Handle an exit request for the whole program. Checking if all is saved before
2021-04-12 10:51:53 +02:00
* closing the app. The user can either choose to exit or go back to the
* application and save.
*
2021-02-23 17:10:33 +01:00
* @param event
*/
2021-02-23 15:10:09 +01:00
@Subscribe
2021-04-26 14:35:47 +02:00
public void handle(ExitApplicationEvent event) {
2021-04-12 10:51:53 +02:00
if (!Model.getFileIsSaved()) {
2021-04-20 17:37:50 +02:00
int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nSave before exit?", "Exit",
2021-04-12 10:51:53 +02:00
JOptionPane.YES_NO_OPTION);
2021-02-23 15:10:09 +01:00
2021-04-26 13:09:23 +02:00
if (g == JOptionPane.YES_OPTION)
this.editorController.saveCodeArea(Model.getActiveFilePath().isEmpty());
2021-04-12 10:51:53 +02:00
}
2021-04-26 13:09:23 +02:00
Platform.exit();
2021-04-12 10:51:53 +02:00
}
}