An open API service indexing awesome lists of open source software.

https://github.com/tharindu714/code-editor-memento

A sleek, dark-themed Java Swing code editor with robust Undo/Redo powered by the Memento Pattern.
https://github.com/tharindu714/code-editor-memento

code java memento-pattern oodp

Last synced: 5 months ago
JSON representation

A sleek, dark-themed Java Swing code editor with robust Undo/Redo powered by the Memento Pattern.

Awesome Lists containing this project

README

          

# πŸŒ’ Code-Editor-Memento

> A sleek, dark-themed Java Swing code editor with robust **Undo/Redo** powered by the **Memento** Pattern.

---

## πŸ“ Scenario

> You are building an **online code editor** that lets users write and edit code in real-time. It needs **Undo** and **Redo** functionality without exposing internal state or hogging memory.


image

---

## βš™οΈ Features

* **Dark Mode UI**: Eye-friendly dark background with light-colored monospaced font.
* **Memento-based History**: Encapsulated state snapshots enable efficient undo/redo.
* **Stack-based Navigation**: Unlimited undo/redo operations via two history stacks.
* **Fluent GUI Controls**: Toolbar buttons for seamless user interaction.

---

## πŸ“Έ GUI Screenshot


image


---

## πŸ“ UML Class Diagram


image

> *Generate and paste UML diagram here.*

---

## πŸ“– Memento Pattern Theory

The **Memento** Pattern provides the ability to restore an object to its previous state without exposing its internal structure. It involves three key participants:

1. **Originator** 🏭

* Holds the current state of the editor document (`String state`).
* Can create a **Memento** capturing its state and restore from it.

2. **Memento** 🎁

* A simple, immutable value object that stores the internal state snapshot.
* Does **not** expose its internals beyond what the Originator needs.

3. **Caretaker** 🀡

* Manages saving and retrieving Memento objects.
* Maintains two stacks: one for **undo** and one for **redo** history.
* Controls when to clear redo history to preserve correct behavior.

### How It Works in Our App

1. **On Every Edit**: A new Memento is saved to the **undo** stack. The **redo** stack is cleared.
2. **Undo** πŸ”™: Pop the top Memento from undo β†’ push it to redo β†’ restore the previous snapshot.
3. **Redo** πŸ”œ: Pop the top Memento from redo β†’ restore it β†’ push it back on undo.
4. **Encapsulation**: The editor’s text content is never exposed; only the Originator knows its state.

---

## πŸš€ Usage

```bash
# Clone the repo
git clone https://github.com/Tharindu714/Code-Editor-Memento.git
# Open in your favorite IDE
gradle run # or mvn compile exec:java
```

---

## 🀝 Contributing

Pull requests and issues are welcome! Feel free to improve docs, add features, or optimize performance.

---

## πŸ“„ License

This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.