View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.layout;
23  
24  import java.io.IOException;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  
30  import org.primefaces.extensions.util.Attrs;
31  import org.primefaces.renderkit.CoreRenderer;
32  
33  /**
34   * Renderer for the {@link LayoutPane} component.
35   *
36   * @author Oleg Varaksin / last modified by Melloware
37   * @version $Revision$
38   * @since 0.6.0
39   */
40  public class LayoutPaneRenderer extends CoreRenderer {
41  
42      @Override
43      public void encodeEnd(final FacesContext fc, final UIComponent component) throws IOException {
44          final ResponseWriter writer = fc.getResponseWriter();
45          final LayoutPane layoutPane = (LayoutPane) component;
46  
47          final String position = layoutPane.getPosition();
48          final String combinedPosition;
49          UIComponent parent = layoutPane.getParent();
50  
51          final StringBuilder combinedPositionBuilder = position == null ? null : new StringBuilder(position);
52          while (parent instanceof LayoutPane) {
53              assert combinedPositionBuilder != null;
54              combinedPositionBuilder.insert(0, ((LayoutPane) parent).getPosition() + Layout.POSITION_SEPARATOR);
55              parent = parent.getParent();
56          }
57          assert combinedPositionBuilder != null;
58          combinedPosition = combinedPositionBuilder.toString();
59  
60          // save combined position
61          layoutPane.setCombinedPosition(combinedPosition);
62  
63          boolean hasSubPanes = false;
64          for (final UIComponent subChild : layoutPane.getChildren()) {
65              // check first level
66              if (hasSubPanes) {
67                  break;
68              }
69  
70              if (subChild instanceof LayoutPane) {
71                  if (!subChild.isRendered()) {
72                      continue;
73                  }
74  
75                  hasSubPanes = true;
76              }
77              else {
78                  for (final UIComponent subSubChild : subChild.getChildren()) {
79                      // check second level
80                      if (subSubChild instanceof LayoutPane) {
81                          if (!subSubChild.isRendered()) {
82                              continue;
83                          }
84  
85                          hasSubPanes = true;
86  
87                          break;
88                      }
89                  }
90              }
91          }
92  
93          final UIComponent header = layoutPane.getFacet("header");
94  
95          writer.startElement("div", null);
96          writer.writeAttribute("id", layoutPane.getClientId(fc), "id");
97          if (hasSubPanes) {
98              writer.writeAttribute(Attrs.CLASS, Layout.STYLE_CLASS + position + " " + Layout.STYLE_CLASS_PANE_WITH_SUBPANES, null);
99          }
100         else {
101             if (header != null) {
102                 writer.writeAttribute(Attrs.CLASS, Layout.STYLE_CLASS + position + " " + Layout.STYLE_CLASS_PANE, null);
103             }
104             else {
105                 if (layoutPane.getStyleClassContent() != null) {
106                     writer.writeAttribute(Attrs.CLASS,
107                                 Layout.STYLE_CLASS + position + " " + Layout.STYLE_CLASS_PANE + " "
108                                             + Layout.STYLE_CLASS_PANE_CONTENT + " "
109                                             + layoutPane.getStyleClassContent(),
110                                 null);
111                 }
112                 else {
113                     writer.writeAttribute(Attrs.CLASS,
114                                 Layout.STYLE_CLASS + position + " " + Layout.STYLE_CLASS_PANE + " "
115                                             + Layout.STYLE_CLASS_PANE_CONTENT,
116                                 null);
117                 }
118 
119                 if (layoutPane.getStyleContent() != null) {
120                     writer.writeAttribute(Attrs.STYLE, layoutPane.getStyleContent(), null);
121                 }
122             }
123         }
124 
125         writer.writeAttribute("data-combinedposition", combinedPosition, null);
126 
127         // encode header
128         if (header != null) {
129             String headerStyleClass = getStyleClassBuilder(fc)
130                         .add(Layout.STYLE_CLASS_PANE_HEADER)
131                         .add(layoutPane.getStyleClassHeader())
132                         .build();
133             writer.startElement("div", null);
134             writer.writeAttribute(Attrs.CLASS, headerStyleClass, null);
135 
136             if (layoutPane.getStyleHeader() != null) {
137                 writer.writeAttribute(Attrs.STYLE, layoutPane.getStyleHeader(), null);
138             }
139 
140             header.encodeAll(fc);
141 
142             writer.endElement("div");
143         }
144 
145         String contentStyleClass = getStyleClassBuilder(fc)
146                     .add(Layout.STYLE_CLASS_LAYOUT_CONTENT)
147                     .add(Layout.STYLE_CLASS_PANE_CONTENT)
148                     .add(layoutPane.getStyleClassContent())
149                     .build();
150 
151         // encode content
152         if (header != null) {
153             writer.startElement("div", null);
154             writer.writeAttribute(Attrs.CLASS, contentStyleClass, null);
155 
156             if (layoutPane.getStyleContent() != null) {
157                 writer.writeAttribute(Attrs.STYLE, "border:none; " + layoutPane.getStyleContent(), null);
158             }
159             else {
160                 writer.writeAttribute(Attrs.STYLE, "border:none", null);
161             }
162 
163             renderChildren(fc, layoutPane);
164 
165             writer.endElement("div");
166         }
167         else {
168             renderChildren(fc, layoutPane);
169         }
170 
171         writer.endElement("div");
172     }
173 
174     @Override
175     public boolean getRendersChildren() {
176         return true;
177     }
178 
179     @Override
180     public void encodeChildren(final FacesContext fc, final UIComponent component) {
181         // nothing to do
182     }
183 }