Hello,

I'm trying to build a Property Edit Block for a Class that stores
collections of items of different types.
In all the examples I found using ajaxformloop it was possible to access
straight to the list or collection to add or remove elements. Or there was
the possibility to use the OnSubmit event because it was all coded inside a
page.

But using EditBlockContribution I don't know how should I access to the
context to add/remove data to these collections. And from where I tried I
received a runtime exceptions: No object of type
org.apache.tapestry5.services.PropertyEditContext is available from the
Environment

Is there a way to retrieve the context at some point to add, remove or edit
the data for this case?

Thanks in advance

Code example:

/* Each ItemTypeX extends the abstract ItemType and adds a different set of
properties needed */
public class Items {
   private Collection<ItemTypeA> itemsTypeA;
   private Collection<ItemTypeB> itemsTypeB;
   /* constructors */
   /* getters & sertters */
}

Added in *AppModule*:
/*...*/
configuration.add(Items.class, "Items");
/*...*/
configuration.add(new EditBlockContribution("Items",
"ItemsPropertyEditBlocks", "Items"));
/*...*/

*ItemsPropertyEditBlocks.tml:*
<div xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
   <t:block t:id="Items">
       A-Types:
       <t:ajaxformloop t:id="ajaxFormAType" source="itemsTypeA"
value="itemTypeA" encoder="encoderTypeA">
           <t:beaneditor t:object="itemTypeA"
t:model="modelItemTypeA"></t:beaneditor>
           <t:removerowlink>remove</t:removerowlink>
       </t:ajaxformloop>
       B-Type:  
       < !-- same as TypeA -- >
</div>

*ItemsPropertyEditBlocks.java*
/* imports */
public class ItemsPropertyEditBlocks {

   @Property
   @Environmental
   private PropertyEditContext context;

   @Inject
   private BeanModelSource beanModelSource;

   @Inject
   private Messages messages;

   /*
    * Item Types A
    */
   @Property
   private ItemTypeA itemTypeA;

   public Collection<ItemTypeA> getItemsTypeA(){
       return ((Items)context.getPropertyValue()).getItemsTypeA();
   }

   public BeanModel<?> getModelItemTypeA(){
       return beanModelSource.createEditModel(ItemTypeA.class, messages);
   }

   private HashMap<Long, ItemTypeA> mapA = new HashMap<Long, ItemTypeA>();

   public ValueEncoder<ItemTypeA> getEncoderTypeA(){
       return new ValueEncoder<ItemTypeA>() {
           
           public String toClient(ItemTypeA item) {
               mapA.put(item.getItemId(), item);
               return item.getItemId().toString();
           }
           
           public ItemTypeA toValue(String itemId) {
               return mapA.get(new Long(itemId));
           }
       };
   }

   private static long newIds = -1;
   @OnEvent(component="ajaxFormAType",value=EventConstants.ADD_ROW)
   public Object addRowTypeA(){
       ItemTypeA newItem = new ItemTypeA();
       newItem.setItemId(new Long(newIds--));
       return newItem;
   }

   @OnEvent(component="ajaxFormAType",value=EventConstants.REMOVE_ROW)
   public void removeRowTypeA(ItemTypeA itemA){
       mapB.remove(itemA);
   }

   /*
    * Item Types B
    */
   /* same as TypeA */
}

--
View this message in context: 
http://tapestry.1045711.n5.nabble.com/EditBlockContribution-for-a-class-that-contains-Collections-tp4990059p4990059.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to