diff --git a/src/main/java/app/Main.java b/src/main/java/app/Main.java index befe033..f5950e2 100644 --- a/src/main/java/app/Main.java +++ b/src/main/java/app/Main.java @@ -1,6 +1,7 @@ package app; import java.io.IOException; +import java.util.Optional; import javafx.application.Application; import javafx.fxml.FXMLLoader; @@ -9,8 +10,6 @@ import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; import app.events.FileSaveStateChangedEvent; -import app.events.LanguageChangedEvent; -import app.events.ThemeChangedEvent; import app.model.Model; import app.settings.SettingsProvider; @@ -71,6 +70,8 @@ public class Main extends Application { MainController mainController = fxmlLoader.getController(); SettingsProvider SP = new SettingsProvider(mainController.getEventBus()); SP.loadSettings(); + Model.setActiveFilePath(Optional.empty()); + Model.setProjectPath(Optional.empty()); mainController.getEventBus().post(new FileSaveStateChangedEvent(true)); mainController.setHostServices(getHostServices()); } diff --git a/src/main/java/app/MainController.java b/src/main/java/app/MainController.java index 8ec3baf..0a078ee 100644 --- a/src/main/java/app/MainController.java +++ b/src/main/java/app/MainController.java @@ -139,7 +139,7 @@ public class MainController implements Initializable { JOptionPane.YES_NO_OPTION); if (g == JOptionPane.YES_OPTION) { - this.eventBus.post(new SaveFileEvent(false)); + this.eventBus.post(new SaveFileEvent()); Platform.exit(); } } else { diff --git a/src/main/java/app/controllers/EditorController.java b/src/main/java/app/controllers/EditorController.java index 1b15b62..395d716 100644 --- a/src/main/java/app/controllers/EditorController.java +++ b/src/main/java/app/controllers/EditorController.java @@ -1,7 +1,6 @@ package app.controllers; import java.net.URL; -import java.nio.file.Path; import java.util.Collection; import java.util.ResourceBundle; @@ -158,11 +157,13 @@ public class EditorController implements Initializable, Controller { public void saveCodeArea(boolean isNewFile) { Stage stage = (Stage) editor.getScene().getWindow(); + isNewFile = Model.getActiveFilePath().isEmpty(); + if (isNewFile && FileOperations.saveFileWithDialog(stage, editor.getText())) { this.eventBus.post(new OpenFileEvent(Model.getActiveFilePath())); this.eventBus.post(new FileSaveStateChangedEvent(true)); } - else if (FileOperations.saveFile(Model.getProjectPath().toString(), editor.getText())) { + else if (FileOperations.saveFile(Model.getActiveFilePath().orElseThrow(), editor.getText())) { this.eventBus.post(new FileSaveStateChangedEvent(true)); } } diff --git a/src/main/java/app/controllers/FiletreeController.java b/src/main/java/app/controllers/FiletreeController.java index 45a0bab..db4bc4a 100644 --- a/src/main/java/app/controllers/FiletreeController.java +++ b/src/main/java/app/controllers/FiletreeController.java @@ -18,6 +18,7 @@ import com.google.common.eventbus.Subscribe; import app.events.FileSelectedEvent; import app.events.OpenProjectEvent; +import app.events.SaveFileEvent; import app.model.Model; import app.service.FiletreeOperations; import javafx.fxml.Initializable; @@ -94,4 +95,10 @@ public class FiletreeController implements Initializable, Controller { this.showTree(event.getPath().toString()); } + @Subscribe + private void handle(SaveFileEvent event) { + if (event.getIsNewFile()) + Model.getProjectPath().ifPresent(path -> this.showTree(path.toString())); + } + } diff --git a/src/main/java/app/controllers/MenubarController.java b/src/main/java/app/controllers/MenubarController.java index fbd8e3e..c0ab513 100644 --- a/src/main/java/app/controllers/MenubarController.java +++ b/src/main/java/app/controllers/MenubarController.java @@ -23,7 +23,6 @@ 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; @@ -34,7 +33,6 @@ import javafx.scene.control.MenuBar; import javafx.scene.control.RadioMenuItem; import javafx.scene.control.ToggleGroup; import javafx.stage.Stage; -import java.util.Optional; /** * A FXML controller that controls the menubar component of the UI @@ -121,10 +119,7 @@ public class MenubarController implements Initializable, Controller { */ @FXML private void handleSaveFile() { - if (Model.getActiveFilePath() != null) - this.eventBus.post(new SaveFileEvent(false)); - else // New file needs a path - handleSaveAsFile(); + this.eventBus.post(new SaveFileEvent()); } /** @@ -132,7 +127,7 @@ public class MenubarController implements Initializable, Controller { */ @FXML private void handleSaveAsFile() { - this.eventBus.post(new SaveFileEvent(true)); + handleSaveFile(); } /** diff --git a/src/main/java/app/events/SaveFileEvent.java b/src/main/java/app/events/SaveFileEvent.java index c8210d0..e2850de 100644 --- a/src/main/java/app/events/SaveFileEvent.java +++ b/src/main/java/app/events/SaveFileEvent.java @@ -1,5 +1,7 @@ package app.events; +import app.model.Model; + public class SaveFileEvent extends Event { private boolean isNewFile; @@ -11,8 +13,8 @@ public class SaveFileEvent extends Event { * * @param isNewFile The path of the selected file */ - public SaveFileEvent(boolean isNewFile) { - this.isNewFile = isNewFile; + public SaveFileEvent() { + this.isNewFile = Model.getActiveFilePath().isEmpty(); } /** diff --git a/src/main/java/app/service/FileOperations.java b/src/main/java/app/service/FileOperations.java index f710e1b..c472f08 100644 --- a/src/main/java/app/service/FileOperations.java +++ b/src/main/java/app/service/FileOperations.java @@ -27,9 +27,6 @@ public class FileOperations { if (chosenFile == null) throw new FileNotFoundException(); - // if (chosenFile != null) { - // String correctFormat = chosenFile.getAbsolutePath().replace("\\", "\\\\"); - // } return chosenFile; @@ -48,35 +45,31 @@ public class FileOperations { return dir; } - public static boolean saveFile(String filepath, String content) { + public static boolean saveFile(Path filepath, String content) { + try (PrintWriter writer = new PrintWriter(filepath.toFile())) { + writer.println(content); + return true; - try (PrintWriter writer = new PrintWriter(new File(filepath))) { - writer.println(content); } catch (FileNotFoundException ex) { DialogBoxes.showErrorMessage("Could not save file!"); System.err.println(filepath); return false; } - return true; - } public static boolean saveFileWithDialog(Stage stage, String content) { FileChooser fc = new FileChooser(); fc.setTitle("Save as"); - Model.getProjectPath().ifPresent(path -> fc.setInitialDirectory(path.toFile())); - - FileChooser.ExtensionFilter extJava = new FileChooser.ExtensionFilter("Java", "*.java"); - FileChooser.ExtensionFilter extMd = new FileChooser.ExtensionFilter("Markdown", "*.md"); - - fc.getExtensionFilters().addAll(extJava, extMd); + Model + .getProjectPath() + .ifPresent(path -> fc.setInitialDirectory(path.toFile())); File chosenLocation = fc.showSaveDialog(stage); if (chosenLocation == null) return false; - if (saveFile(chosenLocation.getAbsolutePath(), content)) { + if (saveFile(chosenLocation.toPath(), content)) { Model.setActiveFilePath(Optional.of(chosenLocation.toPath())); return true; } @@ -102,7 +95,5 @@ public class FileOperations { } return result; - } - } diff --git a/src/main/java/app/service/FiletreeOperations.java b/src/main/java/app/service/FiletreeOperations.java index 71e8b62..85f9eb5 100644 --- a/src/main/java/app/service/FiletreeOperations.java +++ b/src/main/java/app/service/FiletreeOperations.java @@ -19,13 +19,6 @@ public class FiletreeOperations { // FIXME: File specific icons not working properly // TODO: Clean up code that is not in use - - // Creating the images for the icons. - Image folder = new Image(getClass().getResourceAsStream("/graphics/folder.png")); - Image md = new Image(getClass().getResourceAsStream("/graphics/md.png")); - Image java = new Image(getClass().getResourceAsStream("/graphics/java.png")); - Image placeholder = new Image(getClass().getResourceAsStream("/graphics/placeholder.png")); - // TODO: Error check for recursiveness, and files without icons /** @@ -53,7 +46,14 @@ public class FiletreeOperations { } } else { - checkExtensions(file, parent); + + try { + CheckBoxTreeItem element = + new CheckBoxTreeItem<>(file.getName(), new ImageView(getIconForFile(file))); + parent.getChildren().add(element); + } catch (Exception e) { + System.err.println("[ERROR]: DEFAULT FILE ICON NOT FOUND"); + } } } @@ -72,29 +72,6 @@ public class FiletreeOperations { } - /** - * A help function to check if the extensions match .java or .md to insert the - * specific icons and sending it to another help funtion createExtension to - * create the new CheckboxTreeItem that will add to the parent. - */ - private static void checkExtensions(File file, CheckBoxTreeItem parent) { - String name = file.getName(); - String ext = (name.substring(file.getName().lastIndexOf(".") + 1, file.getName().length())); - - try { - createExtension(name, getIconForFile(file), parent); - } catch (Exception e) { - System.err.println("[ERROR]: DEFAULT FILE ICON NOT FOUND"); - } - - // if ("java".equals(ext)) - // createExtension(name, java, parent); - // else if ("md".equals(ext)) - // createExtension(name, md, parent); - // else - // createExtension(name, placeholder, parent); - } - private static Image getIconForFile(File file) throws IOException { Image icon; @@ -124,23 +101,24 @@ public class FiletreeOperations { return icon; } - private static void createExtension(String name, Image image, CheckBoxTreeItem parent) { - CheckBoxTreeItem element = new CheckBoxTreeItem<>(name, new ImageView(image)); - parent.getChildren().add(element); - } - public static Path getPathOfTreeItem(TreeItem item) { - String root = Model.getProjectPath().orElseThrow().getFileName().toString(); - String path = ""; - while (!root.equals(item.getValue())) { - path = File.separator + item.getValue() + path; - item = item.getParent(); - } + final String rootFolderName = + Model + .getProjectPath() + .orElseThrow() + .getFileName() + .toString(); - path = Model.getProjectPath().toString() + path; - Path pathToString = Paths.get(path); + String path = ""; + while (!rootFolderName.equals(item.getValue())) { + path = File.separator + item.getValue() + path; + item = item.getParent(); + } - return pathToString; - } - + path = Model.getProjectPath().orElseThrow().toString() + path; + + Path pathToString = Paths.get(path); + + return pathToString; + } }