You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Josh Chappelle <jc...@4redi.com> on 2010/02/12 18:04:21 UTC

Palette selected item

I have a Palette component that I need to be able to fire an event when an
item on the right side(selected side) has been clicked. Additionally I need
to know what item was clicked because I'm going to be populating another
panel based on that click. 

 

I have created my own component called SelectableOptions that extends
AbstractOptions and implements IOnChangeListener. Then I took the
implementing code that is in DropDownChoice(since it is a good example of a
component that implements IOnChangeListener) and put it in my component. 

 

I am able to receive the onSelectionChanged event and I'm able to pass the
object that the user clicked to that method. However, when you click the
option and the event is fired, a full post back happens and all the items on
the right side of the palette is lost. Basically the component is
re-initialized. 

 

Can anyone help guide me in the right direction? I have posted the code
below:

 

public class SelectableOptions extends AbstractOptions implements
IOnChangeListener

{

            public SelectableOptions(String id, Palette palette, IModel
selectionModel)

            {

                        super(id, palette);

                        setModel(selectionModel);

            }

 

            protected void onComponentTag(final ComponentTag tag)

            {

                        checkComponentTag(tag, "select");

 

                        CharSequence url =
urlFor(IOnChangeListener.INTERFACE);

 

                        Form form = (Form)findParent(Form.class);

                        if (form != null)

                        {

                                    RequestContext rc =
RequestContext.get();

                                    if (rc.isPortletRequest())

                                    {

                                                // restore url back to real
wicket path as its going to be interpreted by the

                                                // form itself

                                                url =
((PortletRequestContext)rc).getLastEncodedPath();

                                    }

                                    tag.put("onchange",
form.getJsForInterfaceUrl(url));

                        }

                        else

                        {

                                    // TODO: following doesn't work with
portlets, should be posted to a dynamic hidden

                                    // form

                                    // with an ActionURL or something

                                    tag.put("onchange",
"window.location.href='" + url + (url.toString().indexOf('?') > -1 ? "&amp;"
: "?") + getInputName() + "=' + this.options[this.selectedIndex].value;");

                        }

 

                        super.onComponentTag(tag);

                        IValueMap attrs = tag.getAttributes();

 

                        String onFocus =
getPalette().getSelectionOnFocusJS();

                        if (onFocus != null)

                        {

                                    attrs.put("onFocus", onFocus);

                        }

//                      tag.getAttributes().put("ondblclick",
getPalette().getRemoveOnClickJS());

            }

            

            protected void onComponentTagBody(MarkupStream markupStream,
ComponentTag openTag)

            {

                        final AppendingStringBuffer buffer = new
AppendingStringBuffer(128);

                        Iterator options = getOptionsIterator();

                        IChoiceRenderer renderer =
getPalette().getChoiceRenderer();

 

                        while (options.hasNext())

                        {

                                    final Object choice = options.next();

                                    String id = renderer.getIdValue(choice,
0);

                                    Object displayValue =
renderer.getDisplayValue(choice);

                                    Class displayClass = displayValue ==
null ? null : displayValue.getClass();

                                    String value =
getConverter(displayClass).convertToString(displayValue, getLocale());

                                    value = getLocalizer().getString(id +
"." + value, this, value);

 

                                    buffer.append("\n<option
value=\"").append(id).append("\"");

 

                                    Map additionalAttributesMap =
getAdditionalAttributes(choice);

                                    if (additionalAttributesMap != null)

                                    {

                                                Iterator iter =
additionalAttributesMap.keySet().iterator();

                                                while (iter.hasNext())

                                                {

                                                            String next =
(String)iter.next();

                                                            buffer.append("
" + next + "=\"" +

 
additionalAttributesMap.get(next).toString() + "\"");

                                                }

                                    }

 

 
buffer.append(">").append(value).append("</option>");

 

                        }

 

                        buffer.append("\n");

 

                        replaceComponentTagBody(markupStream, openTag,
buffer);

            }

 

 

            @Override

            protected Iterator getOptionsIterator()

            {

                        return getPalette().getSelectedChoices();

            }

 

            public final void onSelectionChanged()

            {

                        Iterator iter = getOptionsIterator();

                        System.out.println("Still here");

                        while(iter.hasNext())

                        {

 
System.out.println(iter.next().toString());

                        }

                        convertInput();

                        updateModel();

                        onSelectionChanged(getModelObject());

            }

 

            public void updateModel()

            {

                        setModelObject(getConvertedInput());

            }

            

            protected void onSelectionChanged(final Object newSelection)

            {

            }

 

            protected boolean getStatelessHint()

            {

                        return false;

            }

}

 

/************************* THIS IS HOW I AM PLUGGING THE ABOVE CLASS IN TO
MY PALETTE ************************************************************/

                        Palette palette = new Palette("palette", new
Model(new ArrayList<String>()), new Model((Serializable)choices), renderer,
10, true)

                        {

 

                                    @Override

                                    protected Component
newSelectionComponent()

                                    {

                                                return new
SelectableOptions("selection", this, new Model())

                                                {

 

                                                            @Override

                                                            protected void
onSelectionChanged(Object newSelection)

                                                            {

 
if(newSelection != null)

                                                                        {

 
System.out.println("You selected " + newSelection.toString());

                                                                        }

                                                            }

                                                            

                                                };

                                                

                                                

                                    }

                        };

 

 

Thanks,

 

Josh