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.component.Facets;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.util.ComponentUtils;
25  
26  import javax.faces.component.UIComponent;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.StringTokenizer;
30  import java.util.regex.Pattern;
31  
32  public final class LayoutUtils {
33  
34    private static final Pattern TOKEN_PATTERN = Pattern.compile("^(\\d*px|\\d*\\*|\\d*%|auto|fixed)$");
35  
36    private LayoutUtils() {
37      // to prevent instantiation
38    }
39  
40    public static boolean checkTokens(final String columns) {
41      final StringTokenizer st = new StringTokenizer(columns, ";");
42      while (st.hasMoreTokens()) {
43        final String token = st.nextToken();
44        if (!TOKEN_PATTERN.matcher(token).matches()) {
45          return false;
46        }
47      }
48      return true;
49    }
50  
51    public static List<UIComponent> findLayoutChildren(final UIComponent container) {
52      final List<UIComponent> result = new ArrayList<UIComponent>();
53      addLayoutChildren(container, result);
54      return result;
55    }
56  
57    private static void addLayoutChildren(final UIComponent component, final List<UIComponent> result) {
58      for (final UIComponent child : component.getChildren()) {
59        if (child instanceof Visual) {
60          result.add(child);
61        } else {
62          // Child seems to be transparent for layout, like UIForm. 
63          // So we try to add the inner components.
64          addLayoutChildren(child, result);
65        }
66      }
67  
68      final UIComponent child = component.getFacet(UIComponent.COMPOSITE_FACET_NAME);
69      if (child instanceof Visual) {
70        result.add(child);
71      } else if (child != null) {
72        // Child seems to be transparent for layout, like UIForm.
73        // So we try to add the inner components.
74        addLayoutChildren(child, result);
75      }
76    }
77  
78    /**
79     * This is needed, when the layout manager is set by a facet.
80     * This should be only used by the classic &lt;tc:gridLayout&gt;
81     * @since 3.0.0
82     * @deprecated since 3.0.0
83     */
84    @Deprecated
85    public static UIComponent getLayoutManager(UIComponent component) {
86      final UIComponent base;
87  
88      final UIComponent compositeFacet = component.getFacet(UIComponent.COMPOSITE_FACET_NAME);
89      if (compositeFacet != null) {
90        base = compositeFacet.getChildren().get(0);
91      } else {
92        base = component;
93      }
94  
95      final UIComponent layoutFacet = ComponentUtils.getFacet(base, Facets.layout);
96      if (layoutFacet != null) {
97        return layoutFacet;
98      } else {
99        return null;
100     }
101   }
102 }