Skip to content

Commit f04c434

Browse files
committed
Added create new todo dialog
1 parent 86a83b0 commit f04c434

File tree

6 files changed

+104
-37
lines changed

6 files changed

+104
-37
lines changed

ainm.todo

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
Task 1
2-
Task 2
3-
Task 3
4-
Go to school
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
1+
[x] [x]Task 1
2+
[x] [x]Task 2
3+
[x] [ ]Task 3
4+
[ ] [ ]Go to school
5+
[x] [ ]Task 4
6+
[ ] [ ]New todo
7+
[x] [ ]New todo
8+
[x] [x]New todo
9+
[ ] [x]New todo
10+
[x] [x]New todo
11+
[x] [ ]New todo
12+
[ ] [ ]New todo
13+
[x] [ ]New todo
14+
[ ] [x]New todo
15+
[ ] [x]New todo
16+
[x] [x]New todo
17+
[x] [x]New todo
18+
[ ] [ ]New todo
19+
[x] New todo
20+
[x] New todo

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ void initialize() {
6868
@Override
6969
public void run() {
7070
// Updates file explorer
71-
// if (current_project_path != "") {
72-
// Platform.runLater(() -> open_registered_project(current_project_path));
73-
// }
71+
// if (current_project_path != "") {
72+
// Platform.runLater(() -> open_registered_project(current_project_path));
73+
// }
7474
// Updates the time button every 2 seconds
7575
int minutes = LocalTime.now().getMinute();
7676
int hours = LocalTime.now().getHour();
@@ -287,16 +287,17 @@ void new_todo_list(ActionEvent event) throws IOException {
287287
// Create the file
288288
String directoryPath = createFileDialogController.getDirectory();
289289
String fileName = createFileDialogController.getFilename();
290-
String fullPath = ((directoryPath.endsWith("/")) ? directoryPath : directoryPath + "/") + fileName + ".todo";
290+
String fullPath = ((directoryPath.endsWith("/")) ? directoryPath : directoryPath + "/") + fileName
291+
+ ".todo";
291292
File file = new File(fullPath);
292293
if (!file.exists()) {
293294
file.createNewFile();
294295
}
295296
// Load the todolist view in current tab
296-
FXMLLoader loader = new FXMLLoader();
297-
loader.setLocation("/fxml/views/todo/todo_view.fxml");
297+
loader = new FXMLLoader();
298+
loader.setLocation(getClass().getResource("/fxml/views/todo/todo_view.fxml"));
298299
Node todolistView = loader.load();
299-
TodolistController controller = loader.getController();
300+
TodolistController controller = loader.getController();
300301
controller.openedFile = Paths.get(fullPath);
301302
addViewToCurrentTab(todolistView);
302303
}
@@ -305,10 +306,10 @@ void new_todo_list(ActionEvent event) throws IOException {
305306

306307
@FXML
307308
void saveFileAs() throws IOException {
308-
//Open file explorer
309+
// Open file explorer
309310
FileChooser chooser = new FileChooser();
310311
File file = chooser.showSaveDialog(null);
311-
//Save current view to file
312+
// Save current view to file
312313
Object controller = getController(get_current_tab().getContent());
313314
if (controller.getClass().equals(new TodolistController().getClass())) {
314315
((TodolistController) controller).saveFileAs(Paths.get(file.getAbsolutePath()));
@@ -317,7 +318,7 @@ void saveFileAs() throws IOException {
317318

318319
@FXML
319320
void saveCurrentFile() throws IOException {
320-
//Save current view to file
321+
// Save current view to file
321322
Object controller = getController(get_current_tab().getContent());
322323
if (controller.getClass().equals(new TodolistController().getClass())) {
323324
((TodolistController) controller).saveCurrentFile();
@@ -326,14 +327,14 @@ void saveCurrentFile() throws IOException {
326327

327328
void openFile(String filename) throws IOException {
328329
if (filename.endsWith(".todo")) {
329-
//Load todolist view
330+
// Load todolist view
330331
FXMLLoader loader = new FXMLLoader();
331332
loader.setLocation(getClass().getResource("/fxml/views/todo/todo_view.fxml"));
332333
Node view = loader.load();
333-
//Load todolist
334+
// Load todolist
334335
TodolistController todolistController = loader.getController();
335336
todolistController.loadFile(Paths.get(filename));
336-
//Open todolist view
337+
// Open todolist view
337338
addViewToCurrentTab(view);
338339
}
339340
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.ainm.controllers.todolist;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.scene.control.TextField;
5+
6+
public class CreateNewTodoController {
7+
@FXML
8+
private TextField todoField;
9+
10+
public String getTodo() {
11+
return todoField.getText();
12+
}
13+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ String getTodo() {
1717
}
1818

1919
Boolean isDone() {
20-
return checkBox.isChecked()
20+
return checkBox.isSelected();
21+
}
22+
23+
void setDone(Boolean value) {
24+
checkBox.setSelected(value);
2125
}
2226
}

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,50 @@
44
import java.io.IOException;
55
import java.nio.file.Files;
66
import java.nio.file.Path;
7+
import java.util.Optional;
78
import java.util.stream.Stream;
89

910
import javafx.event.ActionEvent;
1011
import javafx.fxml.FXML;
1112
import javafx.fxml.FXMLLoader;
1213
import javafx.scene.Node;
14+
import javafx.scene.control.ButtonType;
15+
import javafx.scene.control.Dialog;
16+
import javafx.scene.control.DialogPane;
1317
import javafx.scene.layout.HBox;
1418
import javafx.scene.layout.VBox;
1519

1620
public class TodolistController {
1721

18-
Path openedFile;
22+
public Path openedFile;
1923

2024
@FXML
2125
private VBox todos_list;
2226

2327
@FXML
2428
void add_todo_item(ActionEvent event) throws IOException {
25-
addTodo("New todo");
29+
FXMLLoader loader = new FXMLLoader();
30+
loader.setLocation(getClass().getResource("/fxml/dialogs/AddTodo.fxml"));
31+
DialogPane addTodoDialog = loader.load();
32+
CreateNewTodoController controller = loader.getController();
33+
Dialog<ButtonType> dialog = new Dialog<>();
34+
dialog.setDialogPane(addTodoDialog);
35+
dialog.setTitle("Add new todo");
36+
Optional<ButtonType> result = dialog.showAndWait();
37+
if (result.get() == ButtonType.FINISH) {
38+
addTodo(controller.getTodo(), false);
39+
}
2640
}
2741

28-
void addTodo(String todo) throws IOException {
42+
void addTodo(String todo, Boolean checked) throws IOException {
2943
// Load a new todo item
3044
FXMLLoader loader = new FXMLLoader();
3145
loader.setLocation(getClass().getResource("/fxml/views/todo/todo_item.fxml"));
3246
Node todo_item = loader.load();
3347
//Set todo text
3448
TodoitemController todoController = loader.getController();
3549
todoController.setTodo(todo);
50+
todoController.setDone(checked);
3651
// Add todo to todolist
3752
todos_list.getChildren().add(todo_item);
3853
}
@@ -43,9 +58,8 @@ public void loadFile(Path file) {
4358
try (Stream<String> lines = Files.lines(file)) {
4459
lines.forEach((line) -> {
4560
try {
46-
addTodo(line);
61+
addTodo(line.replace("[x] ", "").replace("[ ] ", ""), (line.startsWith("[x] ") ? true:false));
4762
} catch (IOException e) {
48-
// TODO Auto-generated catch block
4963
e.printStackTrace();
5064
}
5165
});
@@ -73,7 +87,7 @@ void saveFile(Path file) throws IOException {
7387
String saveData = "";
7488
for(Node todoItem:todos_list.getChildren()) {
7589
TodoitemController controller = (TodoitemController) getController(todoItem);
76-
saveData += "[" + ((controller.isDone() ? "x":" ") + "]" + controller.getTodo() + "\n";
90+
saveData += "[" + ((controller.isDone()) ? "x":" ") + "] " + controller.getTodo() + "\n";
7791
}
7892
//Save this string to the given file
7993
Files.writeString(file, saveData);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.geometry.Insets?>
4+
<?import javafx.scene.control.ButtonType?>
5+
<?import javafx.scene.control.DialogPane?>
6+
<?import javafx.scene.control.Label?>
7+
<?import javafx.scene.control.TextField?>
8+
<?import javafx.scene.layout.VBox?>
9+
10+
11+
<DialogPane xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.ainm.controllers.todolist.CreateNewTodoController">
12+
<content>
13+
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="199.0" prefWidth="378.0">
14+
<children>
15+
<Label text="What do you have todo?">
16+
<VBox.margin>
17+
<Insets bottom="20.0" />
18+
</VBox.margin>
19+
</Label>
20+
<TextField fx:id="todoField" prefHeight="33.0" prefWidth="278.0">
21+
<VBox.margin>
22+
<Insets left="50.0" right="50.0" />
23+
</VBox.margin>
24+
</TextField>
25+
</children>
26+
</VBox>
27+
</content>
28+
<buttonTypes>
29+
<ButtonType fx:constant="FINISH" />
30+
<ButtonType fx:constant="CANCEL" />
31+
</buttonTypes>
32+
</DialogPane>

0 commit comments

Comments
 (0)