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(final String columns) {
48      final StringTokenizer st = new StringTokenizer(columns, ";");
49      while (st.hasMoreTokens()) {
50        final 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(final LayoutContainer container) {
59      final List<LayoutComponent> result = new ArrayList<LayoutComponent>();
60      addLayoutChildren((UIComponent) container, result);
61      return result;
62    }
63  
64    private static void addLayoutChildren(final UIComponent component, final List<LayoutComponent> result) {
65      for (final 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      final UIComponent child = component.getFacet(UIComponent.COMPOSITE_FACET_NAME);
76      if (child instanceof LayoutComponent) {
77        result.add((LayoutComponent) child);
78      } else if (child != null) {
79        // Child seems to be transparent for layout, like UIForm.
80        // So we try to add the inner components.
81        addLayoutChildren(child, result);
82      }
83    }
84  
85    public static Measure getBorderEnd(final Orientation orientation, final LayoutBox container) {
86      return orientation == Orientation.HORIZONTAL ? container.getBorderRight() : container.getBorderBottom();
87    }
88  
89    public static Measure getBorderBegin(final Orientation orientation, final LayoutBox container) {
90      return orientation == Orientation.HORIZONTAL ? container.getBorderLeft() : container.getBorderTop();
91    }
92  
93    public static Measure getPaddingEnd(final Orientation orientation, final LayoutBox container) {
94      return orientation == Orientation.HORIZONTAL ? container.getPaddingRight() : container.getPaddingBottom();
95    }
96  
97    public static Measure getPaddingBegin(final Orientation orientation, final LayoutBox container) {
98      return orientation == Orientation.HORIZONTAL ? container.getPaddingLeft() : container.getPaddingTop();
99    }
100 
101   public static Measure getCurrentSize(final Orientation orientation, final LayoutBase component) {
102     return orientation == Orientation.HORIZONTAL ? component.getCurrentWidth() : component.getCurrentHeight();
103   }
104 
105   public static void setCurrentSize(final Orientation orientation, final LayoutBase component, Measure size) {
106     if (orientation == Orientation.HORIZONTAL) {
107       final Measure width = component.getWidth();
108       if (width != null) { // to not override set sizes
109         size = width;
110       } else {
111         final Measure maximumWidth = component.getMaximumWidth();
112         if (size.greaterThan(maximumWidth)) {
113           size = maximumWidth;
114         }
115         final Measure minimumWidth = component.getMinimumWidth();
116         if (size.lessThan(minimumWidth)) {
117           size = minimumWidth;
118         }
119       }
120       component.setCurrentWidth(size);
121     } else {
122       final Measure height = component.getHeight();
123       if (height != null) { // to not override set sizes
124         size = height;
125       } else {
126         final Measure maximumHeight = component.getMaximumHeight();
127         if (size.greaterThan(maximumHeight)) {
128           size = maximumHeight;
129         }
130         final Measure minimumHeight = component.getMinimumHeight();
131         if (size.lessThan(minimumHeight)) {
132           size = minimumHeight;
133         }
134       }
135       component.setCurrentHeight(size);
136     }
137   }
138 }