E
- the type of the elements contained in the Listpublic abstract class ObservableListBase<E> extends java.util.AbstractList<E> implements ObservableList<E>
ObservableList
implementations.
The base class provides two functionalities for the implementing classes.
addListener
and removeListener
methods.
fireChange(javafx.collections.ListChangeListener.Change)
method is provided
for notifying the listeners with a Change
object.
ListChangeListener.Change
object. There are various methods called
next*
, like nextAdd(int, int)
for new items in the lists or nextRemove(int, java.lang.Object)
for
an item being removed from the list.
These methods must be always enclosed in beginChange()
and endChange()
block.
See the example below.
public void removeOddIndexes() { beginChange(); try { for (int i = 1; i < size(); ++i) { remove(i); } } finally { endChange(); } } public void remove(int i) { beginChange(); try { E removed = ... //do some stuff that will actually remove the element at index i nextRemove(i, removed); } finally { endChange(); } }The
try
/finally
blocks in the example are needed only if there's a possibility for an exception to occur
inside a beginChange()
/ endChange()
block
Note: If you want to create modifiable ObservableList
implementation, consider
using ModifiableObservableListBase
as a superclass.
Note: In order to create list with sequential access, you should override AbstractList.listIterator()
,
AbstractList.iterator()
methods and use them in AbstractList.get(int)
, AbstractCollection.size()
and other methods accordingly.
ObservableList
,
ListChangeListener.Change
,
ModifiableObservableListBase
Modifier and Type | Field and Description |
---|---|
private ListChangeBuilder<E> |
changeBuilder |
private <any> |
listenerHelper |
Constructor and Description |
---|
ObservableListBase() |
Modifier and Type | Method and Description |
---|---|
boolean |
addAll(E... elements)
A convenience method for var-arg addition of elements.
|
void |
addListener(InvalidationListener listener)
Add a listener to this observable list.
|
protected void |
beginChange()
Begins a change block.
|
protected void |
endChange()
Ends the change block.
|
protected void |
fireChange(ListChangeListener.Change<? extends E> change)
Notifies all listeners of a change
|
protected boolean |
hasListeners()
Returns true if there are some listeners registered for this list.
|
protected void |
nextAdd(int from,
int to)
Adds a new add operation to the change.
|
protected void |
nextPermutation(int from,
int to,
int[] perm)
Adds a new permutation operation to the change.
|
protected void |
nextRemove(int idx,
E removed)
Adds a new remove operation to the change with single item removed.
|
protected void |
nextRemove(int idx,
java.util.List<? extends E> removed)
Adds a new remove operation to the change with multiple items removed.
|
protected void |
nextReplace(int from,
int to,
java.util.List<? extends E> removed)
Adds a new replace operation to the change.
|
protected void |
nextSet(int idx,
E old)
Adds a new set operation to the change.
|
protected void |
nextUpdate(int pos)
Adds a new update operation to the change.
|
void |
remove(int from,
int to)
A simplified way of calling
sublist(from, to).clear() . |
boolean |
removeAll(E... elements)
A convenience method for var-arg usage of the
removeAll method. |
void |
removeListener(InvalidationListener listener)
Tries to remove a listener from this observable list.
|
boolean |
retainAll(E... elements)
A convenience method for var-arg usage of the
retainAll method. |
boolean |
setAll(java.util.Collection<? extends E> col)
Clears the ObservableList and adds all elements from the collection.
|
boolean |
setAll(E... elements)
Clears the ObservableList and adds all the elements passed as var-args.
|
add, add, addAll, clear, equals, get, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
filtered, sorted, sorted
add, add, addAll, addAll, clear, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray
private <any> listenerHelper
private final ListChangeBuilder<E> changeBuilder
protected final void nextUpdate(int pos)
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
pos
- the position in the list where the updated element resides.protected final void nextSet(int idx, E old)
nextRemove(idx); nextAdd(idx, idx + 1);
.
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
idx
- the index of the item that was setold
- the old value at the idx
position.protected final void nextReplace(int from, int to, java.util.List<? extends E> removed)
nextRemove(from, removed); nextAdd(from, to);
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
from
- the index where the items were replacedto
- the end index (exclusive) of the range where the new items resideremoved
- the list of items that were removedprotected final void nextRemove(int idx, java.util.List<? extends E> removed)
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
idx
- the index where the items were removedremoved
- the list of items that were removedprotected final void nextRemove(int idx, E removed)
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
idx
- the index where the item was removedremoved
- the item that was removedprotected final void nextPermutation(int from, int to, int[] perm)
"i"
contains the index, where the item from the index "i"
was moved.
It's not necessary to provide the smallest permutation possible. It's correct to always call this method
with nextPermutation(0, size(), permutation);
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
from
- marks the beginning (inclusive) of the range that was permutatedto
- marks the end (exclusive) of the range that was permutatedperm
- the permutation in that range. Even if from != 0
, the array should
contain the indexes of the list. Therefore, such permutation would not contain indexes of range (0, from)
protected final void nextAdd(int from, int to)
Note: needs to be called inside beginChange()
/ endChange()
block.
Note: needs to reflect the current state of the list.
from
- marks the beginning (inclusive) of the range that was addedto
- marks the end (exclusive) of the range that was addedprotected final void beginChange()
next*
methods is called.
For every beginChange()
, there must be a corresponding endChange()
call.
beginChange()
calls can be nested in a beginChange()
/endChange()
block.
endChange()
protected final void endChange()
ObservableList
, the
Change
is constructed and all listeners are notified.
Ending a nested block doesn't fire a notification.
beginChange()
public final void addListener(InvalidationListener listener)
ObservableList
addListener
in interface ObservableList<E>
listener
- the listener for listening to the list changespublic final void removeListener(InvalidationListener listener)
ObservableList
removeListener
in interface ObservableList<E>
listener
- a listener to removeprotected final void fireChange(ListChangeListener.Change<? extends E> change)
change
- an object representing the change that was doneprotected final boolean hasListeners()
public boolean addAll(E... elements)
ObservableList
addAll
in interface ObservableList<E>
elements
- the elements to addpublic boolean setAll(E... elements)
ObservableList
setAll
in interface ObservableList<E>
elements
- the elements to setpublic boolean setAll(java.util.Collection<? extends E> col)
ObservableList
setAll
in interface ObservableList<E>
col
- the collection with elements that will be added to this observableArrayListpublic boolean removeAll(E... elements)
ObservableList
removeAll
method.removeAll
in interface ObservableList<E>
elements
- the elements to be removedpublic boolean retainAll(E... elements)
ObservableList
retainAll
method.retainAll
in interface ObservableList<E>
elements
- the elements to be retainedpublic void remove(int from, int to)
ObservableList
sublist(from, to).clear()
. As this is a common operation,
ObservableList has this method for convenient usage.remove
in interface ObservableList<E>
from
- the start of the range to remove (inclusive)to
- the end of the range to remove (exclusive)