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