Hi

I was busy so couldn't write an example for you. I had never used this
library directly but I have read its source when I started learning tapestry
5 months ago and it is a great great help. A lot of my components are just a
modification of these components( at times the same component but
reimplemented to learn).

Here is your example.

The class file.

package com.google.code.tapestryaddons.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.OptionGroupModel;
import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.internal.OptionModelImpl;
import org.apache.tapestry5.util.AbstractSelectModel;
import org.chenillekit.tapestry.core.encoders.MultipleValueEncoder;

import com.google.code.tapestryaddons.MyObject;

public class TestPage {
   //Selected list
   @Property
   @Persist(PersistenceConstants.FLASH)
   private List<MyObject> selectedList;

   //List to supply data from
   @Property
   private List<MyObject> myList;

   void onActivate(){
      //Initialize the selected list
      if(selectedList == null){
         selectedList = new ArrayList<MyObject>();
      }

      //Fill dummy data. In your case you have to load data
      if(myList == null){
         //Dummy Data
         myList = new ArrayList<MyObject>();
         for(int i = 0; i < 10; ++i){
            myList.add(new MyObject(i, "MyObject #" + i));
         }
      }
   }

   //Log the result
   void onSuccess(){
      System.out.println(selectedList);
   }

   //Encoder
   public MultipleValueEncoder<MyObject> getEncoder(){
      return new MultipleValueEncoder<MyObject>(){

         public String toClient(MyObject object) {
            return object.getId().toString();
         }

         public List<MyObject> toValue(String[] values) {
            List<MyObject> objects = new ArrayList<MyObject>();
            for(String value: values){
               for(MyObject myObject: myList)
                  if(myObject.getId().equals(Integer.parseInt(value))){
                     objects.add(myObject);
                  }
               }

            return objects;
         }

      };
   }

   //Model
   public SelectModel getModel(){
      return new AbstractSelectModel(){

         public List<OptionGroupModel> getOptionGroups() {
            return null;
         }

         public List<OptionModel> getOptions() {
            final List<OptionModel> options = new ArrayList<OptionModel>();
            for(final MyObject myObject: myList){
               options.add(new OptionModelImpl(myObject.getName(),
myObject));
            }
            return options;
         }

      };
   }
}


The template is

<html xmlns:t='http://tapestry.apache.org/schema/tapestry_5_1_0.xsd'>
   <body>
      <t:form>
         <t:ck.MultipleSelect value='selectedList' model='model'
encoder='encoder'/>
         <input type='submit' value='submit'/>
      </t:form>
   </body>
</html>

and finally MyObject is a simple pojo

package com.google.code.tapestryaddons;

public class MyObject {
   private Integer id;
   private String name;

   public MyObject(Integer id, String name){
      this.id = id;
      this.setName(name);
   }

   public Integer getId(){
      return id;
   }

   public void setId(Integer id){
      this.id = id;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getName() {
      return name;
   }

   @Override
   public int hashCode(){
      return id.hashCode();
   }

   @Override
   public boolean equals(Object o){
      if(o == null || !(o instanceof MyObject)){
         return false;
      }

      MyObject myObject = (MyObject)o;
      return id.equals(myObject.getId());
   }

   @Override
   public String toString(){
      return name + "(" + id + ")";
   }
}


Hope it helps

regards
Taha





On Mon, Apr 18, 2011 at 11:51 PM, TG <tapestry...@hotmail.com> wrote:

> ANymore kind soul out there that  can help?
>
> Thanks.
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Tapestry-5-2-5-Select-component-s-multiple-property-tp4304338p4311399.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