001    package org.apache.myfaces.tobago.facelets;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import com.sun.facelets.FaceletContext;
021    import com.sun.facelets.tag.MetaRule;
022    import com.sun.facelets.tag.Metadata;
023    import com.sun.facelets.tag.MetadataTarget;
024    import com.sun.facelets.tag.TagAttribute;
025    import org.apache.myfaces.tobago.component.Attributes;
026    import org.apache.myfaces.tobago.component.UIGridLayout;
027    import org.apache.myfaces.tobago.layout.Measure;
028    
029    public class GridLayoutRule extends MetaRule {
030    
031      public static final GridLayoutRule INSTANCE = new GridLayoutRule();
032    
033      public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget metadataTarget) {
034        if (metadataTarget.isTargetInstanceOf(UIGridLayout.class)) {
035          if (attribute.isLiteral()) {
036            if (Attributes.COLUMN_SPACING.equals(name)) {
037              return new ColumnSpacingMapper(attribute);
038            }
039            if (Attributes.ROW_SPACING.equals(name)) {
040              return new RowSpacingMapper(attribute);
041            }
042            if (Attributes.CELLSPACING.equals(name)) {
043              return new CellspacingMapper(attribute);
044            }
045            if (Attributes.MARGIN_LEFT.equals(name)) {
046              return new MarginLeftMapper(attribute);
047            }
048            if (Attributes.MARGIN_TOP.equals(name)) {
049              return new MarginTopMapper(attribute);
050            }
051            if (Attributes.MARGIN_RIGHT.equals(name)) {
052              return new MarginRightMapper(attribute);
053            }
054            if (Attributes.MARGIN_BOTTOM.equals(name)) {
055              return new MarginBottomMapper(attribute);
056            }
057            if (Attributes.MARGIN.equals(name)) {
058              return new MarginMapper(attribute);
059            }
060          }
061        }
062        return null;
063      }
064    
065      static final class ColumnSpacingMapper extends Metadata {
066        private final TagAttribute attribute;
067    
068        ColumnSpacingMapper(TagAttribute attribute) {
069          this.attribute = attribute;
070        }
071    
072        public void applyMetadata(FaceletContext ctx, Object instance) {
073          UIGridLayout gridLayout = (UIGridLayout) instance;
074          gridLayout.setColumnSpacing(Measure.parse(attribute.getValue()));
075        }
076      }
077    
078      static final class RowSpacingMapper extends Metadata {
079        private final TagAttribute attribute;
080    
081        RowSpacingMapper(TagAttribute attribute) {
082          this.attribute = attribute;
083        }
084    
085        public void applyMetadata(FaceletContext ctx, Object instance) {
086          UIGridLayout gridLayout = (UIGridLayout) instance;
087          gridLayout.setRowSpacing(Measure.parse(attribute.getValue()));
088        }
089      }
090    
091      static final class CellspacingMapper extends Metadata {
092        private final TagAttribute attribute;
093    
094        CellspacingMapper(TagAttribute attribute) {
095          this.attribute = attribute;
096        }
097    
098        public void applyMetadata(FaceletContext ctx, Object instance) {
099          UIGridLayout gridLayout = (UIGridLayout) instance;
100          gridLayout.setCellspacing(Measure.parse(attribute.getValue()));
101        }
102      }
103    
104      static final class MarginLeftMapper extends Metadata {
105        private final TagAttribute attribute;
106    
107        MarginLeftMapper(TagAttribute attribute) {
108          this.attribute = attribute;
109        }
110    
111        public void applyMetadata(FaceletContext ctx, Object instance) {
112          UIGridLayout gridLayout = (UIGridLayout) instance;
113          gridLayout.setMarginLeft(Measure.valueOf(attribute.getValue()));
114        }
115      }
116    
117      static final class MarginTopMapper extends Metadata {
118        private final TagAttribute attribute;
119    
120        MarginTopMapper(TagAttribute attribute) {
121          this.attribute = attribute;
122        }
123    
124        public void applyMetadata(FaceletContext ctx, Object instance) {
125          UIGridLayout gridLayout = (UIGridLayout) instance;
126          gridLayout.setMarginTop(Measure.valueOf(attribute.getValue()));
127        }
128      }
129    
130      static final class MarginRightMapper extends Metadata {
131        private final TagAttribute attribute;
132    
133        MarginRightMapper(TagAttribute attribute) {
134          this.attribute = attribute;
135        }
136    
137        public void applyMetadata(FaceletContext ctx, Object instance) {
138          UIGridLayout gridLayout = (UIGridLayout) instance;
139          gridLayout.setMarginRight(Measure.valueOf(attribute.getValue()));
140        }
141      }
142    
143      static final class MarginBottomMapper extends Metadata {
144        private final TagAttribute attribute;
145    
146        MarginBottomMapper(TagAttribute attribute) {
147          this.attribute = attribute;
148        }
149    
150        public void applyMetadata(FaceletContext ctx, Object instance) {
151          UIGridLayout gridLayout = (UIGridLayout) instance;
152          gridLayout.setMarginBottom(Measure.valueOf(attribute.getValue()));
153        }
154      }
155    
156      static final class MarginMapper extends Metadata {
157        private final TagAttribute attribute;
158    
159        MarginMapper(TagAttribute attribute) {
160          this.attribute = attribute;
161        }
162    
163        public void applyMetadata(FaceletContext ctx, Object instance) {
164          UIGridLayout gridLayout = (UIGridLayout) instance;
165          gridLayout.setMargin(Measure.valueOf(attribute.getValue()));
166        }
167      }
168    }