You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by kamiseq <ka...@gmail.com> on 2011/04/21 23:17:57 UTC

formComponentPanel with content generated by RepeatingView

hi,
Im trying to implement a simple form component that accepts List of Strings

*public class AccountsPanel extends FormComponentPanel<List<String>>
{
**    **public AccountsPanel(String id, ArrayList<String> accounts) {
**    **    **super(id, new Model(accounts));
        buildComponents();
    }
}

*and I am using RepeatingView to create as many TextField as many Strings
are in the List, and this is fine but how then convert each edited value of
the TextField back to List of Strings???

using convertInput would be a good choice I guess but I have no idea how to
iterate over panel's components and read value
*
@Override
protected void convertInput()
{
    List<String> accounts = getModelObject();*
    ????
    *setConvertedInput(accounts);
}*

or maybe this is happening automagically :D ????

pozdrawiam
Paweł Kamiński

kamiseq@gmail.com
pkaminski.prv@gmail.com
______________________

Re: formComponentPanel with content generated by RepeatingView

Posted by kamiseq <ka...@gmail.com>.
yep I did it like that and it works

public class Editor extends FormComponentPanel<List<String>> {
    private ListView<String> item;

    /**
     * If you dont have ArrayList you can always copy your collection to new
ArrayList.
     *
     * @param id of the control
     * @param accounts a list of all accounts assigned to the peron.
     */
    public Editor(String id, IModel items) {
        super(id, items);
        buildComponents();
    }

    private void buildComponents()
    {
        setOutputMarkupPlaceholderTag(false);
        add(new SimpleAttributeModifier("class", "editor-panel"));

        final List<String> items = getModelObject();
        item = new ListView<String>("item", items){

            @Override
            protected void populateItem(ListItem<String> listItem)
            {
                String number = listItem.getModelObject();
                listItem.add(new NumberInput("number", number));
                listItem.add(new RemoveButton("remove", number, items));
            }
        };
        item.setReuseItems(true);
        Link addButton = new Link("add"){

            @Override
            public void onClick() {
                items.add("");
                item.detach();
            }
        };
        add(addButton);
        add(item);
    }

    @Override
    protected void onComponentTag(ComponentTag tag) {
        tag.setName("section");
        super.onComponentTag(tag);
    }

    @Override
    protected void convertInput()
    {
        setConvertedInput(saveConvertedData());
    }

    private List<String> saveConvertedData()
    {
        final List<String> items = new ArrayList<String>();
        item.visitChildren(NumberInput.class, new
Component.IVisitor<NumberInput>(){
            @Override
            public Object component(NumberInput component) {
                String number = component.getConvertedInput();
                items.add(number);

                return Component.IVisitor.CONTINUE_TRAVERSAL;
            }
        });
        return items;
    }
}

thanks for help

pozdrawiam
Paweł Kamiński

kamiseq@gmail.com
pkaminski.prv@gmail.com
______________________

Re: formComponentPanel with content generated by RepeatingView

Posted by Pedro Santos <pe...@gmail.com>.
On Sat, Apr 23, 2011 at 2:45 PM, kamiseq <ka...@gmail.com> wrote:

> heh i thought I was smart :] as each component in FormComponentPanel is
> converted and validated so it would be easy to intercept value set on
> components model and add it to List (which is model of whole FCPanel)
> because Im not really converting components value to something bigger.
>
> the only problem is getting value for each component as interface is only
> getObject and setObject so there is no information model could decide which
> value goes to which component
>

Use a ListView instead of RepeatingView, it recreate its list item
components each render and it already resolve which value goes to which
component.


> ;]
>
> pozdrawiam
> Paweł Kamiński
>
> kamiseq@gmail.com
> pkaminski.prv@gmail.com
> ______________________
>



-- 
Pedro Henrique Oliveira dos Santos

Re: formComponentPanel with content generated by RepeatingView

Posted by kamiseq <ka...@gmail.com>.
heh i thought I was smart :] as each component in FormComponentPanel is
converted and validated so it would be easy to intercept value set on
components model and add it to List (which is model of whole FCPanel)
because Im not really converting components value to something bigger.

the only problem is getting value for each component as interface is only
getObject and setObject so there is no information model could decide which
value goes to which component

;]

pozdrawiam
Paweł Kamiński

kamiseq@gmail.com
pkaminski.prv@gmail.com
______________________

Re: formComponentPanel with content generated by RepeatingView

Posted by Pedro Santos <pe...@gmail.com>.
You can set any model in your form component panel. But its value will be
set during the form processing, after the validation step.
FormComponent#convertInput is invoked before the validation step, and it
only sets the converted input in the FormComponent (see
FormComponent#setConvertedInput). It don't set the model already.

On Fri, Apr 22, 2011 at 4:12 AM, kamiseq <ka...@gmail.com> wrote:

> hej,
> on the second thought I can implement (in this simple case) a model
> backuped
> by List or Set and then let Form (or FormComponentPanel) to process all
> fields.
>
> pozdrawiam
> Paweł Kamiński
>
> kamiseq@gmail.com
> pkaminski.prv@gmail.com
> ______________________
>



-- 
Pedro Henrique Oliveira dos Santos

Re: formComponentPanel with content generated by RepeatingView

Posted by kamiseq <ka...@gmail.com>.
hej,
on the second thought I can implement (in this simple case) a model backuped
by List or Set and then let Form (or FormComponentPanel) to process all
fields.

pozdrawiam
Paweł Kamiński

kamiseq@gmail.com
pkaminski.prv@gmail.com
______________________

Re: formComponentPanel with content generated by RepeatingView

Posted by Pedro Santos <pe...@gmail.com>.
Override convertInput is a good option. Keep in mind that at this point of
the form processing (converting input), component models will not be updated
yet. So you need to rely on children converted data
(see FormComponet#getConvertedInput) in order to assemble the
FormComponentPanel converted input.
In order to access your children components, you can use the visitor API,
e.g. MarkupContainer#visitChildren

On Thu, Apr 21, 2011 at 6:17 PM, kamiseq <ka...@gmail.com> wrote:

> hi,
> Im trying to implement a simple form component that accepts List of Strings
>
> *public class AccountsPanel extends FormComponentPanel<List<String>>
> {
> **    **public AccountsPanel(String id, ArrayList<String> accounts) {
> **    **    **super(id, new Model(accounts));
>        buildComponents();
>    }
> }
>
> *and I am using RepeatingView to create as many TextField as many Strings
> are in the List, and this is fine but how then convert each edited value of
> the TextField back to List of Strings???
>
> using convertInput would be a good choice I guess but I have no idea how to
> iterate over panel's components and read value
> *
> @Override
> protected void convertInput()
> {
>    List<String> accounts = getModelObject();*
>    ????
>    *setConvertedInput(accounts);
> }*
>
> or maybe this is happening automagically :D ????
>
> pozdrawiam
> Paweł Kamiński
>
> kamiseq@gmail.com
> pkaminski.prv@gmail.com
> ______________________
>



-- 
Pedro Henrique Oliveira dos Santos