Disable buttons during async job execution (closes #7339)

This commit is contained in:
kifir23917 2025-01-22 20:45:07 +03:00 committed by kifir23917
parent 5ddbdd4519
commit 0ae9d2becf
2 changed files with 33 additions and 6 deletions

View File

@ -52,7 +52,11 @@ public class BasicUpdaterPanel extends JPanel {
super(new VerticalFlowLayout());
this.updateOperationCallbacks = updateOperationCallbacks;
singleAsyncJobExecutor = new SingleAsyncJobExecutor(updateOperationCallbacks, doNotUseStatusWindow);
singleAsyncJobExecutor = new SingleAsyncJobExecutor(
updateOperationCallbacks,
doNotUseStatusWindow,
() -> SwingUtilities.invokeLater(this::refreshButtons)
);
updateCalibrations = new UpdateCalibrations(singleAsyncJobExecutor);
if (isWindows()) {
@ -171,7 +175,7 @@ public class BasicUpdaterPanel extends JPanel {
private void setUpdateFirmwareJob(final AsyncJob updateFirmwareJob) {
this.updateFirmwareJob = Optional.of(updateFirmwareJob);
hideStatusMessage();
updateFirmwareButton.setEnabled(true);
refreshButtons();
Optional<String> updateFirmwareButtonText = Optional.empty();
if (updateFirmwareJob instanceof OpenBltAutoJob) {
updateFirmwareButtonText = Optional.of("Auto Update Firmware");
@ -224,7 +228,7 @@ public class BasicUpdaterPanel extends JPanel {
private void setPortToUpdateCalibrations(final SerialPortScanner.PortResult port) {
portToUpdateCalibrations = Optional.of(port);
updateCalibrationsButton.setEnabled(true);
refreshButtons();
}
private void resetPortToUpdateCalibrations() {
@ -233,15 +237,18 @@ public class BasicUpdaterPanel extends JPanel {
}
private void onUpdateFirmwareButtonClicked(final ActionEvent actionEvent) {
disableButtons();
CompatibilityOptional.ifPresentOrElse(updateFirmwareJob,
value -> {
singleAsyncJobExecutor.startJob(value, updateFirmwareButton);
},
() -> log.error("Update firmware job is is not defined.")
);
refreshButtons();
}
private void onUpdateCalibrationsButtonClicked(final ActionEvent actionEvent) {
disableButtons();
CompatibilityOptional.ifPresentOrElse(portToUpdateCalibrations,
port -> {
updateCalibrations.updateCalibrationsAction(port, updateCalibrationsButton);
@ -254,5 +261,16 @@ public class BasicUpdaterPanel extends JPanel {
);
}
);
refreshButtons();
}
private void refreshButtons() {
updateFirmwareButton.setEnabled(updateFirmwareJob.isPresent() && singleAsyncJobExecutor.isNotInProgress());
updateCalibrationsButton.setEnabled(portToUpdateCalibrations.isPresent() && singleAsyncJobExecutor.isNotInProgress());
}
private void disableButtons() {
updateFirmwareButton.setEnabled(false);
updateCalibrationsButton.setEnabled(false);
}
}

View File

@ -19,7 +19,8 @@ public class SingleAsyncJobExecutor {
SingleAsyncJobExecutor(
final UpdateOperationCallbacks updateOperationCallbacks,
final boolean doNotUseStatusWindow
final boolean doNotUseStatusWindow,
final Runnable onJobInProgressFinished
) {
this.updateOperationCallbacks = updateOperationCallbacks;
this.doNotUseStatusWindow = doNotUseStatusWindow;
@ -29,12 +30,12 @@ public class SingleAsyncJobExecutor {
final Optional<AsyncJob> prevJobInProgress = setJobInProgressIfEmpty(job);
if (!prevJobInProgress.isPresent()) {
if (doNotUseStatusWindow) {
AsyncJobExecutor.INSTANCE.executeJob(job, updateOperationCallbacks, this::resetJobInProgress);
AsyncJobExecutor.INSTANCE.executeJob(job, updateOperationCallbacks, this::handleJobInProgressFinished);
} else {
AsyncJobExecutor.INSTANCE.executeJobWithStatusWindow(
job,
updateOperationCallbacks,
this::resetJobInProgress
this::handleJobInProgressFinished
);
}
} else {
@ -47,6 +48,10 @@ public class SingleAsyncJobExecutor {
}
}
public synchronized boolean isNotInProgress() {
return !jobInProgress.isPresent();
}
private synchronized Optional<AsyncJob> setJobInProgressIfEmpty(final AsyncJob job) {
final Optional<AsyncJob> prevJobInProgress = jobInProgress;
if (!prevJobInProgress.isPresent()) {
@ -58,4 +63,8 @@ public class SingleAsyncJobExecutor {
private synchronized void resetJobInProgress() {
jobInProgress = Optional.empty();
}
private void handleJobInProgressFinished() {
resetJobInProgress();
}
}