It is quite easy to use dojo components with Wicket, especially with the
declarative approach.

Here is a behavior that I wrote few days ago (so it is not bug free)
that could be used for most of the dijit.form.** components:

package com.mycompany.dojo;

import java.util.Locale;

import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.util.convert.IConverter;
import org.apache.wicket.util.value.IValueMap;

@SuppressWarnings("serial")
public class DojoOnChangeBehavior<T> extends AbstractDefaultAjaxBehavior
{

        private static final String parameterName = "dojoOnChangeValue";

        private final String dojoType;
        
        public DojoOnChangeBehavior(String dojoType) {
                this.dojoType = dojoType;
        }
        
        @SuppressWarnings("unchecked")
        @Override
        protected void onBind() {
                super.onBind();
                
                if (false == getComponent() instanceof FormComponent) {
                        throw new
WicketRuntimeException(DojoOnChangeBehavior.class.getName()+" can be
used only with implementations of "+FormComponent.class.getName());
                }
        }
        
        @Override
        public void renderHead(IHeaderResponse response) {
                super.renderHead(response);
                response.renderJavascript(String.format("dojo.require('%s')",
dojoType), "dojo.require."+dojoType);
        }
        
        @Override
        protected void onComponentTag(ComponentTag tag) {
                super.onComponentTag(tag);
                IValueMap attributes = tag.getAttributes();
                attributes.put("dojoType", dojoType);
                
                CharSequence callbackUrl = getCallbackUrl(false);
                String dojoCallbackUrl = "wicketAjaxGet('"+callbackUrl
+"&"+parameterName+"='+arguments[0])";
//              String dojoCallbackUrl = "alert(arguments[0])";
                attributes.put("onChange", dojoCallbackUrl);
        }
                
        @Override
        protected void respond(AjaxRequestTarget target) {
                final Request request = RequestCycle.get().getRequest();
                final String value = request.getParameter(parameterName);
                onChange(target, value);        
        }
        
        @SuppressWarnings("unchecked")
        protected void onChange(final AjaxRequestTarget target,
                        final String selectedValue) {
                
                final FormComponent<T> formComponent = (FormComponent<T>)
getComponent();
                IConverter converter =
formComponent.getConverter(formComponent.getType());
                if (converter != null) {
                        Locale locale = formComponent.getLocale();
                        Object convertedObject = 
converter.convertToObject(selectedValue,
locale);
                        formComponent.setConvertedInput((T) convertedObject);
                } else {
                        formComponent.setConvertedInput((T) selectedValue);
                }
                formComponent.updateModel();
        }


}


And the usage is:

TextField<String> startDate = new TextField<String>("startDate", new
PropertyModel<String>(trigger, "startDate"));
                add(startDate);
                startDate.add(new
DojoOnChangeBehavior<String>("dijit.form.DateTextBox"));
                

or
DropDownChoice<ContentType> contentTypesSelect = 
                        new DropDownChoice<ContentType>("contentTypes", new
PropertyModel<ContentType>(content, "selectedType"),
content.getTypes());
contentTypesSelect..add(new
DojoOnChangeBehavior<ContentType>("dijit.form.FilteringSelect"));

(here you need a registered converter for 'ContentType' class)

On Wed, 2010-02-17 at 10:17 -0600, Josh Chappelle wrote:
> I would also like to know the answer to this. The only reason our team
> hasn't moved to 1.4 is because we have dependencies on some dojo projects
> but none of the ones we use work in 1.4. 
> 
> Josh
> 
> -----Original Message-----
> From: Mauro Ciancio [mailto:maurocian...@gmail.com] 
> Sent: Wednesday, February 17, 2010 9:27 AM
> To: Wicket Mailing List
> Subject: TimePicker component?
> 
> Hello everyone,
> 
>   I'm looking for a TimePicker component. I searched in the list
> and found the dojo time picker from wicket-stuff but isn't working
> with wicket 1.4.x. Throws an exception when the component is
> being rendered.
> 
>   Is there any other component? Is any dojo integration compatible
> with 1.4.x?
> 
> Thanks in advance.
> Cheers!



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

Reply via email to