001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008-2010 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.tapestry.core.components;
016    
017    import org.apache.tapestry5.ComponentEventCallback;
018    import org.apache.tapestry5.ComponentResources;
019    import org.apache.tapestry5.EventContext;
020    import org.apache.tapestry5.Link;
021    import org.apache.tapestry5.MarkupWriter;
022    import org.apache.tapestry5.annotations.Environmental;
023    import org.apache.tapestry5.annotations.Import;
024    import org.apache.tapestry5.annotations.OnEvent;
025    import org.apache.tapestry5.annotations.Parameter;
026    import org.apache.tapestry5.corelib.components.Checkbox;
027    import org.apache.tapestry5.internal.util.Holder;
028    import org.apache.tapestry5.ioc.annotations.Inject;
029    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
030    
031    /**
032     * A "ajaxed" <a href="http://tapestry.apache.org/t5components/tapestry-core/component-parameters.html#orgapachetapestrycorelibcomponentscheckbox">Checkbox</a> component.
033     *
034     * @version $Id: AjaxCheckbox.java 674 2010-07-29 12:47:25Z homburgs $
035     */
036    @Import(library = {"../Chenillekit.js", "AjaxCheckbox.js"})
037    public class AjaxCheckbox extends Checkbox
038    {
039            /**
040             * The value of this constant is {@value}.
041             */
042            public static final String EVENT_NAME = "checkboxclicked";
043    
044            /**
045             * The value of this constant is {@value}.
046             */
047            private static final String INTERNAL_EVENT_NAME = "internal_clicked";
048    
049            /**
050             * the javascript callback function (optional).
051             * function has the response text as parameter
052             */
053            @Parameter(required = false, defaultPrefix = "literal")
054            private String onCompleteCallback;
055    
056            /**
057             * The context for the link (optional parameter). This list of values will be converted into strings and included in
058             * the URI. The strings will be coerced back to whatever their values are and made available to event handler
059             * methods.
060             */
061            @Parameter
062            private Object[] context;
063    
064            @Inject
065            private ComponentResources resources;
066    
067            /**
068             * RenderSupport to get unique client side id.
069             */
070            @Environmental
071            private JavaScriptSupport javascriptSupport;
072    
073    
074            /**
075             * Mixin afterRender phrase occurs after the component itself. This is where we write the &lt;div&gt;
076             * element and the JavaScript.
077             *
078             * @param writer
079             */
080            void afterRender(MarkupWriter writer)
081            {
082                    String event = INTERNAL_EVENT_NAME;
083                    String ajaxString = "new Ck.AjaxCheckbox('%s', '%s'";
084    
085                    if (onCompleteCallback != null)
086                    {
087                            ajaxString += ",'" + onCompleteCallback + "'";
088                            event = EVENT_NAME;
089                    }
090    
091                    ajaxString += ");";
092    
093                    Link link = resources.createEventLink(event, context);
094    
095                    javascriptSupport.addScript(ajaxString, getClientId(), link.toAbsoluteURI());
096            }
097    
098            @OnEvent(value = AjaxCheckbox.INTERNAL_EVENT_NAME)
099            Object checkboxClicked(EventContext context)
100            {
101                    final Holder<Object> valueHolder = Holder.create();
102    
103                    ComponentEventCallback callback = new ComponentEventCallback<Object>()
104                    {
105                            public boolean handleResult(Object result)
106                            {
107                                    valueHolder.put(result);
108                                    return true;
109                            }
110                    };
111    
112                    resources.triggerContextEvent(AjaxCheckbox.EVENT_NAME, context, callback);
113    
114                    return valueHolder.get();
115            }
116    }