Lot of stuff
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<Controller> 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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
84
src/main/java/app/settings/SettingsProvider.java
Normal file
84
src/main/java/app/settings/SettingsProvider.java
Normal file
@@ -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<String> settings = new ArrayList<>();
|
||||
List<String> 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();
|
||||
}
|
||||
|
||||
}
|
||||
9
src/main/java/app/settings/SettingsProviderI.java
Normal file
9
src/main/java/app/settings/SettingsProviderI.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package app.settings;
|
||||
|
||||
public interface SettingsProviderI {
|
||||
|
||||
void loadSettings();
|
||||
|
||||
void saveSettings();
|
||||
|
||||
}
|
||||
3
src/main/resources/BNNsettings/settings.dat
Normal file
3
src/main/resources/BNNsettings/settings.dat
Normal file
@@ -0,0 +1,3 @@
|
||||
- Settings:
|
||||
ProgrammingLanguage = Java
|
||||
Theme = Solarized Light
|
||||
@@ -58,10 +58,11 @@
|
||||
<items>
|
||||
<!-- TODO: Generate buttons based on classes -->
|
||||
<RadioMenuItem text="Monokai"
|
||||
selected="true"
|
||||
fx:id="toggleMonokai"
|
||||
onAction="#handleThemeChange"
|
||||
toggleGroup="$themeToggleGroup"/>
|
||||
<RadioMenuItem text="Solarized Light"
|
||||
fx:id="toggleSolarized_Light"
|
||||
onAction="#handleThemeChange"
|
||||
toggleGroup="$themeToggleGroup"/>
|
||||
</items>
|
||||
|
||||
Reference in New Issue
Block a user