Graphic Rendering System 1.0
A Java-based graphic rendering system implementing various design patterns
Loading...
Searching...
No Matches
CommandManager.java
Go to the documentation of this file.
1package com.example.graphics.command;
2
3import java.util.Stack;
4
9public class CommandManager {
10 private final Stack<Command> undoStack;
11 private final Stack<Command> redoStack;
12
16 public CommandManager() {
17 undoStack = new Stack<>();
18 redoStack = new Stack<>();
19 }
20
26 command.execute();
27 undoStack.push(command);
28 redoStack.clear(); // Clear redo stack when a new command is executed
29 }
30
35 public boolean undo() {
36 if (undoStack.isEmpty()) {
37 return false;
38 }
39
41 command.undo();
42 redoStack.push(command);
43 return true;
44 }
45
50 public boolean redo() {
51 if (redoStack.isEmpty()) {
52 return false;
53 }
54
56 command.execute();
57 undoStack.push(command);
58 return true;
59 }
60
65 public boolean canUndo() {
66 return !undoStack.isEmpty();
67 }
68
73 public boolean canRedo() {
74 return !redoStack.isEmpty();
75 }
76
80 public void clearHistory() {
81 undoStack.clear();
82 redoStack.clear();
83 }
84}