Skip to content

Commit 803e8bc

Browse files
committed
Registered projects get saved now
1 parent 32725d0 commit 803e8bc

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ javafx {
1919
}
2020

2121
repositories {
22-
// Use Maven Central for resolving dependencies.
22+
// Use Maven Central for resolving dependencies.
2323
mavenCentral()
2424
}
2525

@@ -32,6 +32,7 @@ dependencies {
3232

3333
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
3434
implementation 'com.google.guava:guava:30.1.1-jre'
35+
implementation 'com.github.cliftonlabs:json-simple:3.1.0'
3536
}
3637

3738
tasks.named('test') {

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

+58-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.awt.Desktop;
44
import java.io.File;
55
import java.io.IOException;
6+
import java.io.Reader;
67
import java.net.URI;
78
import java.net.URISyntaxException;
9+
import java.nio.file.Files;
810
import java.nio.file.Path;
911
import java.nio.file.Paths;
1012
import java.time.LocalDate;
@@ -17,6 +19,10 @@
1719

1820
import org.ainm.controllers.todolist.TodolistController;
1921

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+
2026
import javafx.application.Platform;
2127
import javafx.event.ActionEvent;
2228
import javafx.fxml.FXML;
@@ -61,6 +67,12 @@ public class MainController {
6167

6268
@FXML
6369
void initialize() {
70+
try {
71+
loadProjectList();
72+
} catch (IOException | JsonException e1) {
73+
// TODO Auto-generated catch block
74+
e1.printStackTrace();
75+
}
6476
// Set date from date button
6577
date_button.setText(LocalDate.now().toString());
6678
Timer timer = new Timer();
@@ -136,14 +148,26 @@ void add_project(ActionEvent event) {
136148
});
137149
RegisteredProjectsListMenu.getItems().add(projectMenuItem);
138150
open_registered_project(project.toString());
151+
try {
152+
saveProjectList();
153+
} catch (IOException e1) {
154+
// TODO Auto-generated catch block
155+
e1.printStackTrace();
156+
}
139157
}
140158

141159
@FXML
142160
void delete_current_project(ActionEvent event) {
143-
close_project();
144161
// Remove the current project from the registered list
145162
registered_projects.remove(current_project_path);
146163
createProjectList();
164+
close_project();
165+
try {
166+
saveProjectList();
167+
} catch (IOException e1) {
168+
// TODO Auto-generated catch block
169+
e1.printStackTrace();
170+
}
147171
}
148172

149173
void createProjectList() {
@@ -347,6 +371,39 @@ void addViewToCurrentTab(Node view) {
347371
get_current_tab().setContent(view);
348372
}
349373

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+
350407
Tab get_current_tab() {
351408
SingleSelectionModel<Tab> selectionModel = tab_pane.getSelectionModel();
352409
return (selectionModel.getSelectedItem());

0 commit comments

Comments
 (0)