Hi Pedro,

Thanks for all your help so far. locationsInput.listAsSet() indeed returns
the data that is persisted. To try something else, I have created an IModel
class called LocationsModel as below and I've changed the concerning bit to:

listChoice.setDefaultModel(new LocationsModel(new
Model(details),details.getLocations()));
//listChoice.setModelObject(locationsInput.listAsSet());

Unfortunately the result is the same: the data is persisted correctly into
my database, but the checkboxes are not checked when the page is reloaded or
reopened. I can confirm that details.getLocations() contains all the
locations. Btw, details.getlocations returns a HasSet.

   private static class LocationsModel implements IModel {
        /** the selected sites. */
       private IModel locationContainingModel;
       private Set<Location> locations;

       public LocationsModel(IModel locationContainingModel, Set<Location>
locations){
           this.locationContainingModel = locationContainingModel;
           this.locations = locations;
       }

       public Object getObject() {
           Details details = (Details) locationContainingModel.getObject();
           return details.getLocations();
       }

       public void setObject(Object object) {
           Details details = (Details) locationContainingModel.getObject();
           details.setLocations((Set<Location>)object);
       }

       public void detach() {
           locationContainingModel.detach();
       }
   }

On Tue, Mar 15, 2011 at 12:22 PM, Pedro Santos <pedros...@gmail.com> wrote:

> Hi Sjoerd, double check the listChoice. If you want it presenting the
> persisted data, you should use an model containg it, does the
> locationsInput.listAsSet() give you that?
>
> change
>
> listChoice.setDefaultModel(new
> CompoundPropertyModel<LocationsInput>(locationsInput));
> listChoice.setModelObject(locationsInput.listAsSet());
>
> to
>
> listChoice.setDefaultModel(modelContainingCollectionOfPersistedData);
>
> In the example the Input type has an instance variable named: "sites". This
> is exactly the wicket id of the CheckBoxMultipleChoice.
>
> class Input {
>  public List sites = new ArrayList();
> }
>
> final Input input = new Input();
> setModel(new CompoundPropertyModel(input));
> CheckBoxMultipleChoice listChoice = new CheckBoxMultipleChoice("sites",
> SITES);  <-- the model object of thi component will be the "sites" list at
> the Input
>
> On Tue, Mar 15, 2011 at 1:00 PM, Sjoerd Smeets <ssme...@gmail.com> wrote:
>
>> Hi Pedro,
>>
>> I've changed the code as you suggested and the result is the same. The
>> data is persisted correctly (as it was in previous cases as well), however
>> the check boxes are not checked when the page is reopened. Just to be clear,
>>  the locations variable of LocationsInput is set properly when
>>
>> final LocationsInput locationsInput = new LocationsInput(details);
>>
>> is being called. I can also confirm this when I check the variables is
>> debug mode and when I add some extra logging.
>>
>> Am I correct that the checkboxes should be checked when
>> setDefaultModel(new
>> CompoundPropertyModel<LocationsInput>(locationsInput));
>> is called?
>>
>> I understand the purpose of CompoundPropertyModels and PropertyModels. In
>> the example code 
>> (example<http://wicketstuff.org/wicket13/compref/?wicket:bookmarkablePage=:org.apache.wicket.examples.compref.CheckBoxMultipleChoicePage>),
>> the CompoundPropertyModel is used, but it is not exactly clear why to me.
>>
>> Could it be that the problem is that in the example an ArrayList of
>> Strings is returned and in my code an ArrayList of Locations is returned by
>> the LocationInput class?
>>
>> Alternatively, is there an other way to manually check the checkboxes via
>> loop or something? Performance is of less concern.
>>
>> Thanks again,
>> Sjoerd
>>
>> On Tue, Mar 15, 2011 at 11:03 AM, Pedro Santos <pedros...@gmail.com>wrote:
>>
>>> Your code is not saving the collection of checked locations,
>>>
>>> change
>>>
>>>  details.setLocations(locationsInput.listAsSet());
>>> ...
>>> up.persist(details);
>>>
>>> to
>>>
>>> details.setLocations(listChoice.getModelObject())
>>> ...
>>> up.persist(details);
>>>
>>> and it will start to use the collection in the CheckBoxMultipleChoice
>>> model, so the persisted data will be the user input, and not an new
>>> collection created inside the listAsSet method.
>>>
>>> there is good info about CPM in:
>>> https://cwiki.apache.org/WICKET/working-with-wicket-models.html#WorkingwithWicketmodels-CompoundPropertyModels
>>>
>>> On Tue, Mar 15, 2011 at 10:54 AM, Sjoerd Smeets <ssme...@gmail.com>wrote:
>>>
>>>> Hi Pedro,
>>>>
>>>> Thanks for your reply. I'm not sure if I understand what you mean with "use
>>>> nested components id as property expressions". The persisting part works
>>>> fine with this code, however checking the correct boxes when the details
>>>> page opens does not work. I have tried the following:
>>>> ====
>>>>
>>>> setDefaultModel(new CompoundPropertyModel<LocationsInput>(locationsInput));
>>>> ====
>>>>
>>>> listChoice.setDefaultModel(new 
>>>> CompoundPropertyModel<LocationsInput>(locationsInput));
>>>> ====
>>>>
>>>> listChoice.setDefaultModel(new 
>>>> CompoundPropertyModel<LocationsInput>(locationsInput));
>>>> listChoice.setModelObject(locationsInput.listAsSet());
>>>>
>>>> and they all persist the check checkboxes correctly when the user hits
>>>> the submit button, but when reopening the page, the checkboxes are not
>>>> checked. What is the easiest way to do this?
>>>>
>>>> Thanks again,
>>>> Sjoerd
>>>>
>>>> On Tue, Mar 15, 2011 at 7:40 AM, Pedro Santos <pedros...@gmail.com>wrote:
>>>>
>>>>> Hi, this code is weird:
>>>>>
>>>>>
>>>>> listChoice.setDefaultModel(new 
>>>>> CompoundPropertyModel<LocationsInput>(locationsInput));
>>>>>        listChoice.setModelObject(locationsInput.listAsSet());
>>>>>
>>>>> because CompoundPropertyModel is useful to use nested components id as
>>>>> property expressions
>>>>>
>>>>> If details in form submit code is an LocationsInput, then the user
>>>>> input will never be considered.
>>>>>
>>>>> At listAsSet an new collection is created and passed as parameter to
>>>>> listChoice:
>>>>> class LocationsInput {
>>>>>         public HashSet<Location> listAsSet(){
>>>>>            return new HashSet<Location>(locations);
>>>>>        }
>>>>> }
>>>>> And a new one is used to be persisted:
>>>>> onSubmit{
>>>>>     details.setLocations(locationsInput.listAsSet());
>>>>>     up.persist(details);
>>>>> }
>>>>>
>>>>> On Mon, Mar 14, 2011 at 2:53 PM, Sjoerd Smeets <ssme...@gmail.com>wrote:
>>>>>
>>>>>>  Hi,
>>>>>>
>>>>>> I'm facing an issue where I'm not able to check the checkboxes of a
>>>>>> list of
>>>>>> Locations that are already have been persisted in the Details object
>>>>>> (similar like the preselection of checkboxes). I followed the
>>>>>> CheckBoxMultipleChoicePage example
>>>>>> (example<
>>>>>> http://wicketstuff.org/wicket13/compref/?wicket:bookmarkablePage=:org.apache.wicket.examples.compref.CheckBoxMultipleChoicePage
>>>>>> >
>>>>>>
>>>>>> ).
>>>>>>
>>>>>> So basically when a user wants to edit their detail page, it should
>>>>>> select
>>>>>> the checkboxes of the locations the user already selected. saving the
>>>>>> form
>>>>>> works fine, so when I select locations, it is persisted correctly to
>>>>>> the
>>>>>> details object. Could someone indicate what I am missing?
>>>>>>
>>>>>> I have the following code:
>>>>>>
>>>>>> public class DetailsPage extends Panel {
>>>>>>
>>>>>>    ....
>>>>>>    private final List<Location> LOCATIONS = getLocs();
>>>>>>
>>>>>>    public void generateForm(){
>>>>>>
>>>>>>        final LocationsInput locationsInput = new
>>>>>> LocationsInput(details);
>>>>>>        add(new FeedbackPanel("feedBack"));
>>>>>>        ....
>>>>>>
>>>>>>        Form editDetails = new Form("detailsForm"){
>>>>>>
>>>>>>            protected void onSubmit() {
>>>>>>
>>>>>>                detailsPersistence up = new detailsPersistence();
>>>>>>                details.setLocations(locationsInput.listAsSet());
>>>>>>                logger.info("Saving the following locs:
>>>>>> {}",locationsInput.toString());
>>>>>>                if(newdetails){
>>>>>>                    up.editDetails(details);
>>>>>>                } else {
>>>>>>                    up.updatedetails(details);
>>>>>>                }
>>>>>>                setResponsePage(OverViewDetailssPage.class);
>>>>>>            }
>>>>>>        };
>>>>>>
>>>>>>        ...
>>>>>>        CheckBoxMultipleChoice<Location> listChoice =
>>>>>>                new CheckBoxMultipleChoice<Location>("locations",
>>>>>> LOCATIONS);
>>>>>>        listChoice.setDefaultModel(new
>>>>>> CompoundPropertyModel<LocationsInput>(locationsInput));
>>>>>>        listChoice.setModelObject(locationsInput.listAsSet());
>>>>>>        editDetails.add(listChoice);
>>>>>>
>>>>>>        add(editDetails);
>>>>>>
>>>>>>    }
>>>>>>
>>>>>>       private static class LocationsInput implements IClusterable {
>>>>>>        /** the selected sites. */
>>>>>>        public List<Location> locations = new ArrayList();
>>>>>>
>>>>>>        public LocationsInput(){
>>>>>>            Location roermond = new Location();
>>>>>>            roermond.setName("Roermond");
>>>>>>            locations.add(roermond);
>>>>>>        }
>>>>>>
>>>>>>        /** adds pre-selected items to the choices list */
>>>>>>        public LocationsInput(details details)
>>>>>>        {
>>>>>>
>>>>>>            Set<Location> locs = details.getLocations();
>>>>>>            for(Location loc : locs){
>>>>>>                logger.info("details has {} as a
>>>>>> location.",loc.getName());
>>>>>>                locations.add(loc);
>>>>>>            }
>>>>>>        }
>>>>>>
>>>>>>        @Override
>>>>>>        public String toString(){
>>>>>>            return "locations = " + listAsString(locations);
>>>>>>        }
>>>>>>
>>>>>>        private String listAsString(List<Location> list){
>>>>>>            StringBuffer b = new StringBuffer();
>>>>>>            for (Iterator<Location> i = list.iterator(); i.hasNext();){
>>>>>>                b.append(i.next().getName());
>>>>>>                if(i.hasNext()){
>>>>>>                    b.append(", ");
>>>>>>                }
>>>>>>            }
>>>>>>            return b.toString();
>>>>>>        }
>>>>>>
>>>>>>        public String[] listAsStringArray(){
>>>>>>            String[] locNames = new String[locations.size()];
>>>>>>            int i =0;
>>>>>>            for(Location loc : locations){
>>>>>>                locNames[i] = loc.getName();
>>>>>>                i++;
>>>>>>            }
>>>>>>            for(String l : locNames){
>>>>>>                logger.info("String array: {}",l);
>>>>>>            }
>>>>>>            return locNames;
>>>>>>        }
>>>>>>
>>>>>>        public HashSet<Location> listAsSet(){
>>>>>>            return new HashSet<Location>(locations);
>>>>>>        }
>>>>>>    }
>>>>>>
>>>>>>    private List<Location> getLocs(){
>>>>>>        LocationPersistence up4 = new LocationPersistence();
>>>>>>        return up4.getAllLocations();
>>>>>>    }
>>>>>>
>>>>>> }
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Pedro Henrique Oliveira dos Santos
>>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Pedro Henrique Oliveira dos Santos
>>>
>>
>>
>
>
> --
> Pedro Henrique Oliveira dos Santos
>

Reply via email to