1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39
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
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 }