Repository navigation
Expand file tree
/
Copy pathLinkedListController.java
More file actions
839 lines (673 loc) · 30.9 KB
/
Copy pathLinkedListController.java
File metadata and controls
839 lines (673 loc) · 30.9 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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
package com.simulator;
import javafx.animation.*;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.List;
import javafx.fxml.FXMLLoader; // ← ADD THIS
import javafx.scene.Parent; // ← ADD THIS
import javafx.scene.Scene;
import javafx.stage.Modality; // ← ADD THIS
public class LinkedListController {
@FXML private TextField inputField;
@FXML private TextField positionField;
@FXML private TextField searchField;
@FXML private Button insertBeginButton, insertEndButton, insertPositionButton;
@FXML private Button deleteValueButton, deletePositionButton, searchButton;
@FXML private Button clearButton;
@FXML private HBox listVisualization;
@FXML private Label statusLabel, sizeLabel, resultLabel;
@FXML private TextArea operationHistory;
@FXML private ProgressBar operationProgress;
@FXML private Button codesButton;
private LinkedListModel listModel;
private List<NodeElement> visualNodes;
private AnimationService animationService;
private Timeline currentAnimation;
private boolean isOperationRunning = false;
@FXML
public void initialize() {
System.out.println("LinkedListController initialized");
listModel = new LinkedListModel();
visualNodes = new ArrayList<>();
animationService = AnimationService.getInstance();
setupComponents();
setupEventHandlers();
updateDisplay();
System.out.println("LinkedList module ready");
}
@FXML
private void openCodesPopup() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/code-popup.fxml"));
Parent root = loader.load();
CodePopupController controller = loader.getController();
Stage popupStage = new Stage();
popupStage.initOwner(listVisualization.getScene().getWindow());
popupStage.initModality(Modality.NONE);
popupStage.setTitle("LinkedList Code Examples");
Scene scene = new Scene(root, 1200, 800);
// 🎯 ENHANCED: Copy stylesheets and apply theme
boolean isDarkMode = ThemeManager.getInstance().isDarkMode();
// Copy stylesheets from main app
scene.getStylesheets().clear();
scene.getStylesheets().addAll(listVisualization.getScene().getStylesheets());
// Apply theme classes to scene and popup roots
String themeClass = isDarkMode ? "dark-theme" : "light-theme";
String removeClass = isDarkMode ? "light-theme" : "dark-theme";
scene.getRoot().getStyleClass().removeAll(removeClass);
scene.getRoot().getStyleClass().add(themeClass);
root.getStyleClass().removeAll(removeClass);
root.getStyleClass().add(themeClass);
root.getStyleClass().addAll("main-layout", "algorithm-page", "code-popup-window");
popupStage.setScene(scene);
popupStage.setMinWidth(1000);
popupStage.setMinHeight(700);
popupStage.setResizable(true);
// Center on parent window
Stage parentStage = (Stage) listVisualization.getScene().getWindow();
popupStage.setX(parentStage.getX() + 50);
popupStage.setY(parentStage.getY() + 50);
controller.setParentController(this);
// 🎯 CRITICAL: Handle popup closing properly
popupStage.setOnCloseRequest(e -> {
controller.cleanup(); // Ensure cleanup happens
});
popupStage.show();
logOperation("CODES_POPUP_OPENED | LinkedList code examples displayed");
} catch (Exception e) {
e.printStackTrace();
showAlert("Error", "Could not open code examples: " + e.getMessage());
}
}
private void setupComponents() {
// Configure input validation
inputField.textProperty().addListener((obs, oldText, newText) -> {
if (!newText.matches("\\d*")) {
inputField.setText(newText.replaceAll("[^\\d]", ""));
}
if (newText.length() > 3) {
inputField.setText(newText.substring(0, 3));
}
});
positionField.textProperty().addListener((obs, oldText, newText) -> {
if (!newText.matches("\\d*")) {
positionField.setText(newText.replaceAll("[^\\d]", ""));
}
});
searchField.textProperty().addListener((obs, oldText, newText) -> {
if (!newText.matches("\\d*")) {
searchField.setText(newText.replaceAll("[^\\d]", ""));
}
if (newText.length() > 3) {
searchField.setText(newText.substring(0, 3));
}
});
inputField.setPromptText("Enter value (1-999)");
positionField.setPromptText("Position");
searchField.setPromptText("Search value");
if (operationHistory != null) {
operationHistory.setText("=== LinkedList Operations History ===\n");
operationHistory.setEditable(false);
}
}
private void setupEventHandlers() {
inputField.setOnAction(e -> insertAtEnd());
positionField.setOnAction(e -> insertAtPosition());
searchField.setOnAction(e -> searchValue());
}
@FXML
private void insertAtBeginning() {
if (isOperationRunning) return;
String input = inputField.getText().trim();
if (input.isEmpty()) {
showAlert("Invalid Input", "Please enter a value to insert.");
return;
}
try {
int value = Integer.parseInt(input);
if (value < 1 || value > 999) {
showAlert("Invalid Range", "Please enter a number between 1 and 999.");
return;
}
isOperationRunning = true;
updateControlStates();
// Add to model
listModel.insertAtBeginning(value);
// Create visual node
NodeElement nodeElement = createNodeElement(value, 0);
visualNodes.add(0, nodeElement);
// Update positions of existing nodes
updateNodePositions();
// Animate insertion
animateInsertAtBeginning(nodeElement, () -> {
isOperationRunning = false;
updateControlStates();
updateDisplay();
});
inputField.clear();
logOperation("INSERT_BEGIN " + value + " | Size: " + listModel.size());
} catch (NumberFormatException e) {
showAlert("Invalid Input", "Please enter a valid integer.");
}
}
@FXML
private void insertAtEnd() {
if (isOperationRunning ) return;
String input = inputField.getText().trim();
if (input.isEmpty()) {
showAlert("Invalid Input", "Please enter a value to insert.");
return;
}
try {
int value = Integer.parseInt(input);
if (value < 1 || value > 999) {
showAlert("Invalid Range", "Please enter a number between 1 and 999.");
return;
}
isOperationRunning = true;
updateControlStates();
// Add to model
listModel.insertAtEnd(value);
// Create visual node
NodeElement nodeElement = createNodeElement(value, visualNodes.size());
visualNodes.add(nodeElement);
// Animate insertion
animateInsertAtEnd(nodeElement, () -> {
isOperationRunning = false;
updateControlStates();
updateDisplay();
});
inputField.clear();
logOperation("INSERT_END " + value + " | Size: " + listModel.size());
} catch (NumberFormatException e) {
showAlert("Invalid Input", "Please enter a valid integer.");
}
}
@FXML
private void insertAtPosition() {
if (isOperationRunning) return;
String input = inputField.getText().trim();
String posInput = positionField.getText().trim();
if (input.isEmpty() || posInput.isEmpty()) {
showAlert("Invalid Input", "Please enter both value and position.");
return;
}
try {
int value = Integer.parseInt(input);
int position = Integer.parseInt(posInput);
if (value < 1 || value > 999) {
showAlert("Invalid Range", "Please enter a value between 1 and 999.");
return;
}
if (position < 0 || position > listModel.size()) {
showAlert("Invalid Position", "Position must be between 0 and " + listModel.size());
return;
}
isOperationRunning = true;
updateControlStates();
// Add to model
listModel.insertAtPosition(position, value);
// Create visual node
NodeElement nodeElement = createNodeElement(value, position);
visualNodes.add(position, nodeElement);
// Update positions of nodes after insertion point
updateNodePositions();
// Animate insertion
animateInsertAtPosition(nodeElement, position, () -> {
isOperationRunning = false;
updateControlStates();
updateDisplay();
});
inputField.clear();
positionField.clear();
logOperation("INSERT_POS " + value + " at " + position + " | Size: " + listModel.size());
} catch (NumberFormatException e) {
showAlert("Invalid Input", "Please enter valid integers.");
}
}
@FXML
private void deleteValue() {
if (isOperationRunning) return;
String input = inputField.getText().trim();
if (input.isEmpty()) {
showAlert("Invalid Input", "Please enter a value to delete.");
return;
}
try {
int value = Integer.parseInt(input);
if (listModel.isEmpty()) {
showAlert("List Empty", "Cannot delete from an empty list.");
return;
}
// Find the node to delete
int position = findNodePosition(value);
if (position == -1) {
showAlert("Value Not Found", "Value " + value + " not found in the list.");
return;
}
isOperationRunning = true;
updateControlStates();
// Remove from model
boolean deleted = listModel.delete(value);
if (deleted) {
// Get visual node
NodeElement nodeToDelete = visualNodes.get(position);
// Animate deletion
animateDelete(nodeToDelete, position, () -> {
visualNodes.remove(position);
updateNodePositions();
isOperationRunning = false;
updateControlStates();
updateDisplay();
});
inputField.clear();
logOperation("DELETE " + value + " | Size: " + listModel.size());
}
} catch (NumberFormatException e) {
showAlert("Invalid Input", "Please enter a valid integer.");
}
}
@FXML
private void deleteAtPosition() {
if (isOperationRunning ) return;
String posInput = positionField.getText().trim();
if (posInput.isEmpty()) {
showAlert("Invalid Input", "Please enter a position to delete.");
return;
}
try {
int position = Integer.parseInt(posInput);
if (listModel.isEmpty()) {
showAlert("List Empty", "Cannot delete from an empty list.");
return;
}
if (position < 0 || position >= listModel.size()) {
showAlert("Invalid Position", "Position must be between 0 and " + (listModel.size() - 1));
return;
}
isOperationRunning = true;
updateControlStates();
// Get value for logging
int value = visualNodes.get(position).getValue();
// Remove from model
boolean deleted = listModel.deleteAtPosition(position);
if (deleted) {
// Get visual node
NodeElement nodeToDelete = visualNodes.get(position);
// Animate deletion
animateDelete(nodeToDelete, position, () -> {
visualNodes.remove(position);
updateNodePositions();
isOperationRunning = false;
updateControlStates();
updateDisplay();
});
positionField.clear();
logOperation("DELETE_POS " + value + " at " + position + " | Size: " + listModel.size());
}
} catch (NumberFormatException e) {
showAlert("Invalid Input", "Please enter a valid integer.");
}
}
@FXML
private void searchValue() {
String input = searchField.getText().trim();
if (input.isEmpty()) {
showAlert("Invalid Input", "Please enter a value to search.");
return;
}
try {
int value = Integer.parseInt(input);
if (listModel.isEmpty()) {
resultLabel.setText("❌ List is empty");
resultLabel.setStyle("-fx-text-fill: #ef4444;");
return;
}
// Animate search process
animateSearch(value, (position) -> {
if (position != -1) {
resultLabel.setText("✅ Found " + value + " at position " + position);
resultLabel.setStyle("-fx-text-fill: #10b981;");
highlightNode(position);
} else {
resultLabel.setText("❌ Value " + value + " not found");
resultLabel.setStyle("-fx-text-fill: #ef4444;");
}
logOperation("SEARCH " + value + " | Result: " + (position != -1 ? "Found at " + position : "Not found"));
});
} catch (NumberFormatException e) {
showAlert("Invalid Input", "Please enter a valid integer.");
}
}
@FXML
private void clearList() {
if (isOperationRunning ) return;
if (listModel.isEmpty()) {
showAlert("List Empty", "List is already empty.");
return;
}
Alert confirmAlert = new Alert(Alert.AlertType.CONFIRMATION);
confirmAlert.setTitle("Clear List");
confirmAlert.setHeaderText("Confirm Clear Operation");
confirmAlert.setContentText("Are you sure you want to clear all " + listModel.size() + " elements?");
if (confirmAlert.showAndWait().orElse(ButtonType.CANCEL) == ButtonType.OK) {
isOperationRunning = true;
updateControlStates();
animateClear(() -> {
listModel.clear();
visualNodes.clear();
listVisualization.getChildren().clear();
isOperationRunning = false;
updateControlStates();
updateDisplay();
logOperation("CLEAR_ALL | List emptied");
});
}
}
private NodeElement createNodeElement(int value, int position) {
VBox nodeContainer = new VBox();
nodeContainer.setAlignment(javafx.geometry.Pos.CENTER);
nodeContainer.setSpacing(5);
nodeContainer.getStyleClass().add("list-node");
// Main node circle
Label valueLabel = new Label(String.valueOf(value));
valueLabel.getStyleClass().add("node-value");
valueLabel.setStyle("-fx-background-color: #3b82f6; -fx-text-fill: white; -fx-font-weight: bold; " +
"-fx-font-size: 16px; -fx-min-width: 50; -fx-min-height: 50; " +
"-fx-background-radius: 25; -fx-alignment: center;");
// Position label
Label posLabel = new Label("pos: " + position);
posLabel.setStyle("-fx-font-size: 10px; -fx-text-fill: #64748b;");
// Arrow (if not the last node)
Label arrowLabel = new Label("→");
arrowLabel.setStyle("-fx-font-size: 20px; -fx-text-fill: #64748b; -fx-font-weight: bold;");
nodeContainer.getChildren().addAll(valueLabel, posLabel);
return new NodeElement(nodeContainer, valueLabel, arrowLabel, value, position);
}
private void updateVisualization() {
Platform.runLater(() -> {
listVisualization.getChildren().clear();
for (int i = 0; i < visualNodes.size(); i++) {
NodeElement nodeElement = visualNodes.get(i);
listVisualization.getChildren().add(nodeElement.getContainer());
// Add arrow if not the last node
if (i < visualNodes.size() - 1) {
listVisualization.getChildren().add(nodeElement.getArrow());
}
}
// Add null terminator
if (!visualNodes.isEmpty()) {
Label nullLabel = new Label("null");
nullLabel.setStyle("-fx-font-size: 14px; -fx-text-fill: #94a3b8; -fx-font-style: italic;");
listVisualization.getChildren().add(nullLabel);
}
});
}
private void animateInsertAtBeginning(NodeElement nodeElement, Runnable onComplete) {
VBox container = nodeElement.getContainer();
container.setTranslateY(-100);
container.setOpacity(0);
container.setScaleX(0.5);
container.setScaleY(0.5);
listVisualization.getChildren().add(0, container);
ParallelTransition insertAnim = new ParallelTransition();
TranslateTransition slideDown = animationService.createTranslateTransition(container, 0, 0, 600);
FadeTransition fadeIn = animationService.createFadeAnimation(container, 0, 1, 600);
ScaleTransition scaleUp = animationService.createScaleTransition(container, 1.0, 1.0, 600);
insertAnim.getChildren().addAll(slideDown, fadeIn, scaleUp);
insertAnim.setOnFinished(e -> {
updateVisualization();
onComplete.run();
});
insertAnim.play();
}
private void animateInsertAtEnd(NodeElement nodeElement, Runnable onComplete) {
VBox container = nodeElement.getContainer();
container.setTranslateX(200);
container.setOpacity(0);
container.setScaleX(0.5);
container.setScaleY(0.5);
listVisualization.getChildren().add(container);
ParallelTransition insertAnim = new ParallelTransition();
TranslateTransition slideIn = animationService.createTranslateTransition(container, 0, 0, 600);
FadeTransition fadeIn = animationService.createFadeAnimation(container, 0, 1, 600);
ScaleTransition scaleUp = animationService.createScaleTransition(container, 1.0, 1.0, 600);
insertAnim.getChildren().addAll(slideIn, fadeIn, scaleUp);
insertAnim.setOnFinished(e -> {
updateVisualization();
onComplete.run();
});
insertAnim.play();
}
private void animateInsertAtPosition(NodeElement nodeElement, int position, Runnable onComplete) {
VBox container = nodeElement.getContainer();
container.setTranslateY(-100);
container.setOpacity(0);
container.setScaleX(0.5);
container.setScaleY(0.5);
// Insert at correct position in visualization
int visualIndex = position * 2; // Account for arrows
if (visualIndex < listVisualization.getChildren().size()) {
listVisualization.getChildren().add(visualIndex, container);
} else {
listVisualization.getChildren().add(container);
}
ParallelTransition insertAnim = new ParallelTransition();
TranslateTransition slideDown = animationService.createTranslateTransition(container, 0, 0, 600);
FadeTransition fadeIn = animationService.createFadeAnimation(container, 0, 1, 600);
ScaleTransition scaleUp = animationService.createScaleTransition(container, 1.0, 1.0, 600);
insertAnim.getChildren().addAll(slideDown, fadeIn, scaleUp);
insertAnim.setOnFinished(e -> {
updateVisualization();
onComplete.run();
});
insertAnim.play();
}
private void animateDelete(NodeElement nodeElement, int position, Runnable onComplete) {
VBox container = nodeElement.getContainer();
ParallelTransition deleteAnim = new ParallelTransition();
ScaleTransition shrink = animationService.createScaleTransition(container, 0, 0, 500);
FadeTransition fadeOut = animationService.createFadeAnimation(container, 1, 0, 500);
TranslateTransition moveUp = animationService.createTranslateTransition(container, 0, -100, 500);
RotateTransition rotate = new RotateTransition(Duration.millis(500), container);
rotate.setByAngle(360);
deleteAnim.getChildren().addAll(shrink, fadeOut, moveUp, rotate);
deleteAnim.setOnFinished(e -> {
listVisualization.getChildren().remove(container);
onComplete.run();
});
deleteAnim.play();
}
private void animateSearch(int value, SearchCallback callback) {
Timeline searchAnim = new Timeline();
int position = listModel.search(value);
// Animate through each node
for (int i = 0; i < visualNodes.size(); i++) {
final int index = i;
KeyFrame searchFrame = new KeyFrame(
Duration.millis((i + 1) * 300),
e -> {
highlightSearchNode(index);
if (index == visualNodes.size() - 1 || (position != -1 && index == position)) {
Platform.runLater(() -> callback.onSearchComplete(position));
}
}
);
searchAnim.getKeyFrames().add(searchFrame);
if (position != -1 && i == position) {
break;
}
}
searchAnim.play();
}
private void animateClear(Runnable onComplete) {
if (visualNodes.isEmpty()) {
onComplete.run();
return;
}
ParallelTransition clearAnim = new ParallelTransition();
for (int i = 0; i < visualNodes.size(); i++) {
VBox container = visualNodes.get(i).getContainer();
TranslateTransition moveOut = animationService.createTranslateTransition(container, -300, 0, 600);
moveOut.setDelay(Duration.millis(i * 100));
FadeTransition fadeOut = animationService.createFadeAnimation(container, 1, 0, 600);
fadeOut.setDelay(Duration.millis(i * 100));
RotateTransition rotate = new RotateTransition(Duration.millis(600), container);
rotate.setByAngle(720);
rotate.setDelay(Duration.millis(i * 100));
ParallelTransition nodeAnim = new ParallelTransition(moveOut, fadeOut, rotate);
clearAnim.getChildren().add(nodeAnim);
}
clearAnim.setOnFinished(e -> onComplete.run());
clearAnim.play();
}
private void updateNodePositions() {
for (int i = 0; i < visualNodes.size(); i++) {
NodeElement node = visualNodes.get(i);
node.setPosition(i);
// Update position label
Label posLabel = (Label) ((VBox) node.getContainer()).getChildren().get(1);
posLabel.setText("pos: " + i);
}
}
private int findNodePosition(int value) {
for (int i = 0; i < visualNodes.size(); i++) {
if (visualNodes.get(i).getValue() == value) {
return i;
}
}
return -1;
}
private void highlightNode(int position) {
if (position >= 0 && position < visualNodes.size()) {
VBox container = visualNodes.get(position).getContainer();
Label valueLabel = visualNodes.get(position).getValueLabel();
Timeline highlight = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(valueLabel.styleProperty(),
"-fx-background-color: #10b981; -fx-text-fill: white; -fx-font-weight: bold; " +
"-fx-font-size: 16px; -fx-min-width: 50; -fx-min-height: 50; " +
"-fx-background-radius: 25; -fx-alignment: center;")),
new KeyFrame(Duration.millis(1000), new KeyValue(valueLabel.styleProperty(),
"-fx-background-color: #3b82f6; -fx-text-fill: white; -fx-font-weight: bold; " +
"-fx-font-size: 16px; -fx-min-width: 50; -fx-min-height: 50; " +
"-fx-background-radius: 25; -fx-alignment: center;"))
);
ScaleTransition pulse = animationService.createScaleTransition(container, 1.2, 1.2, 300);
pulse.setAutoReverse(true);
pulse.setCycleCount(2);
ParallelTransition highlightAnim = new ParallelTransition(highlight, pulse);
highlightAnim.play();
}
}
private void highlightSearchNode(int position) {
if (position >= 0 && position < visualNodes.size()) {
Label valueLabel = visualNodes.get(position).getValueLabel();
Timeline searchHighlight = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(valueLabel.styleProperty(),
"-fx-background-color: #f59e0b; -fx-text-fill: white; -fx-font-weight: bold; " +
"-fx-font-size: 16px; -fx-min-width: 50; -fx-min-height: 50; " +
"-fx-background-radius: 25; -fx-alignment: center;")),
new KeyFrame(Duration.millis(500), new KeyValue(valueLabel.styleProperty(),
"-fx-background-color: #3b82f6; -fx-text-fill: white; -fx-font-weight: bold; " +
"-fx-font-size: 16px; -fx-min-width: 50; -fx-min-height: 50; " +
"-fx-background-radius: 25; -fx-alignment: center;"))
);
searchHighlight.play();
}
}
private void updateDisplay() {
Platform.runLater(() -> {
if (statusLabel != null) {
statusLabel.setText(listModel.isEmpty() ?
"LinkedList is Empty" :
"LinkedList has " + listModel.size() + " elements");
}
if (sizeLabel != null) {
sizeLabel.setText("Size: " + listModel.size());
}
updateVisualization();
updateControlStates();
});
}
private void updateControlStates() {
boolean isEmpty = listModel.isEmpty();
boolean isRunning = isOperationRunning ;
if (insertBeginButton != null) insertBeginButton.setDisable(isRunning);
if (insertEndButton != null) insertEndButton.setDisable(isRunning);
if (insertPositionButton != null) insertPositionButton.setDisable(isRunning);
if (deleteValueButton != null) deleteValueButton.setDisable(isEmpty || isRunning);
if (deletePositionButton != null) deletePositionButton.setDisable(isEmpty || isRunning);
if (searchButton != null) searchButton.setDisable(isEmpty || isRunning);
if (clearButton != null) clearButton.setDisable(isEmpty || isRunning);
if (inputField != null) inputField.setDisable(isRunning);
if (positionField != null) positionField.setDisable(isRunning);
if (searchField != null) searchField.setDisable(isRunning);
}
public void logOperation(String operation) {
if (operationHistory != null) {
operationHistory.appendText(operation + "\n");
operationHistory.setScrollTop(Double.MAX_VALUE);
}
System.out.println("LinkedList Operation: " + operation);
}
@FXML
private void goBack() {
try {
if (currentAnimation != null) {
currentAnimation.stop();
}
Stage currentStage = (Stage) listVisualization.getScene().getWindow();
// ✅ Use hide() instead of close() for back navigation
currentStage.hide();
System.out.println("LinkedList module hidden (can be reopened)");
} catch (Exception e) {
System.err.println("Error hiding LinkedList window: " + e.getMessage());
}
}
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void showInfo(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
// Callback interface for search
@FunctionalInterface
private interface SearchCallback {
void onSearchComplete(int position);
}
// Inner class for visual node elements
private static class NodeElement {
private final VBox container;
private final Label valueLabel;
private final Label arrow;
private final int value;
private int position;
public NodeElement(VBox container, Label valueLabel, Label arrow, int value, int position) {
this.container = container;
this.valueLabel = valueLabel;
this.arrow = arrow;
this.value = value;
this.position = position;
}
public VBox getContainer() { return container; }
public Label getValueLabel() { return valueLabel; }
public Label getArrow() { return arrow; }
public int getValue() { return value; }
public int getPosition() { return position; }
public void setPosition(int position) { this.position = position; }
}
}