Repository navigation
Expand file tree
/
Copy pathNavigationService.java
More file actions
634 lines (552 loc) · 24.6 KB
/
Copy pathNavigationService.java
File metadata and controls
634 lines (552 loc) · 24.6 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package com.simulator;
import javafx.animation.*;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ScrollPane;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
/**
* ENTERPRISE-GRADE NAVIGATION SERVICE
* Enhanced with professional maximization support, multi-monitor handling,
* and responsive layout management for the Algorithm Simulator
*/
public class NavigationService {
private final List<Stage> openWindows;
// Configuration constants
private static final double WINDOW_OFFSET = 30.0;
private static final int MAX_CONCURRENT_WINDOWS = 10; // Increased for better productivity
private static final String ICON_PATH = "/icons/algorithm-simulator.png";
private static final double SCREEN_USAGE_FACTOR = 0.92; // Allow more screen usage
// Animation constants
private static final Duration ANIMATION_DURATION = Duration.millis(250);
private static final Duration FADE_DURATION = Duration.millis(200);
private final Map<String, Stage> stageCache = new HashMap<>();
public NavigationService() {
this.openWindows = new ArrayList<>();
System.out.println("🚀 NavigationService initialized with enhanced maximization support");
}
/**
* ENHANCED: Async module opening with better error handling and responsive design
*/
public CompletableFuture<Stage> openModuleAsync(String fxmlPath, String title, int width, int height) {
return CompletableFuture.supplyAsync(() -> {
try {
return openModuleSync(fxmlPath, title, width, height);
} catch (Exception e) {
Platform.runLater(() -> handleModuleError(title, e));
return null;
}
});
}
private void addStylesheets(Scene scene) {
try {
scene.getStylesheets().add(getClass().getResource("/css/main.css").toExternalForm());
} catch (Exception e) {
System.err.println("Could not load main.css");
}
}
private void showWindow(Stage stage) {
stage.show(); // That's it - no complex animations
}
private void positionWindow(Stage stage) {
Rectangle2D screen = Screen.getPrimary().getVisualBounds();
if (openWindows.isEmpty()) {
// ✅ PERFECT CENTER for first window
double centerX = screen.getMinX() + (screen.getWidth() - stage.getWidth()) / 2;
double centerY = screen.getMinY() + (screen.getHeight() - stage.getHeight()) / 2;
stage.setX(centerX);
stage.setY(centerY);
System.out.println("🎯 First window centered at: (" + centerX + ", " + centerY + ")");
} else {
// ✅ IMPROVED CASCADE from center, not fixed offset
double cascadeOffset = openWindows.size() * WINDOW_OFFSET; // Uses your 30px constant
double baseX = screen.getMinX() + (screen.getWidth() - stage.getWidth()) / 2;
double baseY = screen.getMinY() + (screen.getHeight() - stage.getHeight()) / 2;
// Apply cascade offset to centered position
stage.setX(baseX + cascadeOffset);
stage.setY(baseY + cascadeOffset);
System.out.println("📐 Cascaded window at: (" + (baseX + cascadeOffset) + ", " + (baseY + cascadeOffset) + ")");
}
}
/**
* ENHANCED: Synchronous module opening with full maximization support
*/
public Stage openModule(String fxmlPath, String title, int width, int height) {
return openModuleSync(fxmlPath, title, width, height);
}
private Stage openModuleSync(String fxmlPath, String title, int width, int height) {
try {
System.out.println("🔍 Opening module: " + title);
// ✅ ENHANCED: Check stage cache first
if (stageCache.containsKey(title)) {
Stage cachedStage = stageCache.get(title);
if (cachedStage.isShowing()) {
// Window is already visible, bring to front
System.out.println("🔄 Bringing existing window to front: " + title);
cachedStage.toFront();
cachedStage.requestFocus();
return cachedStage;
} else {
// Window exists but hidden, show it
System.out.println("♻️ Reusing hidden stage: " + title);
Platform.runLater(() -> {
cachedStage.show();
cachedStage.toFront();
cachedStage.requestFocus();
});
// Add back to tracking since it's now visible
if (!openWindows.contains(cachedStage)) {
openWindows.add(cachedStage);
}
return cachedStage;
}
}
// Enhanced window limit check
if (openWindows.size() >= MAX_CONCURRENT_WINDOWS) {
showWarning("Window Limit Reached",
"Maximum of " + MAX_CONCURRENT_WINDOWS + " windows allowed for optimal performance.");
return null;
}
// Enhanced FXML loading
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(fxmlPath));
if (loader.getLocation() == null) {
showError("FXML Resource Missing", "Could not locate FXML file: " + fxmlPath);
return null;
}
Parent root = loader.load();
Object controller = loader.getController();
// Create scroll pane
ScrollPane scrollPane = createEnhancedScrollPane(root);
// Create stage
Stage moduleStage = createModuleStage(title, width, height);
// Create scene
Scene scene = new Scene(scrollPane, width, height);
addStylesheets(scene);
ThemeManager.getInstance().registerScene(scene);
moduleStage.setScene(scene);
// Position window
positionWindow(moduleStage);
// Add to tracking
openWindows.add(moduleStage);
// ✅ ENHANCED: Add to cache for reuse
stageCache.put(title, moduleStage);
// ⭐ ENHANCED LIFECYCLE SETUP:
setupEnhancedWindowLifecycle(moduleStage, controller, title);
// Setup keyboard shortcuts
setupAdvancedKeyboardShortcuts(scene, moduleStage);
// Show window
showWithEnhancedAnimation(moduleStage);
System.out.println("✅ Module opened successfully: " + title);
return moduleStage;
} catch (Exception e) {
handleModuleError(title, e);
return null;
}
}
/**
* ✅ ENHANCED: Proper window lifecycle with cache management
*/
private void setupEnhancedWindowLifecycle(Stage stage, Object controller, String title) {
// Handle window hiding (for back button navigation)
stage.setOnHiding(e -> {
System.out.println("👁️ Stage hidden (can be reopened): " + title);
// Remove from openWindows but keep in cache
openWindows.remove(stage);
});
// Handle window closing (red X button or explicit close)
stage.setOnCloseRequest(e -> {
System.out.println("🗑️ Stage closing completely: " + title);
// Remove from both tracking lists
openWindows.remove(stage);
stageCache.remove(title);
// Cleanup controller
if (controller instanceof BaseController) {
try {
((BaseController) controller).cleanup();
} catch (Exception ex) {
System.err.println("Cleanup error: " + ex.getMessage());
}
}
// Unregister from theme manager
if (stage.getScene() != null) {
ThemeManager.getInstance().unregisterScene(stage.getScene());
}
System.out.println("✅ Stage cleanup completed: " + title);
});
}
/**
* ENHANCED: Responsive scroll pane with better performance
*/
private ScrollPane createEnhancedScrollPane(Parent content) {
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(content);
scrollPane.setFitToWidth(true);
scrollPane.setFitToHeight(true);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.getStyleClass().addAll("scroll-pane", "enhanced-scroll");
// Enhanced smooth scrolling with momentum
scrollPane.setPannable(true);
scrollPane.setVvalue(0);
scrollPane.setHvalue(0);
// ENHANCED: Advanced scroll behaviors
scrollPane.setOnScroll(event -> {
if (event.isControlDown()) {
// Future: Ctrl + scroll for zoom functionality
event.consume();
} else {
// Smooth momentum scrolling
double deltaY = event.getDeltaY() * 0.002;
scrollPane.setVvalue(scrollPane.getVvalue() - deltaY);
}
});
// Performance optimization
scrollPane.viewportBoundsProperty().addListener((obs, oldBounds, newBounds) -> {
// Optimize content rendering for large scroll areas
if (newBounds.getWidth() > 2000 || newBounds.getHeight() > 1500) {
scrollPane.getStyleClass().add("large-viewport");
}
});
return scrollPane;
}
private Stage createModuleStage(String title, int width, int height) {
Stage stage = new Stage();
stage.setTitle(title);
// CRITICAL: Simple, clean initialization
stage.initStyle(StageStyle.DECORATED);
stage.initModality(Modality.NONE);
stage.setResizable(true);
stage.setAlwaysOnTop(false); // The minimize fix
// Set owner
try {
Stage primaryStage = AlgorithmSimulatorApplication.getPrimaryStage();
if (primaryStage != null && primaryStage.isShowing()) {
stage.initOwner(primaryStage);
}
} catch (Exception e) {
// Ignore
}
// Simple size setup
Rectangle2D screen = Screen.getPrimary().getVisualBounds();
stage.setWidth(Math.min(width, screen.getWidth() * 0.9));
stage.setHeight(Math.min(height, screen.getHeight() * 0.9));
stage.setMinWidth(800);
stage.setMinHeight(600);
return stage;
}
/**
* SIMPLIFIED: Essential window lifecycle only
*/
private void setupWindowLifecycle(Stage stage, Object controller) {
// Only the essential close handling
stage.setOnCloseRequest(e -> {
openWindows.remove(stage);
// Cleanup controller
if (controller instanceof BaseController) {
try {
((BaseController) controller).cleanup();
} catch (Exception ex) {
System.err.println("Cleanup error: " + ex.getMessage());
}
}
// Unregister from theme manager
if (stage.getScene() != null) {
ThemeManager.getInstance().unregisterScene(stage.getScene());
}
});
}
/**
* ENHANCED: Comprehensive keyboard shortcuts with minimize support
*/
private void setupAdvancedKeyboardShortcuts(Scene scene, Stage stage) {
scene.setOnKeyPressed(event -> {
if (event.isControlDown()) {
switch (event.getCode()) {
case W:
// Ctrl+W to close window
stage.close();
event.consume();
break;
case T:
// Ctrl+T to toggle theme
ThemeManager.getInstance().toggleTheme();
event.consume();
break;
case MINUS:
case SUBTRACT:
// Ctrl+- to minimize (ENHANCED)
stage.setIconified(true);
System.out.println("➖ Window minimized via Ctrl+-: " + stage.getTitle());
event.consume();
break;
case DOWN:
// Ctrl+Down to minimize (alternative)
if (event.isControlDown()) {
stage.setIconified(true);
System.out.println("➖ Window minimized via Ctrl+Down: " + stage.getTitle());
event.consume();
}
break;
case PLUS:
case EQUALS:
case ADD:
// Ctrl++ to maximize/restore
stage.setMaximized(!stage.isMaximized());
event.consume();
break;
case UP:
// Ctrl+Up to maximize (alternative)
if (event.isControlDown()) {
stage.setMaximized(!stage.isMaximized());
event.consume();
}
break;
case M:
// Ctrl+M to maximize/restore (alternative)
stage.setMaximized(!stage.isMaximized());
event.consume();
break;
case D:
// Ctrl+D to minimize (alternative shortcut)
stage.setIconified(true);
System.out.println("➖ Window minimized via Ctrl+D: " + stage.getTitle());
event.consume();
break;
case R:
// Ctrl+R to refresh/reset
System.out.println("🔄 Refresh requested for: " + stage.getTitle());
event.consume();
break;
case DIGIT1:
case DIGIT2:
case DIGIT3:
case DIGIT4:
case DIGIT5:
// Ctrl+1-5 to switch between open windows
int windowIndex = Integer.parseInt(event.getCode().getName().substring(5)) - 1;
if (windowIndex < openWindows.size()) {
Stage targetWindow = openWindows.get(windowIndex);
if (targetWindow.isIconified()) {
targetWindow.setIconified(false); // Restore if minimized
}
targetWindow.toFront();
targetWindow.requestFocus();
}
event.consume();
break;
}
}
// Standalone key shortcuts
switch (event.getCode()) {
case ESCAPE:
// Escape to close window
stage.close();
event.consume();
break;
case F11:
// F11 to toggle fullscreen
stage.setFullScreen(!stage.isFullScreen());
event.consume();
break;
case F9:
// F9 to minimize (common shortcut)
stage.setIconified(true);
System.out.println("➖ Window minimized via F9: " + stage.getTitle());
event.consume();
break;
case F10:
// F10 to maximize/restore
stage.setMaximized(!stage.isMaximized());
event.consume();
break;
case F5:
// F5 to refresh
System.out.println("🔄 F5 refresh requested for: " + stage.getTitle());
event.consume();
break;
}
});
}
/**
* ENHANCED: Professional show animation with window controls verification
*/
private void showWithEnhancedAnimation(Stage stage) {
// Start with invisible stage
stage.setOpacity(0.0);
stage.show();
// ADDED: Verify window controls are enabled after showing
Platform.runLater(() -> {
// Force enable all window operations
stage.setResizable(true);
stage.setIconified(false);
// Debug: Print window state
System.out.println("🔍 Window controls state for " + stage.getTitle() + ":");
System.out.println(" - Resizable: " + stage.isResizable());
System.out.println(" - Iconified: " + stage.isIconified());
System.out.println(" - Maximized: " + stage.isMaximized());
System.out.println(" - Showing: " + stage.isShowing());
});
// Create multiple parallel animations for professional feel
FadeTransition contentFade = new FadeTransition(ANIMATION_DURATION, stage.getScene().getRoot());
contentFade.setFromValue(0.0);
contentFade.setToValue(1.0);
contentFade.setInterpolator(Interpolator.EASE_OUT);
// Stage opacity animation
Timeline stageOpacity = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(stage.opacityProperty(), 0.0)),
new KeyFrame(ANIMATION_DURATION, new KeyValue(stage.opacityProperty(), 1.0, Interpolator.EASE_OUT))
);
// Enhanced scale animation
ScaleTransition scaleIn = new ScaleTransition(ANIMATION_DURATION, stage.getScene().getRoot());
scaleIn.setFromX(0.92);
scaleIn.setFromY(0.92);
scaleIn.setToX(1.0);
scaleIn.setToY(1.0);
scaleIn.setInterpolator(Interpolator.EASE_OUT);
// Professional entrance animation
ParallelTransition showAnimation = new ParallelTransition(contentFade, stageOpacity, scaleIn);
showAnimation.setOnFinished(e -> {
// Final setup after animation
if (stage.getScene() != null && stage.getScene().getRoot() != null) {
stage.getScene().getRoot().getStyleClass().add("window-loaded");
}
// ADDED: Final verification of window controls
Platform.runLater(() -> {
System.out.println("✅ Animation complete - Window controls should be fully functional");
});
});
showAnimation.play();
}
// ENHANCED: Error handling with user-friendly messages
private void handleModuleError(String title, Exception e) {
e.printStackTrace();
String detailedMessage = analyzeError(e);
showError("Module Loading Failed",
"Failed to open " + title + "\n\n" +
"Issue: " + detailedMessage + "\n\n" +
"Troubleshooting:\n" +
"• Verify FXML files are in resources/fxml/\n" +
"• Check controller class compilation\n" +
"• Ensure all dependencies are available\n" +
"• Restart application if problem persists\n\n" +
"Technical details:\n" + e.getMessage());
}
private String analyzeError(Exception e) {
String message = e.getMessage();
if (message == null) message = e.getClass().getSimpleName();
if (message.contains("FXMLLoader")) {
return "FXML loading error - check file structure";
} else if (message.contains("Controller")) {
return "Controller initialization error";
} else if (message.contains("Scene")) {
return "Scene creation error";
} else if (message.contains("CSS") || message.contains("stylesheet")) {
return "Stylesheet loading error";
} else {
return "Unexpected error during module initialization";
}
}
private void showError(String title, String message) {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(title);
alert.setHeaderText("Navigation Service Error");
alert.setContentText(message);
// Apply current theme to alert
try {
if (alert.getDialogPane().getScene() != null) {
ThemeManager.getInstance().registerScene(alert.getDialogPane().getScene());
}
} catch (Exception e) {
System.out.println("ℹ️ Could not apply theme to error dialog");
}
alert.showAndWait();
});
}
private void showWarning(String title, String message) {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle(title);
alert.setHeaderText("Navigation Service Warning");
alert.setContentText(message);
// Apply current theme to alert
try {
if (alert.getDialogPane().getScene() != null) {
ThemeManager.getInstance().registerScene(alert.getDialogPane().getScene());
}
} catch (Exception e) {
System.out.println("ℹ️ Could not apply theme to warning dialog");
}
alert.showAndWait();
});
}
/**
* ENHANCED: Close all windows with staggered animation
*/
public void closeAllWindows() {
if (openWindows.isEmpty()) return;
List<Stage> windowsCopy = new ArrayList<>(openWindows);
for (int i = 0; i < windowsCopy.size(); i++) {
Stage window = windowsCopy.get(i);
// Staggered animation for visual appeal
Timeline delayedClose = new Timeline(
new KeyFrame(Duration.millis(i * 100), e -> {
FadeTransition fadeOut = new FadeTransition(FADE_DURATION, window.getScene().getRoot());
fadeOut.setToValue(0.0);
fadeOut.setOnFinished(closeEvent -> window.close());
fadeOut.play();
})
);
delayedClose.play();
}
// Clear the list after all animations
Timeline clearList = new Timeline(
new KeyFrame(Duration.millis(windowsCopy.size() * 100 + 300), e -> openWindows.clear())
);
clearList.play();
}
// ENHANCED: Utility methods with better functionality
public int getOpenWindowCount() {
return openWindows.size();
}
public List<String> getOpenWindowTitles() {
return openWindows.stream()
.map(Stage::getTitle)
.toList();
}
public boolean isModuleOpen(String title) {
return openWindows.stream()
.anyMatch(stage -> stage.getTitle().equals(title));
}
private Stage getWindowByTitle(String title) {
return openWindows.stream()
.filter(stage -> stage.getTitle().equals(title))
.findFirst()
.orElse(null);
}
public void bringWindowToFront(String title) {
openWindows.stream()
.filter(stage -> stage.getTitle().equals(title))
.findFirst()
.ifPresent(stage -> {
if (stage.isIconified()) {
stage.setIconified(false);
}
stage.toFront();
stage.requestFocus();
});
}
}