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.RendererTypes;
23  import org.apache.myfaces.tobago.component.Tags;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.internal.component.AbstractUIGridLayout;
26  import org.apache.myfaces.tobago.internal.component.AbstractUIStyle;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.layout.MeasureList;
29  import org.apache.myfaces.tobago.renderkit.RendererBase;
30  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlRoleValues;
34  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
35  
36  import javax.faces.context.FacesContext;
37  import java.io.IOException;
38  
39  public class GridLayoutRenderer<T extends AbstractUIGridLayout> extends RendererBase<T> {
40  
41    @Override
42    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
43      final TobagoResponseWriter writer = getResponseWriter(facesContext);
44      final Markup markup = component.getMarkup();
45  
46      writer.startElement(HtmlElements.DIV);
47      writer.writeAttribute(HtmlAttributes.ROLE, HtmlRoleValues.PRESENTATION.toString(), false);
48      writer.writeIdAttribute(component.getClientId(facesContext));
49      writer.writeClassAttribute(
50          TobagoClass.GRID_LAYOUT,
51          TobagoClass.GRID_LAYOUT.createMarkup(markup),
52          markup != null && markup.contains(Markup.SPREAD) ? TobagoClass.SPREAD : null);
53  
54      final MeasureList columns = MeasureList.parse(component.getColumns());
55      final MeasureList rows = MeasureList.parse(component.getRows());
56  
57      final AbstractUIStyle style = (AbstractUIStyle) facesContext.getApplication().createComponent(
58          facesContext, Tags.style.componentType(), RendererTypes.Style.name());
59      style.setTransient(true);
60  
61      /*
62       * If the column attribute contains and 'auto' value but not an 'fr' value,
63       * the behavior of the 'auto' value is the same as a 'fr' value.
64       * So if there is only an 'auto' value we add a hidden 'fr' value.
65       * https://issues.apache.org/jira/browse/TOBAGO-2002
66       */
67      if (columns.stream().anyMatch(measure -> Measure.Unit.AUTO.equals(measure.getUnit()))
68          && columns.stream().noneMatch(measure -> Measure.Unit.FR.equals(measure.getUnit()))) {
69        columns.add(new Measure(1, Measure.Unit.FR));
70      }
71  
72      style.setGridTemplateColumns(columns.serialize());
73      style.setGridTemplateRows(rows.serialize());
74      component.getChildren().add(style);
75    }
76  
77    @Override
78    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
79      final TobagoResponseWriter writer = getResponseWriter(facesContext);
80  
81      writer.endElement(HtmlElements.DIV);
82    }
83  }