added interfaces for file management

This commit is contained in:
Oystein 2021-04-12 15:51:57 +02:00
parent 6d1794421c
commit 38ea6a91fe
3 changed files with 22 additions and 5 deletions

View File

@ -120,7 +120,7 @@ public class MainController implements Initializable {
} }
/** /**
* Handle an exit request for the whole program Checking if all is saved before * Handle an exit request for the whole program. Checking if all is saved before
* closing the app. The user can either choose to exit or go back to the * closing the app. The user can either choose to exit or go back to the
* application and save. * application and save.
* *
@ -129,7 +129,7 @@ public class MainController implements Initializable {
@Subscribe @Subscribe
private void handle(ExitApplicationEvent event) { private void handle(ExitApplicationEvent event) {
if (!Model.getFileIsSaved()) { if (!Model.getFileIsSaved()) {
int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nSave before you exit?", "Exit", int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nGo back to save?", "Exit",
JOptionPane.YES_NO_OPTION); JOptionPane.YES_NO_OPTION);
if (!(g == JOptionPane.YES_OPTION)) { if (!(g == JOptionPane.YES_OPTION)) {

View File

@ -29,6 +29,7 @@ import app.events.SaveFileEvent;
import app.events.ToggleCommentEvent; import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent; import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent; import app.events.UndoEvent;
import app.interfaces.FileManagement;
import app.events.FileSaveStateChangedEvent; import app.events.FileSaveStateChangedEvent;
import app.events.FileSelectedEvent; import app.events.FileSelectedEvent;
import app.model.Model; import app.model.Model;
@ -41,7 +42,7 @@ 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
*/ */
public class EditorController implements Initializable, Controller { public class EditorController implements Initializable, Controller, FileManagement {
@FXML @FXML
private CodeArea editor; private CodeArea editor;
@ -144,7 +145,7 @@ public class EditorController implements Initializable, Controller {
* @param filePath The path of the file * @param filePath The path of the file
* @throws FileNotFoundException * @throws FileNotFoundException
*/ */
private void setEditorContent(String filePath) { public void setEditorContent(String filePath) {
if (filePath == null) { if (filePath == null) {
editor.clear(); editor.clear();
editor.appendText("// New File"); editor.appendText("// New File");
@ -173,8 +174,11 @@ public class EditorController implements Initializable, Controller {
/** /**
* Saving/Writing to the file based on the spesific filepath. Otherwise it will * 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. * open an error dialog to give the user feedback about what has happened.
*
* @param filePath The path of the file
* @throws FileNotFoundException
*/ */
private void saveCodeArea(String filePath) { public void saveCodeArea(String filePath) {
try (PrintWriter writer = new PrintWriter(new File(filePath))) { try (PrintWriter writer = new PrintWriter(new File(filePath))) {
if (filePath.endsWith(".java") || filePath.endsWith(".md")) { if (filePath.endsWith(".java") || filePath.endsWith(".md")) {
writer.println(editor.getText()); writer.println(editor.getText());

View File

@ -0,0 +1,13 @@
package app.interfaces;
/*
* An interface that contains two methods for reading and writing to files.
*/
public interface FileManagement {
void setEditorContent(String filePath);
void saveCodeArea(String filePath);
}