Repository navigation
Expand file tree
/
Copy pathBaseController.java
More file actions
159 lines (138 loc) · 4.87 KB
/
Copy pathBaseController.java
File metadata and controls
159 lines (138 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Enhanced BaseController.java
package com.simulator;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Slider;
import javafx.application.Platform;
import java.net.URL;
import java.util.ResourceBundle;
public abstract class BaseController implements Initializable {
protected AnimationService animationService;
protected ThemeManager themeManager;
protected boolean isAnimating = false;
protected boolean isPaused = false;
protected Slider speedSlider;
@Override
public final void initialize(URL location, ResourceBundle resources) {
// Initialize common services
animationService = AnimationService.getInstance();
themeManager = ThemeManager.getInstance();
// Register scene for theme updates
Platform.runLater(() -> {
if (getScene() != null) {
themeManager.registerScene(getScene());
}
});
// Template method pattern - called in order
initializeServices();
setupComponents();
setupEventHandlers();
configureAnimations();
setupKeyboardShortcuts();
System.out.println(getClass().getSimpleName() + " initialized successfully");
}
// Common initialization
protected void initializeServices() {
// Override if needed
}
// Abstract methods - must be implemented by subclasses
protected abstract void setupComponents();
protected abstract void setupEventHandlers();
protected abstract void configureAnimations();
// Common functionality with default implementation
protected void setupKeyboardShortcuts() {
// Default keyboard shortcuts for all modules
Platform.runLater(() -> {
if (getScene() != null) {
getScene().setOnKeyPressed(event -> {
if (event.isControlDown()) {
switch (event.getCode()) {
case SPACE -> toggleAnimation();
case R -> resetVisualization();
case T -> themeManager.toggleTheme();
case ESCAPE -> closeModule();
}
}
});
}
});
}
// Common animation control methods
protected void startAnimation() {
if (!isAnimating) {
isAnimating = true;
isPaused = false;
updateControlStates();
onAnimationStart();
}
}
protected void pauseAnimation() {
if (isAnimating) {
isPaused = !isPaused;
updateControlStates();
onAnimationPause(isPaused);
}
}
protected void stopAnimation() {
isAnimating = false;
isPaused = false;
updateControlStates();
onAnimationStop();
}
protected void resetVisualization() {
stopAnimation();
onReset();
updateControlStates();
}
protected void toggleAnimation() {
if (isAnimating) {
pauseAnimation();
} else {
startAnimation();
}
}
// Abstract animation lifecycle methods
protected abstract void onAnimationStart();
protected abstract void onAnimationPause(boolean isPaused);
protected abstract void onAnimationStop();
protected abstract void onReset();
// Common UI update
protected void updateControlStates() {
// Override in subclasses to update UI state
}
// Utility methods
protected void showError(String title, String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
protected void showInfo(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
protected javafx.scene.Scene getScene() {
// Override in subclasses to return current scene
return null;
}
protected void closeModule() {
// Override to handle module closing
Platform.runLater(() -> {
if (getScene() != null && getScene().getWindow() instanceof javafx.stage.Stage) {
((javafx.stage.Stage) getScene().getWindow()).close();
}
});
}
// Cleanup method for proper resource management
public void cleanup() {
if (getScene() != null) {
themeManager.unregisterScene(getScene());
}
stopAnimation();
System.out.println(getClass().getSimpleName() + " cleaned up");
}
}