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