From 087b132362590d480cd0a21473100638c2af683a Mon Sep 17 00:00:00 2001 From: LucaaMueller Date: Sun, 25 May 2025 18:57:16 +0200 Subject: [PATCH] Refactoring - Extract Method, Large Class --- .../pic16f84/Controller_Frontend.java | 38 ++++++++----------- .../simulator/pic16f84/ExecutionState.java | 19 ++++++++++ .../FrontendControllerInterface.java | 4 -- 3 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 src/main/java/fabrik/simulator/pic16f84/ExecutionState.java diff --git a/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java b/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java index b8e436a..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,21 +80,21 @@ public class Controller_Frontend extends PICComponent implements FrontendControl } - public boolean isSleeping (){ - return isSleeping; - } + //Refactoring public void stopRunFromBackend(String message) { - isAutoRunActive = false; + ExecutionState.setAutoRunActive(false); + handleSleepOrReset(); showStopDialog(message); + } private void handleSleepOrReset() { - if (isSleeping) { - wakeUpFromSleep(); + if (ExecutionState.isSleeping()) { + ExecutionState.wakeUp(); } else { dataRegister.resetPC(); } @@ -123,31 +121,26 @@ public class Controller_Frontend extends PICComponent implements FrontendControl } + //Refactoring Ende - public void sleep() { - isSleeping = true; - } - public void wakeUpFromSleep() { - isSleeping = false; - } @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 { @@ -167,6 +160,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl } + private void updateExecutionTimeMultiplier() { String selectedOption = executionTimeComboBox.getValue(); commands.setExecutionTimeMultiplier(selectedOption); @@ -181,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; @@ -210,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(); } @@ -285,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); }