1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.statechangednotifier;
20
21 import java.io.IOException;
22
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIInput;
25 import javax.faces.component.UIOutput;
26 import javax.faces.context.FacesContext;
27 import javax.faces.context.ResponseWriter;
28 import javax.faces.convert.ConverterException;
29 import javax.faces.FacesException;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.myfaces.custom.dojo.DojoConfig;
33 import org.apache.myfaces.custom.dojo.DojoUtils;
34 import org.apache.myfaces.renderkit.html.util.AddResource;
35 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
36
37 import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
38 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
39 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
40 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
41 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
42 import org.apache.myfaces.shared_tomahawk.renderkit.html.util.FormInfo;
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class StateChangedNotifierRenderer extends HtmlRenderer {
57
58 public void decode(FacesContext facesContext, UIComponent component) {
59 RendererUtils.checkParamValidity(facesContext, component, null);
60
61 if (component instanceof UIInput) {
62 HtmlRendererUtils.decodeUIInput(facesContext, component);
63 } else {
64 throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
65 }
66 }
67
68 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException {
69 RendererUtils.checkParamValidity(facesContext, uiComponent, StateChangedNotifier.class);
70
71 StateChangedNotifier notifier = (StateChangedNotifier) uiComponent;
72
73 if (notifier.getDisabled() != null) {
74
75 if (notifier.getDisabled().booleanValue())
76 return;
77 }
78
79 renderInitialization(facesContext, uiComponent, notifier);
80
81 encodeJavascript(facesContext, notifier);
82
83 renderInputHidden(facesContext, uiComponent);
84
85 }
86
87 public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException {
88 RendererUtils.checkParamValidity(facesContext, uiComponent, UIOutput.class);
89
90 return RendererUtils.getConvertedUIOutputValue(facesContext, (UIOutput) uiComponent, submittedValue);
91 }
92
93 private void encodeJavascript(FacesContext facesContext, StateChangedNotifier notifier) {
94 String notifierClientId = notifier.getClientId(facesContext);
95
96 String replacedClientId = notifierClientId.replaceAll(":", "_");
97 String initFunctionName = "init_" + replacedClientId;
98
99 FormInfo formInfo = RendererUtils.findNestingForm(notifier,facesContext);
100 if(formInfo == null)
101 throw new FacesException("StateChangedNotifier must be embedded in a form.");
102
103 String formId = formInfo.getFormName();
104
105 String notifierVar = replacedClientId + "Notifier";
106
107 StringBuffer sb = new StringBuffer();
108 sb.append("dojo.addOnLoad(window, '" + initFunctionName + "');\n");
109 sb.append("function " + initFunctionName + "() {\n");
110 sb.append(notifierVar + " = new org_apache_myfaces_StateChangedNotifier('" + notifierVar + "','" + formId + "','" + notifierClientId + "','" + notifier.getConfirmationMessage() + "',");
111
112 String excludedCommandIds = notifier.getExcludedIds();
113
114 if (excludedCommandIds != null) {
115 sb.append("'" + excludedCommandIds + "');\n");
116 } else {
117 sb.append("'');\n");
118 }
119
120 sb.append("setTimeout('" + replacedClientId + "Notifier.prepareNotifier()',500);\n");
121
122 sb.append("}\n");
123
124 AddResource addResource = AddResourceFactory.getInstance(facesContext);
125 addResource.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, sb.toString());
126 }
127
128 private void renderInitialization(FacesContext facesContext, UIComponent uiComponent, StateChangedNotifier notifier)
129 throws IOException {
130 String javascriptLocation = (String) notifier.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
131 DojoUtils.addMainInclude(facesContext, uiComponent, javascriptLocation, new DojoConfig());
132 DojoUtils.addRequire(facesContext, uiComponent, "dojo.widget.Dialog");
133 DojoUtils.addRequire(facesContext, uiComponent, "dojo.event.*");
134
135
136
137 writeDialog(facesContext, uiComponent);
138
139 AddResource addResource = AddResourceFactory.getInstance(facesContext);
140
141 if (!DojoUtils.isInlineScriptSet(facesContext, "stateChangedNotifier.js"))
142 addResource.addJavaScriptHere(facesContext, StateChangedNotifierRenderer.class, "stateChangedNotifier.js");
143
144 String styleLocation = (String) uiComponent.getAttributes().get(JSFAttr.STYLE_LOCATION);
145
146
147 if (StringUtils.isNotBlank(styleLocation)) {
148 addResource.addStyleSheet(facesContext, AddResource.HEADER_BEGIN, styleLocation + "/default.css");
149 } else {
150 addResource.addStyleSheet(facesContext, AddResource.HEADER_BEGIN, StateChangedNotifierRenderer.class, "css/default.css");
151 }
152 }
153
154 private void renderInputHidden(FacesContext facesContext, UIComponent uiComponent) throws IOException {
155 RendererUtils.checkParamValidity(facesContext, uiComponent, UIInput.class);
156
157 ResponseWriter writer = facesContext.getResponseWriter();
158
159 writer.startElement(HTML.INPUT_ELEM, uiComponent);
160 writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
161
162 String clientId = uiComponent.getClientId(facesContext);
163 writer.writeAttribute(HTML.ID_ATTR, clientId, null);
164 writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
165
166 String value = RendererUtils.getStringValue(facesContext, uiComponent);
167
168 if (value != null) {
169 writer.writeAttribute(HTML.VALUE_ATTR, value, JSFAttr.VALUE_ATTR);
170 }
171
172 writer.endElement(HTML.INPUT_ELEM);
173 }
174
175
176
177
178
179
180
181
182 private void writeDialog(FacesContext facesContext, UIComponent uiComponent) throws IOException {
183
184
185
186
187
188
189
190
191
192 String notifierClientId = uiComponent.getClientId(facesContext);
193 String replacedClientId = notifierClientId.replaceAll(":", "_");
194 String dialogVar = replacedClientId + "Notifier_Dialog";
195 String yesText = (String) uiComponent.getAttributes().get("yesText");
196 String noText = (String) uiComponent.getAttributes().get("noText");
197
198 yesText = (yesText == null) ? "Yes" : yesText;
199 noText = (noText == null) ? "No" : noText;
200
201 ResponseWriter writer = facesContext.getResponseWriter();
202 writer.startElement(HTML.DIV_ELEM, uiComponent);
203 writer.writeAttribute(HTML.ID_ATTR, dialogVar, null);
204 writer.writeAttribute(HTML.STYLE_ATTR, "position:absolute; visibility: hidden;", null);
205 writer.startElement(HTML.DIV_ELEM, uiComponent);
206 writer.writeAttribute(HTML.ID_ATTR, dialogVar + "_Content", null);
207 writer.endElement(HTML.DIV_ELEM);
208 writer.write(HTML.NBSP_ENTITY);
209 writer.startElement(HTML.INPUT_ELEM, uiComponent);
210 writer.writeAttribute(HTML.TYPE_ATTR, HTML.BUTTON_ELEM, null);
211 writer.writeAttribute(HTML.ID_ATTR, dialogVar + "_Yes", null);
212 writer.writeAttribute(HTML.VALUE_ATTR, yesText, null);
213 writer.endElement(HTML.INPUT_ELEM);
214 writer.write(HTML.NBSP_ENTITY);
215 writer.startElement(HTML.INPUT_ELEM, uiComponent);
216 writer.writeAttribute(HTML.TYPE_ATTR, "button", null);
217 writer.writeAttribute(HTML.ID_ATTR, dialogVar + "_No", null);
218 writer.writeAttribute(HTML.VALUE_ATTR, noText, null);
219 writer.endElement(HTML.INPUT_ELEM);
220 writer.endElement(HTML.DIV_ELEM);
221 writer.write(HTML.NBSP_ENTITY);
222 }
223 }