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,29 @@ 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 if (type == dontSaveAndContinue) {
saved.set(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems weird to set saved to true here: this is not logic. Is it really needed?

return true;
} else {
return false;
}
}).orElse(false);
}

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

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

@Override
public boolean canBeClosed() {
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 canBeClosed() {
//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
Expand Up @@ -5,3 +5,7 @@ NewName=New name
Rename=Rename
RenameContingency=Rename contingency
Remove=Remove
UnsavedQuitConfirmation=is not saved, its modifications will be lost. Would you like to save the file first ?
SaveAndContinue=Save and Continue
DontSaveAndContinue=Don't save and Continue
Cancel=Cancel
Copy link
Contributor

Choose a reason for hiding this comment

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

Sort entries by alphabetical order: it will be easier to find the missing properties

Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ NewName=Nouveau nom
Rename=Renommer
RenameContingency=Renommer la contingence
Remove=Supprimer
UnsavedQuitConfirmation=n'est pas sauvegard�, ses modifications seront perdues. Voulez vous enregistrer ce fichier d'abord ?
SaveAndContinue=Enregistrer et Continuer
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo issue: Enregistrer et continuer

DontSaveAndContinue=Continuer sans enregistrer
Cancel=Annuler
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remark

8 changes: 7 additions & 1 deletion gse-app/src/main/java/com/powsybl/gse/app/GseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public void start(Stage stage) {
}

stage.show();

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

@Override
Expand All @@ -76,7 +83,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 canBeClosed = 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() {
canBeClosed = true;
savePreferences();

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

public boolean canBeClosed() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename to isClosable?

return canBeClosed;
}
}
16 changes: 15 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 @@ -677,7 +679,15 @@ 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++;
tab.setOnCloseRequest(event -> {
if (!viewer.canBeClosed()) {
event.consume();
} else {
viewer.dispose();
}
});
tab.setOnClosed(event -> tabsOpen--);
tab.setGraphic(graphic);
tab.setTooltip(new Tooltip(tabName));
tab.setUserData(tabKey);
Expand Down Expand Up @@ -864,4 +874,8 @@ public void dispose() {
taskItems.dispose();
closeViews();
}

public boolean canBeClosed() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remark

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ProjectPane extends Tab which already has a final isClosable() method. I did not rename it here.

return tabsOpen == 0;
}
}
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 canBeClosed() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remark

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 canBeClosed() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remark

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

@Override
public boolean canBeClosed() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remark

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

void dispose();

boolean canBeClosed();
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remark

}