|
3 | 3 | import java.awt.Desktop; |
4 | 4 | import java.io.File; |
5 | 5 | import java.io.IOException; |
| 6 | +import java.io.Reader; |
6 | 7 | import java.net.URI; |
7 | 8 | import java.net.URISyntaxException; |
| 9 | +import java.nio.file.Files; |
8 | 10 | import java.nio.file.Path; |
9 | 11 | import java.nio.file.Paths; |
10 | 12 | import java.time.LocalDate; |
|
17 | 19 |
|
18 | 20 | import org.ainm.controllers.todolist.TodolistController; |
19 | 21 |
|
| 22 | +import com.github.cliftonlabs.json_simple.JsonException; |
| 23 | +import com.github.cliftonlabs.json_simple.JsonObject; |
| 24 | +import com.github.cliftonlabs.json_simple.Jsoner; |
| 25 | + |
20 | 26 | import javafx.application.Platform; |
21 | 27 | import javafx.event.ActionEvent; |
22 | 28 | import javafx.fxml.FXML; |
@@ -61,6 +67,12 @@ public class MainController { |
61 | 67 |
|
62 | 68 | @FXML |
63 | 69 | void initialize() { |
| 70 | + try { |
| 71 | + loadProjectList(); |
| 72 | + } catch (IOException | JsonException e1) { |
| 73 | + // TODO Auto-generated catch block |
| 74 | + e1.printStackTrace(); |
| 75 | + } |
64 | 76 | // Set date from date button |
65 | 77 | date_button.setText(LocalDate.now().toString()); |
66 | 78 | Timer timer = new Timer(); |
@@ -136,14 +148,26 @@ void add_project(ActionEvent event) { |
136 | 148 | }); |
137 | 149 | RegisteredProjectsListMenu.getItems().add(projectMenuItem); |
138 | 150 | open_registered_project(project.toString()); |
| 151 | + try { |
| 152 | + saveProjectList(); |
| 153 | + } catch (IOException e1) { |
| 154 | + // TODO Auto-generated catch block |
| 155 | + e1.printStackTrace(); |
| 156 | + } |
139 | 157 | } |
140 | 158 |
|
141 | 159 | @FXML |
142 | 160 | void delete_current_project(ActionEvent event) { |
143 | | - close_project(); |
144 | 161 | // Remove the current project from the registered list |
145 | 162 | registered_projects.remove(current_project_path); |
146 | 163 | createProjectList(); |
| 164 | + close_project(); |
| 165 | + try { |
| 166 | + saveProjectList(); |
| 167 | + } catch (IOException e1) { |
| 168 | + // TODO Auto-generated catch block |
| 169 | + e1.printStackTrace(); |
| 170 | + } |
147 | 171 | } |
148 | 172 |
|
149 | 173 | void createProjectList() { |
@@ -347,6 +371,39 @@ void addViewToCurrentTab(Node view) { |
347 | 371 | get_current_tab().setContent(view); |
348 | 372 | } |
349 | 373 |
|
| 374 | + void saveProjectList() throws IOException { |
| 375 | + //Saves the registered projects in an json file |
| 376 | + //Create json |
| 377 | + JsonObject json = new JsonObject(); |
| 378 | + json.put("registeredProjects", registered_projects); |
| 379 | + //Write json to file |
| 380 | + String userDirectoryPath = System.getProperty("user.home") + "/.ideasbasic"; |
| 381 | + Path userDirectory = Paths.get(userDirectoryPath); |
| 382 | + if (!Files.exists(userDirectory)) { |
| 383 | + Files.createDirectory(userDirectory); |
| 384 | + } |
| 385 | + Path file = Paths.get(userDirectoryPath + "/config.json"); |
| 386 | + if (!Files.exists(file)) { |
| 387 | + Files.createFile(file); |
| 388 | + } |
| 389 | + Files.writeString(file, json.toJson()); |
| 390 | + } |
| 391 | + |
| 392 | + void loadProjectList() throws IOException, JsonException { |
| 393 | + //Read the config.json file |
| 394 | + String userDirectoryPath = System.getProperty("user.home") + "/.ideasbasic"; |
| 395 | + Path userDirectory = Paths.get(userDirectoryPath); |
| 396 | + if(Files.exists(userDirectory)) { |
| 397 | + Reader reader = Files.newBufferedReader(Paths.get(userDirectoryPath + "/config.json")); |
| 398 | + JsonObject parser = (JsonObject) Jsoner.deserialize(reader); |
| 399 | + //Get the registered projects from parser |
| 400 | + registered_projects = (List<String>) parser.get("registeredProjects"); |
| 401 | + //Create the listmenu |
| 402 | + createProjectList(); |
| 403 | + } |
| 404 | + |
| 405 | + } |
| 406 | + |
350 | 407 | Tab get_current_tab() { |
351 | 408 | SingleSelectionModel<Tab> selectionModel = tab_pane.getSelectionModel(); |
352 | 409 | return (selectionModel.getSelectedItem()); |
|
0 commit comments