From 2ecda54d7f5746a0909aea6951243310f7f3477e Mon Sep 17 00:00:00 2001 From: Oystein Date: Wed, 7 Apr 2021 03:26:26 +0200 Subject: [PATCH] added som restrictions to filetype and error alert --- .../app/controllers/EditorController.java | 39 +++++++++++++++---- .../app/controllers/MenubarController.java | 14 ++++++- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/main/java/app/controllers/EditorController.java b/src/main/java/app/controllers/EditorController.java index b4905ac..58133ef 100644 --- a/src/main/java/app/controllers/EditorController.java +++ b/src/main/java/app/controllers/EditorController.java @@ -33,6 +33,8 @@ import app.model.Model; import app.service.LanguageOperations; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; /** * A FXML controller that controls the editor component of the UI @@ -140,23 +142,44 @@ public class EditorController implements Initializable, Controller { * @throws FileNotFoundException */ private void setEditorContent(String filePath) { - try (Scanner sc = new Scanner(new File(filePath))) { + if (filePath == null) { editor.clear(); - while (sc.hasNextLine()) { - editor.appendText(sc.nextLine()); - editor.appendText("\n"); + return; + } + try (Scanner sc = new Scanner(new File(filePath))) { + if (filePath.endsWith(".java") || filePath.endsWith(".md")) { + editor.clear(); + while (sc.hasNextLine()) { + editor.appendText(sc.nextLine()); + editor.appendText("\n"); + } + } else { + throw new FileNotFoundException(); } + } catch (FileNotFoundException ex) { - // TODO: add better error handling and user feedback + Alert error = new Alert(AlertType.ERROR); + error.setContentText("Could not be opened!\nMust be a java or md file. Try again."); + + error.showAndWait(); System.err.println(filePath); } } private void saveCodeArea(String filePath) { try (PrintWriter writer = new PrintWriter(new File(filePath))) { - writer.println(editor.getText()); - } catch (FileNotFoundException e) { - e.printStackTrace(); + if (filePath.endsWith(".java") || filePath.endsWith(".md")) { + writer.println(editor.getText()); + } else { + throw new FileNotFoundException(); + } + + } catch (FileNotFoundException ex) { + Alert error = new Alert(AlertType.ERROR); + error.setContentText("Could not save file!\nMust be a java or md file. Try again."); + + error.showAndWait(); + System.err.println(filePath); } } diff --git a/src/main/java/app/controllers/MenubarController.java b/src/main/java/app/controllers/MenubarController.java index 22750e0..4d755ad 100644 --- a/src/main/java/app/controllers/MenubarController.java +++ b/src/main/java/app/controllers/MenubarController.java @@ -66,12 +66,22 @@ public class MenubarController implements Initializable, Controller { /* ------------------------------------------------------------------------ */ @FXML private void handleNewFile() { - + Model.setActiveFilePath(null); + this.eventBus.post(new FileSelectedEvent(null)); } - @FXML + @FXML // temp solution private void handleNewFolder() { + // File chosenLocation = fc.showDialog(stage); + + // if (chosenLocation != null) { + // chosenLocation.mkdir(); + // Model.setActiveFilePath(null); + // this.eventBus.post(new FileSelectedEvent(null)); + // this.eventBus.post(new OpenProjectEvent(chosenLocation.toString())); + // } + } /* ------------------------------------------------------------------------ */