Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.form;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
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 org.apache.tapestry.event.PageBeginRenderListener and implement org.apache.tapestry.event.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(java.util.Set) and the mutator method should be a call to updateSet(java.util.Set,boolean).

Author(s):
Howard Lewis Ship
Since:
3.0
 
 
 public class ListEditMap
 {
     private Map _map = new HashMap();
 
     private List _keys = new ArrayList();
 
     private Set _deletedKeys;
 
     private Object _currentKey;

    
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).
 
 
     public void add(Object keyObject value)
     {
          = key;
 
         .add();
         .put(value);
     }

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

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

    
Returns the current key within the map.
    public Object getKey()
    {
        return ;
    }

    
Returns the value for the key (set using setKey(java.lang.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).
    public Object getValue()
    {
        return .get();
    }

    
Returns true if the current key is in the set of deleted keys.
    public boolean isDeleted()
    {
        return checkSet();
    }

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

    
Adds or removes the current key from the set of deleted keys.
    public void setDeleted(boolean value)
    {
         = updateSet(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 java.util.HashSet is created and returned.
    protected Set updateSet(Set setboolean value)
    {
        Set updatedSet = set;
        if (value)
        {
            if (updatedSet == null)
                updatedSet = new HashSet();
            updatedSet.add();
        }
        else
        {
            if (updatedSet != null)
                updatedSet.remove();
        }
        return updatedSet;
    }

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

    
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.
    public void purgeDeletedKeys()
    {
        if ( == null)
            return;
        .keySet().removeAll();
        .removeAll();
         = null;
    }

    
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
    protected List convertSetToList(Set set)
    {
        if (Tapestry.isEmpty(set))
            return .;
        return new ArrayList(set);
    }

    
Returns all the values stored in the map, in the order in which values were added to the map using add(java.lang.Object,java.lang.Object).
    public List getAllValues()
    {
        int count = .size();
        List result = new ArrayList(count);
        for (int i = 0; i < counti++)
        {
            Object key = .get(i);
            Object value = .get(key);
            result.add(value);
        }
        return result;
    }

    
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(java.lang.Object,java.lang.Object).
    public List getValues()
    {
        int deletedCount = Tapestry.size();
        if (deletedCount == 0)
            return getAllValues();
        int count = .size();
        List result = new ArrayList(count - deletedCount);
        for (int i = 0; i < counti++)
        {
            Object key = .get(i);
            if (.contains(key))
                continue;
            Object value = .get(key);
            result.add(value);
        }
        return result;
    }
New to GrepCode? Check out our FAQ X