Repository: https://github.com/Tharindu714/Memento-Text-Editor-Application.git
A sleek, colourful Java Swing demo that implements the Memento design pattern to provide reliable multi-step undo/redo for a text editor. Snapshots (mementos) are captured on demand and automatically (debounced), allowing users to safely revert to any previous editing state.
- ✅ Memento Pattern:
Snapshot(memento),EditorOriginator(originator),HistoryManager(caretaker). - ⏱️ Debounced auto-save: snapshots are created after idle so typing isn’t saved on every keystroke.
- 🔁 Multi-level Undo / Redo: step backward or forward through saved snapshots; double-click a snapshot to restore.
- 🎛️ Attractive UI: smart-home–inspired gradient header, editor pane with monospace font, history panel, shortcuts (Ctrl+Z / Ctrl+Y / Ctrl+S).
- 🛡️ Anti-pattern rules applied: immutable mementos, capped history to avoid memory bloat, decoupled UI and history management.
- Manual snapshot save and automatic snapshot on idle.
- Undo / Redo buttons and keyboard shortcuts (Ctrl+Z / Ctrl+Y).
- History list with timestamps and short previews; double-click to restore any snapshot.
- Configurable history cap (default 60).
- Status messages show snapshot actions and undo/redo feedback.
Requires Java 8+.
# from repo root
javac TextEditor_Memento_GUI.java
java TextEditor_Memento_GUIThe app opens a colourful Smart Editor window. Snapshots are stored in memory (demo). For persistence add disk-write logic in HistoryManager.
Core components
Snapshot— Memento: immutable object that stores the editor content and timestamp.EditorOriginator— Originator: knows how to create and restoreSnapshots from current editor content.HistoryManager— Caretaker: stores a list of snapshots, the current index, and supports undo/redo and pruning to a maximum size.EditorFrame— Swing UI: editor area, history panel, controls and header.
Flow
- User edits text → after idle (debounce) or on manual save →
Originator.createSnapshot()→HistoryManager.add(snapshot). - Undo →
HistoryManager.undo()returns previous snapshot →Originator.restore(snapshot)→ UI updates text area. - Redo similarly returns later snapshot.
- ❌ No exposing mutable editor internals — snapshots are immutable and only
Originatorcreates/restores them. - ✅ No unlimited history — history is capped (default 60); oldest snapshots removed when cap exceeds.
- ✅ No saving every keystroke — debounce implemented (1.2s) to limit snapshot frequency.
- ✅ Separation of concerns —
HistoryManagerdoes not directly manipulate UI, only storesSnapshotobjects.
Paste into https://www.plantuml.com/plantuml to render diagrams.
- Launch the app.
- Type some text, wait 1–2 seconds to allow auto-snapshot, then continue editing.
- Click Undo or press
Ctrl+Zto step back to previous snapshots. - Double-click any item in the history list to restore that specific snapshot.
- Store snapshots as diffs (deltas) or compress snapshots for very large docs to reduce memory.
- Persist snapshots to disk or cloud for session recovery.
- Make
HistoryManagerthread-safe and add pruning policies (LRU, time-based). - Add visual diff preview when hovering a snapshot in history.
Fork, add features (persistence, diff-snapshots, collaborative undo), and open PRs. Include tests for undo/redo correctness.
MIT — reuse and adapt freely.
Made with care — Tharindu's Memento Text Editor demo. Happy editing! 🎉


