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.internal.renderkit.renderer;
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.context.Markup;
26  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
27  import org.apache.myfaces.tobago.internal.util.JsonUtils;
28  import org.apache.myfaces.tobago.internal.util.RenderUtils;
29  import org.apache.myfaces.tobago.model.CollapseMode;
30  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
31  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
34  import org.apache.myfaces.tobago.util.ComponentUtils;
35  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
36  
37  import javax.faces.component.UIComponent;
38  import javax.faces.context.FacesContext;
39  import java.io.IOException;
40  
41  public class PanelRenderer extends PanelRendererBase {
42  
43    @Override
44    public void encodeBegin(final FacesContext facesContext, final UIComponent component) throws IOException {
45  
46      final UIPanel panel = (UIPanel) component;
47      final TobagoResponseWriter writer = getResponseWriter(facesContext);
48      final String clientId = panel.getClientId(facesContext);
49      final boolean collapsed = panel.isCollapsed();
50      final Markup markup = panel.getMarkup();
51  
52      writer.startElement(HtmlElements.DIV);
53      writer.writeIdAttribute(clientId);
54      writer.writeAttribute(DataAttributes.MARKUP, JsonUtils.encode(markup), false);
55  
56      writer.writeClassAttribute(
57          TobagoClass.PANEL,
58          TobagoClass.PANEL.createMarkup(markup),
59          collapsed ? TobagoClass.COLLAPSED : null,
60          panel.getCustomClass(),
61          markup != null && markup.contains(Markup.SPREAD) ? TobagoClass.SPREAD : null);
62  
63      HtmlRendererUtils.writeDataAttributes(facesContext, writer, panel);
64      final String tip = panel.getTip();
65      if (tip != null) {
66        writer.writeAttribute(HtmlAttributes.TITLE, tip, true);
67      }
68  
69      final UIComponent reloadFacet = ComponentUtils.getFacet(panel, Facets.reload);
70      if (reloadFacet instanceof UIReload && reloadFacet.isRendered()) {
71        final UIReload update = (UIReload) reloadFacet;
72        writer.writeAttribute(DataAttributes.RELOAD, Integer.toString(update.getFrequency()), false);
73      }
74  
75      writer.writeCommandMapAttribute(JsonUtils.encode(RenderUtils.getBehaviorCommands(facesContext, panel)));
76  
77      if (panel.getCollapsedMode() != CollapseMode.none) {
78        encodeHidden(writer, clientId, collapsed);
79      }
80    }
81  
82    @Override
83    public void encodeEnd(final FacesContext facesContext, final UIComponent component) throws IOException {
84      final TobagoResponseWriter writer = getResponseWriter(facesContext);
85      writer.endElement(HtmlElements.DIV);
86    }
87  }