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.tobago.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.Facets;
23  import org.apache.myfaces.tobago.component.UIPanel;
24  import org.apache.myfaces.tobago.component.UIReload;
25  import org.apache.myfaces.tobago.internal.component.AbstractUIPanel;
26  import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
29  import org.apache.myfaces.tobago.renderkit.css.Classes;
30  import org.apache.myfaces.tobago.renderkit.css.Position;
31  import org.apache.myfaces.tobago.renderkit.css.Style;
32  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
34  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
35  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
36  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
37  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import javax.faces.component.UIComponent;
42  import javax.faces.context.FacesContext;
43  import javax.faces.context.ResponseWriter;
44  import java.io.IOException;
45  
46  public class PanelRenderer extends LayoutComponentRendererBase {
47  
48    private static final Logger LOG = LoggerFactory.getLogger(PanelRenderer.class);
49  
50    @Override
51    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
52      super.prepareRender(facesContext, component);
53      HtmlRendererUtils.renderDojoDndSource(facesContext, component);
54    }
55  
56    @Override
57    public boolean getRendersChildren() {
58      return true;
59    }
60  
61    @Override
62    public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException {
63      UIPanel component = (UIPanel) uiComponent;
64      for (UIComponent child : component.getChildren()) {
65        RenderUtils.encode(facesContext, child);
66      }
67    }
68  
69    @Override
70    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
71      
72      // UIPanel or UICell (deprecated)
73      AbstractUIPanel panel = (AbstractUIPanel) component;
74      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
75  
76      String clientId = panel.getClientId(facesContext);
77      writer.startElement(HtmlElements.DIV, panel);
78      HtmlRendererUtils.renderDojoDndItem(panel, writer, true);
79      writer.writeIdAttribute(clientId);
80      writer.writeClassAttribute(Classes.create(panel));
81      HtmlRendererUtils.writeDataAttributes(facesContext, writer, panel);
82      Style style = new Style(facesContext, panel);
83      // XXX hotfix for panels in sheets
84      if (style.getPosition() == null) {
85        style.setPosition(Position.RELATIVE);
86      }
87      writer.writeStyleAttribute(style);
88      if (panel instanceof UIPanel && ((UIPanel) panel).getTip() != null) {
89        writer.writeAttribute(HtmlAttributes.TITLE, ((UIPanel) panel).getTip(), true);
90      }
91  
92      // TODO check ajax id?
93      if (!FacesContextUtils.isAjax(facesContext)) {
94        UIComponent facetReload = panel.getFacet(Facets.RELOAD);
95        if (facetReload != null && facetReload instanceof UIReload && facetReload.isRendered()) {
96          UIReload update = (UIReload) facetReload;
97          writer.writeAttribute(DataAttributes.RELOAD, Integer.toString(update.getFrequency()), false);
98        }
99      }
100     HtmlRendererUtils.renderCommandFacet(panel, facesContext, writer);
101     HtmlRendererUtils.encodeContextMenu(facesContext, writer, panel);
102 
103     //HtmlRendererUtils.checkForCommandFacet(panel, facesContext, writer);
104 
105     final Measure borderLeft = panel.getBorderLeft();
106     final Measure borderRight = panel.getBorderRight();
107     final Measure borderTop = panel.getBorderTop();
108     final Measure borderBottom = panel.getBorderBottom();
109 
110     if (borderLeft.greaterThan(Measure.ZERO) || borderRight.greaterThan(Measure.ZERO)
111         || borderTop.greaterThan(Measure.ZERO) || borderBottom.greaterThan(Measure.ZERO)) {
112       writer.startElement(HtmlElements.DIV, panel);
113       writer.writeClassAttribute(Classes.create(panel, "content")); // needed to be scrollable inside of the panel
114       final Style inner = new Style(facesContext, panel);
115       // Todo: FIXME (be null may occur in sheets)
116       if (inner.getWidth() != null) {
117         inner.setWidth(inner.getWidth().subtract(borderLeft).subtract(borderRight));
118       }
119       // Todo: FIXME (be null may occur in sheets)
120       if (inner.getHeight() != null) {
121         inner.setHeight(inner.getHeight().subtract(borderTop).subtract(borderBottom));
122       }
123       inner.setLeft(borderLeft);
124       inner.setTop(borderTop);
125       writer.writeStyleAttribute(inner);
126     }
127   }
128 
129   @Override
130   public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
131     ResponseWriter writer = facesContext.getResponseWriter();
132     AbstractUIPanel panel = (AbstractUIPanel) component;
133     
134     final Measure borderLeft = panel.getBorderLeft();
135     final Measure borderRight = panel.getBorderRight();
136     final Measure borderTop = panel.getBorderTop();
137     final Measure borderBottom = panel.getBorderBottom();
138 
139     if (borderLeft.greaterThan(Measure.ZERO) || borderRight.greaterThan(Measure.ZERO)
140         || borderTop.greaterThan(Measure.ZERO) || borderBottom.greaterThan(Measure.ZERO)) {
141     writer.endElement(HtmlElements.DIV);
142     }
143     writer.endElement(HtmlElements.DIV);
144   }
145 }