Skip to content

Commit

Permalink
Merge pull request #145 from yprie/96-implementation-of-auto-saving
Browse files Browse the repository at this point in the history
96-implementation-of-auto-saving
  • Loading branch information
BaptisteDxm authored Dec 5, 2023
2 parents 3035869 + aa0a815 commit 86b0578
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
41 changes: 37 additions & 4 deletions src/main/java/application/UPMTApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,33 @@
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Platform;
import models.Project;

import java.io.IOException;
import java.util.UUID;



public class UPMTApp {

private Stage primaryStage;
private RootLayoutController rootLayoutController;
private ApplicationCommandFactory appCommandFactory;
private Project currentProject;
private static Project currentProject;
private String currentProjectPath;
private UUID lastSavedCommandId;

private long autoSaveIntervalMillis;


public UPMTApp(Stage primaryStage) throws IOException {


this.primaryStage = primaryStage;
this.appCommandFactory = new ApplicationCommandFactory(this);
this.rootLayoutController = new RootLayoutController(appCommandFactory);
this.autoSaveIntervalMillis = 10000;


Configuration.loadAppConfiguration();
HistoryManager.init(appCommandFactory);
Expand All @@ -47,6 +53,7 @@ public UPMTApp(Stage primaryStage) throws IOException {
appCommandFactory.openProjectManagerCommand().execute();
}

startAutoSave();

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/views/MainView/MainView.fxml"));
Expand All @@ -56,6 +63,7 @@ public UPMTApp(Stage primaryStage) throws IOException {
}



public Stage getPrimaryStage() {
return primaryStage;
}
Expand All @@ -65,7 +73,7 @@ public void setCurrentProject(Project project, String path) {
currentProjectPath = path;
rootLayoutController.setProject(project);
}
public Project getCurrentProject() {
public static Project getCurrentProject() {
return currentProject;
}
public String getCurrentProjectPath() { return currentProjectPath; }
Expand All @@ -79,5 +87,30 @@ public void restartApp() {
if(getCurrentProject() != null)
setCurrentProject(getCurrentProject(), currentProjectPath);
}


public void startAutoSave() {
if (currentProject != null) {
// Créez et démarrez un nouveau thread pour la sauvegarde automatique
Thread autoSaveThread = new Thread(() -> {
while (true) {
try {
// Effectuez la sauvegarde automatique
//currentProject.saveAs("auto_save", getCurrentProjectPath());

// Utilisez Platform.runLater() pour exécuter l'opération sur le thread de l'interface utilisateur
Platform.runLater(() -> appCommandFactory.saveProject().execute());

// Pause pour l'intervalle spécifié
Thread.sleep(autoSaveIntervalMillis);
} catch (InterruptedException e) {
e.printStackTrace();
// Gérer les exceptions si nécessaire
}
}
});
autoSaveThread.setDaemon(true); // Le thread s'exécutera en arrière-plan et se terminera lorsque le programme principal se termine
autoSaveThread.start();
}
}

}
24 changes: 4 additions & 20 deletions src/main/java/application/appCommands/CloseApplicationCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,7 @@ public CloseApplicationCommand(ApplicationCommandFactory appCommandFactory, UPMT

@Override
public Void execute() {

System.out.println("Test checking project unsaved");
//TODO check for unsaved work
boolean workUnsaved = false;
String currentTitle = upmtApp.getPrimaryStage().getTitle();
UUID currentCommandId = HistoryManager.getCurrentCommandId();
UUID lastSavedCommandId = upmtApp.getLastSavedCommandId();
if(currentCommandId != null ){
if(lastSavedCommandId == null){
workUnsaved = true;
}else {
if (HistoryManager.getCurrentCommandId().equals(lastSavedCommandId)) {
System.out.println("Projet sauvegardé");
} else {
workUnsaved = true;
}
}
}
if(workUnsaved) {
if (upmtApp.getCurrentProjectPath() == null) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setHeaderText("");
alert.setTitle(Configuration.langBundle.getString("alert_unsaved_project_title"));
Expand All @@ -57,7 +39,7 @@ public Void execute() {
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeCancel);

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
if (result.get() == buttonTypeOne) {
// ... user chose "Save And Quit"
appCommandFactory.saveProject().execute();
System.exit(0);
Expand All @@ -68,6 +50,8 @@ public Void execute() {
// ... user chose CANCEL or closed the dialog
event.consume();
}
} else {
appCommandFactory.saveProject().execute();
}
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/application/appCommands/SaveProjectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public Void execute() {
ProjectSaver.save(upmtApp.getCurrentProject(), Configuration.getProjectsPath()[0]);
upmtApp.setLastSavedCommandId(HistoryManager.getCurrentCommandId());
new ProjectSavingStatusChangedCommand(upmtApp).execute();
System.out.println("Projet saved");

} catch (Exception e) {
ProjectDialogBox.projectSavingFailed();
e.printStackTrace();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/models/ConcreteCategory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package models;

import application.UPMTApp;
import components.modelisationSpace.category.controllers.ConcreteCategoryController;
import components.modelisationSpace.property.appCommands.AddConcretePropertyCommand;
import components.modelisationSpace.property.appCommands.RemoveConcretePropertyCommand;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/models/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public Project(String name, SchemaTreeRoot baseScheme) {
this.readOnlyInterviews = new ReadOnlyListWrapper<>(this.interviews);

this.selectedInterview = new SimpleObjectProperty<>();

}

public String getName() { return this.name.get(); }
Expand Down

0 comments on commit 86b0578

Please sign in to comment.