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.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      // to prevent instantiation
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          // Child seems to be transparent for layout, like UIForm. 
70          // So we try to add the inner components.
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) { // to not override set sizes
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) { // to not override set sizes
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 }