Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Popup asking to save before closing for ContingencyStoreEditor #94

Merged
merged 12 commits into from
Nov 9, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,26 @@ private ContextMenu createContingencyElementMenu() {
return new ContextMenu(removeItem);
}

private boolean alertSave() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setHeaderText("'" + this.store.getName() + "' " + RESOURCE_BUNDLE.getString("UnsavedQuitConfirmation"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use MessageFormat seems better:

MessageFormat.format(RESOURCE_BUNDLE.getString("UnsavedQuitConfirmation"), store.getName());

ButtonType saveAndContinue = new ButtonType(RESOURCE_BUNDLE.getString("SaveAndContinue"));
ButtonType dontSaveAndContinue = new ButtonType(RESOURCE_BUNDLE.getString("DontSaveAndContinue"));
ButtonType cancel = new ButtonType(RESOURCE_BUNDLE.getString("Cancel"));

alert.getButtonTypes().setAll(saveAndContinue, dontSaveAndContinue, cancel);

Optional<ButtonType> result = alert.showAndWait();
return result.map(type -> {
if (type == saveAndContinue) {
save();
return true;
} else {
return type == dontSaveAndContinue;
}
}).orElse(false);
}

@Override
public void save() {
if (!saved.get()) {
Expand All @@ -306,6 +326,14 @@ public void view() {

@Override
public void dispose() {
// nothing to dispose
//noting to dispose
}

@Override
public boolean isClosable() {
if (!saved.getValue()) {
return alertSave();
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,10 @@ public void dispose() {
storableScript.removeListener(this);
}

@Override
public boolean isClosable() {
//TODO: save before close
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Complete the task associated to this TODO comment. (squid:S1135)

See it in SonarCloud

return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Cancel=Cancel
CreateContingencies=Create contingencies
DontSaveAndContinue=Don't save and continue
EditContingencies=Edit contingencies
Name=Name
NewName=New name
Rename=Rename
RenameContingency=Rename contingency
Remove=Remove
SaveAndContinue=Save and continue
UnsavedQuitConfirmation=is not saved, its modifications will be lost. Would you like to save the file first ?
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Cancel=Annuler
CreateContingencies=Cr�er des contingences
DontSaveAndContinue=Continuer sans enregistrer
EditContingencies=Editer les contingences
Name=Nom
NewName=Nouveau nom
Rename=Renommer
RenameContingency=Renommer la contingence
Remove=Supprimer
SaveAndContinue=Enregistrer et continuer
UnsavedQuitConfirmation=n'est pas sauvegard�, ses modifications seront perdues. Voulez vous enregistrer ce fichier d'abord ?
12 changes: 8 additions & 4 deletions gse-app/src/main/java/com/powsybl/gse/app/GseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public class GseApp extends Application {

private static final Logger LOGGER = LoggerFactory.getLogger(GseApp.class);

private GsePane gsePane;

private ExecutorService executor;

private LocalComputationManager computationManager;
Expand All @@ -40,7 +38,7 @@ public class GseApp extends Application {

@Override
public void start(Stage stage) {
gsePane = new GsePane(new GseContext(executor), appData, this);
GsePane gsePane = new GsePane(new GseContext(executor), appData, this);
stage.setTitle(gsePane.getTitle());
stage.getIcons().addAll(gsePane.getIcons());
Scene scene = new Scene(gsePane, 1200, 800);
Expand All @@ -56,6 +54,13 @@ public void start(Stage stage) {
}

stage.show();

stage.setOnCloseRequest(event -> {
gsePane.dispose();
if (!gsePane.isClosable()) {
event.consume();
}
});
}

@Override
Expand All @@ -76,7 +81,6 @@ public void init() throws Exception {
@Override
public void stop() throws Exception {
super.stop();
gsePane.dispose();
appData.close();
computationManager.close();
executor.shutdownNow();
Expand Down
10 changes: 10 additions & 0 deletions gse-app/src/main/java/com/powsybl/gse/app/GsePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class GsePane extends StackPane {
private final Preferences preferences;
private final Application javaxApplication;

private boolean isClosable = true;

public GsePane(GseContext context, AppData data, Application app) {
this.context = Objects.requireNonNull(context);
this.data = Objects.requireNonNull(data);
Expand Down Expand Up @@ -190,10 +192,18 @@ public List<Image> getIcons() {
}

public void dispose() {
isClosable = true;
savePreferences();

for (Tab tab : tabPane.getTabs()) {
((ProjectPane) tab).dispose();
if (!tab.isClosable()) {
isClosable = false;
}
}
}

public boolean isClosable() {
return isClosable;
}
}
20 changes: 19 additions & 1 deletion gse-app/src/main/java/com/powsybl/gse/app/ProjectPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class ProjectPane extends Tab {

private static final ServiceLoaderCache<ProjectFileExecutionTaskExtension> EXECUTION_TASK_EXTENSION_LOADER = new ServiceLoaderCache<>(ProjectFileExecutionTaskExtension.class);

private int tabsOpen = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this counter is needed: the number of tabs can probably be retrieved elsewhere


private static class TabKey {

private final String nodeId;
Expand Down Expand Up @@ -653,7 +655,23 @@ private void viewFile(ProjectFile file, ProjectFileViewerExtension viewerExtensi
Node graphic = viewerExtension.getMenuGraphic(file);
ProjectFileViewer viewer = viewerExtension.newViewer(file, getContent().getScene(), context);
Tab tab = new MyTab(tabName, viewer.getContent());
tab.setOnClosed(event -> viewer.dispose());
tabsOpen++;
if (this.isClosable()) {
this.setClosable(false);
}
tab.setOnCloseRequest(event -> {
if (!viewer.isClosable()) {
event.consume();
} else {
viewer.dispose();
}
});
tab.setOnClosed(event -> {
tabsOpen--;
if (tabsOpen == 0) {
this.setClosable(true);
}
});
tab.setGraphic(graphic);
tab.setTooltip(new Tooltip(tabName));
tab.setUserData(tabKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,9 @@ public void dispose() {
projectCase.removeListener(this);
}

@Override
public boolean isClosable() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,9 @@ public void view() {
public void dispose() {
// nothing to dispose
}

@Override
public boolean isClosable() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@ public void dispose() {
preContResultPane.savePreferences();
postContResultPane.savePreferences();
}

@Override
public boolean isClosable() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public interface ProjectFileViewer {

void dispose();

boolean isClosable();
}