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  
20  package org.apache.myfaces.custom.dojolayouts;
21  
22  import java.io.IOException;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  import javax.faces.context.ResponseWriter;
27  
28  import org.apache.myfaces.component.StyleAware;
29  import org.apache.myfaces.custom.dojo.DojoConfig;
30  import org.apache.myfaces.custom.dojo.DojoUtils;
31  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
32  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
33  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
34  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
35  
36  /**
37   * Simple renderer to the dojo content pane renders a simple div and the
38   * javascripted initializer we do not use the dojo taglib mechanism here due to
39   * the fact that the dojo tag system is incompatible with adf and other projects
40   * 
41   * @JSFRenderer
42   *   renderKitId = "HTML_BASIC" 
43   *   family = "javax.faces.Output"
44   *   type = "org.apache.myfaces.DojoContentPaneRenderer"
45   *
46   * @author werpu
47   * 
48   */
49  public class DojoContentPaneRenderer extends HtmlRenderer {
50  
51      public void decode(FacesContext context, UIComponent component) {
52          super.decode(context, component);
53  
54      }
55  
56      protected void encodeJavascriptBegin(FacesContext context, UIComponent component) throws IOException {
57          String javascriptLocation = (String) component.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
58          try {
59              if (javascriptLocation != null) {
60                  DojoUtils.addMainInclude(context, component, javascriptLocation, new DojoConfig());
61              } else {
62                  DojoUtils.addMainInclude(context, component, null, new DojoConfig());
63              }
64          }
65  
66          catch (IOException e) {
67              e.printStackTrace();
68          }
69          DojoUtils.addRequire(context, component, "dojo.widget.ContentPane");
70      }
71  
72      public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
73          if ((context == null) || (component == null)) {
74              throw new NullPointerException();
75          }
76  
77          Boolean rendered = (Boolean) component.getAttributes().get("rendered");
78  
79          if ((rendered != null) && (!rendered.booleanValue()))
80              return;
81          encodeJavascriptBegin(context, component);
82  
83          super.encodeBegin(context, component);
84  
85          ResponseWriter writer = context.getResponseWriter();
86          writer.startElement(HTML.DIV_ELEM, component);
87          HtmlRendererUtils.writeIdIfNecessary(writer, component, context);
88  
89          StyleAware pane = (StyleAware) component;
90  
91          String styleClass = pane.getStyleClass();
92          String style = pane.getStyle();
93          if (null != styleClass) {
94              writer.writeAttribute(HTML.CLASS_ATTR, styleClass, null);
95          }
96          if (null != style) {
97              writer.writeAttribute(HTML.STYLE_ATTR, style, null);
98          }
99  
100     }
101 
102     public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
103         // RendererUtils.renderChildren(context, component);
104         // HtmlRendererUtils.writePrettyLineSeparator(context);
105         super.encodeChildren(context, component);
106     }
107 
108     public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
109         super.encodeEnd(context, component);
110         ResponseWriter writer = context.getResponseWriter();
111         writer.endElement(HTML.DIV_ELEM);
112         encodeJavascriptEnd(context, component);
113     }
114 
115     protected void encodeJavascriptEnd(FacesContext context, UIComponent component) throws IOException {
116         
117         String [] attributeNames = {"sizeShare", "id", "adjustPaths", "executeScripts",
118                 "extractContent", "handler", "href", "layoutAlign",
119                 "parseContent", "preload", "refreshOnShow", "widgetVar", "widgetId"};
120 
121         DojoUtils.renderWidgetInitializationCode(context, component, "ContentPane", attributeNames);
122     }
123 
124     public boolean getRendersChildren() {
125         return true;
126     }
127 
128 }