Page:
Undo Redo Stack
Clone
1
Undo Redo Stack
Thibaud edited this page 2026-03-19 22:40:54 +01:00
Plan: Undo/Redo Stack
Overview
Implement undo/redo functionality with a command history stack.
Current State
- Controller handles shape operations (add, delete, move, color change)
- No command history exists
- Selection system tracks selected shapes
Implementation Plan
1. Command pattern
- Create
Commandinterface withexecute()andundo()methods - Create concrete commands:
AddShapeCommand- stores shape reference, removes on undoDeleteShapesCommand- stores shapes list, re-adds on undoMoveShapeCommand- stores shape, original pos, new posChangeColorCommand- stores shape, old/new colorsResizeCommand- stores shape, old/new boundsGroupCommand- composite for batch operations
2. CommandManager class
- Maintains two stacks:
undoStack,redoStack execute(Command): push to undoStack, clear redoStackundo(): pop from undoStack, call undo(), push to redoStackredo(): pop from redoStack, call execute(), push to undoStack- Limit stack size (e.g., 50 commands)
3. Controller integration
- Replace direct shape operations with commands
- Example: instead of
collection.add(shape), docommandManager.execute(new AddShapeCommand(shape, collection))
4. Keyboard shortcuts
- Ctrl+Z: undo
- Ctrl+Y: redo (or Ctrl+Shift+Z)
- Update menu with these shortcuts
5. Menu additions
- Add Edit menu items: Undo, Redo
- Enable/disable based on stack state
Files to create/modify
- Create:
commands/Command.java,commands/CommandManager.java - Create:
commands/AddShapeCommand.java,commands/DeleteShapesCommand.java,commands/MoveShapeCommand.java, etc. - Modify:
ui/Controller.java,App.java(menu)
Edge cases
- Copy/paste after undo: clear redo stack
- Multi-select delete: single compound command
- Application close: no persistence needed (in-memory only)