org.apache.tapestry.form
Class ListEditMap

java.lang.Object
  extended by org.apache.tapestry.form.ListEditMap

public class ListEditMap
extends Object

A utility class often used with the ListEdit component. A ListEditMap is loaded with data objects before the ListEdit renders, and again before the ListEdit rewinds. This streamlines the synchronization of the form against data on the server. It is most useful when the set of objects is of a manageable size (say, no more than a few hundred objects).

The map stores a list of keys, and relates each key to a value. It also tracks a deleted flag for each key.

Usage:
The page or component should implement PageBeginRenderListener and implement PageBeginRenderListener.pageBeginRender(org.apache.tapestry.event.PageEvent) to initialize the map.

The external data (from which keys and values are obtained) is queried, and each key/value pair is added to the map, in the order that items should be presented.

The ListEdit's source parameter should be bound to the map's keys property. The value parameter should be bound to the map's key property.

The ListEdit's listener parameter should be bound to a listener method to synchronize a property of the component from the map. public void synchronize() { ListEditMap map = ...; Type object = (Type)map.getValue(); if (object == null) ... setProperty(object); }

You may also connect a Checkbox's selected parameter to the map's deleted property.

You may track inclusion in other sets by subclassing ListEditMap and implementing new boolean properties. The accessor method should be a call to checkSet(Set) and the mutator method should be a call to updateSet(Set, boolean).

Since:
3.0
Author:
Howard Lewis Ship

Constructor Summary
ListEditMap()
           
 
Method Summary
 void add(Object key, Object value)
          Records the key and value into this map.
protected  boolean checkSet(Set set)
          Returns true if the set contains the current key.
protected  List convertSetToList(Set set)
          Invoked to convert a set into a List.
 List getAllValues()
          Returns all the values stored in the map, in the order in which values were added to the map using add(Object, Object).
 List getDeletedKeys()
          Returns the deleted keys in an unspecified order.
 Object getKey()
          Returns the current key within the map.
 List getKeys()
          Returns a List of keys, in the order that keys were added to the map (using add(Object, Object).
 Object getValue()
          Returns the value for the key (set using setKey(Object)).
 List getValues()
          Returns all the values stored in the map, excluding those whose id has been marked deleted, in the order in which values were added to the map using add(Object, Object).
 boolean isDeleted()
          Returns true if the current key is in the set of deleted keys.
 void purgeDeletedKeys()
          Removes keys and values that are in the set of deleted keys, then clears the set of deleted keys.
 void setDeleted(boolean value)
          Adds or removes the current key from the set of deleted keys.
 void setKey(Object key)
          Sets the key for the map.
protected  Set updateSet(Set set, boolean value)
          Updates the set, adding or removing the current key from it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListEditMap

public ListEditMap()
Method Detail

add

public void add(Object key,
                Object value)
Records the key and value into this map. The keys may be obtained, in the order in which they are added, using getKeys(). This also sets the current key (so that you may invoke setDeleted(boolean), for example).


getKeys

public List getKeys()
Returns a List of keys, in the order that keys were added to the map (using add(Object, Object). The caller must not modify the List.


setKey

public void setKey(Object key)
Sets the key for the map. This defines the key used with the other methods: getValue(), isDeleted(), setDeleted(boolean).


getKey

public Object getKey()
Returns the current key within the map.


getValue

public Object getValue()
Returns the value for the key (set using setKey(Object)). May return null if no such key has been added (this can occur if a data object is deleted between the time a form is rendered and the time a form is submitted).


isDeleted

public boolean isDeleted()
Returns true if the current key is in the set of deleted keys.


checkSet

protected boolean checkSet(Set set)
Returns true if the set contains the current key. Returns false is the set is null, or doesn't contain the current key.


setDeleted

public void setDeleted(boolean value)
Adds or removes the current key from the set of deleted keys.


updateSet

protected Set updateSet(Set set,
                        boolean value)
Updates the set, adding or removing the current key from it. Returns the set passed in. If the value is true and the set is null, an new instance of HashSet is created and returned.


getDeletedKeys

public List getDeletedKeys()
Returns the deleted keys in an unspecified order. Returns a List, which may be empty if no keys have been deleted.


purgeDeletedKeys

public void purgeDeletedKeys()
Removes keys and values that are in the set of deleted keys, then clears the set of deleted keys. After invoking this method, getValues() and getAllValues() will return equivalent lists and getKeys() will no longer show any of the deleted keys. Note that this method does not change the current key. Subclasses that track additional key sets may want to override this method to remove deleted keys from those key sets.


convertSetToList

protected List convertSetToList(Set set)
Invoked to convert a set into a List.

Parameters:
set - a set (which may be empty or null)
Returns:
a list (possibly empty) of the items in the set

getAllValues

public List getAllValues()
Returns all the values stored in the map, in the order in which values were added to the map using add(Object, Object).


getValues

public List getValues()
Returns all the values stored in the map, excluding those whose id has been marked deleted, in the order in which values were added to the map using add(Object, Object).



Copyright © 2006-2011 Apache Software Foundation. All Rights Reserved.