Added some to saving, add more to exceptions tomorrow

master
Oystein 2021-04-06 20:45:15 +02:00
parent 3f8482bb78
commit df41e3df58
5 changed files with 113 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package app.controllers;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Collection;
import java.util.ResourceBundle;
@ -22,6 +23,7 @@ import app.events.EditorChangedEvent;
import app.events.LanguageChangedEvent;
import app.events.PasteEvent;
import app.events.RedoEvent;
import app.events.SaveFile;
import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent;
@ -76,11 +78,12 @@ public class EditorController implements Initializable, Controller {
}
/**
* Uses the {@link app.model.ProgrammingLanguage ProgrammingLanguage} in
* Uses the {@link app.model.ProgrammingLanguage ProgrammingLanguage} in
* {@link app.model.Model Model} to determine whether the current line/selection
* is commented or not, and toggles the comment.
*
* @see app.model.ProgrammingLanguage#commentLine(String) ProgrammingLanguage.commentLine(line)
* @see app.model.ProgrammingLanguage#commentLine(String)
* ProgrammingLanguage.commentLine(line)
*/
private void toggleComment() {
if (editor.getSelectedText().equals("")) {
@ -109,6 +112,7 @@ public class EditorController implements Initializable, Controller {
/**
* Updates the wraptext setting of the code area
*
* @param isWrapText The new value for the setting
*/
private void setWrapText(boolean isWrapText) {
@ -131,6 +135,7 @@ public class EditorController implements Initializable, Controller {
/**
* Updates the content of the editor to a specific filepath
*
* @param filePath The path of the file
* @throws FileNotFoundException
*/
@ -147,11 +152,24 @@ public class EditorController implements Initializable, Controller {
}
}
private void saveCodeArea(String filePath) {
try (PrintWriter writer = new PrintWriter(new File(filePath))) {
writer.println(editor.getText());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Subscribe
private void handle(FileSelectedEvent event) {
this.setEditorContent(event.getPath());
}
@Subscribe
private void handle(SaveFile event) {
this.saveCodeArea(event.getPath());
}
@Subscribe
private void handle(LanguageChangedEvent event) {
this.refreshHighlighting();

View File

@ -10,6 +10,8 @@ import javafx.scene.input.MouseEvent;
import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
@ -143,6 +145,8 @@ public class FiletreeController implements Initializable, Controller {
}
path = Model.getProjectPath() + path;
Path pathToString = Paths.get(path);
Model.setActiveFilePath(pathToString);
this.eventBus.post(new FileSelectedEvent(path));
}
}

View File

@ -1,7 +1,11 @@
package app.controllers;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
@ -16,6 +20,7 @@ import app.events.OpenLinkInBrowserEvent;
import app.events.OpenProjectEvent;
import app.events.PasteEvent;
import app.events.RedoEvent;
import app.events.SaveFile;
import app.events.ThemeChangedEvent;
import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent;
@ -57,7 +62,20 @@ public class MenubarController implements Initializable, Controller {
}
/* ------------------------------------------------------------------------ */
/* FILE */
/* CREATE FILE/DIRECTORY */
/* ------------------------------------------------------------------------ */
@FXML
private void handleNewFile() {
}
@FXML
private void handleNewFolder() {
}
/* ------------------------------------------------------------------------ */
/* OPEN FILE */
/* ------------------------------------------------------------------------ */
/**
@ -93,6 +111,47 @@ public class MenubarController implements Initializable, Controller {
}
/* ------------------------------------------------------------------------ */
/* SAVE FILE */
/* ------------------------------------------------------------------------ */
/**
* Handles whenever the Save button is pressed in the menubar
*/
@FXML
private void handleSaveFile() {
if (Model.getActiveFilePath() != null) {
this.eventBus.post(new SaveFile(Model.getActiveFilePath().toString()));
} else {
handleSaveAsFile();
}
}
/**
* Handles whenever the Save as button is pressed in the menubar
*/
@FXML
private void handleSaveAsFile() {
FileChooser fc = new FileChooser();
fc.setTitle("Save as");
Stage stage = (Stage) menubar.getScene().getWindow();
FileChooser.ExtensionFilter extJava = new FileChooser.ExtensionFilter("Java", "*.java");
FileChooser.ExtensionFilter extMd = new FileChooser.ExtensionFilter("Markdown", "*.md");
fc.getExtensionFilters().addAll(extJava, extMd);
File chosenLocation = fc.showSaveDialog(stage);
if (chosenLocation != null) {
String chosenLocationToString = chosenLocation.toString();
this.eventBus.post(new SaveFile(chosenLocationToString));
this.eventBus.post(new FileSelectedEvent(chosenLocationToString));
Path chosenLocationToPath = Paths.get(chosenLocationToString);
Model.setActiveFilePath(chosenLocationToPath);
}
}
/**
* Handles whenever the programming language is changed from the menubar.
*/

View File

@ -0,0 +1,25 @@
package app.events;
public class SaveFile extends Event {
private String path;
/**
* Event signalizing that a file is to be saved.
*
* Not to be confused with {@link FileSelectedEvent}
*
* @param path The path of the selected file
*/
public SaveFile(String path) {
this.path = path;
}
/**
* @return The path of the selected file to be saved.
*/
public String getPath() {
return this.path;
}
}

View File

@ -18,14 +18,14 @@
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New File" accelerator="Shortcut+n"/>
<MenuItem mnemonicParsing="false" text="New Folder" accelerator="Shortcut+Shift+N"/>
<MenuItem mnemonicParsing="false" text="New File" accelerator="Shortcut+n" onAction="#handleNewFile"/>
<MenuItem mnemonicParsing="false" text="New Folder" accelerator="Shortcut+Shift+N" onAction="#handleNewFolder"/>
<SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Open File" accelerator="Shortcut+o" onAction="#handleOpenFile"/>
<MenuItem mnemonicParsing="false" text="Open Project" accelerator="Shortcut+Shift+O" onAction="#handleOpenProject"/>
<SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Save" accelerator="Shortcut+s"/>
<MenuItem mnemonicParsing="false" text="Save as" accelerator="Shortcut+Shift+S"/>
<MenuItem mnemonicParsing="false" text="Save" accelerator="Shortcut+s" onAction="#handleSaveFile"/>
<MenuItem mnemonicParsing="false" text="Save as" accelerator="Shortcut+Shift+S" onAction="#handleSaveAsFile"/>
<SeparatorMenuItem/>
<fx:define>