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