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