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  package org.apache.myfaces.custom.date;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.view.facelets.ComponentConfig;
23  import javax.faces.view.facelets.ComponentHandler;
24  import javax.faces.view.facelets.FaceletContext;
25  import javax.faces.view.facelets.MetaRule;
26  import javax.faces.view.facelets.MetaRuleset;
27  import javax.faces.view.facelets.Metadata;
28  import javax.faces.view.facelets.MetadataTarget;
29  import javax.faces.view.facelets.TagAttribute;
30  
31  import org.apache.myfaces.custom.calendar.DateBusinessConverter;
32  import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
33  
34  /**
35   * 
36   * @since 1.1.10
37   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
38   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (miƩ, 03 sep 2008) $
39   */
40  public class HtmlInputDateTagHandler extends ComponentHandler
41  {
42  
43      public HtmlInputDateTagHandler(ComponentConfig config)
44      {
45          super(config);
46      }
47  
48      protected MetaRuleset createMetaRuleset(Class type)
49      {
50          MetaRuleset m = super.createMetaRuleset(type).alias("class", "styleClass");
51  
52          m.addRule(DateBusinessConverterRule.INSTANCE);
53  
54          return m;
55      }
56  
57      public static class DateBusinessConverterRule extends MetaRule
58      {
59          public final static DateBusinessConverterRule INSTANCE = new DateBusinessConverterRule();
60  
61          final static class LiteralConverterMetadata extends Metadata
62          {
63  
64              private final Class dateBusinessConverterClass;
65  
66              public LiteralConverterMetadata(String dateBusinessConverterClass)
67              {
68                  try
69                  {
70                      this.dateBusinessConverterClass = ClassUtils
71                              .classForName(dateBusinessConverterClass);
72                  }
73                  catch(ClassNotFoundException e)
74                  {
75                      throw new IllegalArgumentException("Class referenced in calendarConverter not found: "+dateBusinessConverterClass);
76                  }
77                  catch(ClassCastException e)
78                  {
79                      throw new IllegalArgumentException("Class referenced in calendarConverter is not instance of org.apache.myfaces.custom.calendar.CalendarConverter: "+dateBusinessConverterClass);
80                  }
81              }
82  
83              public void applyMetadata(FaceletContext ctx, Object instance)
84              {
85                  ((AbstractHtmlInputDate) instance)
86                          .setDateBusinessConverter((DateBusinessConverter) ClassUtils
87                                  .newInstance(dateBusinessConverterClass));
88              }
89          }
90  
91          final static class DynamicConverterMetadata extends Metadata
92          {
93              private final TagAttribute attr;
94  
95              public DynamicConverterMetadata(TagAttribute attr)
96              {
97                  this.attr = attr;
98              }
99  
100             public void applyMetadata(FaceletContext ctx, Object instance)
101             {
102                 ((UIComponent) instance).setValueExpression("dateBusinessConverter",
103                         attr.getValueExpression(ctx,
104                                 DateBusinessConverter.class));
105             }
106         }
107 
108         public Metadata applyRule(String name, TagAttribute attribute,
109                 MetadataTarget meta)
110         {
111             if (meta.isTargetInstanceOf(AbstractHtmlInputDate.class))
112             {
113                 if ("dateBusinessConverter".equals(name)) {
114                     if (attribute.isLiteral()) {
115                         return new LiteralConverterMetadata(attribute.getValue());
116                     } else {
117                         return new DynamicConverterMetadata(attribute);
118                     }
119                 }
120             }
121             return null;
122         }
123     }
124 }