Skip to content

Commit 27acf4d

Browse files
committed
Changes
1 parent 35e8ab3 commit 27acf4d

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/main/java/org/ainm/controllers/MainController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ void saveFileAs() throws IOException {
311311
}
312312
}
313313

314+
@FXML
315+
void saveCurrentFile() throws IOException {
316+
//Save current view to file
317+
Object controller = getController(get_current_tab().getContent());
318+
if (controller.getClass().equals(new TodolistController().getClass())) {
319+
((TodolistController) controller).saveCurrentFile();
320+
}
321+
}
322+
314323
void openFile(String filename) throws IOException {
315324
if (filename.endsWith(".todo")) {
316325
//Load todolist view

src/main/java/org/ainm/controllers/todolist/TodolistController.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ainm.controllers.todolist;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.nio.file.Files;
56
import java.nio.file.Path;
@@ -14,6 +15,8 @@
1415

1516
public class TodolistController {
1617

18+
Path openedFile;
19+
1720
@FXML
1821
private VBox todos_list;
1922

@@ -35,6 +38,7 @@ void addTodo(String todo) throws IOException {
3538
}
3639

3740
public void loadFile(Path file) {
41+
openedFile = file;
3842
// Add each line from file to todolist
3943
try (Stream<String> lines = Files.lines(file)) {
4044
lines.forEach((line) -> {
@@ -50,7 +54,21 @@ public void loadFile(Path file) {
5054
}
5155
}
5256

53-
public void saveFileAs(Path file) throws IOException {
57+
public void saveFileAs(Path file) {
58+
openedFile = file;
59+
try {
60+
saveFile(file);
61+
} catch (IOException e) {
62+
// TODO Auto-generated catch block
63+
e.printStackTrace();
64+
}
65+
}
66+
67+
public void saveCurrentFile() throws IOException {
68+
saveFile(openedFile);
69+
}
70+
71+
void saveFile(Path file) throws IOException {
5472
//Create a string with a todo per line
5573
String saveData = "";
5674
for(Node todoItem:todos_list.getChildren()) {
@@ -60,7 +78,6 @@ public void saveFileAs(Path file) throws IOException {
6078
//Save this string to the given file
6179
Files.writeString(file, saveData);
6280
}
63-
6481
public static Object getController(Node node) {
6582
Object controller = null;
6683
do {

src/main/resources/fxml/MainView.fxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
</items>
7373
</Menu>
7474
<MenuItem fx:id="menuItemSaveAs" mnemonicParsing="false" onAction="#saveFileAs" text="Save as" />
75-
<MenuItem disable="true" mnemonicParsing="false" text="Save" />
75+
<MenuItem fx:id="menuItemSaveCurrentFile" mnemonicParsing="false" onAction="#saveCurrentFile" text="Save" />
7676
<MenuItem fx:id="menu_item_close_file" mnemonicParsing="false" onAction="#close_file" text="Close" />
7777
</items>
7878
</Menu>

0 commit comments

Comments
 (0)