An element list backed by elements in a document.
Users must specify the type of the
abstract list element and the tag name to be used when creating list items.
An @link
Initializer interprets abstract initial state into document
element attributes. A
Factory adapts document elements to create
abstract list elements.
Use delegation to implement a document based element
list. Create your list that implements the
ElementList interface; at
the construction time, create an instance of this class, then use delegation
for all methods of the
ElementList interface.
If you are using an observable element list, the order of events, when adding
a new element, is as follows:
-
A new element is created and inserted into the document (no calls, yet)
-
The factory supplied at the construction time has the
Factory#adapt(DocumentEventRouter,Object) method called.
-
The element created by the factory is recorded in in-memory data
structure.
-
The
ObservableElementList.Listener#onValueAdded(Object)method is called
In particular this means you can safely call
#get(int) method in
the observable list listener.
A simple example of use:
class MyElement {
class Initialiser {
public final String value;
Initialiser(String value) {
this.value = value;
}
}
private String value;
MyElement(String value) {
this.value = value;
}
}
DBEL.Factory factory = new DBEL.Factory() {
MyElement adapt(ObservableMutableDocument doc, E container) {
return new MyElement(doc.getElementAttribute(container, "myattribute"));
}
}
DBEL.Initialiser initialiser = new DBEL.initialiser() {
public Map makeInitialState(MyElement.Initialiser initialState) {
if (initialState != null) {
return Collections. singletonMap("myattribute", initialState.value);
}
return Collections. emptyMap();
}
}
ElementList list = DocumentBasedElementList.create(doc, container, "mytag",
factory, initialiser);
MyElement newElement = list.add("newvalue");