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.hivemind.HiveMind; 022 import org.apache.tapestry.IComponent; 023 import org.apache.tapestry.IForm; 024 import org.apache.tapestry.IMarkupWriter; 025 import org.apache.tapestry.IRequestCycle; 026 import org.apache.tapestry.IScript; 027 import org.apache.tapestry.PageRenderSupport; 028 import org.apache.tapestry.TapestryUtils; 029 030 /** 031 * Implements a component that submits its enclosing form via a JavaScript link. [ <a 032 * href="../../../../../ComponentReference/LinkSubmit.html">Component Reference </a>] 033 * 034 * @author Richard Lewis-Shell 035 */ 036 037 public abstract class LinkSubmit extends AbstractSubmit 038 { 039 040 /** 041 * The name of an {@link org.apache.tapestry.IRequestCycle} attribute in which the current 042 * submit link is stored. LinkSubmits do not nest. 043 */ 044 045 public static final String ATTRIBUTE_NAME = "org.apache.tapestry.form.LinkSubmit"; 046 047 /** 048 * Checks the submit name ({@link FormConstants#SUBMIT_NAME_PARAMETER}) to see if it matches 049 * this LinkSubmit's assigned element name. 050 */ 051 protected boolean isClicked(IRequestCycle cycle, String name) 052 { 053 String value = cycle.getParameter(FormConstants.SUBMIT_NAME_PARAMETER); 054 055 return name.equals(value); 056 } 057 058 public abstract IScript getScript(); 059 060 /** 061 * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, 062 * org.apache.tapestry.IRequestCycle) 063 */ 064 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) 065 { 066 boolean disabled = isDisabled(); 067 068 IForm form = getForm(); 069 String name = getName(); 070 071 if (!disabled) 072 { 073 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this); 074 075 Map symbols = new HashMap(); 076 symbols.put("form", form); 077 symbols.put("name", name); 078 079 getScript().execute(cycle, pageRenderSupport, symbols); 080 081 writer.begin("a"); 082 writer.attribute("href", (String) symbols.get("href")); 083 084 renderIdAttribute(writer, cycle); 085 086 renderInformalParameters(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 }