|
|
@@ -4,15 +4,21 @@ This library exists to combine together two objectives in updating UI objects:
|
|
|
2) Allow editting of a shared resource from multiple potential call sites without needing a controller object that holds references to every single interested view.
|
|
|
|
|
|
This is a useful construct in the situation where you have a large number of fixed GUI components in a program (for example, rendering the various statistics of a Dungeons and Dragons (c) character), because the connections are made anonymously.
|
|
|
+
|
|
|
## Requirements
|
|
|
+
|
|
|
- Java 7
|
|
|
- org.leumasjaffe.event
|
|
|
- Google Guava
|
|
|
+
|
|
|
## Installation
|
|
|
### Steps
|
|
|
-1) Download or clone this repository.
|
|
|
-2) mvn install
|
|
|
-3) Add the below block to your pom.xml
|
|
|
+
|
|
|
+1. Download or clone this repository.
|
|
|
+2. Open a console window in the directory
|
|
|
+3. `mvn install`
|
|
|
+4. Add the below block to your pom.xml
|
|
|
+
|
|
|
### Maven Artifact
|
|
|
``` xml
|
|
|
<dependency>
|
|
|
@@ -21,35 +27,38 @@ This is a useful construct in the situation where you have a large number of fix
|
|
|
</dependency>
|
|
|
```
|
|
|
## Example Code Snippets
|
|
|
+
|
|
|
+### ObservableListener
|
|
|
+
|
|
|
``` java
|
|
|
-class SpellBook extends Observable {
|
|
|
- int spellSlotsRemaining, maxSpellSlots;
|
|
|
- List<List<Spell>> spellsAtLevel;
|
|
|
-}
|
|
|
-
|
|
|
-class SpellBookUI extends JPanel {
|
|
|
- ObservableListener<JTextField, SpellBook> listener;
|
|
|
- SpellBookUI(SpellBook model) {
|
|
|
- JTextField spellsRemaining = new JTextField();
|
|
|
- add(spellsRemaining);
|
|
|
-
|
|
|
- // Set up a listener to anonymously update when model changes
|
|
|
- listener = new ObservableListener<>(spellsRemaining, (text, book) -> {
|
|
|
- text.setText(book.getSpellSlotsRemaining());
|
|
|
- });
|
|
|
-
|
|
|
- // Automatically invokes callback, setting the text field
|
|
|
- listener.setObserved(model);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-class CastSpellCommand {
|
|
|
- SpellBook book;
|
|
|
- void cast(Spell spell) {
|
|
|
- // ...
|
|
|
- --book.spellSlotsRemaining;
|
|
|
- // Will automatically invoke the code in the ObservableListener above
|
|
|
- ObserverDispatch.notifySubscribers(book);
|
|
|
- }
|
|
|
-}
|
|
|
+ class SpellBook extends Observable {
|
|
|
+ int spellSlotsRemaining, maxSpellSlots;
|
|
|
+ List<List<Spell>> spellsAtLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ class SpellBookUI extends JPanel {
|
|
|
+ ObservableListener<JTextField, SpellBook> listener;
|
|
|
+ SpellBookUI(SpellBook model) {
|
|
|
+ JTextField spellsRemaining = new JTextField();
|
|
|
+ add(spellsRemaining);
|
|
|
+
|
|
|
+ // Set up a listener to anonymously update when model changes
|
|
|
+ listener = new ObservableListener<>(spellsRemaining, (text, book) -> {
|
|
|
+ text.setText(book.getSpellSlotsRemaining());
|
|
|
+ });
|
|
|
+
|
|
|
+ // Automatically invokes callback, setting the text field
|
|
|
+ listener.setObserved(model);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class CastSpellCommand {
|
|
|
+ SpellBook book;
|
|
|
+ void cast(Spell spell) {
|
|
|
+ // ...
|
|
|
+ --book.spellSlotsRemaining;
|
|
|
+ // Will automatically invoke the code in the ObservableListener above
|
|
|
+ ObserverDispatch.notifySubscribers(book);
|
|
|
+ }
|
|
|
+ }
|
|
|
```
|