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  import org.apache.myfaces.tobago.component.UIFlowLayout;
24  import org.apache.myfaces.tobago.layout.Measure;
25  import org.apache.myfaces.tobago.layout.TextAlign;
26  
27  import javax.faces.view.facelets.FaceletContext;
28  import javax.faces.view.facelets.MetaRule;
29  import javax.faces.view.facelets.Metadata;
30  import javax.faces.view.facelets.MetadataTarget;
31  import javax.faces.view.facelets.TagAttribute;
32  
33  public class FlowLayoutRule extends MetaRule {
34  
35    public static final FlowLayoutRule INSTANCE = new FlowLayoutRule();
36  
37    public Metadata applyRule(final String name, final TagAttribute attribute, final MetadataTarget metadataTarget) {
38      if (metadataTarget.isTargetInstanceOf(UIFlowLayout.class)) {
39        if (attribute.isLiteral()) {
40          if (Attributes.TEXT_ALIGN.equals(name)) {
41            return new TextAlignMapper(attribute);
42          }
43          if (Attributes.MARGIN_LEFT.equals(name)) {
44            return new MarginLeftMapper(attribute);
45          }
46          if (Attributes.MARGIN_TOP.equals(name)) {
47            return new MarginTopMapper(attribute);
48          }
49          if (Attributes.MARGIN_RIGHT.equals(name)) {
50            return new MarginRightMapper(attribute);
51          }
52          if (Attributes.MARGIN_BOTTOM.equals(name)) {
53            return new MarginBottomMapper(attribute);
54          }
55          if (Attributes.MARGIN.equals(name)) {
56            return new MarginMapper(attribute);
57          }
58        }
59      }
60      return null;
61    }
62  
63    static final class TextAlignMapper extends Metadata {
64      private final TagAttribute attribute;
65  
66      TextAlignMapper(final TagAttribute attribute) {
67        this.attribute = attribute;
68      }
69  
70      public void applyMetadata(final FaceletContext ctx, final Object instance) {
71        final UIFlowLayout gridLayout = (UIFlowLayout) instance;
72        gridLayout.setTextAlign(TextAlign.parse(attribute.getValue()));
73      }
74    }
75  
76    static final class MarginLeftMapper extends Metadata {
77      private final TagAttribute attribute;
78  
79      MarginLeftMapper(final TagAttribute attribute) {
80        this.attribute = attribute;
81      }
82  
83      public void applyMetadata(final FaceletContext ctx, final Object instance) {
84        final UIFlowLayout gridLayout = (UIFlowLayout) instance;
85        gridLayout.setMarginLeft(Measure.valueOf(attribute.getValue()));
86      }
87    }
88  
89    static final class MarginTopMapper extends Metadata {
90      private final TagAttribute attribute;
91  
92      MarginTopMapper(final TagAttribute attribute) {
93        this.attribute = attribute;
94      }
95  
96      public void applyMetadata(final FaceletContext ctx, final Object instance) {
97        final UIFlowLayout gridLayout = (UIFlowLayout) instance;
98        gridLayout.setMarginTop(Measure.valueOf(attribute.getValue()));
99      }
100   }
101 
102   static final class MarginRightMapper extends Metadata {
103     private final TagAttribute attribute;
104 
105     MarginRightMapper(final TagAttribute attribute) {
106       this.attribute = attribute;
107     }
108 
109     public void applyMetadata(final FaceletContext ctx, final Object instance) {
110       final UIFlowLayout gridLayout = (UIFlowLayout) instance;
111       gridLayout.setMarginRight(Measure.valueOf(attribute.getValue()));
112     }
113   }
114 
115   static final class MarginBottomMapper extends Metadata {
116     private final TagAttribute attribute;
117 
118     MarginBottomMapper(final TagAttribute attribute) {
119       this.attribute = attribute;
120     }
121 
122     public void applyMetadata(final FaceletContext ctx, final Object instance) {
123       final UIFlowLayout gridLayout = (UIFlowLayout) instance;
124       gridLayout.setMarginBottom(Measure.valueOf(attribute.getValue()));
125     }
126   }
127 
128   static final class MarginMapper extends Metadata {
129     private final TagAttribute attribute;
130 
131     MarginMapper(final TagAttribute attribute) {
132       this.attribute = attribute;
133     }
134 
135     public void applyMetadata(final FaceletContext ctx, final Object instance) {
136       final UIFlowLayout gridLayout = (UIFlowLayout) instance;
137       gridLayout.setMargin(Measure.valueOf(attribute.getValue()));
138     }
139   }
140 }