diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index 4eb3edf..5796298 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -54,7 +54,7 @@ public class Controller implements Initializable { private MenuItem menuCloseTab, menuSave; private final BooleanProperty tabIsTable = new SimpleBooleanProperty(false); - private final Map tabMap = new HashMap<>(); + private final Map 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) c -> theTest = null); @@ -77,8 +77,10 @@ public class Controller implements Initializable { root.getScene().getWindow().setOnCloseRequest(event -> { for (Tab tab : tabPane.getTabs()) { EventHandler 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 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 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(); } diff --git a/src/main/java/es/kauron/jstudy/controller/TestController.java b/src/main/java/es/kauron/jstudy/controller/TestController.java index ce7526f..956df7b 100644 --- a/src/main/java/es/kauron/jstudy/controller/TestController.java +++ b/src/main/java/es/kauron/jstudy/controller/TestController.java @@ -129,5 +129,10 @@ public class TestController implements Initializable { item.set(ending); pauseCheckBox.setSelected(true); pauseCheckBox.setDisable(true); + clock.stop(); + } + + void stopTimer() { + clock.stop(); } } diff --git a/src/main/java/es/kauron/jstudy/util/Clock.java b/src/main/java/es/kauron/jstudy/util/Clock.java index d4440df..6cd58e0 100644 --- a/src/main/java/es/kauron/jstudy/util/Clock.java +++ b/src/main/java/es/kauron/jstudy/util/Clock.java @@ -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)); }