package ctbrec.ui.action; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ctbrec.Model; import ctbrec.recorder.Recorder; import ctbrec.ui.controls.Dialogs; import javafx.application.Platform; import javafx.scene.control.Button; public class CheckModelAccountAction { private static final Logger LOG = LoggerFactory.getLogger(CheckModelAccountAction.class); private Button b; private Recorder recorder; public CheckModelAccountAction(Button b, Recorder recorder) { this.b = b; this.recorder = recorder; } public void execute() { String buttonText = b.getText(); b.setDisable(true); Runnable checker = (() -> { List deletedAccounts = new ArrayList<>(); try { List models = recorder.getModels(); int total = models.size(); for (int i = 0; i < total; i++) { final int counter = i+1; Platform.runLater(() -> b.setText(buttonText + ' ' + counter + '/' + total)); Model modelToCheck = models.get(i); try { if (!modelToCheck.exists()) { deletedAccounts.add(modelToCheck); } } catch (IOException e) { LOG.warn("Couldn't check, if model account still exists", e); } } } finally { Platform.runLater(() -> { b.setDisable(false); b.setText(buttonText); if (!deletedAccounts.isEmpty()) { StringBuilder sb = new StringBuilder(); for (Model deletedModel : deletedAccounts) { String name = deletedModel.getDisplayName() + " ".repeat(30); name = name.substring(0, 30); sb.append(name).append(' ').append('(').append(deletedModel.getUrl()).append(')').append('\n'); } boolean remove = Dialogs.showConfirmDialog("Deleted Accounts", sb.toString(), "The following accounts seem to have been deleted. Do you want to remove them?", b.getScene()); if (remove) { new StopRecordingAction(b, deletedAccounts, recorder).execute(); } } }); } }); new Thread(checker).start(); } }