diff --git a/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java b/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java index 33adadc..6b5ea96 100644 --- a/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java +++ b/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java @@ -72,8 +72,6 @@ public class Controller_Frontend extends PICComponent implements FrontendControl @FXML private CheckBox wdtCheck; - private static volatile boolean isAutoRunActive = false; - private static volatile boolean isSleeping = false; public Controller_Frontend() { @@ -82,55 +80,67 @@ public class Controller_Frontend extends PICComponent implements FrontendControl } - public boolean isSleeping (){ - return isSleeping; + + + + //Refactoring + + public void stopRunFromBackend(String message) { + ExecutionState.setAutoRunActive(false); + + handleSleepOrReset(); + showStopDialog(message); + + } + private void handleSleepOrReset() { + if (ExecutionState.isSleeping()) { + ExecutionState.wakeUp(); + } else { + dataRegister.resetPC(); + } } - public void stopRunFromBackend(String message){ - isAutoRunActive = false; - if (isSleeping) - wakeUpFromSleep(); - else - dataRegister.resetPC(); + private static void showStopDialog(String message) { Stage stoppedStage = new Stage(); stoppedStage.setTitle("Programm unterbrochen!"); + VBox vbox = new VBox(); vbox.setAlignment(javafx.geometry.Pos.CENTER); + Label grundlabel = new Label("Grund: " + message); grundlabel.setStyle("-fx-font-size: 16px; -fx-font-weight: bold;"); + Label ueberlabel = new Label("Programm unterbrochen!"); - vbox.getChildren().add(ueberlabel); - vbox.getChildren().add(grundlabel); + vbox.getChildren().addAll(ueberlabel, grundlabel); VBox.setMargin(grundlabel, new javafx.geometry.Insets(10, 10, 10, 10)); + Scene scene = new Scene(vbox, 300, 90); stoppedStage.setAlwaysOnTop(true); stoppedStage.setScene(scene); stoppedStage.show(); } - public void sleep() { - isSleeping = true; - } - public void wakeUpFromSleep() { - isSleeping = false; - } + + //Refactoring Ende + + @FXML private void stopAutoRun(ActionEvent event) { - isAutoRunActive = false; + ExecutionState.setAutoRunActive(false); } @FXML public void autoRunGUI(ActionEvent event) { - if (!isAutoRunActive) { - isAutoRunActive = true; + if (!ExecutionState.isAutoRunActive()) { + ExecutionState.setAutoRunActive(true) ; } Thread autoRunThread = new Thread(() -> { try { - while (dataRegister.getPC() < prog.length && isAutoRunActive){ + while (dataRegister.getPC() < prog.length && ExecutionState.isAutoRunActive()){ Platform.runLater(() -> { try { @@ -150,6 +160,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl } + private void updateExecutionTimeMultiplier() { String selectedOption = executionTimeComboBox.getValue(); commands.setExecutionTimeMultiplier(selectedOption); @@ -164,7 +175,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl int currentIndex; // Aktuelle Zeile abrufen - if (!isSleeping) + if (!ExecutionState.isSleeping()) currentIndex = ind[dataRegister.getPC()]; else currentIndex = ind[dataRegister.getPC()]-1; @@ -193,14 +204,14 @@ public class Controller_Frontend extends PICComponent implements FrontendControl lstContentListView.getSelectionModel().select(currentIndex); String selectedRowStyle; - if (!isSleeping) + if (!ExecutionState.isSleeping()) selectedRowStyle = "-fx-background-color: red; -fx-text-fill: white;"; else selectedRowStyle = "-fx-background-color: teal; -fx-text-fill: white;"; markSelectedRow(currentIndex, selectedRowStyle); - if (!isSleeping) { + if (!ExecutionState.isSleeping()) { commands.decode(prog[dataRegister.getPC()]); dataRegister.increasePC(); } @@ -268,7 +279,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl } catch (NullPointerException ignored) {} } programStack.reset(); - wakeUpFromSleep(); + ExecutionState.wakeUp(); breakpoints.clear(); ioPorts.refreshUI(getTRISbuttons(), getPORTbuttons()); commands.resetTotalExecutionTime(); diff --git a/src/main/java/fabrik/simulator/pic16f84/ExecutionState.java b/src/main/java/fabrik/simulator/pic16f84/ExecutionState.java new file mode 100644 index 0000000..f35c969 --- /dev/null +++ b/src/main/java/fabrik/simulator/pic16f84/ExecutionState.java @@ -0,0 +1,19 @@ +package fabrik.simulator.pic16f84; + +public class ExecutionState { + + private static boolean isAutoRunActive = false; + private static boolean isSleeping = false; + private static double executionTimeMultiplier = 1; + + public static boolean isAutoRunActive() { return isAutoRunActive; } + public static void setAutoRunActive(boolean active) { isAutoRunActive = active; } + + public static boolean isSleeping() { return isSleeping; } + public static void sleep() { isSleeping = true; } + public static void wakeUp() { isSleeping = false; } + + public static double getExecutionTimeMultiplier() { return executionTimeMultiplier; } + public static void setExecutionTimeMultiplier(double multiplier) { executionTimeMultiplier = multiplier; } + } + diff --git a/src/main/java/fabrik/simulator/pic16f84/interfaces/FrontendControllerInterface.java b/src/main/java/fabrik/simulator/pic16f84/interfaces/FrontendControllerInterface.java index bc7aeb8..fb3e010 100644 --- a/src/main/java/fabrik/simulator/pic16f84/interfaces/FrontendControllerInterface.java +++ b/src/main/java/fabrik/simulator/pic16f84/interfaces/FrontendControllerInterface.java @@ -2,11 +2,7 @@ package fabrik.simulator.pic16f84.interfaces; public interface FrontendControllerInterface extends PICComponentInterface { - void sleep(); - void wakeUpFromSleep(); - - boolean isSleeping(); void stopRunFromBackend(String watchdogTimer); }