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.facelets;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.view.facelets.FaceletContext;
26  import javax.faces.view.facelets.TagAttribute;
27  import javax.faces.view.facelets.TagConfig;
28  import javax.faces.view.facelets.TagHandler;
29  import java.io.IOException;
30  import java.util.Map;
31  
32  /**
33   * @since 4.0.0
34   */
35  public class GridLayoutConstraintHandler extends TagHandler {
36  
37    private final TagAttribute columnSpan;
38    private final TagAttribute rowSpan;
39    private final TagAttribute gridColumn;
40    private final TagAttribute gridRow;
41  
42    public GridLayoutConstraintHandler(final TagConfig config) {
43      super(config);
44  
45      final TagAttribute oldColumn = getAttribute(Attributes.column.getName()); // deprecated
46      final TagAttribute newColumn = getAttribute(Attributes.columnSpan.getName());
47      columnSpan = newColumn != null ? newColumn : oldColumn;
48  
49      final TagAttribute oldRow = getAttribute(Attributes.row.getName()); // deprecated
50      final TagAttribute newRow = getAttribute(Attributes.rowSpan.getName());
51      rowSpan = newRow != null ? newRow : oldRow;
52  
53      gridColumn = getAttribute(Attributes.gridColumn.getName());
54  
55      gridRow = getAttribute(Attributes.gridRow.getName());
56    }
57  
58    @Override
59    public void apply(final FaceletContext faceletContext, final UIComponent parent) throws IOException {
60      final Map<String, Object> attributes = parent.getAttributes();
61  
62      if (columnSpan != null) {
63        if (columnSpan.isLiteral()) {
64          attributes.put(Attributes.columnSpan.getName(), Integer.valueOf(columnSpan.getValue()));
65        } else {
66          parent.setValueExpression(Attributes.columnSpan.getName(),
67              columnSpan.getValueExpression(faceletContext, Integer.TYPE));
68        }
69      }
70  
71      if (rowSpan != null) {
72        if (rowSpan.isLiteral()) {
73          attributes.put(Attributes.rowSpan.getName(), Integer.valueOf(rowSpan.getValue()));
74        } else {
75          parent.setValueExpression(Attributes.rowSpan.getName(),
76              rowSpan.getValueExpression(faceletContext, Integer.TYPE));
77        }
78      }
79  
80      if (gridColumn != null) {
81        if (gridColumn.isLiteral()) {
82          attributes.put(Attributes.gridColumn.getName(), Integer.valueOf(gridColumn.getValue()));
83        } else {
84          parent.setValueExpression(Attributes.gridColumn.getName(),
85              gridColumn.getValueExpression(faceletContext, Integer.TYPE));
86        }
87      }
88  
89      if (gridRow != null) {
90        if (gridRow.isLiteral()) {
91          attributes.put(Attributes.gridRow.getName(), Integer.valueOf(gridRow.getValue()));
92        } else {
93          parent.setValueExpression(Attributes.gridRow.getName(),
94              gridRow.getValueExpression(faceletContext, Integer.TYPE));
95        }
96      }
97    }
98  }