com.jgoodies.binding.list
Class ListHolder

java.lang.Object
  extended by com.jgoodies.binding.beans.Model
      extended by com.jgoodies.binding.list.ListHolder
All Implemented Interfaces:
Observable, Serializable, ListModel
Direct Known Subclasses:
SelectionInList2

public class ListHolder
extends Model
implements ListModel

A ListModel implementation that looks up its elements from a List held by a ValueModel. This class provides public convenience methods for firing ListDataEvents, see the methods #fireContentsChanged, #fireIntervalAdded, and #fireIntervalRemoved.

The current class name "ListHolder" and accessor #getListHolder may confuse users: what's the difference between the class (a ListModel) and its list holder (a ValueModel)?

TODO: Find a better name and provide a transition to the new class name. I consider using "IndirectList" as the new name. The Binding 1.2 may offer the new class and keep the old as deprecated version. In the Binding 1.3 (or 2.0) the deprecated class can be removed.

Since:
1.1
Version:
$Revision: 1.8 $
Author:
Karsten Lentzsch
See Also:
ListModelHolder, SelectionInList2, Serialized Form

Field Summary
protected  List list
          Holds a copy of the listHolder's value.
static String PROPERTYNAME_LIST
          The name of the bound read-write list property.
static String PROPERTYNAME_LIST_HOLDER
          The name of the bound read-write listHolder property.
 
Constructor Summary
ListHolder()
          Constructs a ListHolder with an empty initial ArrayList.
ListHolder(List list)
          Constructs a ListHolder on the given List.
ListHolder(ValueModel listHolder)
          Constructs a ListHolder on the given List.
 
Method Summary
 void addListDataListener(ListDataListener l)
          Adds a listener to the list that's notified each time a change to the data model occurs.
 void fireContentsChanged(int index0, int index1)
          Notifies all registered ListDataListeners that the contents of one or more list elements has changed.
 void fireIntervalAdded(int index0, int index1)
          Notifies all registered ListDataListeners that one or more elements have been added to this SelectionInList's List/ListModel.
 void fireIntervalRemoved(int index0, int index1)
          Notifies all registered ListDataListeners that one or more elements have been removed from this SelectionInList's List/ListModel.
protected  void fireListChanged(int oldLastIndex, int newLastIndex)
          Notifies all registered ListDataListeners that this ListModel has changed from an old list to a new list content.
 Object getElementAt(int index)
          Returns the value at the specified index.
 List getList()
          Returns the contents of the list holder.
 ListDataListener[] getListDataListeners()
          Returns an array of all the list data listeners registered on this SelectionInList.
 ValueModel getListHolder()
          Returns the ValueModel that holds the List we delegate to.
 int getSize()
          Returns the length of the list, 0 if the list is null.
protected  int getSize(List aListOrNull)
          Returns the lists size or 0 if the list is null.
 boolean isEmpty()
          Checks and answers if the list is empty or null.
 void removeListDataListener(ListDataListener l)
          Removes a listener from the list that's notified each time a change to the data model occurs.
 void setList(List newList)
          Sets the given List as value of the list holder.
 void setListHolder(ValueModel newListHolder)
          Sets a new List holder.
protected  void updateList(List oldList, List newList)
          Fires a property change for list and a ListModel contents change event.
 
Methods inherited from class com.jgoodies.binding.beans.Model
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, equals, fireMulticastPropertyChange, fireMultiplePropertiesChanged, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTYNAME_LIST

public static final String PROPERTYNAME_LIST
The name of the bound read-write list property.

See Also:
Constant Field Values

PROPERTYNAME_LIST_HOLDER

public static final String PROPERTYNAME_LIST_HOLDER
The name of the bound read-write listHolder property.

See Also:
Constant Field Values

list

protected List list
Holds a copy of the listHolder's value. Used as the old List when the listHolder's value changes. Required because a ValueModel may use null as old value, but this ListModelHolder must know about the old and the new List.

Constructor Detail

ListHolder

public ListHolder()
Constructs a ListHolder with an empty initial ArrayList.


ListHolder

public ListHolder(List list)
Constructs a ListHolder on the given List.

Parameters:
list - the initial List

ListHolder

public ListHolder(ValueModel listHolder)
Constructs a ListHolder on the given List.

Parameters:
listHolder - provides the List
Throws:
NullPointerException - if the listHolder is null
ClassCastException - if the listHolder contents is not an instance of List
Method Detail

getList

public final List getList()
Returns the contents of the list holder.

Returns:
the contents of the list holder.
See Also:
setList(List)

setList

public final void setList(List newList)
Sets the given List as value of the list holder.

Parameters:
newList - the List to be set as new list content
See Also:
getList()

getListHolder

public final ValueModel getListHolder()
Returns the ValueModel that holds the List we delegate to.

Returns:
the ValueModel that holds the List we delegate to.

setListHolder

public final void setListHolder(ValueModel newListHolder)
Sets a new List holder. Does nothing if old and new holder are equal. Removes the list change handler from the old holder and adds it to the new one.

Parameters:
newListHolder - the list holder to be set
Throws:
NullPointerException - if the new list holder is null
ClassCastException - if the new list holder's value is not a List

isEmpty

public final boolean isEmpty()
Checks and answers if the list is empty or null.

Returns:
true if the list is empty or null, false otherwise

getSize

public final int getSize()
Returns the length of the list, 0 if the list is null.

Specified by:
getSize in interface ListModel
Returns:
the size of the list, 0 if the list is null

getElementAt

public final Object getElementAt(int index)
Returns the value at the specified index.

Specified by:
getElementAt in interface ListModel
Parameters:
index - the requested index
Returns:
the value at index.
Throws:
NullPointerException - if the list holder's content is null

addListDataListener

public final void addListDataListener(ListDataListener l)
Adds a listener to the list that's notified each time a change to the data model occurs.

Specified by:
addListDataListener in interface ListModel
Parameters:
l - the ListDataListener to be added

removeListDataListener

public final void removeListDataListener(ListDataListener l)
Removes a listener from the list that's notified each time a change to the data model occurs.

Specified by:
removeListDataListener in interface ListModel
Parameters:
l - the ListDataListener to be removed

getListDataListeners

public final ListDataListener[] getListDataListeners()
Returns an array of all the list data listeners registered on this SelectionInList.

Returns:
all of this model's ListDataListeners, or an empty array if no list data listeners are currently registered
See Also:
addListDataListener(ListDataListener), removeListDataListener(ListDataListener)

fireContentsChanged

public final void fireContentsChanged(int index0,
                                      int index1)
Notifies all registered ListDataListeners that the contents of one or more list elements has changed. The changed elements are specified by the closed interval index0, index1 -- the end points are included. Note that index0 need not be less than or equal to index1.

Parameters:
index0 - one end of the new interval
index1 - the other end of the new interval
See Also:
ListModel, ListDataListener, ListDataEvent

fireIntervalAdded

public final void fireIntervalAdded(int index0,
                                    int index1)
Notifies all registered ListDataListeners that one or more elements have been added to this SelectionInList's List/ListModel. The new elements are specified by a closed interval index0, index1 -- the end points are included. Note that index0 need not be less than or equal to index1.

Parameters:
index0 - one end of the new interval
index1 - the other end of the new interval
See Also:
ListModel, ListDataListener, ListDataEvent

fireIntervalRemoved

public final void fireIntervalRemoved(int index0,
                                      int index1)
Notifies all registered ListDataListeners that one or more elements have been removed from this SelectionInList's List/ListModel. index0 and index1 are the end points of the interval that's been removed. Note that index0 need not be less than or equal to index1.

Parameters:
index0 - one end of the removed interval, including index0
index1 - the other end of the removed interval, including index1
See Also:
ListModel, ListDataListener, ListDataEvent

updateList

protected void updateList(List oldList,
                          List newList)
Fires a property change for list and a ListModel contents change event.

Parameters:
oldList - the old List
newList - the new List

fireListChanged

protected final void fireListChanged(int oldLastIndex,
                                     int newLastIndex)
Notifies all registered ListDataListeners that this ListModel has changed from an old list to a new list content. If the old and new list have the same size, a content change event is fired. Otherwise, two events are fired: a remove event for the old list's interval, and a add event for the new list's interval.

This method is invoked by #updateList during the transition from an old List(Model) to a new List(Model).

Parameters:
oldLastIndex - the last index of the old list
newLastIndex - the last index of the new list

getSize

protected final int getSize(List aListOrNull)
Returns the lists size or 0 if the list is null.

Parameters:
aListOrNull - a List or null
Returns:
the list's size or 0 if the list is null


Copyright © 2002-2006 JGoodies Karsten Lentzsch. All Rights Reserved.