You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Johan Compagner <jc...@gmail.com> on 2008/04/01 08:39:33 UTC

Re: ListView with a Set instead of a List

1> because you dont have that detacheable list in the listview?? You
are just giving a ArrayList directly to the listview

2> i dont see exactly where you call getModelObject on in onsubmit but
i guess thats on page? Then it is loaded yes, this has nothing to do
with the listview model.

On 3/28/08, reikje <re...@matenga.de> wrote:
>
> I have a rather specific question about a problem I am having. We have a
> domain object (OperatorSyncConfig) that we need to diplay in a page. That
> object contain a Set of FunctionSetting objects which I want to render in a
> ListView. For each ListItem (FunctionSetting) it should be possible to
> change the timeout value and then with one Form submit save the whole
> OperatorSyncConfig objects. Here is a snippet of the objects:
>
> public class OperatorSyncConfig implements Serializable
> {
>     private Set<FunctionSetting> m_functionSettings = new
> LinkedHashSet<FunctionSetting>();
>
>     public Set<FunctionSetting> getFunctionSettings()
>     {
>         return Collections.unmodifiableSet(new
> LinkedHashSet<FunctionSetting>(m_functionSettings));
>     }
>
>     .. no setter method for the set, just single add and remove accessors
> }
>
> public class FunctionSetting implements Serializable
> {
>     private Long m_timeoutInMilliseconds;
>     .. get and set for that
> }
>
>
> The WebPage looks in short like that:
>
> public class EditOperatorPage extends WebPage
> {
>      LoadableDetachableModel model = new LoadableDetachableModel() { ... }
> // load the OperatorSyncConfig
>      setModel(model);
>
>      Form updateDeleteForm = new Form("updateForm") {
>             @Override
>             protected void onSubmit() {
>                    // getModelObject and save
>             }
>      };
>
>      OperatorSyncConfig operatorSyncConfig = (OperatorSyncConfig)
> getModelObject();
>      Set<FunctionSetting> settingsAsSet =
> operatorSyncConfig.getFunctionSettings();
>      List<FunctionSetting> settings = new
> ArrayList<FunctionSetting>(settingsAsSet);
>
>      updateDeleteForm.add(new ListView("operatorFunctionsList", settings)
>         {
>             @Override
>             protected void populateItem(ListItem item)
>             {
>                 FunctionSetting functionSetting = (FunctionSetting)
> item.getModelObject();
>
>                 item.add(new TextField("Timeout", new
> PropertyModel(functionSetting, "timeoutInMilliseconds")));
>             }
>         });
>
>        ....
> }
>
> The problem is that in the onSubmit of the Form I will always get the "old"
> version of the OperatorSyncConfig since getPage().getModelObject() in there
> will trigger LoadableDetachableModel.load() and get the object from the
> Database. If I debug, I see that the new value for timeoutInMilliseconds is
> bound properly to the FunctionSetting object in the model of the ListItem.
> However, the model of the whole ListView contains an ArrayList which itself
> contains different FunctionSetting objects (not the ones contained in the
> Page model OperatorSyncConfig.getFunctionSettings()). By different I mean
> they are equal but different objects in memory.
>
> Question is:
> 1) why are there different FunctionSetting objects in the Page model and the
> ListView model
> 2) why does onSubmit() call load() on the DetachableModel if form values
> have been bound already
> --
> View this message in context:
> http://www.nabble.com/ListView-with-a-Set-instead-of-a-List-tp16349670p16349670.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: ListView with a Set instead of a List

Posted by Al Maw <wi...@almaw.com>.
// Simply:
IModel listModel = new AbstractReadOnlyModel() {
    public Object getObject() {
        return new ArrayList(mySet);
    }
};

// Or if you want to wrap a property model (not that this is as nice):
IModel listModel = new PropertyModel(bean, "mySetHere") {
    public Object getObject() {
        return new ArrayList((Set)super.getObject());
    }
};

// Then just:
new ListView("foo", listModel) {
    public void populateItem(ListItem item) {
        // ...
    }
};



In your original code you go:
return Collections.unmodifiableSet(new
LinkedHashSet<FunctionSetting>(m_functionSettings))

Which seems rather complex. If you're going to give the user a copy, why do
you care if they modify it? If you're building this all just for Wicket,
then you're quite mad and should figure out a way to make this much simpler.

You ought to think about your API contracts. If you're relying on your Set
being sorted in order to have it make sense in Wicket (which it seems you
are, given you've implemented this using a LinkedHashSet internally) then
you should expose it as a SortedSet through the API.

You could then either use a RepeatingView to display the items (which would
be trivial) or write a little model wrapping class that converts a List to a
SortedSet and back again. If you're only going to use the set as a read-only
data source for Wicket, and you're going to update the underlying set object
in your other code (e.g. onClick handlers) then you can get away with just
wrapping it as in my provided code.

Regards,

Alastair

On Tue, Apr 1, 2008 at 6:03 PM, reikje <re...@matenga.de> wrote:

>
> 1> True, but why is it important that is has to be the same collection? I
> only care about changes being propagated to the ListItems, I don't care if
> they are contained in a Set within the Model or in a List given to the
> ListView. In other words, if you are saying, that you have to give the
> same
> List from the Model to the ListView, means you cannot render a Model
> object
> with a Set using a ListView. This is quite a restriction you have to be
> aware of then I think. We choosed the Set on purpose to avoid duplicates,
> there must be a way to render the contents of a Set using any Repeater
> component.
>
> 2> Yes, in my code snippet where it says "// getModelObject and save" this
> is very I call getModelObject().
>
>
>
> Johan Compagner wrote:
> >
> > 1> because you dont have that detacheable list in the listview?? You
> > are just giving a ArrayList directly to the listview
> >
> > 2> i dont see exactly where you call getModelObject on in onsubmit but
> > i guess thats on page? Then it is loaded yes, this has nothing to do
> > with the listview model.
> >
> > On 3/28/08, reikje <re...@matenga.de> wrote:
> >>
> >> I have a rather specific question about a problem I am having. We have
> a
> >> domain object (OperatorSyncConfig) that we need to diplay in a page.
> That
> >> object contain a Set of FunctionSetting objects which I want to render
> in
> >> a
> >> ListView. For each ListItem (FunctionSetting) it should be possible to
> >> change the timeout value and then with one Form submit save the whole
> >> OperatorSyncConfig objects. Here is a snippet of the objects:
> >>
> >> public class OperatorSyncConfig implements Serializable
> >> {
> >>     private Set<FunctionSetting> m_functionSettings = new
> >> LinkedHashSet<FunctionSetting>();
> >>
> >>     public Set<FunctionSetting> getFunctionSettings()
> >>     {
> >>         return Collections.unmodifiableSet(new
> >> LinkedHashSet<FunctionSetting>(m_functionSettings));
> >>     }
> >>
> >>     .. no setter method for the set, just single add and remove
> accessors
> >> }
> >>
> >> public class FunctionSetting implements Serializable
> >> {
> >>     private Long m_timeoutInMilliseconds;
> >>     .. get and set for that
> >> }
> >>
> >>
> >> The WebPage looks in short like that:
> >>
> >> public class EditOperatorPage extends WebPage
> >> {
> >>      LoadableDetachableModel model = new LoadableDetachableModel() {
> ...
> >> }
> >> // load the OperatorSyncConfig
> >>      setModel(model);
> >>
> >>      Form updateDeleteForm = new Form("updateForm") {
> >>             @Override
> >>             protected void onSubmit() {
> >>                    // getModelObject and save
> >>             }
> >>      };
> >>
> >>      OperatorSyncConfig operatorSyncConfig = (OperatorSyncConfig)
> >> getModelObject();
> >>      Set<FunctionSetting> settingsAsSet =
> >> operatorSyncConfig.getFunctionSettings();
> >>      List<FunctionSetting> settings = new
> >> ArrayList<FunctionSetting>(settingsAsSet);
> >>
> >>      updateDeleteForm.add(new ListView("operatorFunctionsList",
> settings)
> >>         {
> >>             @Override
> >>             protected void populateItem(ListItem item)
> >>             {
> >>                 FunctionSetting functionSetting = (FunctionSetting)
> >> item.getModelObject();
> >>
> >>                 item.add(new TextField("Timeout", new
> >> PropertyModel(functionSetting, "timeoutInMilliseconds")));
> >>             }
> >>         });
> >>
> >>        ....
> >> }
> >>
> >> The problem is that in the onSubmit of the Form I will always get the
> >> "old"
> >> version of the OperatorSyncConfig since getPage().getModelObject() in
> >> there
> >> will trigger LoadableDetachableModel.load() and get the object from the
> >> Database. If I debug, I see that the new value for
> timeoutInMilliseconds
> >> is
> >> bound properly to the FunctionSetting object in the model of the
> >> ListItem.
> >> However, the model of the whole ListView contains an ArrayList which
> >> itself
> >> contains different FunctionSetting objects (not the ones contained in
> the
> >> Page model OperatorSyncConfig.getFunctionSettings()). By different I
> mean
> >> they are equal but different objects in memory.
> >>
> >> Question is:
> >> 1) why are there different FunctionSetting objects in the Page model
> and
> >> the
> >> ListView model
> >> 2) why does onSubmit() call load() on the DetachableModel if form
> values
> >> have been bound already
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/ListView-with-a-Set-instead-of-a-List-tp16349670p16349670.html
> >> Sent from the Wicket - User mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> >> For additional commands, e-mail: users-help@wicket.apache.org
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/ListView-with-a-Set-instead-of-a-List-tp16349670p16418733.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: ListView with a Set instead of a List

Posted by reikje <re...@matenga.de>.
1> True, but why is it important that is has to be the same collection? I
only care about changes being propagated to the ListItems, I don't care if
they are contained in a Set within the Model or in a List given to the
ListView. In other words, if you are saying, that you have to give the same
List from the Model to the ListView, means you cannot render a Model object
with a Set using a ListView. This is quite a restriction you have to be
aware of then I think. We choosed the Set on purpose to avoid duplicates,
there must be a way to render the contents of a Set using any Repeater
component.

2> Yes, in my code snippet where it says "// getModelObject and save" this
is very I call getModelObject().  



Johan Compagner wrote:
> 
> 1> because you dont have that detacheable list in the listview?? You
> are just giving a ArrayList directly to the listview
> 
> 2> i dont see exactly where you call getModelObject on in onsubmit but
> i guess thats on page? Then it is loaded yes, this has nothing to do
> with the listview model.
> 
> On 3/28/08, reikje <re...@matenga.de> wrote:
>>
>> I have a rather specific question about a problem I am having. We have a
>> domain object (OperatorSyncConfig) that we need to diplay in a page. That
>> object contain a Set of FunctionSetting objects which I want to render in
>> a
>> ListView. For each ListItem (FunctionSetting) it should be possible to
>> change the timeout value and then with one Form submit save the whole
>> OperatorSyncConfig objects. Here is a snippet of the objects:
>>
>> public class OperatorSyncConfig implements Serializable
>> {
>>     private Set<FunctionSetting> m_functionSettings = new
>> LinkedHashSet<FunctionSetting>();
>>
>>     public Set<FunctionSetting> getFunctionSettings()
>>     {
>>         return Collections.unmodifiableSet(new
>> LinkedHashSet<FunctionSetting>(m_functionSettings));
>>     }
>>
>>     .. no setter method for the set, just single add and remove accessors
>> }
>>
>> public class FunctionSetting implements Serializable
>> {
>>     private Long m_timeoutInMilliseconds;
>>     .. get and set for that
>> }
>>
>>
>> The WebPage looks in short like that:
>>
>> public class EditOperatorPage extends WebPage
>> {
>>      LoadableDetachableModel model = new LoadableDetachableModel() { ...
>> }
>> // load the OperatorSyncConfig
>>      setModel(model);
>>
>>      Form updateDeleteForm = new Form("updateForm") {
>>             @Override
>>             protected void onSubmit() {
>>                    // getModelObject and save
>>             }
>>      };
>>
>>      OperatorSyncConfig operatorSyncConfig = (OperatorSyncConfig)
>> getModelObject();
>>      Set<FunctionSetting> settingsAsSet =
>> operatorSyncConfig.getFunctionSettings();
>>      List<FunctionSetting> settings = new
>> ArrayList<FunctionSetting>(settingsAsSet);
>>
>>      updateDeleteForm.add(new ListView("operatorFunctionsList", settings)
>>         {
>>             @Override
>>             protected void populateItem(ListItem item)
>>             {
>>                 FunctionSetting functionSetting = (FunctionSetting)
>> item.getModelObject();
>>
>>                 item.add(new TextField("Timeout", new
>> PropertyModel(functionSetting, "timeoutInMilliseconds")));
>>             }
>>         });
>>
>>        ....
>> }
>>
>> The problem is that in the onSubmit of the Form I will always get the
>> "old"
>> version of the OperatorSyncConfig since getPage().getModelObject() in
>> there
>> will trigger LoadableDetachableModel.load() and get the object from the
>> Database. If I debug, I see that the new value for timeoutInMilliseconds
>> is
>> bound properly to the FunctionSetting object in the model of the
>> ListItem.
>> However, the model of the whole ListView contains an ArrayList which
>> itself
>> contains different FunctionSetting objects (not the ones contained in the
>> Page model OperatorSyncConfig.getFunctionSettings()). By different I mean
>> they are equal but different objects in memory.
>>
>> Question is:
>> 1) why are there different FunctionSetting objects in the Page model and
>> the
>> ListView model
>> 2) why does onSubmit() call load() on the DetachableModel if form values
>> have been bound already
>> --
>> View this message in context:
>> http://www.nabble.com/ListView-with-a-Set-instead-of-a-List-tp16349670p16349670.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/ListView-with-a-Set-instead-of-a-List-tp16349670p16418733.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org