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.RendererTypes;
23  import org.apache.myfaces.tobago.component.Tags;
24  import org.apache.myfaces.tobago.component.Visual;
25  import org.apache.myfaces.tobago.layout.AlignItems;
26  import org.apache.myfaces.tobago.layout.Measure;
27  import org.apache.myfaces.tobago.layout.MeasureList;
28  import org.apache.myfaces.tobago.util.ComponentUtils;
29  
30  import javax.faces.component.UIComponent;
31  import javax.faces.context.FacesContext;
32  import javax.faces.event.ComponentSystemEvent;
33  import javax.faces.event.ListenerFor;
34  import javax.faces.event.PostAddToViewEvent;
35  
36  /**
37   * {@link org.apache.myfaces.tobago.internal.taglib.component.FlexLayoutTagDeclaration}
38   *
39   * @since 3.0.0
40   */
41  @ListenerFor(systemEventClass = PostAddToViewEvent.class)
42  public abstract class AbstractUIFlexLayout extends AbstractUILayoutBase {
43  
44    public static final String COMPONENT_FAMILY = "org.apache.myfaces.tobago.FlexLayout";
45  
46    @Override
47    public void processEvent(final ComponentSystemEvent event) {
48  
49      if (event instanceof PostAddToViewEvent) {
50        final FacesContext facesContext = getFacesContext();
51  
52        final boolean horizontal = isHorizontal();
53  
54        MeasureList tokens = horizontal ? getColumns() : getRows();
55        if (tokens == null) {
56          tokens = new MeasureList();
57          tokens.add(Measure.AUTO);
58        }
59  
60        if (tokens.getSize() > 0) {
61          int i = 0;
62  
63          for (final UIComponent child : getChildren()) {
64            if (child instanceof Visual) {
65              if (i >= tokens.getSize()) {
66                i = 0;
67              }
68              final Measure token = tokens.get(i++);
69              final Measure.Unit unit = token.getUnit();
70              if (unit != Measure.Unit.AUTO) {
71                AbstractUIStyle style = ComponentUtils.findChild(child, AbstractUIStyle.class);
72                if (style == null) {
73                  style = (AbstractUIStyle) facesContext.getApplication().createComponent(
74                      facesContext, Tags.style.componentType(), RendererTypes.Style.name());
75                  style.setTransient(true);
76                  child.getChildren().add(style);
77                }
78                if (unit == Measure.Unit.FR) {
79                  final float factor = token.getValue();
80                  style.setFlexGrow(factor);
81                  style.setFlexShrink(0);
82                  style.setFlexBasis(Measure.ZERO);
83  //                style.setFlexBasis(Measure.AUTO); // is unbalanced when mixing e.g. <p> with <textarea>
84                } else {
85                  if (horizontal) {
86                    style.setWidth(token);
87                  } else {
88                    style.setHeight(token);
89                  }
90                }
91              }
92            }
93          }
94        }
95      }
96    }
97  
98    public abstract MeasureList getColumns();
99  
100   public abstract MeasureList getRows();
101 
102   public abstract AlignItems getAlignItems();
103 
104   public boolean isHorizontal() {
105     return getRows() == null;
106   }
107 }