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.layout;
21
22 import org.apache.myfaces.tobago.layout.LayoutBase;
23 import org.apache.myfaces.tobago.layout.LayoutBox;
24 import org.apache.myfaces.tobago.layout.LayoutComponent;
25 import org.apache.myfaces.tobago.layout.LayoutContainer;
26 import org.apache.myfaces.tobago.layout.Measure;
27 import org.apache.myfaces.tobago.layout.Orientation;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import javax.faces.component.UIComponent;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.StringTokenizer;
35 import java.util.regex.Pattern;
36
37 public final class LayoutUtils {
38
39 private static final Logger LOG = LoggerFactory.getLogger(LayoutUtils.class);
40
41 private static final Pattern TOKEN_PATTERN = Pattern.compile("^(\\d*px|\\d*\\*|\\d*%|auto|fixed)$");
42
43 private LayoutUtils() {
44
45 }
46
47 public static boolean checkTokens(String columns) {
48 StringTokenizer st = new StringTokenizer(columns, ";");
49 while (st.hasMoreTokens()) {
50 String token = st.nextToken();
51 if (!TOKEN_PATTERN.matcher(token).matches()) {
52 return false;
53 }
54 }
55 return true;
56 }
57
58 public static List<LayoutComponent> findLayoutChildren(LayoutContainer container) {
59 List<LayoutComponent> result = new ArrayList<LayoutComponent>();
60 addLayoutChildren((UIComponent) container, result);
61 return result;
62 }
63
64 private static void addLayoutChildren(UIComponent component, List<LayoutComponent> result) {
65 for (UIComponent child : component.getChildren()) {
66 if (child instanceof LayoutComponent) {
67 result.add((LayoutComponent) child);
68 } else {
69
70
71 addLayoutChildren(child, result);
72 }
73 }
74 }
75
76 public static Measure getBorderEnd(Orientation orientation, LayoutBox container) {
77 return orientation == Orientation.HORIZONTAL ? container.getBorderRight() : container.getBorderBottom();
78 }
79
80 public static Measure getBorderBegin(Orientation orientation, LayoutBox container) {
81 return orientation == Orientation.HORIZONTAL ? container.getBorderLeft() : container.getBorderTop();
82 }
83
84 public static Measure getPaddingEnd(Orientation orientation, LayoutBox container) {
85 return orientation == Orientation.HORIZONTAL ? container.getPaddingRight() : container.getPaddingBottom();
86 }
87
88 public static Measure getPaddingBegin(Orientation orientation, LayoutBox container) {
89 return orientation == Orientation.HORIZONTAL ? container.getPaddingLeft() : container.getPaddingTop();
90 }
91
92 public static Measure getCurrentSize(Orientation orientation, LayoutBase component) {
93 return orientation == Orientation.HORIZONTAL ? component.getCurrentWidth() : component.getCurrentHeight();
94 }
95
96 public static void setCurrentSize(Orientation orientation, LayoutBase component, Measure size) {
97 if (orientation == Orientation.HORIZONTAL) {
98 final Measure width = component.getWidth();
99 if (width != null) {
100 size = width;
101 } else {
102 final Measure maximumWidth = component.getMaximumWidth();
103 if (size.greaterThan(maximumWidth)) {
104 size = maximumWidth;
105 }
106 final Measure minimumWidth = component.getMinimumWidth();
107 if (size.lessThan(minimumWidth)) {
108 size = minimumWidth;
109 }
110 }
111 component.setCurrentWidth(size);
112 } else {
113 final Measure height = component.getHeight();
114 if (height != null) {
115 size = height;
116 } else {
117 final Measure maximumHeight = component.getMaximumHeight();
118 if (size.greaterThan(maximumHeight)) {
119 size = maximumHeight;
120 }
121 final Measure minimumHeight = component.getMinimumHeight();
122 if (size.lessThan(minimumHeight)) {
123 size = minimumHeight;
124 }
125 }
126 component.setCurrentHeight(size);
127 }
128 }
129 }