Skip to content

Commit

Permalink
Prevent loss of unsaved profile changes when loading profiles via sec…
Browse files Browse the repository at this point in the history
…ond-instance activation
  • Loading branch information
bwRavencl committed Aug 15, 2024
1 parent 39d3ee3 commit 44f3b52
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/main/java/de/bwravencl/controllerbuddy/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,27 @@ private void handleRemainingCommandLine(final CommandLine commandLine) {
}
}

private boolean handleUnsavedChanges() {
if (!unsavedChanges) {
return true;
}

final var path = currentFile != null ? currentFile.getAbsolutePath() : strings.getString("UNTITLED");

final var selectedOption = JOptionPane.showConfirmDialog(frame,
MessageFormat.format(strings.getString("SAVE_CHANGES_DIALOG_TEXT"), path),
strings.getString("WARNING_DIALOG_TITLE"), JOptionPane.YES_NO_CANCEL_OPTION);

return switch (selectedOption) {
case JOptionPane.YES_OPTION -> {
saveProfile();
yield !unsavedChanges;
}
case JOptionPane.NO_OPTION -> true;
default -> false;
};
}

private void initOpenVrOverlay() {
final var profile = input.getProfile();

Expand Down Expand Up @@ -2087,7 +2108,7 @@ public void newActivation(final String[] args) {
final var gameControllerDbPath = commandLine.getOptionValue(OPTION_GAME_CONTROLLER_DB);

EventQueue.invokeLater(() -> {
if (cmdProfilePath != null) {
if (cmdProfilePath != null && handleUnsavedChanges()) {
main.loadProfile(new File(cmdProfilePath), false, true);
}

Expand Down Expand Up @@ -4381,30 +4402,11 @@ private abstract class UnsavedChangesAwareAction extends AbstractAction {
@Serial
private static final long serialVersionUID = 1387266903295357716L;

@SuppressWarnings("fallthrough")
@Override
public void actionPerformed(final ActionEvent e) {
if (unsavedChanges) {
final var path = currentFile != null ? currentFile.getAbsolutePath() : strings.getString("UNTITLED");

final var selectedOption = JOptionPane.showConfirmDialog(frame,
MessageFormat.format(strings.getString("SAVE_CHANGES_DIALOG_TEXT"), path),
strings.getString("WARNING_DIALOG_TITLE"), JOptionPane.YES_NO_CANCEL_OPTION);

switch (selectedOption) {
case JOptionPane.YES_OPTION:
saveProfile();
if (unsavedChanges) {
return;
}
case JOptionPane.NO_OPTION:
break;
default:
return;
}
if (handleUnsavedChanges()) {
doAction();
}

doAction();
}

protected abstract void doAction();
Expand Down

0 comments on commit 44f3b52

Please sign in to comment.