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.suggestajax.inputsuggestajax;
20  
21  import javax.faces.application.Application;
22  import javax.faces.el.ValueBinding;
23  import javax.faces.webapp.UIComponentTag;
24  
25  import org.apache.myfaces.custom.suggestajax.AbstractSuggestAjaxTag;
26  import org.apache.myfaces.custom.suggestajax.SuggestAjax;
27  
28  import com.sun.facelets.FaceletContext;
29  import com.sun.facelets.tag.MetaRuleset;
30  import com.sun.facelets.tag.MethodRule;
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  public class InputSuggestAjaxTagHandler extends HtmlComponentHandler
36  {
37      
38      private static final Class[] itemLabelMethodParamList = new Class[]{Object.class};
39      
40      private static final String ITEM_LABEL_METHOD = "itemLabelMethod";
41      private static final String SUGGESTED_ITEMS_METHOD = "suggestedItemsMethod";
42      private static final String MAX_SUGGESTED_ITEMS = "maxSuggestedItems";
43  
44      private TagAttribute suggestedItemsMethodAttr;
45      private TagAttribute maxSuggestedItemsAttr;
46      
47      public InputSuggestAjaxTagHandler(ComponentConfig config)
48      {
49          super(config);
50          suggestedItemsMethodAttr = getAttribute(SUGGESTED_ITEMS_METHOD);
51          maxSuggestedItemsAttr = getAttribute(MAX_SUGGESTED_ITEMS);
52      }
53      
54      protected void setAttributes(FaceletContext ctx, Object instance)
55      {
56          super.setAttributes(ctx, instance);
57  
58          Application app = ctx.getFacesContext().getApplication();
59          
60          SuggestAjax comp = (SuggestAjax) instance;
61          
62          if (maxSuggestedItemsAttr != null){
63              String maxSuggestedItems = maxSuggestedItemsAttr.getValue();
64              
65              if (maxSuggestedItems != null)
66              {
67                  if (UIComponentTag.isValueReference(maxSuggestedItems))
68                  {
69                      ValueBinding vb = app.createValueBinding(maxSuggestedItems);
70                      comp.setValueBinding(MAX_SUGGESTED_ITEMS, vb);
71                  }
72                  else
73                  {
74                      comp.getAttributes().put(MAX_SUGGESTED_ITEMS, Integer.valueOf(maxSuggestedItems));
75                  }
76              } 
77          }
78          
79          if (suggestedItemsMethodAttr != null){
80              String suggestedItemsMethod = suggestedItemsMethodAttr.getValue();
81              if (suggestedItemsMethod != null)
82              {
83                  AbstractSuggestAjaxTag.setSuggestedItemsMethodProperty(ctx.getFacesContext(),
84                          comp,suggestedItemsMethod);
85              }
86          }
87      }
88      
89      protected MetaRuleset createMetaRuleset(Class type)
90      {
91          return super.createMetaRuleset(type).addRule(
92                  new MethodRule(ITEM_LABEL_METHOD, 
93                          String.class, itemLabelMethodParamList));        
94      }
95      
96  }