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  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Stack;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.context.ResponseWriter;
30  import javax.servlet.http.HttpServletRequest;
31  
32  import org.apache.myfaces.custom.dojo.DojoUtils;
33  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
34  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
35  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
36  
37  /**
38   * 
39   * @JSFRenderer
40   *   renderKitId = "HTML_BASIC" 
41   *   family = "javax.faces.Output"
42   *   type = "org.apache.myfaces.DojoSplitPaneRenderer"
43   *
44   * @author werpu
45   *
46   */
47  public class DojoSplitPaneRenderer extends DojoContentPaneRenderer {
48      protected void encodeJavascriptBegin(FacesContext context, UIComponent component) throws IOException {
49          super.encodeJavascriptBegin(context, component);
50          ResponseWriter writer = context.getResponseWriter();
51          //writer.startElement(HTML.SCRIPT_ELEM, component);
52          //writer.writeAttribute(HTML.TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
53          //writer.write("dojo.hostenv.setModulePrefix('org.apache.myfaces.dojolayouts', '../dojolayouts.ResourceLoader');\n");
54          //writer.endElement(HTML.SCRIPT_ELEM);
55          DojoUtils.addRequire(context, component, "dojo.widget.SplitContainer");
56  
57          //DojoUtils.addRequire(context, component, "org.apache.myfaces.dojolayouts.MyfacesSplitContainer");
58  
59      }
60  
61      public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
62          if ((context == null) || (component == null)) {
63              throw new NullPointerException();
64          }
65  
66          Boolean rendered = (Boolean) component.getAttributes().get("rendered");
67  
68          if ((rendered != null) && (!rendered.booleanValue()))
69              return;
70  
71          super.encodeBegin(context, component);
72      }
73  
74      public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
75          RendererUtils.renderChildren(context, component);
76          HtmlRendererUtils.writePrettyLineSeparator(context);
77          Stack stack = getChildsStack(context, component);
78          List children = component.getChildren();
79  
80          for (Iterator cit = children.iterator(); cit.hasNext();) {
81              UIComponent child = (UIComponent) cit.next();
82              if (!child.isRendered())
83                  continue;
84              if (child instanceof DojoContentPane) {
85                  stack.push(DojoUtils.calculateWidgetVarName(context, child));
86              }
87          }
88      }
89  
90      private Stack getChildsStack(FacesContext context, UIComponent component) {
91          //Stack menuStack = (Stack) ((HttpServletRequest) context.getExternalContext().getRequest()).getAttribute(component.getClientId(context)
92          //        + DojoSplitPaneRenderer.class.toString());
93          Stack menuStack = (Stack) context.getExternalContext().getRequestMap().get(component.getClientId(context)
94                  + DojoSplitPaneRenderer.class.toString());
95          if (menuStack != null)
96              return menuStack;
97  
98          menuStack = new Stack();
99          //((HttpServletRequest) context.getExternalContext().getRequest()).setAttribute(component.getClientId(context) + DojoSplitPaneRenderer.class.toString(),
100         //        menuStack);
101         context.getExternalContext().getRequestMap().put(
102                 component.getClientId(context) + DojoSplitPaneRenderer.class.toString(),
103                 menuStack);
104         return menuStack;
105     }
106 
107     public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
108         super.encodeEnd(context, component);
109     }
110 
111     protected void encodeJavascriptEnd(FacesContext context, UIComponent component) throws IOException {
112         // called by super.encodeEnd
113         //"sizeShare",
114         String [] attributeNames = {"activeSizing", "orientation", 
115                 "sizerWidth", "persist", "startPoint", "lastPoint",  "widgetVar", "widgetId"};
116         
117         
118         String panelComponentVar = DojoUtils.calculateWidgetVarName(component.getClientId(context));
119         
120         DojoUtils.renderWidgetInitializationCode(context, component, "SplitContainer", attributeNames);
121         ResponseWriter writer = context.getResponseWriter();
122         writer.startElement(HTML.SCRIPT_ELEM, component);
123         writer.writeAttribute(HTML.TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
124 
125         Stack stack = this.getChildsStack(context, component);
126         while (!stack.isEmpty()) {
127             String javascriptVar = (String) stack.pop();
128             writer.write(panelComponentVar + ".addChild(" + javascriptVar + ");\n");
129         }
130         writer.write(panelComponentVar+".postCreate();\n");
131         
132         writer.endElement(HTML.SCRIPT_ELEM);
133     }
134 
135     public boolean getRendersChildren() {
136         return true;
137     }
138 
139 }