1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared_impl.renderkit.html; |
20 | |
|
21 | |
import org.apache.myfaces.shared_impl.config.MyfacesConfig; |
22 | |
import org.apache.myfaces.shared_impl.renderkit.JSFAttr; |
23 | |
import org.apache.myfaces.shared_impl.renderkit.RendererUtils; |
24 | |
import org.apache.myfaces.shared_impl.renderkit.html.util.FormInfo; |
25 | |
import org.apache.myfaces.shared_impl.renderkit.html.util.JavascriptUtils; |
26 | |
|
27 | |
import javax.faces.component.UICommand; |
28 | |
import javax.faces.component.UIComponent; |
29 | |
import javax.faces.component.ValueHolder; |
30 | |
import javax.faces.component.html.HtmlCommandButton; |
31 | |
import javax.faces.context.ExternalContext; |
32 | |
import javax.faces.context.FacesContext; |
33 | |
import javax.faces.context.ResponseWriter; |
34 | |
import javax.faces.event.ActionEvent; |
35 | |
import java.io.IOException; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class HtmlButtonRendererBase |
45 | |
extends HtmlRenderer |
46 | |
{ |
47 | |
private static final String IMAGE_BUTTON_SUFFIX_X = ".x"; |
48 | |
private static final String IMAGE_BUTTON_SUFFIX_Y = ".y"; |
49 | |
|
50 | |
public static final String ACTION_FOR_LIST = "org.apache.myfaces.ActionForList"; |
51 | |
|
52 | |
public void decode(FacesContext facesContext, UIComponent uiComponent) |
53 | |
{ |
54 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class); |
55 | |
|
56 | |
|
57 | 0 | if (!isReset(uiComponent) && isSubmitted(facesContext, uiComponent)) |
58 | |
{ |
59 | 0 | uiComponent.queueEvent(new ActionEvent(uiComponent)); |
60 | |
|
61 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.initPartialValidationAndModelUpdate(uiComponent, facesContext); |
62 | |
} |
63 | 0 | } |
64 | |
|
65 | |
private static boolean isReset(UIComponent uiComponent) |
66 | |
{ |
67 | 0 | return "reset".equals((String) uiComponent.getAttributes().get(HTML.TYPE_ATTR)); |
68 | |
} |
69 | |
|
70 | |
private static boolean isSubmitted(FacesContext facesContext, UIComponent uiComponent) |
71 | |
{ |
72 | 0 | String clientId = uiComponent.getClientId(facesContext); |
73 | 0 | Map paramMap = facesContext.getExternalContext().getRequestParameterMap(); |
74 | 0 | return paramMap.containsKey(clientId) || paramMap.containsKey(clientId + IMAGE_BUTTON_SUFFIX_X) || paramMap.containsKey(clientId + IMAGE_BUTTON_SUFFIX_Y); |
75 | |
} |
76 | |
|
77 | |
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) |
78 | |
throws IOException |
79 | |
{ |
80 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class); |
81 | |
|
82 | 0 | String clientId = uiComponent.getClientId(facesContext); |
83 | |
|
84 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 0 | if (JavascriptUtils.isJavascriptAllowed(facesContext.getExternalContext())) |
91 | |
{ |
92 | 0 | if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll()) { |
93 | 0 | HtmlRendererUtils.renderFormSubmitScript(facesContext); |
94 | |
} |
95 | |
} |
96 | |
|
97 | 0 | writer.startElement(HTML.INPUT_ELEM, uiComponent); |
98 | |
|
99 | 0 | writer.writeAttribute(HTML.ID_ATTR, clientId, org.apache.myfaces.shared_impl.renderkit.JSFAttr.ID_ATTR); |
100 | 0 | writer.writeAttribute(HTML.NAME_ATTR, clientId, JSFAttr.ID_ATTR); |
101 | |
|
102 | 0 | String image = getImage(uiComponent); |
103 | |
|
104 | 0 | ExternalContext externalContext = facesContext.getExternalContext(); |
105 | |
|
106 | 0 | if (image != null) |
107 | |
{ |
108 | 0 | writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_IMAGE, org.apache.myfaces.shared_impl.renderkit.JSFAttr.TYPE_ATTR); |
109 | 0 | String src = facesContext.getApplication().getViewHandler().getResourceURL( |
110 | |
facesContext, image); |
111 | 0 | writer.writeURIAttribute(HTML.SRC_ATTR, externalContext.encodeResourceURL(src), |
112 | |
org.apache.myfaces.shared_impl.renderkit.JSFAttr.IMAGE_ATTR); |
113 | 0 | } |
114 | |
else |
115 | |
{ |
116 | 0 | String type = getType(uiComponent); |
117 | |
|
118 | 0 | if (type == null || !isReset(uiComponent)) |
119 | |
{ |
120 | 0 | type = HTML.INPUT_TYPE_SUBMIT; |
121 | |
} |
122 | 0 | writer.writeAttribute(HTML.TYPE_ATTR, type, org.apache.myfaces.shared_impl.renderkit.JSFAttr.TYPE_ATTR); |
123 | 0 | Object value = getValue(uiComponent); |
124 | 0 | if (value != null) |
125 | |
{ |
126 | 0 | writer.writeAttribute(org.apache.myfaces.shared_impl.renderkit.html.HTML.VALUE_ATTR, value, org.apache.myfaces.shared_impl.renderkit.JSFAttr.VALUE_ATTR); |
127 | |
} |
128 | |
} |
129 | 0 | if (JavascriptUtils.isJavascriptAllowed(externalContext)) |
130 | |
{ |
131 | 0 | StringBuffer onClick = buildOnClick(uiComponent, facesContext, writer); |
132 | 0 | if (onClick.length() != 0){ |
133 | 0 | writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null); |
134 | |
} |
135 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, |
136 | |
HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_ONCLICK); |
137 | 0 | } |
138 | |
else |
139 | |
{ |
140 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, |
141 | |
HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED); |
142 | |
} |
143 | |
|
144 | 0 | if (isDisabled(facesContext, uiComponent)) |
145 | |
{ |
146 | 0 | writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, org.apache.myfaces.shared_impl.renderkit.JSFAttr.DISABLED_ATTR); |
147 | |
} |
148 | |
|
149 | 0 | if (isReadonly(facesContext, uiComponent)) |
150 | |
{ |
151 | 0 | writer.writeAttribute(HTML.READONLY_ATTR, Boolean.TRUE, org.apache.myfaces.shared_impl.renderkit.JSFAttr.READONLY_ATTR); |
152 | |
} |
153 | |
|
154 | 0 | writer.endElement(HTML.INPUT_ELEM); |
155 | |
|
156 | 0 | HtmlFormRendererBase.renderScrollHiddenInputIfNecessary( |
157 | |
findNestingForm(uiComponent, facesContext).getForm(), facesContext, writer); |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
protected StringBuffer buildOnClick(UIComponent uiComponent, FacesContext facesContext, ResponseWriter writer) |
162 | |
throws IOException |
163 | |
{ |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | 0 | FormInfo formInfo = findNestingForm(uiComponent, facesContext); |
192 | 0 | if (formInfo == null) |
193 | |
{ |
194 | 0 | throw new IllegalArgumentException("Component " + uiComponent.getClientId(facesContext) + " must be embedded in an form"); |
195 | |
} |
196 | 0 | String formName = formInfo.getFormName(); |
197 | 0 | UIComponent nestingForm = formInfo.getForm(); |
198 | |
|
199 | 0 | StringBuffer onClick = new StringBuffer(); |
200 | 0 | String commandOnClick = (String)uiComponent.getAttributes().get(HTML.ONCLICK_ATTR); |
201 | |
|
202 | 0 | if (commandOnClick != null) |
203 | |
{ |
204 | 0 | onClick.append(commandOnClick); |
205 | 0 | onClick.append(';'); |
206 | |
} |
207 | |
|
208 | 0 | if (JavascriptUtils.isRenderClearJavascriptOnButton(facesContext.getExternalContext()) || |
209 | |
MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isRenderHiddenFieldsForLinkParams() ) |
210 | |
{ |
211 | |
|
212 | 0 | HtmlRendererUtils.appendClearHiddenCommandFormParamsFunctionCall(onClick, formName); |
213 | |
} |
214 | |
|
215 | 0 | if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll()) { |
216 | 0 | HtmlRendererUtils.appendAutoScrollAssignment(onClick, formName); |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | 0 | return onClick; |
229 | |
} |
230 | |
|
231 | |
protected void addHiddenCommandParameter(FacesContext facesContext, UIComponent nestingForm, String hiddenFieldName) |
232 | |
{ |
233 | 0 | if (nestingForm != null) |
234 | |
{ |
235 | 0 | HtmlFormRendererBase.addHiddenCommandParameter(facesContext, nestingForm, hiddenFieldName); |
236 | |
} |
237 | 0 | } |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
protected FormInfo findNestingForm(UIComponent uiComponent, FacesContext facesContext) |
244 | |
{ |
245 | 0 | return RendererUtils.findNestingForm(uiComponent, facesContext); |
246 | |
} |
247 | |
|
248 | |
protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent) |
249 | |
{ |
250 | |
|
251 | 0 | if (uiComponent instanceof HtmlCommandButton) |
252 | |
{ |
253 | 0 | return ((HtmlCommandButton)uiComponent).isDisabled(); |
254 | |
} |
255 | |
|
256 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false); |
257 | |
|
258 | |
} |
259 | |
|
260 | |
protected boolean isReadonly(FacesContext facesContext, UIComponent uiComponent) |
261 | |
{ |
262 | 0 | if (uiComponent instanceof HtmlCommandButton) |
263 | |
{ |
264 | 0 | return ((HtmlCommandButton)uiComponent).isReadonly(); |
265 | |
} |
266 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getBooleanAttribute(uiComponent, HTML.READONLY_ATTR, false); |
267 | |
} |
268 | |
|
269 | |
private String getImage(UIComponent uiComponent) |
270 | |
{ |
271 | 0 | if (uiComponent instanceof HtmlCommandButton) |
272 | |
{ |
273 | 0 | return ((HtmlCommandButton)uiComponent).getImage(); |
274 | |
} |
275 | 0 | return (String)uiComponent.getAttributes().get(JSFAttr.IMAGE_ATTR); |
276 | |
} |
277 | |
|
278 | |
private String getType(UIComponent uiComponent) |
279 | |
{ |
280 | 0 | if (uiComponent instanceof HtmlCommandButton) |
281 | |
{ |
282 | 0 | return ((HtmlCommandButton)uiComponent).getType(); |
283 | |
} |
284 | 0 | return (String)uiComponent.getAttributes().get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.TYPE_ATTR); |
285 | |
} |
286 | |
|
287 | |
private Object getValue(UIComponent uiComponent) |
288 | |
{ |
289 | 0 | if (uiComponent instanceof ValueHolder) |
290 | |
{ |
291 | 0 | return ((ValueHolder)uiComponent).getValue(); |
292 | |
} |
293 | 0 | return uiComponent.getAttributes().get(JSFAttr.VALUE_ATTR); |
294 | |
} |
295 | |
} |