-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndoRedoManager.java
More file actions
62 lines (54 loc) · 1.67 KB
/
UndoRedoManager.java
File metadata and controls
62 lines (54 loc) · 1.67 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
import java.util.LinkedList;
public class UndoRedoManager {
private LinkedList<Task> history;
private int currentStateIndex;
public UndoRedoManager() {
this.history = new LinkedList<>();
this.currentStateIndex = -1;
}
public void saveState(Task rootTask) {
while (history.size() > currentStateIndex + 1) {
history.removeLast();
}
history.add(cloneTaskTree(rootTask));
currentStateIndex++;
}
public Task undo() {
if (canUndo()) {
currentStateIndex--;
System.out.println("Aksi dibatalkan (Undo).");
return cloneTaskTree(history.get(currentStateIndex));
}
System.out.println("Tidak ada aksi untuk di-undo.");
return null;
}
public Task redo() {
if (canRedo()) {
currentStateIndex++;
System.out.println("Aksi diulangi (Redo).");
return cloneTaskTree(history.get(currentStateIndex));
}
System.out.println("Tidak ada aksi untuk di-redo.");
return null;
}
public boolean canUndo() {
return currentStateIndex > 0;
}
public boolean canRedo() {
return currentStateIndex < history.size() - 1;
}
private Task cloneTaskTree(Task original) {
if (original == null) return null;
Task clone = new Task(
original.getId(),
original.getName(),
original.getDescription(),
original.getPriority(),
original.getDeadline()
);
for (Task subTask : original.getSubTasks()) {
clone.addSubTask(cloneTaskTree(subTask));
}
return clone;
}
}