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.standard.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIGridLayout;
24  import org.apache.myfaces.tobago.internal.layout.Cell;
25  import org.apache.myfaces.tobago.internal.layout.Grid;
26  import org.apache.myfaces.tobago.internal.layout.OriginCell;
27  import org.apache.myfaces.tobago.layout.LayoutToken;
28  import org.apache.myfaces.tobago.layout.LayoutTokens;
29  import org.apache.myfaces.tobago.layout.Measure;
30  import org.apache.myfaces.tobago.renderkit.RendererBase;
31  import org.apache.myfaces.tobago.renderkit.css.Classes;
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.HtmlRoleValues;
36  import org.apache.myfaces.tobago.renderkit.html.JsonUtils;
37  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
38  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
39  import org.apache.myfaces.tobago.util.ComponentUtils;
40  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import javax.faces.component.UIComponent;
45  import javax.faces.context.FacesContext;
46  import java.io.IOException;
47  
48  public class GridLayoutRenderer extends RendererBase {
49  
50    private static final Logger LOG = LoggerFactory.getLogger(GridLayoutRenderer.class);
51  
52    // todo: may use: http://www.w3.org/TR/css3-grid-layout/ (currently only IE 10 and higher)
53  
54    @Override
55    public void encodeBegin(final FacesContext facesContext, final UIComponent component) throws IOException {
56      final AbstractUIGridLayout gridLayout = (AbstractUIGridLayout) component;
57      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
58  
59      writer.startElement(HtmlElements.TABLE);
60      writer.writeAttribute(HtmlAttributes.ROLE, HtmlRoleValues.PRESENTATION.toString(), false);
61      writer.writeClassAttribute(Classes.create(gridLayout));
62  
63      final StringBuilder builder = new StringBuilder();
64  
65      builder.append("{\"columns\":");
66      JsonUtils.encode(gridLayout.getGrid().getColumns(), builder);
67      builder.append(",");
68  
69      builder.append("\"rows\":");
70      JsonUtils.encode(gridLayout.getGrid().getRows(), builder);
71      builder.append("}");
72  
73      writer.writeAttribute(DataAttributes.LAYOUT, builder.toString(), true);
74  
75      writer.startElement(HtmlElements.COLGROUP);
76      final LayoutTokens columns = gridLayout.getGrid().getColumns();
77      for (LayoutToken column : columns.getTokens()) {
78          writer.startElement(HtmlElements.COL);
79          writer.endElement(HtmlElements.COL);
80      }
81      writer.endElement(HtmlElements.COLGROUP);
82      writer.startElement(HtmlElements.TBODY);
83    }
84  
85    @Override
86    public void encodeChildren(final FacesContext facesContext, final UIComponent component) throws IOException {
87      final AbstractUIGridLayout gridLayout = (AbstractUIGridLayout) component;
88  /*
89      final UIComponent container = gridLayout.getParent();
90      if (!container.isRendered()) {
91        return;
92      }
93      RenderUtils.encodeChildren(facesContext, container);
94  */
95  
96      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
97      final Grid grid = gridLayout.getGrid();
98      final int columns = grid.getColumnCount();
99      final int rows = grid.getRowCount();
100 
101     for (int i = 0; i < rows; i++) {
102       writer.startElement(HtmlElements.TR);
103       for (int j = 0; j < columns; j++) {
104         final Cell cell = grid.getCell(j, i);
105         if (cell instanceof OriginCell) {
106           writer.startElement(HtmlElements.TD);
107           final int columnSpan = cell.getColumnSpan();
108           if (columnSpan > 1) {
109             writer.writeAttribute(HtmlAttributes.COLSPAN, columnSpan);
110           }
111           final int rowSpan = cell.getRowSpan();
112           if (rowSpan > 1) {
113             writer.writeAttribute(HtmlAttributes.ROWSPAN, rowSpan);
114           }
115 
116           final UIComponent element = cell.getComponent();
117           StringBuilder builder = new StringBuilder();
118           builder.append("{");
119           final Measure width = Measure.valueOf(ComponentUtils.getAttribute(element, Attributes.width));
120           if (width != null) {
121             builder.append("\"width\":");
122 //            builder.append("{\"pixel\":");
123             builder.append(width.serialize());
124 //            builder.append("}");
125             builder.append(",");
126           }
127           final Measure height = Measure.valueOf(ComponentUtils.getAttribute(element, Attributes.height));
128           if (height != null) {
129             builder.append("\"height\":");
130 //            builder.append("{\"pixel\":");
131             builder.append(height.serialize());
132 //            builder.append("}");
133             builder.append(",");
134           }
135           if (builder.length() > 1) {
136             builder.deleteCharAt(builder.length() - 1);
137           }
138           // todo: add more attributes
139           builder.append("}");
140 
141           if (builder.length() > 2) { // empty is not need to render
142             ComponentUtils.putDataAttribute(element, "tobago-layout", builder.toString());
143           }
144           RenderUtils.encode(facesContext, element);
145 
146           writer.endElement(HtmlElements.TD);
147         }
148       }
149       writer.endElement(HtmlElements.TR);
150     }
151   }
152 
153   @Override
154   public void encodeEnd(final FacesContext facesContext, final UIComponent component) throws IOException {
155     final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
156 
157     writer.endElement(HtmlElements.TBODY);
158     writer.endElement(HtmlElements.TABLE);
159   }
160 }