View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.timednotifier;
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  
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.myfaces.custom.dojo.DojoConfig;
32  import org.apache.myfaces.custom.dojo.DojoUtils;
33  import org.apache.myfaces.renderkit.html.util.AddResource;
34  import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
35  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
36  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
37  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
38  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
39  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
40  
41  /**
42   * 
43   * 
44   * @JSFRenderer
45   *   renderKitId = "HTML_BASIC" 
46   *   family = "javax.faces.Output"
47   *   type = "org.apache.myfaces.TimedNotifierRenderer"
48   * 
49   * @author werpu
50   * The html renderer for the timed notifier
51   *
52   */
53  public class TimedNotifierRenderer extends HtmlRenderer {
54  
55      public void decode(FacesContext facesContext, UIComponent component) {
56          RendererUtils.checkParamValidity(facesContext, component, null);
57  
58          if (component instanceof UIInput) {
59              HtmlRendererUtils.decodeUIInput(facesContext, component);
60          } else {
61              throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
62          }
63      }
64  
65      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException {
66          RendererUtils.checkParamValidity(facesContext, uiComponent, TimedNotifier.class);
67  
68          TimedNotifier notifier = (TimedNotifier) uiComponent;
69  
70          if (notifier.getDisabled() != null) {
71  
72              if (notifier.getDisabled().booleanValue())
73                  return;
74          }
75  
76          renderInitialization(facesContext, uiComponent, notifier);
77  
78          encodeJavascript(facesContext, notifier);
79  
80     
81      }
82  
83      public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException {
84          RendererUtils.checkParamValidity(facesContext, uiComponent, UIOutput.class);
85  
86          return RendererUtils.getConvertedUIOutputValue(facesContext, (UIOutput) uiComponent, submittedValue);
87      }
88  
89      private void encodeJavascript(FacesContext facesContext, TimedNotifier notifier) throws IOException {
90          String notifierClientId = notifier.getClientId(facesContext);
91  
92          String replacedClientId = notifierClientId.replaceAll(":", "_");
93          String dialogVar        = replacedClientId + "Notifier_Dialog";
94          String confirmVar         = dialogVar + "_Yes";
95          
96          Integer timeShow            = notifier.getShowDelay();
97          Integer timeHide            = notifier.getHideDelay();
98          
99          UIComponent confirmComp = notifier.getConfirm();
100        
101             
102        
103        
104 
105         String notifierVar = replacedClientId + "Notifier";
106 
107         StringBuffer sb = new StringBuffer();
108         sb.append("function "+replacedClientId+"() {\n");
109         if(confirmComp == null)
110             sb.append("var "+ notifierVar + " = new myfaces_TimedNotifier('" + 
111                 dialogVar + "','" + confirmVar + "','" + notifierClientId + "',"+
112                 timeShow.toString()+","+timeHide.toString()+");\n");
113         else
114               sb.append("var "+ notifierVar + " = new myfaces_TimedNotifier('" + 
115                     dialogVar + "',null,'" + notifierClientId + "',"+
116                     timeShow.toString()+","+timeHide.toString()+");\n");
117                 
118         sb.append( notifierVar + ".showDialog();\n");
119         sb.append("};\n");
120         sb.append("dojo.lang.setTimeout("+replacedClientId+","+timeShow.toString()+");");
121 
122         ResponseWriter writer = facesContext.getResponseWriter();
123         writer.startElement(HTML.SCRIPT_ELEM, notifier);
124         writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
125         writer.write(sb.toString());
126         writer.endElement(HTML.SCRIPT_ELEM);
127 
128     }
129 
130    
131 
132     private void renderInitialization(FacesContext facesContext, UIComponent uiComponent, TimedNotifier notifier) throws IOException {
133         String javascriptLocation = (String) notifier.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
134         DojoUtils.addMainInclude(facesContext, uiComponent, javascriptLocation, new DojoConfig());
135         DojoUtils.addRequire(facesContext, uiComponent, "dojo.widget.Dialog");
136         DojoUtils.addRequire(facesContext, uiComponent, "dojo.event.*");
137 
138         // DojoUtils.addRequire(facesContext, "dojo.xml.Parse");
139 
140         writeDialog(facesContext, notifier);
141 
142         AddResource addResource = AddResourceFactory.getInstance(facesContext);
143 
144         if (!DojoUtils.isInlineScriptSet(facesContext, "stateChangedNotifier.js"))
145             addResource.addJavaScriptHere(facesContext, TimedNotifierRenderer.class, "timednotifier.js");
146 
147         String styleLocation = (String) uiComponent.getAttributes().get(JSFAttr.STYLE_LOCATION);
148 
149         // we need a style def for the dialog system
150         if (StringUtils.isNotBlank(styleLocation)) {
151             addResource.addStyleSheet(facesContext, AddResource.HEADER_BEGIN, styleLocation + "/default.css");
152         } else {
153             addResource.addStyleSheet(facesContext, AddResource.HEADER_BEGIN, TimedNotifierRenderer.class, "css/default.css");
154         }
155     }
156 
157    
158 
159     /**
160      * dialog definition this one is needed for the dojoized dialog
161      *
162      * @param facesContext
163      * @param uiComponent
164      * @throws IOException
165      */
166     private void writeDialog(FacesContext facesContext, TimedNotifier uiComponent) throws IOException {
167         /*
168          * <div id="form1__idJsp1Notifier_Dialog" style="position:absolute;
169          * visibility: hidden;"> <div id="form1__idJsp1Notifier_Dialog_Content">
170          * values have changed do you really want to <br> submit the values
171          * </div> <input type="button" id="form1__idJsp1Notifier_Dialog_Yes"
172          * value="yes" /> <input type="button"
173          * id="form1__idJsp1Notifier_Dialog_No" value="no" /> </div>
174          */
175         TimedNotifier notifier = (TimedNotifier) uiComponent;
176         
177         String notifierClientId = uiComponent.getClientId(facesContext);
178         String replacedClientId = notifierClientId.replaceAll(":", "_");
179         String dialogVar        = replacedClientId + "Notifier_Dialog";
180         String yesText          = (String) uiComponent.getAttributes().get("yesText");
181    
182         yesText = (yesText == null) ? "Yes" : yesText;
183    
184         String content = uiComponent.getConfirmationMessage();
185         
186         String styleClass   = uiComponent.getStyleClass();
187         
188         ResponseWriter writer = facesContext.getResponseWriter();
189         writer.startElement(HTML.DIV_ELEM, uiComponent);
190         writer.writeAttribute(HTML.ID_ATTR, dialogVar, null);
191         writer.writeAttribute(HTML.STYLE_ATTR, "position:absolute; visibility: hidden;", null);
192         writer.writeAttribute(HTML.CLASS_ATTR, styleClass,null);
193         writer.startElement(HTML.DIV_ELEM, uiComponent);
194         writer.writeAttribute(HTML.CLASS_ATTR, styleClass,null);
195            
196         writer.startElement(HTML.DIV_ELEM, uiComponent);
197         writer.writeAttribute(HTML.ID_ATTR, dialogVar + "_Content", null);
198         
199         UIComponent contentComp = notifier.getContent();
200         if(contentComp == null)
201             writer.write(content);
202         else
203             RendererUtils.renderChild(facesContext, contentComp);
204         writer.endElement(HTML.DIV_ELEM);
205         writer.write(HTML.NBSP_ENTITY);
206         UIComponent confirmComp = notifier.getConfirm();
207         if(confirmComp != null)
208             RendererUtils.renderChild(facesContext, confirmComp);
209         else {
210             writer.startElement(HTML.INPUT_ELEM, uiComponent);
211             writer.writeAttribute(HTML.TYPE_ATTR, HTML.BUTTON_ELEM, null);
212             writer.writeAttribute(HTML.ID_ATTR, dialogVar + "_Yes", null);
213             writer.writeAttribute(HTML.VALUE_ATTR, yesText, null);
214             writer.endElement(HTML.INPUT_ELEM);
215         }
216         writer.write(HTML.NBSP_ENTITY);
217         writer.endElement(HTML.DIV_ELEM);
218         writer.endElement(HTML.DIV_ELEM);
219     }
220 }