From d2f7ad182813788ca351ded39a1f6b458041205d Mon Sep 17 00:00:00 2001 From: Oystein Date: Tue, 20 Apr 2021 17:37:50 +0200 Subject: [PATCH] Lot of stuff --- dependency-reduced-pom.xml | 169 ++++++++++++++++++ pom.xml | 60 +++---- src/main/java/app/Main.java | 11 +- src/main/java/app/MainController.java | 15 +- .../app/controllers/MenubarController.java | 33 ++-- .../java/app/events/ThemeChangedEvent.java | 6 +- src/main/java/app/model/Model.java | 19 ++ src/main/java/app/service/FileOperations.java | 28 +-- .../java/app/settings/SettingsProvider.java | 84 +++++++++ .../java/app/settings/SettingsProviderI.java | 9 + src/main/resources/BNNsettings/settings.dat | 3 + .../resources/fxml/components/Menubar.fxml | 3 +- src/test/java/app/MainTest.java | 74 -------- 13 files changed, 362 insertions(+), 152 deletions(-) create mode 100644 dependency-reduced-pom.xml create mode 100644 src/main/java/app/settings/SettingsProvider.java create mode 100644 src/main/java/app/settings/SettingsProviderI.java create mode 100644 src/main/resources/BNNsettings/settings.dat delete mode 100644 src/test/java/app/MainTest.java diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml new file mode 100644 index 0000000..8e3b937 --- /dev/null +++ b/dependency-reduced-pom.xml @@ -0,0 +1,169 @@ + + + 4.0.0 + tdt4100-v2021 + banana-editor + 1.0.0 + + + + maven-compiler-plugin + 3.8.0 + + 15 + true + true + + --enable-preview + + + + + maven-surefire-plugin + 3.0.0-M5 + + @{argLine} --enable-preview + 1 + true + + + + org.openjfx + javafx-maven-plugin + 0.0.3 + + app.Main + + + + org.jacoco + jacoco-maven-plugin + 0.8.6 + + + default-prepare-agent + + prepare-agent + + + + default-report + prepare-package + + report + + + + + + maven-shade-plugin + 3.0.0 + + + package + + shade + + + + + app.MainLauncher + + + + + + + + + + + org.junit.jupiter + junit-jupiter + 5.8.0-M1 + test + + + junit-jupiter-api + org.junit.jupiter + + + junit-jupiter-params + org.junit.jupiter + + + junit-jupiter-engine + org.junit.jupiter + + + + + org.testfx + testfx-core + 4.0.16-alpha + test + + + hamcrest-core + org.hamcrest + + + assertj-core + org.assertj + + + + + org.testfx + testfx-junit5 + 4.0.16-alpha + test + + + org.hamcrest + hamcrest + 2.1 + test + + + org.testfx + openjfx-monocle + jdk-12.0.1+2 + test + + + org.mockito + mockito-inline + 3.8.0 + test + + + mockito-core + org.mockito + + + + + org.mockito + mockito-junit-jupiter + 2.23.0 + test + + + mockito-core + org.mockito + + + junit-jupiter-api + org.junit.jupiter + + + + + + 15 + 15 + UTF-8 + + + diff --git a/pom.xml b/pom.xml index 4a8fb95..e800d3e 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,4 @@ - + @@ -30,9 +29,9 @@ - org.fxmisc.richtext - richtextfx - 0.10.5 + org.fxmisc.richtext + richtextfx + 0.10.5 @@ -44,10 +43,10 @@ - org.junit.jupiter - junit-jupiter - 5.8.0-M1 - test + org.junit.jupiter + junit-jupiter + 5.8.0-M1 + test @@ -102,7 +101,7 @@ maven-plugin - + @@ -138,7 +137,7 @@ - + maven-surefire-plugin @@ -184,25 +183,24 @@ - org.apache.maven.plugins - maven-shade-plugin - 3.0.0 - - - package - - shade - - - - - app.MainLauncher - - - - - + org.apache.maven.plugins + maven-shade-plugin + 3.0.0 + + + package + + shade + + + + + app.MainLauncher + + + + + diff --git a/src/main/java/app/Main.java b/src/main/java/app/Main.java index ec43f2d..befe033 100644 --- a/src/main/java/app/Main.java +++ b/src/main/java/app/Main.java @@ -12,6 +12,7 @@ import app.events.FileSaveStateChangedEvent; import app.events.LanguageChangedEvent; import app.events.ThemeChangedEvent; import app.model.Model; +import app.settings.SettingsProvider; public class Main extends Application { @@ -24,6 +25,7 @@ public class Main extends Application { /** * Boilerplate function to launch the application. + * * @param args Additional arguments from commandline */ public static void main(String[] args) { @@ -40,7 +42,9 @@ public class Main extends Application { } /** - * Loads all FXML documents of the main UI and initializes all correlated subcontrollers + * Loads all FXML documents of the main UI and initializes all correlated + * subcontrollers + * * @throws IOException */ private void loadFXML() throws IOException { @@ -65,14 +69,15 @@ public class Main extends Application { scene.getStylesheets().setAll("", ""); MainController mainController = fxmlLoader.getController(); - mainController.getEventBus().post(new LanguageChangedEvent("Java")); - mainController.getEventBus().post(new ThemeChangedEvent("Monokai")); + SettingsProvider SP = new SettingsProvider(mainController.getEventBus()); + SP.loadSettings(); mainController.getEventBus().post(new FileSaveStateChangedEvent(true)); mainController.setHostServices(getHostServices()); } /** * The entrypoint of the application. + * * @param window The primary window of the application */ @Override diff --git a/src/main/java/app/MainController.java b/src/main/java/app/MainController.java index ed6b470..8ec3baf 100644 --- a/src/main/java/app/MainController.java +++ b/src/main/java/app/MainController.java @@ -13,6 +13,7 @@ import app.controllers.*; import app.events.ExitApplicationEvent; import app.events.LanguageChangedEvent; import app.events.OpenLinkInBrowserEvent; +import app.events.SaveFileEvent; import app.events.ThemeChangedEvent; import app.model.Model; import javafx.application.HostServices; @@ -66,14 +67,9 @@ public class MainController implements Initializable { return hostServices; } - //TODO: Document + // TODO: Document public List getInnerControllers() { - return List.of( - editorController, - filetreeController, - modelineController, - menubarController - ); + return List.of(editorController, filetreeController, modelineController, menubarController); } /** @@ -139,10 +135,11 @@ public class MainController implements Initializable { @Subscribe private void handle(ExitApplicationEvent event) { if (!Model.getFileIsSaved()) { - int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nGo back to save?", "Exit", + int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nSave before exit?", "Exit", JOptionPane.YES_NO_OPTION); - if (!(g == JOptionPane.YES_OPTION)) { + if (g == JOptionPane.YES_OPTION) { + this.eventBus.post(new SaveFileEvent(false)); Platform.exit(); } } else { diff --git a/src/main/java/app/controllers/MenubarController.java b/src/main/java/app/controllers/MenubarController.java index 4de1da3..28ee796 100644 --- a/src/main/java/app/controllers/MenubarController.java +++ b/src/main/java/app/controllers/MenubarController.java @@ -47,8 +47,12 @@ public class MenubarController implements Initializable, Controller { @FXML private ToggleGroup languageToggleGroup; + @FXML + private ToggleGroup themeToggleGroup; + @Override - public void initialize(URL url, ResourceBundle resourceBundle) {} + public void initialize(URL url, ResourceBundle resourceBundle) { + } @Override public void setEventBus(EventBus eventBus) { @@ -68,10 +72,7 @@ public class MenubarController implements Initializable, Controller { @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. + } /* ------------------------------------------------------------------------ */ @@ -244,15 +245,19 @@ public class MenubarController implements Initializable, Controller { */ @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); + this.languageToggleGroup.getToggles().stream().map(RadioMenuItem.class::cast) + .filter(t -> t.getId().equals("toggle" + event.getLanguage())).findFirst().orElseThrow().setSelected(true); + } + + /** + * Updates menubuttons whenever the theme is changed + */ + + @Subscribe + private void handle(ThemeChangedEvent event) { + this.themeToggleGroup.getToggles().stream().map(RadioMenuItem.class::cast) + .filter(t -> t.getId().equals("toggle" + event.getTheme().replace(" ", "_"))).findFirst().orElseThrow().setSelected(true); + } } diff --git a/src/main/java/app/events/ThemeChangedEvent.java b/src/main/java/app/events/ThemeChangedEvent.java index ef66e6e..8f6f21d 100644 --- a/src/main/java/app/events/ThemeChangedEvent.java +++ b/src/main/java/app/events/ThemeChangedEvent.java @@ -1,5 +1,7 @@ package app.events; +import app.model.Model; + /** * Event signalizing that the theme of the applicaton has been changed */ @@ -9,12 +11,14 @@ public class ThemeChangedEvent extends Event { /** * Event signalizing that the theme of the applicaton has been changed + * * @param theme The name of the theme */ public ThemeChangedEvent(String theme) { + Model.setTheme(theme); this.theme = theme; } - + /** * @return The name of the theme */ diff --git a/src/main/java/app/model/Model.java b/src/main/java/app/model/Model.java index 92e1d9f..00102b4 100644 --- a/src/main/java/app/model/Model.java +++ b/src/main/java/app/model/Model.java @@ -2,6 +2,7 @@ package app.model; import java.nio.file.Path; +import app.settings.SettingsProvider; import javafx.scene.Scene; /** @@ -15,7 +16,9 @@ public class Model { private static Path activeFilePath; private static Path currentProjectPath; private static ProgrammingLanguage currentProgrammingLanguage; + private static String theme; private static Scene scene; + private static SettingsProvider settings; public static Path getActiveFilePath() { return activeFilePath; @@ -41,10 +44,22 @@ public class Model { return scene; } + public static String getTheme() { + return theme; + } + public static boolean getFileIsSaved() { return fileIsSaved; } + public static SettingsProvider getSettingsProvider() { + return settings; + } + + public static void setTheme(String theme) { + Model.theme = theme; + } + public static void setLanguage(ProgrammingLanguage language) { Model.currentProgrammingLanguage = language; } @@ -57,4 +72,8 @@ public class Model { Model.fileIsSaved = fileIsSaved; } + public static void setSettingsProvider(SettingsProvider settings) { + Model.settings = settings; + } + } \ No newline at end of file diff --git a/src/main/java/app/service/FileOperations.java b/src/main/java/app/service/FileOperations.java index 5e84b65..7ffc89b 100644 --- a/src/main/java/app/service/FileOperations.java +++ b/src/main/java/app/service/FileOperations.java @@ -27,7 +27,7 @@ public class FileOperations { if (chosenFile == null) throw new FileNotFoundException(); // if (chosenFile != null) { - // String correctFormat = chosenFile.getAbsolutePath().replace("\\", "\\\\"); + // String correctFormat = chosenFile.getAbsolutePath().replace("\\", "\\\\"); // } return chosenFile; @@ -48,17 +48,11 @@ public class FileOperations { } public static boolean saveFile(String filepath, String content) { - + try (PrintWriter writer = new PrintWriter(new File(filepath))) { - if (filepath.endsWith(".java") || filepath.endsWith(".md")) { writer.println(content); - } else { - throw new FileNotFoundException(); - } - } catch (FileNotFoundException ex) { - - DialogBoxes.showErrorMessage("Could not save file!\nMust be a java or md file. Try again."); + DialogBoxes.showErrorMessage("Could not save file!"); System.err.println(filepath); return false; } @@ -93,26 +87,22 @@ public class FileOperations { public static String readFile(Path filePath) { if (filePath == null) - return ""; - + return ""; + String result = ""; try (Scanner sc = new Scanner(filePath.toFile())) { - if (filePath.endsWith(".java") || filePath.endsWith(".md")) { - while (sc.hasNextLine()) { - result += (sc.nextLine() + "\n"); - } - } else { - throw new FileNotFoundException(); + while (sc.hasNextLine()) { + result += (sc.nextLine() + "\n"); } } catch (FileNotFoundException ex) { - DialogBoxes.showErrorMessage("Could not be opened!\nMust be a java or md file or not null. Try again."); + DialogBoxes.showErrorMessage("Could not be opened!"); System.err.println(filePath); } return result; } - + } diff --git a/src/main/java/app/settings/SettingsProvider.java b/src/main/java/app/settings/SettingsProvider.java new file mode 100644 index 0000000..30ddc0e --- /dev/null +++ b/src/main/java/app/settings/SettingsProvider.java @@ -0,0 +1,84 @@ +package app.settings; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; + +import app.events.LanguageChangedEvent; +import app.events.ThemeChangedEvent; +import app.model.Model; + +public class SettingsProvider implements SettingsProviderI { + + private static EventBus eventBus; + private static final String SETTINGS_PATH = "/BNNsettings/settings.dat"; + + public SettingsProvider(EventBus eB) { + setEventBus(eB); + Model.setSettingsProvider(this); + } + + public void setEventBus(EventBus eB) { + eventBus = eB; + SettingsProvider.eventBus.register(this); + } + + @Override + public void loadSettings() { + List settings = new ArrayList<>(); + List legalSettings = Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light"); + try (Scanner sc = new Scanner(new File(getClass().getResource(SETTINGS_PATH).getPath()))) { + + while (sc.hasNextLine()) { + var nextLine = sc.nextLine().trim(); + if (nextLine.isEmpty() || nextLine.startsWith("-")) { + continue; + } else { + settings.add(nextLine.substring(nextLine.indexOf("=") + 2)); + } + } + + if (legalSettings.containsAll(settings)) { + eventBus.post(new LanguageChangedEvent(settings.get(0))); + eventBus.post(new ThemeChangedEvent(settings.get(1))); + } else { + throw new IOException(); + } + + } catch (IOException e) { + eventBus.post(new LanguageChangedEvent("Java")); + eventBus.post(new ThemeChangedEvent("Monokai")); + } + + } + + @Override + public void saveSettings() { + try (PrintWriter writer = new PrintWriter(new File(getClass().getResource(SETTINGS_PATH).getPath()))) { + writer.println("- Settings:"); + writer.println("Programming Language = " + Model.getLanguage().getName()); + writer.println("Theme = " + Model.getTheme()); + } catch (IOException e) { + System.err.println(e); + } + + } + + @Subscribe + private void handle(ThemeChangedEvent event) { + saveSettings(); + } + + @Subscribe + private void handle(LanguageChangedEvent event) { + saveSettings(); + } + +} diff --git a/src/main/java/app/settings/SettingsProviderI.java b/src/main/java/app/settings/SettingsProviderI.java new file mode 100644 index 0000000..8bed651 --- /dev/null +++ b/src/main/java/app/settings/SettingsProviderI.java @@ -0,0 +1,9 @@ +package app.settings; + +public interface SettingsProviderI { + + void loadSettings(); + + void saveSettings(); + +} diff --git a/src/main/resources/BNNsettings/settings.dat b/src/main/resources/BNNsettings/settings.dat new file mode 100644 index 0000000..8778ce7 --- /dev/null +++ b/src/main/resources/BNNsettings/settings.dat @@ -0,0 +1,3 @@ +- Settings: +ProgrammingLanguage = Java +Theme = Solarized Light \ No newline at end of file diff --git a/src/main/resources/fxml/components/Menubar.fxml b/src/main/resources/fxml/components/Menubar.fxml index 1844bca..0131d6b 100644 --- a/src/main/resources/fxml/components/Menubar.fxml +++ b/src/main/resources/fxml/components/Menubar.fxml @@ -58,10 +58,11 @@ diff --git a/src/test/java/app/MainTest.java b/src/test/java/app/MainTest.java deleted file mode 100644 index 1030f64..0000000 --- a/src/test/java/app/MainTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package app; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.IOException; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.platform.commons.annotation.Testable; - -import app.controllers.*; - -import java.util.List; -import java.util.stream.Collectors; - -import javafx.scene.layout.BorderPane; - -import app.testing.FxTestTemplate; - - -@Testable -public class MainTest extends FxTestTemplate { - - @Test - @DisplayName("Check that the stage title is correct") - public void should_have_stage_title() { - assertEquals("Banana Editor", this.getStage().getTitle()); - } - - @Test - @Order(1) - @DisplayName("Check that the stage has an icon") - public void should_have_stage_icon() { - assertEquals(1, this.getStage().getIcons().size()); - } - - - @Test - @Order(2) - @DisplayName("Check that the root element is present") - public void should_have_root() { - BorderPane app = (BorderPane) find("#root"); - assertNotNull(app); - } - - @Test - @Order(3) - @DisplayName("Check that all subcontrollers are present") - public void should_have_subcontrollers() { - this - .getMainController() - .getInnerControllers() - .forEach((Controller controller) -> assertNotNull(controller)); - } - - @Test - @DisplayName("Check that the scene is correct") - public void should_have_scene() throws IOException { - assertNotNull(this.getStage().getScene()); - } - - @Test - @DisplayName("Check that the CSS is set") - public void should_have_css() { - List expectedCSS = - List.of("/styling/themes/monokai.css", "/styling/languages/java.css") - .stream() - .map(p -> getClass().getResource(p).toExternalForm()) - .collect(Collectors.toList()); - assertEquals(expectedCSS, this.getStage().getScene().getStylesheets()); - } -} \ No newline at end of file