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 Command interface with execute() and undo() methods
  • Create concrete commands:
    • AddShapeCommand - stores shape reference, removes on undo
    • DeleteShapesCommand - stores shapes list, re-adds on undo
    • MoveShapeCommand - stores shape, original pos, new pos
    • ChangeColorCommand - stores shape, old/new colors
    • ResizeCommand - stores shape, old/new bounds
    • GroupCommand - composite for batch operations

2. CommandManager class

  • Maintains two stacks: undoStack, redoStack
  • execute(Command): push to undoStack, clear redoStack
  • undo(): pop from undoStack, call undo(), push to redoStack
  • redo(): 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), do commandManager.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)