|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
LinkSubmit.java | 0% | 0% | 0% | 0% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.form;
|
|
16 |
|
|
17 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
18 |
import org.apache.tapestry.IActionListener;
|
|
19 |
import org.apache.tapestry.IBinding;
|
|
20 |
import org.apache.tapestry.IForm;
|
|
21 |
import org.apache.tapestry.IMarkupWriter;
|
|
22 |
import org.apache.tapestry.IRequestCycle;
|
|
23 |
import org.apache.tapestry.Tapestry;
|
|
24 |
import org.apache.tapestry.html.Body;
|
|
25 |
|
|
26 |
/**
|
|
27 |
* Implements a component that submits its enclosing form via a JavaScript link. [ <a
|
|
28 |
* href="../../../../../ComponentReference/LinkSubmit.html">Component Reference </a>]
|
|
29 |
*
|
|
30 |
* @author Richard Lewis-Shell
|
|
31 |
*/
|
|
32 |
|
|
33 |
public abstract class LinkSubmit extends AbstractFormComponent |
|
34 |
{ |
|
35 |
/**
|
|
36 |
* The name of an {@link org.apache.tapestry.IRequestCycle}attribute in which the current
|
|
37 |
* submit link is stored. LinkSubmits do not nest.
|
|
38 |
*/
|
|
39 |
|
|
40 |
public static final String ATTRIBUTE_NAME = "org.apache.tapestry.form.LinkSubmit"; |
|
41 |
|
|
42 |
/**
|
|
43 |
* The name of an {@link org.apache.tapestry.IRequestCycle}attribute in which the link submit
|
|
44 |
* component that generates the javascript function is stored. The function is only required
|
|
45 |
* once per page (containing a form with a non-disabled LinkSubmit)
|
|
46 |
*/
|
|
47 |
public static final String ATTRIBUTE_FUNCTION_NAME = "org.apache.tapestry.form.LinkSubmit_function"; |
|
48 |
|
|
49 | 0 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
50 |
{ |
|
51 |
|
|
52 | 0 |
IForm form = getForm(cycle); |
53 | 0 |
String formName = form.getName(); |
54 |
|
|
55 | 0 |
boolean rewinding = form.isRewinding();
|
56 |
|
|
57 | 0 |
String name = form.getElementId(this);
|
58 |
|
|
59 | 0 |
IMarkupWriter wrappedWriter; |
60 |
|
|
61 | 0 |
if (cycle.getAttribute(ATTRIBUTE_NAME) != null) |
62 | 0 |
throw new ApplicationRuntimeException(Tapestry.getMessage("LinkSubmit.may-not-nest"), |
63 |
this, null, null); |
|
64 |
|
|
65 | 0 |
cycle.setAttribute(ATTRIBUTE_NAME, this);
|
66 |
|
|
67 | 0 |
boolean disabled = isDisabled();
|
68 | 0 |
if (!disabled)
|
69 |
{ |
|
70 | 0 |
if (!rewinding)
|
71 |
{ |
|
72 | 0 |
Body body = Body.get(cycle); |
73 |
|
|
74 | 0 |
if (body == null) |
75 | 0 |
throw new ApplicationRuntimeException(Tapestry.format( |
76 |
"must-be-contained-by-body",
|
|
77 |
"LinkSubmit"), this, null, null); |
|
78 |
|
|
79 |
// make sure the submit function is on the page (once)
|
|
80 | 0 |
if (cycle.getAttribute(ATTRIBUTE_FUNCTION_NAME) == null) |
81 |
{ |
|
82 | 0 |
body |
83 |
.addBodyScript("function submitLink(form, elementId) { form._linkSubmit.value = elementId; if (form.onsubmit == null || form.onsubmit()) form.submit(); }");
|
|
84 | 0 |
cycle.setAttribute(ATTRIBUTE_FUNCTION_NAME, this);
|
85 |
} |
|
86 |
|
|
87 |
// one hidden field per form:
|
|
88 | 0 |
String formHiddenFieldAttributeName = ATTRIBUTE_FUNCTION_NAME + formName; |
89 | 0 |
if (cycle.getAttribute(formHiddenFieldAttributeName) == null) |
90 |
{ |
|
91 | 0 |
writer.beginEmpty("input");
|
92 | 0 |
writer.attribute("type", "hidden"); |
93 | 0 |
writer.attribute("name", "_linkSubmit"); |
94 | 0 |
cycle.setAttribute(formHiddenFieldAttributeName, this);
|
95 |
} |
|
96 |
} |
|
97 |
else
|
|
98 |
{ |
|
99 |
// How to know which Submit link was actually
|
|
100 |
// clicked? When submitted, it sets its elementId into a hidden field
|
|
101 |
|
|
102 | 0 |
String value = cycle.getRequestContext().getParameter("_linkSubmit");
|
103 |
|
|
104 |
// If the value isn't the elementId of this component, then this link wasn't
|
|
105 |
// selected.
|
|
106 |
|
|
107 | 0 |
if (value != null && value.equals(name)) |
108 |
{ |
|
109 | 0 |
IBinding selectedBinding = getBinding("selected");
|
110 | 0 |
if (selectedBinding != null) |
111 | 0 |
selectedBinding.setObject(getTag()); |
112 | 0 |
IActionListener listener = getListener(); |
113 | 0 |
if (listener != null) |
114 | 0 |
listener.actionTriggered(this, cycle);
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 | 0 |
writer.begin("a");
|
119 | 0 |
writer.attribute("href", "javascript:submitLink(document." + formName + ",\"" + name |
120 |
+ "\");");
|
|
121 |
|
|
122 |
// Allow the wrapped components a chance to render.
|
|
123 |
// Along the way, they may interact with this component
|
|
124 |
// and cause the name variable to get set.
|
|
125 |
|
|
126 | 0 |
wrappedWriter = writer.getNestedWriter(); |
127 |
} |
|
128 |
else
|
|
129 | 0 |
wrappedWriter = writer; |
130 |
|
|
131 | 0 |
renderBody(wrappedWriter, cycle); |
132 |
|
|
133 | 0 |
if (!disabled)
|
134 |
{ |
|
135 |
// Generate additional attributes from informal parameters.
|
|
136 |
|
|
137 | 0 |
renderInformalParameters(writer, cycle); |
138 |
|
|
139 |
// Dump in HTML provided by wrapped components
|
|
140 |
|
|
141 | 0 |
wrappedWriter.close(); |
142 |
|
|
143 |
// Close the <a> tag
|
|
144 |
|
|
145 | 0 |
writer.end(); |
146 |
} |
|
147 |
|
|
148 | 0 |
cycle.removeAttribute(ATTRIBUTE_NAME); |
149 |
} |
|
150 |
|
|
151 |
public abstract boolean isDisabled(); |
|
152 |
|
|
153 |
public abstract void setDisabled(boolean disabled); |
|
154 |
|
|
155 |
public abstract IActionListener getListener();
|
|
156 |
|
|
157 |
public abstract void setListener(IActionListener listener); |
|
158 |
|
|
159 |
public abstract Object getTag();
|
|
160 |
|
|
161 |
public abstract void setTag(Object tag); |
|
162 |
} |
|