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.component;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.internal.layout.Grid;
25  import org.apache.myfaces.tobago.internal.layout.LayoutUtils;
26  import org.apache.myfaces.tobago.internal.layout.OriginCell;
27  import org.apache.myfaces.tobago.internal.util.StringUtils;
28  import org.apache.myfaces.tobago.layout.LayoutTokens;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import javax.faces.component.UIComponent;
34  import javax.faces.context.FacesContext;
35  import java.util.List;
36  
37  public abstract class AbstractUIGridLayout extends AbstractUILayoutBase implements Visual {
38  
39    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIGridLayout.class);
40  
41    public static final String COMPONENT_FAMILY = "org.apache.myfaces.tobago.GridLayout";
42  
43    private Grid grid;
44  
45    /**
46     * Initialize the grid and remove the current width and height values from the component, recursively.
47     */
48    public void init() {
49  
50      final UIComponent parent = getParent();
51      if (!parent.isRendered()) {
52        return;
53      }
54  
55      grid = new Grid(LayoutTokens.parse(getColumns()), LayoutTokens.parse(getRows()));
56  
57      final List<UIComponent> components = LayoutUtils.findLayoutChildren(parent);
58      for (final UIComponent component : components) {
59        final int columnSpan = ComponentUtils.getIntAttribute(component, Attributes.columnSpan, 1);
60        final int rowSpan = ComponentUtils.getIntAttribute(component, Attributes.rowSpan, 1);
61        grid.add(new OriginCell(component), columnSpan, rowSpan);
62        if (LOG.isDebugEnabled()) {
63          LOG.debug("\n" + grid);
64        }
65      }
66    }
67  
68    public abstract String getRows();
69  
70    public abstract void setRows(String rows);
71  
72    public abstract String getColumns();
73  
74    public abstract void setColumns(String columns);
75  
76    public abstract boolean isRigid();
77  
78    public Grid getGrid() {
79      if (grid == null) {
80        init();
81      }
82      return grid;
83    }
84  
85    @Override
86    public boolean getRendersChildren() {
87      return false;
88    }
89  
90    public String toString() {
91      final StringBuilder builder = new StringBuilder();
92      builder.append(getClass().getSimpleName()).append("#");
93      builder.append(getClientId(FacesContext.getCurrentInstance()));
94      builder.append("\n");
95      if (grid != null) {
96        builder.append(StringUtils.repeat("  ", 4));
97        builder.append("horiz.: ");
98        final LayoutTokens rows = grid.getRows();
99        for (int i = 0; i < rows.getSize(); i++) {
100         if (i != 0) {
101           builder.append(StringUtils.repeat("  ", 4 + 4));
102         }
103         builder.append(rows.get(i));
104         builder.append("\n");
105       }
106       builder.append(StringUtils.repeat("  ", 4));
107       builder.append("verti.: ");
108       final LayoutTokens columns = grid.getColumns();
109       for (int i = 0; i < columns.getSize(); i++) {
110         if (i != 0) {
111           builder.append(StringUtils.repeat("  ", 4 + 4));
112         }
113         builder.append(columns.get(i));
114         builder.append("\n");
115       }
116     }
117     builder.setLength(builder.length() - 1);
118     return builder.toString();
119   }
120 }