You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Bertrand Guay-Paquet <be...@step.polymtl.ca> on 2011/10/31 14:49:14 UTC

Dynamically choosing component to add

Hi,

Was is the proper way to choose which panel to add to a page based on a 
model's object value? Currently, I directly access the model and check 
the value during construction of the page. However, this is wrong since 
a model's object shouldn't be accessed until component rendering (see 
thread "LDMs load too early - hold outdated application data").

One obvious approach is to add all possible panels to the page and 
override their isVisible() method to inspect the model objects. This 
seems like wasted processing and memory so I'd like to avoid it.

The page's onConfigure() and onBeforeRender() methods are the candidates 
I can think of to inspect the model object's value and add the proper 
panel. Is one better or is there another solution? Are there other 
issues with this approach?

Regards,
Bertrand

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


Re: Dynamically choosing component to add

Posted by James Carman <ja...@carmanconsulting.com>.
What exactly are you trying to do?  Different view elements based on the
state of your model or its type.
On Oct 31, 2011 9:50 AM, "Bertrand Guay-Paquet" <be...@step.polymtl.ca>
wrote:

> Hi,
>
> Was is the proper way to choose which panel to add to a page based on a
> model's object value? Currently, I directly access the model and check the
> value during construction of the page. However, this is wrong since a
> model's object shouldn't be accessed until component rendering (see thread
> "LDMs load too early - hold outdated application data").
>
> One obvious approach is to add all possible panels to the page and
> override their isVisible() method to inspect the model objects. This seems
> like wasted processing and memory so I'd like to avoid it.
>
> The page's onConfigure() and onBeforeRender() methods are the candidates I
> can think of to inspect the model object's value and add the proper panel.
> Is one better or is there another solution? Are there other issues with
> this approach?
>
> Regards,
> Bertrand
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: AW: Dynamically choosing component to add

Posted by tech7 <te...@gmail.com>.
I have tried this example so it is working perfect :)
Thank you for your support.

-----
Wicket-Java
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4116037.html
Sent from the Users forum 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: Dynamically choosing component to add

Posted by Seçil Aydın <te...@gmail.com>.
Thank you very much Thomas, i will follow the way that you provided.

Also thank you for your response Ritesh.

Regards.

-----
Wicket-Java
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4119794.html
Sent from the Users forum 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: Dynamically choosing component to add

Posted by Ritesh Jharia <ri...@gmail.com>.
Hi,

you can use a RefreshingView, with the Model getting updated on the button
click, which will be storing the number of times the fields needs to be
added.
And in populateItem(), add the fields.
--
Ritesh

2011/11/29 Seçil Aydın <te...@gmail.com>

> Thomas and Hans, thank you for your responses.
>
> Mine issue is a little bit different, as you can see in the sample picture.
> When page is opened only one couple of textfield should be displayed.When
> user clicks on button, couple of textfields should be added to the page.Is
> there any way?
>
> http://apache-wicket.1842946.n4.nabble.com/file/n4118851/page_sample.png
>
> With my best regards.
>
>
>
> -----
> Wicket-Java
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4118851.html
> Sent from the Users forum 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: Dynamically choosing component to add

Posted by Thomas Götz <to...@richmountain.de>.
I created a short example (Quickstart): http://cl.ly/3u2o260z1b3F441T1T1M
You need to care for your models yourself (for the TextFields), just an example how to dynamically add components.

Cheers,
   -Tom


Seçil Aydın wrote:

> Thomas and Hans, thank you for your responses.
> 
> Mine issue is a little bit different, as you can see in the sample picture.
> When page is opened only one couple of textfield should be displayed.When
> user clicks on button, couple of textfields should be added to the page.Is
> there any way?
> 
> http://apache-wicket.1842946.n4.nabble.com/file/n4118851/page_sample.png 
> 
> With my best regards.
> 
> 
> 
> -----
> Wicket-Java
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4118851.html
> Sent from the Users forum 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: Dynamically choosing component to add

Posted by Seçil Aydın <te...@gmail.com>.
Thomas and Hans, thank you for your responses.

Mine issue is a little bit different, as you can see in the sample picture.
When page is opened only one couple of textfield should be displayed.When
user clicks on button, couple of textfields should be added to the page.Is
there any way?

http://apache-wicket.1842946.n4.nabble.com/file/n4118851/page_sample.png 

With my best regards.



-----
Wicket-Java
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4118851.html
Sent from the Users forum 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: Dynamically choosing component to add

Posted by Hans Lesmeister 2 <ha...@lessy-software.de>.
Hi, 


Thomas Götz wrote
> 
> final TextField textField = new TextField("textField", new Model());
> textField.setVisible(false);
> add(textField);
> ...
> 

Because textField is initially invisible, Don't forget to
textField.setOutputMarkupPlaceholderTag(true);


-----
-- 
Regards, 
Hans 

http://cantaa.de 

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4118782.html
Sent from the Users forum 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: Dynamically choosing component to add

Posted by Thomas Götz <to...@richmountain.de>.
If you only need one TextField that should be displayed upon click on a button:

final TextField textField = new TextField("textField", new Model());
textField.setVisible(false);
add(textField);

AjaxButton button = new AjaxButton("button") {
    @Override
    protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        textField.setVisible(true);
        target.add(textField);
    }

    @Override
    protected void onError(AjaxRequestTarget target, Form<?> form) {
    }
};
add(button);


If you need to display a new TextField upon *each* click on the button: use a repeater (e.g. RepeatingView and friends).

Cheers,
   -Tom



On 29.11.2011, 09:04h tech7 wrote:

> Many thanks.
> 
> I am also trying to add new textfield to the page when user clicks on a
> button.
> How can I do that? Is there any proper way?
> 
> 
> -----
> Wicket-Java
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4118343.html
> Sent from the Users forum 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: AW: Dynamically choosing component to add

Posted by tech7 <te...@gmail.com>.
Many thanks.

I am also trying to add new textfield to the page when user clicks on a
button.
How can I do that? Is there any proper way?


-----
Wicket-Java
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4118343.html
Sent from the Users forum 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: AW: Dynamically choosing component to add

Posted by Bertrand Guay-Paquet <be...@step.polymtl.ca>.
Hi,
> I think I just responded to you in your own thread - where I didn't know
> what you were trying to do.  I still don't claim to fully understand, but I
> think I better understand your question here.  If you mean that you have
> one panel that *may possibly contain any one of X panels*, then here's one
> solution:
>
> ContainingPanel.html:
>
> <wicket:panel>
>    Some markup here
>    Blah, blah, blah...
>    <div wicket:id="childPanel"></div>
>    More surrounding markup...
> </wicket:panel>
>
> ContainingPanel.java:
>
> class ContainingPanel extends Panel {
>
>    public void onBeforeRender() {
>      if(getModelObject() is of some type) {
>        addOrReplace(new SomePanel("childPanel"));
>      } else if(getModelObject() is of some other type) {
>        addOrReplace(new SomeOtherPanel("childPanel"));
>      }
>    }
> }
>
> You only need one wicket:id in your containing panel.  You just swap at
> runtime which component actually shows up in that spot.
>
I did almost exactly as Jeremy mentioned. Here is what I use:
   public void onBeforeRender() {
     Component component = get("childPanel");
     if(getModelObject() is of some type) {
       if(component instanceof SomePanel == false)
         addOrReplace(new SomePanel("childPanel"));
     } else if(getModelObject() is of some other type) {
       if(component instanceof SomeOtherPanel == false)
         addOrReplace(new SomeOtherPanel("childPanel"));
     }
   }

As you can see, the difference is that I don't replace the component if 
it's already of the correct type. This is important if SomePanel or 
SomeOtherPanel contains state. Without my modifications, when the parent 
container is rendered, the child panels will always be reset to its 
initial state.

Bertrand

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


Re: AW: Dynamically choosing component to add

Posted by Jeremy Thomerson <je...@wickettraining.com>.
On Fri, Nov 25, 2011 at 1:26 PM, Fabiosakiyam <fa...@gmail.com>wrote:

> Hi Bertrand,
>
> I have almost the same problem you do (i think, im still a newbie).
> I'm trying to create a component that creates one specific container,
> depending on it's type. So i'd like to add only this container. I'm having
> problem at the .html, since i don't know if i should declare all
> wicket:id="idContainer" or just the chosen one, since each container have
> it's own .html, how to 'link' the component html to the container .html.
>
> I want to know what you did on your .html, 'cause since your adding only
> the
> chosen panel to add, how you declared all possible panels on the .html? i
> wonder if you declared all panels on the .html or only the chosen one...


I think I just responded to you in your own thread - where I didn't know
what you were trying to do.  I still don't claim to fully understand, but I
think I better understand your question here.  If you mean that you have
one panel that *may possibly contain any one of X panels*, then here's one
solution:

ContainingPanel.html:

<wicket:panel>
  Some markup here
  Blah, blah, blah...
  <div wicket:id="childPanel"></div>
  More surrounding markup...
</wicket:panel>

ContainingPanel.java:

class ContainingPanel extends Panel {

  public void onBeforeRender() {
    if(getModelObject() is of some type) {
      addOrReplace(new SomePanel("childPanel"));
    } else if(getModelObject() is of some other type) {
      addOrReplace(new SomeOtherPanel("childPanel"));
    }
  }
}

You only need one wicket:id in your containing panel.  You just swap at
runtime which component actually shows up in that spot.

-- 
Jeremy Thomerson
http://wickettraining.com
*Need a CMS for Wicket?  Use Brix! http://brixcms.org*

Re: AW: Dynamically choosing component to add

Posted by Fabiosakiyam <fa...@gmail.com>.
Hi Bertrand,

I have almost the same problem you do (i think, im still a newbie).
I'm trying to create a component that creates one specific container,
depending on it's type. So i'd like to add only this container. I'm having
problem at the .html, since i don't know if i should declare all
wicket:id="idContainer" or just the chosen one, since each container have
it's own .html, how to 'link' the component html to the container .html.

I want to know what you did on your .html, 'cause since your adding only the
chosen panel to add, how you declared all possible panels on the .html? i
wonder if you declared all panels on the .html or only the chosen one...

Sorry for bad english,

Thanks.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Dynamically-choosing-component-to-add-tp3955869p4108449.html
Sent from the Users forum 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: AW: Dynamically choosing component to add

Posted by Bertrand Guay-Paquet <be...@step.polymtl.ca>.
Thank you Sven!

On 31/10/2011 11:19 AM, Sven Meier wrote:
> onBeforeRender() and addOrReplace() are the right combination. Note that
> according to the javadoc you're not supposed to alter the component
> hierarchy in onConfigure().
>
> Sven
>
> -----Ursprüngliche Nachricht-----
> Von: Bertrand Guay-Paquet [mailto:bernie@step.polymtl.ca]
> Gesendet: Montag, 31. Oktober 2011 15:35
> An: users@wicket.apache.org
> Betreff: Re: Dynamically choosing component to add
>
> Thanks for the answer, but that is not what I am looking for. I already
> know how to replace a component with another one.
>
> My question is: I need to access a model's object to know which panel to
> add to a page (a persisted user preference). Since I shouldn't access a
> model's object in a component's constructor, when (onBeforeRender,
> onConfigure) should I do it to add the proper panel to the page?
>
> Using addOrReplace could very well be part of the overall solution, but
> this does not answer my core question.
>
> On 31/10/2011 9:54 AM, manuelbarzi wrote:
>> MarkupContainer addOrReplace :?
>> .
>>
>>
>>
>> On Mon, Oct 31, 2011 at 2:49 PM, Bertrand Guay-Paquet<
>> bernie@step.polymtl.ca>   wrote:
>>
>>> Hi,
>>>
>>> Was is the proper way to choose which panel to add to a page based on a
>>> model's object value? Currently, I directly access the model and check
> the
>>> value during construction of the page. However, this is wrong since a
>>> model's object shouldn't be accessed until component rendering (see
> thread
>>> "LDMs load too early - hold outdated application data").
>>>
>>> One obvious approach is to add all possible panels to the page and
>>> override their isVisible() method to inspect the model objects. This
> seems
>>> like wasted processing and memory so I'd like to avoid it.
>>>
>>> The page's onConfigure() and onBeforeRender() methods are the candidates
> I
>>> can think of to inspect the model object's value and add the proper
> panel.
>>> Is one better or is there another solution? Are there other issues with
>>> this approach?
>>>
>>> Regards,
>>> Bertrand
>>>
>>> ------------------------------**------------------------------**---------
>>> To unsubscribe, e-mail:
> users-unsubscribe@wicket.**apache.org<us...@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
>
>
>
> ---------------------------------------------------------------------
> 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


AW: Dynamically choosing component to add

Posted by Sven Meier <sv...@meiers.net>.
onBeforeRender() and addOrReplace() are the right combination. Note that
according to the javadoc you're not supposed to alter the component
hierarchy in onConfigure().

Sven

-----Ursprüngliche Nachricht-----
Von: Bertrand Guay-Paquet [mailto:bernie@step.polymtl.ca] 
Gesendet: Montag, 31. Oktober 2011 15:35
An: users@wicket.apache.org
Betreff: Re: Dynamically choosing component to add

Thanks for the answer, but that is not what I am looking for. I already 
know how to replace a component with another one.

My question is: I need to access a model's object to know which panel to 
add to a page (a persisted user preference). Since I shouldn't access a 
model's object in a component's constructor, when (onBeforeRender, 
onConfigure) should I do it to add the proper panel to the page?

Using addOrReplace could very well be part of the overall solution, but 
this does not answer my core question.

On 31/10/2011 9:54 AM, manuelbarzi wrote:
> MarkupContainer addOrReplace :?
> .
>
>
>
> On Mon, Oct 31, 2011 at 2:49 PM, Bertrand Guay-Paquet<
> bernie@step.polymtl.ca>  wrote:
>
>> Hi,
>>
>> Was is the proper way to choose which panel to add to a page based on a
>> model's object value? Currently, I directly access the model and check
the
>> value during construction of the page. However, this is wrong since a
>> model's object shouldn't be accessed until component rendering (see
thread
>> "LDMs load too early - hold outdated application data").
>>
>> One obvious approach is to add all possible panels to the page and
>> override their isVisible() method to inspect the model objects. This
seems
>> like wasted processing and memory so I'd like to avoid it.
>>
>> The page's onConfigure() and onBeforeRender() methods are the candidates
I
>> can think of to inspect the model object's value and add the proper
panel.
>> Is one better or is there another solution? Are there other issues with
>> this approach?
>>
>> Regards,
>> Bertrand
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail:
users-unsubscribe@wicket.**apache.org<us...@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



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


Re: Dynamically choosing component to add

Posted by Bertrand Guay-Paquet <be...@step.polymtl.ca>.
Thanks for the answer, but that is not what I am looking for. I already 
know how to replace a component with another one.

My question is: I need to access a model's object to know which panel to 
add to a page (a persisted user preference). Since I shouldn't access a 
model's object in a component's constructor, when (onBeforeRender, 
onConfigure) should I do it to add the proper panel to the page?

Using addOrReplace could very well be part of the overall solution, but 
this does not answer my core question.

On 31/10/2011 9:54 AM, manuelbarzi wrote:
> MarkupContainer addOrReplace :?
> .
>
>
>
> On Mon, Oct 31, 2011 at 2:49 PM, Bertrand Guay-Paquet<
> bernie@step.polymtl.ca>  wrote:
>
>> Hi,
>>
>> Was is the proper way to choose which panel to add to a page based on a
>> model's object value? Currently, I directly access the model and check the
>> value during construction of the page. However, this is wrong since a
>> model's object shouldn't be accessed until component rendering (see thread
>> "LDMs load too early - hold outdated application data").
>>
>> One obvious approach is to add all possible panels to the page and
>> override their isVisible() method to inspect the model objects. This seems
>> like wasted processing and memory so I'd like to avoid it.
>>
>> The page's onConfigure() and onBeforeRender() methods are the candidates I
>> can think of to inspect the model object's value and add the proper panel.
>> Is one better or is there another solution? Are there other issues with
>> this approach?
>>
>> Regards,
>> Bertrand
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@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: Dynamically choosing component to add

Posted by manuelbarzi <ma...@gmail.com>.
MarkupContainer addOrReplace :?
.



On Mon, Oct 31, 2011 at 2:49 PM, Bertrand Guay-Paquet <
bernie@step.polymtl.ca> wrote:

> Hi,
>
> Was is the proper way to choose which panel to add to a page based on a
> model's object value? Currently, I directly access the model and check the
> value during construction of the page. However, this is wrong since a
> model's object shouldn't be accessed until component rendering (see thread
> "LDMs load too early - hold outdated application data").
>
> One obvious approach is to add all possible panels to the page and
> override their isVisible() method to inspect the model objects. This seems
> like wasted processing and memory so I'd like to avoid it.
>
> The page's onConfigure() and onBeforeRender() methods are the candidates I
> can think of to inspect the model object's value and add the proper panel.
> Is one better or is there another solution? Are there other issues with
> this approach?
>
> Regards,
> Bertrand
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
> For additional commands, e-mail: users-help@wicket.apache.org
>
>