Stop test timer

completes f45341d84a
This commit is contained in:
Carlos Galindo 2019-09-13 13:05:23 +02:00
parent f29cb85566
commit a0e3a1fb17
Signed by: kauron
GPG Key ID: 83E68706DEE119A3
3 changed files with 32 additions and 12 deletions

View File

@ -54,7 +54,7 @@ public class Controller implements Initializable {
private MenuItem menuCloseTab, menuSave;
private final BooleanProperty tabIsTable = new SimpleBooleanProperty(false);
private final Map<Tab, TableController> tabMap = new HashMap<>();
private final Map<Tab, Initializable> tabMap = new HashMap<>();
private String updateURL, updateFileName;
private Tab theTest = null;
@ -67,9 +67,9 @@ public class Controller implements Initializable {
tabIsTable.set(tabMap.get(n) != null);
menuCloseTab.setDisable(!n.isClosable());
menuSave.disableProperty().unbind();
menuSave.setDisable(!tabMap.containsKey(n) || tabMap.get(n).saved.get());
if (tabMap.containsKey(n))
menuSave.disableProperty().bind(tabMap.get(n).saved);
menuSave.setDisable(!isTableTab(n) || ((TableController) tabMap.get(n)).saved.get());
if (isTableTab(n))
menuSave.disableProperty().bind(((TableController) tabMap.get(n)).saved);
}
});
tabPane.getTabs().removeListener((ListChangeListener<Tab>) c -> theTest = null);
@ -77,8 +77,10 @@ public class Controller implements Initializable {
root.getScene().getWindow().setOnCloseRequest(event -> {
for (Tab tab : tabPane.getTabs()) {
EventHandler<Event> handler = tab.getOnCloseRequest();
if (tabMap.containsKey(tab))
tabMap.get(tab).stopTimer();
if (isTableTab(tab))
((TableController) tabMap.get(tab)).stopTimer();
else if (isTestTab(tab))
((TestController) tabMap.get(tab)).stopTimer();
if (tab.isClosable() && handler != null) {
tabPane.getSelectionModel().select(tab);
handler.handle(event);
@ -90,6 +92,14 @@ public class Controller implements Initializable {
new Thread(this::checkUpdate).start();
}
private boolean isTableTab(Tab tab) {
return tabMap.containsKey(tab) && tabMap.get(tab) instanceof TableController;
}
private boolean isTestTab(Tab tab) {
return tabMap.containsKey(tab) && tabMap.get(tab) instanceof TestController;
}
private void checkUpdate() {
// Check new version via gitlab's REST API
String newVersion;
@ -227,9 +237,8 @@ public class Controller implements Initializable {
@FXML
private void onSaveAction(ActionEvent event) {
Tab tab = tabPane.getSelectionModel().getSelectedItem();
if (tabMap.containsKey(tab)) {
tabMap.get(tab).onSaveAction(event);
}
if (isTableTab(tab))
((TableController) tabMap.get(tab)).onSaveAction(event);
}
@FXML
@ -277,9 +286,9 @@ public class Controller implements Initializable {
MenuItem merge = new MenuItem("Merge with other table");
merge.setOnAction(event -> {
Map<String, TableController> choices = new HashMap<>(tabMap.values().size() - 1);
for (TableController tc : tabMap.values())
if (tc != loader.getController())
choices.put(tc.getName(), tc);
for (Initializable tc : tabMap.values())
if (tc instanceof TableController && tc != loader.getController())
choices.put(((TableController) tc).getName(), ((TableController) tc));
ChoiceDialog<String> dialog = new ChoiceDialog<>();
dialog.setTitle("Merging tables...");
@ -330,6 +339,7 @@ public class Controller implements Initializable {
theTest = new Tab("Test: " + tabPane.getSelectionModel().getSelectedItem().getText(), root);
tabPane.getTabs().add(theTest);
tabPane.getSelectionModel().selectLast();
tabMap.put(theTest, loader.getController());
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -129,5 +129,10 @@ public class TestController implements Initializable {
item.set(ending);
pauseCheckBox.setSelected(true);
pauseCheckBox.setDisable(true);
clock.stop();
}
void stopTimer() {
clock.stop();
}
}

View File

@ -40,6 +40,11 @@ public class Clock {
task = null;
}
public void stop() {
timer.cancel();
timer.purge();
}
private void setObservable() {
time.set(String.format("%02d:%02d", seconds / SEC_TO_MIN, seconds % SEC_TO_MIN));
}