added some more with writing and reading to file
This commit is contained in:
parent
79e684228f
commit
1156b827ac
@ -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.
|
- [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.
|
- [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.
|
- [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.
|
||||||
|
@ -8,9 +8,13 @@ import java.util.Collection;
|
|||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
import com.google.common.eventbus.EventBus;
|
import com.google.common.eventbus.EventBus;
|
||||||
import com.google.common.eventbus.Subscribe;
|
import com.google.common.eventbus.Subscribe;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.ObjectUtils.Null;
|
||||||
import org.fxmisc.richtext.CodeArea;
|
import org.fxmisc.richtext.CodeArea;
|
||||||
import org.fxmisc.richtext.LineNumberFactory;
|
import org.fxmisc.richtext.LineNumberFactory;
|
||||||
import org.fxmisc.richtext.model.StyleSpans;
|
import org.fxmisc.richtext.model.StyleSpans;
|
||||||
@ -20,10 +24,11 @@ import org.fxmisc.richtext.model.TwoDimensional.Position;
|
|||||||
import app.events.CopyEvent;
|
import app.events.CopyEvent;
|
||||||
import app.events.CutEvent;
|
import app.events.CutEvent;
|
||||||
import app.events.EditorChangedEvent;
|
import app.events.EditorChangedEvent;
|
||||||
|
import app.events.ExitApplicationEvent;
|
||||||
import app.events.LanguageChangedEvent;
|
import app.events.LanguageChangedEvent;
|
||||||
import app.events.PasteEvent;
|
import app.events.PasteEvent;
|
||||||
import app.events.RedoEvent;
|
import app.events.RedoEvent;
|
||||||
import app.events.SaveFile;
|
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;
|
||||||
@ -172,7 +177,6 @@ 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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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))) {
|
||||||
if (filePath.endsWith(".java") || filePath.endsWith(".md")) {
|
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 */
|
/* SUBSCRIPTIONS */
|
||||||
/* ------------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------------ */
|
||||||
@ -206,10 +228,15 @@ public class EditorController implements Initializable, Controller {
|
|||||||
* Save file (write to file) whenever the save in the menubare is selected
|
* Save file (write to file) whenever the save in the menubare is selected
|
||||||
*/
|
*/
|
||||||
@Subscribe
|
@Subscribe
|
||||||
private void handle(SaveFile event) {
|
private void handle(SaveFileEvent event) {
|
||||||
this.saveCodeArea(event.getPath());
|
this.saveCodeArea(event.getPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
private void handle(ExitApplicationEvent event) {
|
||||||
|
this.exitApp(event.getIsSaved());
|
||||||
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
private void handle(LanguageChangedEvent event) {
|
private void handle(LanguageChangedEvent event) {
|
||||||
this.refreshHighlighting();
|
this.refreshHighlighting();
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
package app.controllers;
|
package app.controllers;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
import com.google.common.eventbus.EventBus;
|
import com.google.common.eventbus.EventBus;
|
||||||
import com.google.common.eventbus.Subscribe;
|
import com.google.common.eventbus.Subscribe;
|
||||||
|
|
||||||
@ -19,7 +23,7 @@ import app.events.OpenLinkInBrowserEvent;
|
|||||||
import app.events.OpenProjectEvent;
|
import app.events.OpenProjectEvent;
|
||||||
import app.events.PasteEvent;
|
import app.events.PasteEvent;
|
||||||
import app.events.RedoEvent;
|
import app.events.RedoEvent;
|
||||||
import app.events.SaveFile;
|
import app.events.SaveFileEvent;
|
||||||
import app.events.ThemeChangedEvent;
|
import app.events.ThemeChangedEvent;
|
||||||
import app.events.ToggleCommentEvent;
|
import app.events.ToggleCommentEvent;
|
||||||
import app.events.ToggleWrapTextEvent;
|
import app.events.ToggleWrapTextEvent;
|
||||||
@ -28,10 +32,12 @@ import app.model.Model;
|
|||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
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.CheckMenuItem;
|
import javafx.scene.control.CheckMenuItem;
|
||||||
import javafx.scene.control.MenuBar;
|
import javafx.scene.control.MenuBar;
|
||||||
import javafx.scene.control.RadioMenuItem;
|
import javafx.scene.control.RadioMenuItem;
|
||||||
import javafx.scene.control.ToggleGroup;
|
import javafx.scene.control.ToggleGroup;
|
||||||
|
import javafx.scene.control.Alert.AlertType;
|
||||||
import javafx.stage.DirectoryChooser;
|
import javafx.stage.DirectoryChooser;
|
||||||
import javafx.stage.FileChooser;
|
import javafx.stage.FileChooser;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
@ -69,16 +75,33 @@ public class MenubarController implements Initializable, Controller {
|
|||||||
this.eventBus.post(new FileSelectedEvent(null));
|
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() {
|
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
|
@FXML
|
||||||
private void handleSaveFile() {
|
private void handleSaveFile() {
|
||||||
if (Model.getActiveFilePath() != null) {
|
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));
|
this.eventBus.post(new FileSaveStateChangedEvent(true));
|
||||||
} else {
|
} else {
|
||||||
handleSaveAsFile();
|
handleSaveAsFile();
|
||||||
@ -164,8 +187,9 @@ public class MenubarController implements Initializable, Controller {
|
|||||||
|
|
||||||
if (chosenLocation != null) {
|
if (chosenLocation != null) {
|
||||||
String chosenLocationToString = chosenLocation.toString();
|
String chosenLocationToString = chosenLocation.toString();
|
||||||
this.eventBus.post(new SaveFile(chosenLocationToString));
|
this.eventBus.post(new SaveFileEvent(chosenLocationToString));
|
||||||
this.eventBus.post(new FileSelectedEvent(chosenLocationToString));
|
this.eventBus.post(new FileSelectedEvent(chosenLocationToString));
|
||||||
|
|
||||||
Path chosenLocationToPath = Paths.get(chosenLocationToString);
|
Path chosenLocationToPath = Paths.get(chosenLocationToString);
|
||||||
Model.setActiveFilePath(chosenLocationToPath);
|
Model.setActiveFilePath(chosenLocationToPath);
|
||||||
this.eventBus.post(new FileSaveStateChangedEvent(true));
|
this.eventBus.post(new FileSaveStateChangedEvent(true));
|
||||||
|
@ -1,8 +1,29 @@
|
|||||||
package app.events;
|
package app.events;
|
||||||
|
|
||||||
|
import app.model.Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event signalizing a shutdown request for the whole applicaton
|
* Event signalizing a shutdown request for the whole applicaton
|
||||||
*/
|
*/
|
||||||
public class ExitApplicationEvent extends Event {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package app.events;
|
package app.events;
|
||||||
|
|
||||||
public class SaveFile extends Event {
|
public class SaveFileEvent extends Event {
|
||||||
|
|
||||||
private String path;
|
private String path;
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ public class SaveFile extends Event {
|
|||||||
*
|
*
|
||||||
* @param path The path of the selected file
|
* @param path The path of the selected file
|
||||||
*/
|
*/
|
||||||
public SaveFile(String path) {
|
public SaveFileEvent(String path) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user