001    // Copyright 2004, 2005 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    
015    package org.apache.tapestry.form;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    import org.apache.hivemind.ApplicationRuntimeException;
021    import org.apache.tapestry.IComponent;
022    import org.apache.tapestry.IForm;
023    import org.apache.tapestry.IMarkupWriter;
024    import org.apache.tapestry.IRequestCycle;
025    import org.apache.tapestry.IScript;
026    import org.apache.tapestry.PageRenderSupport;
027    import org.apache.tapestry.TapestryUtils;
028    
029    /**
030     * Implements a component that submits its enclosing form via a JavaScript link. [ <a
031     * href="../../../../../ComponentReference/LinkSubmit.html">Component Reference </a>]
032     * 
033     * @author Richard Lewis-Shell
034     */
035    
036    public abstract class LinkSubmit extends AbstractSubmit
037    {
038    
039        /**
040         * The name of an {@link org.apache.tapestry.IRequestCycle} attribute in which the current
041         * submit link is stored. LinkSubmits do not nest.
042         */
043    
044        public static final String ATTRIBUTE_NAME = "org.apache.tapestry.form.LinkSubmit";
045    
046        /**
047         * Checks the submit name ({@link FormConstants#SUBMIT_NAME_PARAMETER}) to see if it matches
048         * this LinkSubmit's assigned element name.
049         */
050        protected boolean isClicked(IRequestCycle cycle, String name)
051        {
052            String value = cycle.getParameter(FormConstants.SUBMIT_NAME_PARAMETER);
053            return name.equals(value);
054        }
055    
056        public abstract IScript getScript();
057    
058        /**
059         * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
060         *      org.apache.tapestry.IRequestCycle)
061         */
062        protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
063        {
064            boolean disabled = isDisabled();
065    
066            IForm form = getForm();
067            String name = getName();
068    
069            if (!disabled)
070            {
071                PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
072    
073                Map symbols = new HashMap();
074                symbols.put("form", form);
075                symbols.put("name", name);
076                
077                getScript().execute(this, cycle, pageRenderSupport, symbols);
078                
079                writer.begin("a");
080                writer.attribute("href", (String)symbols.get("href"));
081                
082                renderIdAttribute(writer, cycle);
083    
084                renderInformalParameters(writer, cycle);
085                
086                renderSubmitBindings(writer, cycle);
087            }
088    
089            renderBody(writer, cycle);
090    
091            if (!disabled)
092                writer.end();
093    
094        }
095    
096        /**
097         * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
098         */
099        protected void prepareForRender(IRequestCycle cycle)
100        {
101            IComponent outer = (IComponent) cycle.getAttribute(ATTRIBUTE_NAME);
102    
103            if (outer != null)
104                throw new ApplicationRuntimeException(FormMessages.linkSubmitMayNotNest(this, outer),
105                        this, getLocation(), null);
106    
107            cycle.setAttribute(ATTRIBUTE_NAME, this);
108        }
109    
110        /**
111         * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
112         */
113        protected void cleanupAfterRender(IRequestCycle cycle)
114        {
115            cycle.removeAttribute(ATTRIBUTE_NAME);
116        }
117    
118        /**
119         * Links can not take focus, ever.
120         */
121        protected boolean getCanTakeFocus()
122        {
123            return false;
124        }
125    
126        /**
127         * Returns true; the LinkSubmit's body should render during a rewind, even if the component is
128         * itself disabled.
129         */
130        protected boolean getRenderBodyOnRewind()
131        {
132            return true;
133        }
134    
135    }