package app.controllers; import java.io.File; import java.io.FileNotFoundException; import java.net.URL; import java.util.ResourceBundle; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import app.events.CopyEvent; import app.events.CutEvent; import app.events.ExitApplicationEvent; import app.events.FileSelectedEvent; import app.events.LanguageChangedEvent; import app.events.OpenLinkInBrowserEvent; import app.events.OpenProjectEvent; import app.events.PasteEvent; import app.events.RedoEvent; import app.events.SaveFileEvent; import app.events.ThemeChangedEvent; import app.events.ToggleCommentEvent; import app.events.ToggleWrapTextEvent; import app.events.UndoEvent; import app.model.Model; import app.service.DialogBoxes; import app.service.FileOperations; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.CheckMenuItem; import javafx.scene.control.MenuBar; import javafx.scene.control.RadioMenuItem; import javafx.scene.control.ToggleGroup; import javafx.stage.Stage; /** * A FXML controller that controls the menubar component of the UI */ public class MenubarController implements Initializable, Controller { private EventBus eventBus; @FXML private MenuBar menubar; @FXML private ToggleGroup languageToggleGroup; @Override public void initialize(URL url, ResourceBundle resourceBundle) {} @Override public void setEventBus(EventBus eventBus) { this.eventBus = eventBus; this.eventBus.register(this); } /* ------------------------------------------------------------------------ */ /* CREATE FILE/DIRECTORY */ /* ------------------------------------------------------------------------ */ @FXML private void handleNewFile() { // TODO: Move Model modification inside event Model.setActiveFilePath(null); this.eventBus.post(new FileSelectedEvent(null)); } @FXML private void handleNewFolder() { // Er dette en nødvendig funksjon? Er vel svært få editorer (word f.eks) som har // dette. // Er vel innebygd i stage window at en kan lage en folder direkte fra // filutforskeren. } /* ------------------------------------------------------------------------ */ /* OPEN FILE/PROJECT */ /* ------------------------------------------------------------------------ */ /** * Handles whenever the Open File button is pressed in the menubar */ @FXML public void handleOpenFile() { Stage stage = (Stage) menubar.getScene().getWindow(); try { File file = FileOperations.openFileWithDialog(stage); // TODO: Move Model modification inside event Model.setActiveFilePath(file.toPath()); this.eventBus.post(new FileSelectedEvent(file.toPath())); } catch (FileNotFoundException e) { DialogBoxes.showErrorMessage("File not found!"); } } /** * Handles whenever the Open Project button is pressed in the menubar */ @FXML private void handleOpenProject() { Stage stage = (Stage) menubar.getScene().getWindow(); try { File dir = FileOperations.openFolderWithDialog(stage); // TODO: Move Model modification inside event Model.setProjectPath(dir.toPath()); this.eventBus.post(new OpenProjectEvent(dir.toPath())); } catch (FileNotFoundException e) { DialogBoxes.showErrorMessage("Folder not found!"); } } /* ------------------------------------------------------------------------ */ /* SAVE FILE */ /* ------------------------------------------------------------------------ */ /** * Handles whenever the Save button is pressed in the menubar */ @FXML private void handleSaveFile() { if (Model.getActiveFilePath() != null) this.eventBus.post(new SaveFileEvent(false)); else // New file needs a path handleSaveAsFile(); } /** * Handles whenever the Save as button is pressed in the menubar */ @FXML private void handleSaveAsFile() { this.eventBus.post(new SaveFileEvent(true)); } /** * Handles whenever the programming language is changed from the menubar. */ @FXML private void handleLanguageChange(ActionEvent event) { this.eventBus.post(new LanguageChangedEvent(((RadioMenuItem) event.getSource()).getText())); } /** * Handles whenever the wraptext togglebutton is pressed in the menubar */ @FXML private void handleToggleWraptext(ActionEvent event) { var isSelected = ((CheckMenuItem) event.getSource()).selectedProperty().get(); this.eventBus.post(new ToggleWrapTextEvent(isSelected)); } /** * Handles whenever the theme is changed from the menubar */ @FXML private void handleThemeChange(ActionEvent event) { this.eventBus.post(new ThemeChangedEvent(((RadioMenuItem) event.getSource()).getText())); } /** * Handles whenever the exit button is pressed in the menubar */ @FXML private void handleExitApplication(ActionEvent event) { this.eventBus.post(new ExitApplicationEvent()); } /* ------------------------------------------------------------------------ */ /* EDIT */ /* ------------------------------------------------------------------------ */ /** * Handles whenever the undo button is pressed in the menubar */ @FXML private void handleUndo(ActionEvent event) { this.eventBus.post(new UndoEvent()); } /** * Handles whenever the redo button is pressed in the menubar */ @FXML private void handleRedo(ActionEvent event) { this.eventBus.post(new RedoEvent()); } /** * Handles whenever the copy button is pressed in the menubar */ @FXML private void handleCopy(ActionEvent event) { this.eventBus.post(new CopyEvent()); } /** * Handles whenever the cut button is pressed in the menubar */ @FXML private void handleCut(ActionEvent event) { this.eventBus.post(new CutEvent()); } /** * Handles whenever the paste button is pressed in the menubar */ @FXML private void handlePaste(ActionEvent event) { this.eventBus.post(new PasteEvent()); } /** * Handles whenever the Toggle Comment button is pressed in the menubar */ @FXML private void handleToggleComment(ActionEvent event) { this.eventBus.post(new ToggleCommentEvent()); } /* ------------------------------------------------------------------------ */ /* ABOUT */ /* ------------------------------------------------------------------------ */ /** * Handles whenever the About button is pressed in the menubar */ @FXML private void handleAbout(ActionEvent event) { String aboutLink = "https://gitlab.stud.idi.ntnu.no/oysteikt/tdt4100-project-2021v/-/blob/master/README.md"; this.eventBus.post(new OpenLinkInBrowserEvent(aboutLink)); } /* ------------------------------------------------------------------------ */ /* SUBSCRIPTIONS */ /* ------------------------------------------------------------------------ */ /** * Updates menubuttons whenever the language is changed */ @Subscribe private void handle(LanguageChangedEvent event) { this.languageToggleGroup .getToggles() .stream() .map(RadioMenuItem.class::cast) .filter(t -> t.getId() .equals("toggle" + event.getLanguage())) .findFirst() .orElseThrow() .setSelected(true); } }