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