001    // Copyright Oct 16, 2006 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    // http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    package org.apache.tapestry.dojo.html;
015    
016    import org.apache.tapestry.IMarkupWriter;
017    import org.apache.tapestry.IRequestCycle;
018    import org.apache.tapestry.IScript;
019    import org.apache.tapestry.TapestryUtils;
020    import org.apache.tapestry.dojo.AbstractWidget;
021    import org.apache.tapestry.json.JSONObject;
022    import org.apache.tapestry.services.ResponseBuilder;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    
028    /**
029     * Implementation of dojo Dialog widget.
030     * 
031     */
032    public abstract class Dialog extends AbstractWidget
033    {
034        public abstract boolean isHidden();
035        public abstract void setHidden(boolean hidden);
036        
037        public abstract String getBackgroundColor();
038        
039        public abstract float getOpacity();
040        
041        public abstract boolean getFollowScroll();
042        
043        public abstract boolean getCloseOnBackgroundClick();
044        
045        public abstract int getBlockDuration();
046        
047        public abstract int getLifeTime();
048        
049        public abstract String getToggle();
050        
051        public abstract int getToggleDuration();
052    
053        /**
054         * If called will execute the client side widget checkSize() function to re-size
055         * and re-center the widget.  This is useful in circumstances where you are updating
056         * content within the dialog itself.
057         */
058        public void resize()
059        {
060            setResizing(true);
061        }
062    
063        public void show()
064        {
065            setHidden(false);
066        }
067    
068        public void hide()
069        {
070            setHidden(true);
071        }
072        
073        /**
074         * {@inheritDoc}
075         */
076        public void renderWidget(IMarkupWriter writer, IRequestCycle cycle)
077        {
078            if (!cycle.isRewinding())
079            {
080                writer.begin(getTemplateTagName()); // use element specified
081                renderIdAttribute(writer, cycle); // render id="" client id
082                renderInformalParameters(writer, cycle);
083            }
084            
085            renderBody(writer, cycle);
086            
087            if (!cycle.isRewinding())
088                writer.end();
089            
090            if (!cycle.isRewinding())
091            {
092                JSONObject json = new JSONObject();
093                json.put("bgColor", getBackgroundColor());
094                json.put("bgOpacity", getOpacity());
095                json.put("followScroll", getFollowScroll());
096                json.put("closeOnBackgroundClick", getCloseOnBackgroundClick());
097                json.put("blockDuration", getBlockDuration());
098                json.put("lifeTime", getLifeTime());
099                json.put("toggle", getToggle());
100                json.put("toggleDuration", getToggleDuration());
101    
102                Map parms = new HashMap();
103                parms.put("component", this);
104                parms.put("props", json.toString());
105                
106                getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), parms);
107    
108                if (isResizing())
109                {
110                    if (!getResponseBuilder().isInitializationScriptAllowed(this))
111                    {
112                        getResponseBuilder().updateComponent(getId());
113                    }
114    
115                    getResponseBuilder().addScriptAfterInitialization(this, "dojo.widget.byId('" + getClientId() + "').checkSize();");
116                }
117            }
118        }
119    
120        public abstract ResponseBuilder getResponseBuilder();
121    
122        public abstract boolean isResizing();
123    
124        public abstract void setResizing(boolean value);
125    
126        /** injected. */
127        public abstract IScript getScript();
128    }