Skip to content

Commit 35e8ab3

Browse files
committed
Added Save as option
1 parent 94bf451 commit 35e8ab3

File tree

7 files changed

+70
-3
lines changed

7 files changed

+70
-3
lines changed

ainm.todo

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,16 @@ Task 1
22
Task 2
33
Task 3
44
Go to school
5-
Task 4
5+
Task 4
6+
New todo
7+
New todo
8+
New todo
9+
New todo
10+
New todo
11+
New todo
12+
New todo
13+
New todo
14+
New todo
15+
New todo
16+
New todo
17+
New todo

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import javafx.scene.control.TreeItem;
3737
import javafx.scene.control.TreeView;
3838
import javafx.stage.DirectoryChooser;
39+
import javafx.stage.FileChooser;
3940
import javafx.stage.Stage;
4041

4142
public class MainController {
@@ -298,6 +299,18 @@ void new_todo_list(ActionEvent event) throws IOException {
298299
}
299300
}
300301

302+
@FXML
303+
void saveFileAs() throws IOException {
304+
//Open file explorer
305+
FileChooser chooser = new FileChooser();
306+
File file = chooser.showSaveDialog(null);
307+
//Save current view to file
308+
Object controller = getController(get_current_tab().getContent());
309+
if (controller.getClass().equals(new TodolistController().getClass())) {
310+
((TodolistController) controller).saveFileAs(Paths.get(file.getAbsolutePath()));
311+
}
312+
}
313+
301314
void openFile(String filename) throws IOException {
302315
if (filename.endsWith(".todo")) {
303316
//Load todolist view
@@ -320,4 +333,13 @@ Tab get_current_tab() {
320333
SingleSelectionModel<Tab> selectionModel = tab_pane.getSelectionModel();
321334
return (selectionModel.getSelectedItem());
322335
}
336+
337+
public static Object getController(Node node) {
338+
Object controller = null;
339+
do {
340+
controller = node.getProperties().get("foo");
341+
node = node.getParent();
342+
} while (controller == null && node != null);
343+
return controller;
344+
}
323345
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ public class TodoitemController {
1111
void setTodo(String todo) {
1212
checkBox.setText(todo);
1313
}
14+
15+
String getTodo() {
16+
return checkBox.getText();
17+
}
1418
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javafx.fxml.FXML;
1010
import javafx.fxml.FXMLLoader;
1111
import javafx.scene.Node;
12+
import javafx.scene.layout.HBox;
1213
import javafx.scene.layout.VBox;
1314

1415
public class TodolistController {
@@ -48,4 +49,24 @@ public void loadFile(Path file) {
4849
e.printStackTrace();
4950
}
5051
}
52+
53+
public void saveFileAs(Path file) throws IOException {
54+
//Create a string with a todo per line
55+
String saveData = "";
56+
for(Node todoItem:todos_list.getChildren()) {
57+
TodoitemController controller = (TodoitemController) getController(todoItem);
58+
saveData += controller.getTodo() + "\n";
59+
}
60+
//Save this string to the given file
61+
Files.writeString(file, saveData);
62+
}
63+
64+
public static Object getController(Node node) {
65+
Object controller = null;
66+
do {
67+
controller = node.getProperties().get("foo");
68+
node = node.getParent();
69+
} while (controller == null && node != null);
70+
return controller;
71+
}
5172
}

src/main/resources/fxml/MainView.fxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.ainm.controllers.MainController">
1616
<left>
17-
<TreeView fx:id="file_explorer" prefHeight="306.0" prefWidth="210.0" BorderPane.alignment="CENTER">
17+
<TreeView fx:id="file_explorer" prefHeight="352.0" prefWidth="296.0" BorderPane.alignment="CENTER">
1818
<stylesheets>
1919
<URL value="@../styles/treeview/background.css" />
2020
<URL value="@../styles/treeview/tree_cell.css" />
@@ -71,7 +71,7 @@
7171
<MenuItem disable="true" mnemonicParsing="false" text="New Whiteboard" />
7272
</items>
7373
</Menu>
74-
<MenuItem disable="true" mnemonicParsing="false" text="Save as" />
74+
<MenuItem fx:id="menuItemSaveAs" mnemonicParsing="false" onAction="#saveFileAs" text="Save as" />
7575
<MenuItem disable="true" mnemonicParsing="false" text="Save" />
7676
<MenuItem fx:id="menu_item_close_file" mnemonicParsing="false" onAction="#close_file" text="Close" />
7777
</items>

src/main/resources/fxml/views/todo/todo_item.fxml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
<Insets />
1212
</HBox.margin></CheckBox>
1313
</children>
14+
<properties>
15+
<!-- store controller at key "foo" in properties map -->
16+
<foo><fx:reference source="controller" /></foo>
17+
</properties>
1418
</HBox>

src/main/resources/fxml/views/todo/todo_view.fxml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
<padding>
2222
<Insets bottom="50.0" top="30.0" />
2323
</padding>
24+
<properties>
25+
<!-- store controller at key "foo" in properties map -->
26+
<foo><fx:reference source="controller" /></foo>
27+
</properties>
2428
</ScrollPane>

0 commit comments

Comments
 (0)