From 1156b827acec8fd493994cee5bb2741077e8017f Mon Sep 17 00:00:00 2001 From: Oystein Date: Wed, 7 Apr 2021 12:39:52 +0200 Subject: [PATCH] added some more with writing and reading to file --- README.md | 1 + .../app/controllers/EditorController.java | 33 ++++++++++++-- .../app/controllers/MenubarController.java | 44 ++++++++++++++----- .../java/app/events/ExitApplicationEvent.java | 23 +++++++++- .../{SaveFile.java => SaveFileEvent.java} | 4 +- 5 files changed, 89 insertions(+), 16 deletions(-) rename src/main/java/app/events/{SaveFile.java => SaveFileEvent.java} (83%) diff --git a/README.md b/README.md index f739514..1d4e602 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,4 @@ Probably tastes better than any Apple editor and NetBeans combined. - [mkyoung](https://mkyong.com/java/how-to-create-directory-in-java/) -> Example of how to create a directory/folder. - [Youtube/Cool IT Help](https://www.youtube.com/watch?v=gnXRI3pHxrU&t=727s) -> Showing how to cast (Stage). Example of how FXML, listener/event and DirectyChooser nicely can cooperate. - [Code Makery](https://code.makery.ch/blog/javafx-dialogs-official/) -> Examples of many different javafx dialogs. +- [Oracle](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#button) -> How to use JOption. diff --git a/src/main/java/app/controllers/EditorController.java b/src/main/java/app/controllers/EditorController.java index 76d67c2..ba99179 100644 --- a/src/main/java/app/controllers/EditorController.java +++ b/src/main/java/app/controllers/EditorController.java @@ -8,9 +8,13 @@ import java.util.Collection; import java.util.ResourceBundle; import java.util.Scanner; +import javax.swing.JOptionPane; +import javax.swing.WindowConstants; + import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; +import org.apache.commons.lang.ObjectUtils.Null; import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.LineNumberFactory; import org.fxmisc.richtext.model.StyleSpans; @@ -20,10 +24,11 @@ import org.fxmisc.richtext.model.TwoDimensional.Position; import app.events.CopyEvent; import app.events.CutEvent; import app.events.EditorChangedEvent; +import app.events.ExitApplicationEvent; import app.events.LanguageChangedEvent; import app.events.PasteEvent; import app.events.RedoEvent; -import app.events.SaveFile; +import app.events.SaveFileEvent; import app.events.ToggleCommentEvent; import app.events.ToggleWrapTextEvent; import app.events.UndoEvent; @@ -172,7 +177,6 @@ public class EditorController implements Initializable, Controller { * Saving/Writing to the file based on the spesific filepath. Otherwise it will * open an error dialog to give the user feedback about what has happened. */ - private void saveCodeArea(String filePath) { try (PrintWriter writer = new PrintWriter(new File(filePath))) { if (filePath.endsWith(".java") || filePath.endsWith(".md")) { @@ -190,6 +194,24 @@ public class EditorController implements Initializable, Controller { } } + /** + * Checking if all is saved before closing the app. The user can either choose + * to exit or go back and save. + */ + + private void exitApp(boolean exit) { + if (!exit) { + int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nSave before you exit?", "Exit", + JOptionPane.YES_NO_OPTION); + + if (g == JOptionPane.YES_OPTION) { + // Got stuk here, to get back to "default" and keep condition of file that is + // not saved + } + } + + } + /* ------------------------------------------------------------------------ */ /* SUBSCRIPTIONS */ /* ------------------------------------------------------------------------ */ @@ -206,10 +228,15 @@ public class EditorController implements Initializable, Controller { * Save file (write to file) whenever the save in the menubare is selected */ @Subscribe - private void handle(SaveFile event) { + private void handle(SaveFileEvent event) { this.saveCodeArea(event.getPath()); } + @Subscribe + private void handle(ExitApplicationEvent event) { + this.exitApp(event.getIsSaved()); + } + @Subscribe private void handle(LanguageChangedEvent event) { this.refreshHighlighting(); diff --git a/src/main/java/app/controllers/MenubarController.java b/src/main/java/app/controllers/MenubarController.java index d360231..c74b004 100644 --- a/src/main/java/app/controllers/MenubarController.java +++ b/src/main/java/app/controllers/MenubarController.java @@ -1,11 +1,15 @@ package app.controllers; import java.io.File; +import java.io.IOException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ResourceBundle; +import javax.swing.JOptionPane; + import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; @@ -19,7 +23,7 @@ import app.events.OpenLinkInBrowserEvent; import app.events.OpenProjectEvent; import app.events.PasteEvent; import app.events.RedoEvent; -import app.events.SaveFile; +import app.events.SaveFileEvent; import app.events.ThemeChangedEvent; import app.events.ToggleCommentEvent; import app.events.ToggleWrapTextEvent; @@ -28,10 +32,12 @@ import app.model.Model; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert; import javafx.scene.control.CheckMenuItem; import javafx.scene.control.MenuBar; import javafx.scene.control.RadioMenuItem; import javafx.scene.control.ToggleGroup; +import javafx.scene.control.Alert.AlertType; import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import javafx.stage.Stage; @@ -69,16 +75,33 @@ public class MenubarController implements Initializable, Controller { this.eventBus.post(new FileSelectedEvent(null)); } - @FXML // temp solution + @FXML // temp solution, dette dreper pcen min, kan ikke brukes og logikken er heller + // ikke helt riktig private void handleNewFolder() { - // File chosenLocation = fc.showDialog(stage); + // if (Model.getProjectPath() == null) { + // Alert warning = new Alert(AlertType.WARNING); + // warning.setContentText("You must select a root before creating a + // directory!"); + + // warning.showAndWait(); + // } else { + // String createDirectory = JOptionPane.showInputDialog("Enter name of folder: + // "); + // try { + // if (createDirectory.matches("[a-zA-Z]+")) { + // Path path = Paths.get(Model.getProjectPath().toString() + "\\" + + // createDirectory); + // Files.createDirectory(path); + // this.eventBus.post(new OpenProjectEvent(Model.getProjectPath().toString())); + // } else { + // throw new IOException(); + // } + // } catch (IOException ex) { + // JOptionPane.showMessageDialog(null, "Could not create folder. Try again!"); + // System.out.println(ex); + // } - // if (chosenLocation != null) { - // chosenLocation.mkdir(); - // Model.setActiveFilePath(null); - // this.eventBus.post(new FileSelectedEvent(null)); - // this.eventBus.post(new OpenProjectEvent(chosenLocation.toString())); // } } @@ -136,7 +159,7 @@ public class MenubarController implements Initializable, Controller { @FXML private void handleSaveFile() { if (Model.getActiveFilePath() != null) { - this.eventBus.post(new SaveFile(Model.getActiveFilePath().toString())); + this.eventBus.post(new SaveFileEvent(Model.getActiveFilePath().toString())); this.eventBus.post(new FileSaveStateChangedEvent(true)); } else { handleSaveAsFile(); @@ -164,8 +187,9 @@ public class MenubarController implements Initializable, Controller { if (chosenLocation != null) { String chosenLocationToString = chosenLocation.toString(); - this.eventBus.post(new SaveFile(chosenLocationToString)); + this.eventBus.post(new SaveFileEvent(chosenLocationToString)); this.eventBus.post(new FileSelectedEvent(chosenLocationToString)); + Path chosenLocationToPath = Paths.get(chosenLocationToString); Model.setActiveFilePath(chosenLocationToPath); this.eventBus.post(new FileSaveStateChangedEvent(true)); diff --git a/src/main/java/app/events/ExitApplicationEvent.java b/src/main/java/app/events/ExitApplicationEvent.java index 57a628a..cc71bb8 100644 --- a/src/main/java/app/events/ExitApplicationEvent.java +++ b/src/main/java/app/events/ExitApplicationEvent.java @@ -1,8 +1,29 @@ package app.events; +import app.model.Model; + /** * Event signalizing a shutdown request for the whole applicaton */ public class ExitApplicationEvent extends Event { - + + private boolean isSaved; + + /** + * Event signalizing if a file is saved or modified. + * + * + */ + + public ExitApplicationEvent() { + this.isSaved = Model.getFileIsSaved(); + } + + /** + * @return Whether or not the file has been modified or saved + */ + public boolean getIsSaved() { + return this.isSaved; + } + } diff --git a/src/main/java/app/events/SaveFile.java b/src/main/java/app/events/SaveFileEvent.java similarity index 83% rename from src/main/java/app/events/SaveFile.java rename to src/main/java/app/events/SaveFileEvent.java index f79407e..d543bb8 100644 --- a/src/main/java/app/events/SaveFile.java +++ b/src/main/java/app/events/SaveFileEvent.java @@ -1,6 +1,6 @@ package app.events; -public class SaveFile extends Event { +public class SaveFileEvent extends Event { private String path; @@ -11,7 +11,7 @@ public class SaveFile extends Event { * * @param path The path of the selected file */ - public SaveFile(String path) { + public SaveFileEvent(String path) { this.path = path; }