added som restrictions to filetype and error alert

This commit is contained in:
Oystein 2021-04-07 03:26:26 +02:00
parent df41e3df58
commit 2ecda54d7f
2 changed files with 43 additions and 10 deletions

View File

@ -33,6 +33,8 @@ import app.model.Model;
import app.service.LanguageOperations; import app.service.LanguageOperations;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; 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 * A FXML controller that controls the editor component of the UI
@ -140,23 +142,44 @@ public class EditorController implements Initializable, Controller {
* @throws FileNotFoundException * @throws FileNotFoundException
*/ */
private void setEditorContent(String filePath) { private void setEditorContent(String filePath) {
try (Scanner sc = new Scanner(new File(filePath))) { if (filePath == null) {
editor.clear(); editor.clear();
while (sc.hasNextLine()) { return;
editor.appendText(sc.nextLine()); }
editor.appendText("\n"); 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) { } 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); System.err.println(filePath);
} }
} }
private void saveCodeArea(String filePath) { private void saveCodeArea(String filePath) {
try (PrintWriter writer = new PrintWriter(new File(filePath))) { try (PrintWriter writer = new PrintWriter(new File(filePath))) {
writer.println(editor.getText()); if (filePath.endsWith(".java") || filePath.endsWith(".md")) {
} catch (FileNotFoundException e) { writer.println(editor.getText());
e.printStackTrace(); } 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);
} }
} }

View File

@ -66,12 +66,22 @@ public class MenubarController implements Initializable, Controller {
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
@FXML @FXML
private void handleNewFile() { private void handleNewFile() {
Model.setActiveFilePath(null);
this.eventBus.post(new FileSelectedEvent(null));
} }
@FXML @FXML // temp solution
private void handleNewFolder() { 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()));
// }
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */