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;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.context.FacesContext;
23  import javax.faces.el.MethodBinding;
24  import javax.faces.el.ValueBinding;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.myfaces.generated.taglib.html.ext.HtmlInputTextTag;
29  
30  /**
31   * @author Gerald Muellan
32   *         Date: 25.03.2006
33   *         Time: 17:05:58
34   */
35  public class AbstractSuggestAjaxTag extends HtmlInputTextTag
36  {
37      private final static Class[] DEFAULT_SIGNATURE = new Class[]{String.class};
38      private final static Class[] SUGGEST_ITEM_SIGNATURE = new Class[]{String.class, Integer.class};
39  
40      private static Log log = LogFactory.getLog(AbstractSuggestAjaxTag.class);
41  
42      private String _suggestedItemsMethod;
43      
44      private String _maxSuggestedItems;
45      
46      public void setMaxSuggestedItems(String maxSuggestedItems)
47      {
48          _maxSuggestedItems = maxSuggestedItems;
49      }
50  
51      public void release() {
52  
53          super.release();
54          _maxSuggestedItems = null;
55         _suggestedItemsMethod = null;       
56      }
57  
58      protected void setProperties(UIComponent component) {
59  
60          super.setProperties(component);
61          
62          FacesContext context = getFacesContext();
63  
64          SuggestAjax comp = (SuggestAjax) component;
65          
66          //This part is a special hack for set the method for suggestedItemsMethod
67          //First maxSuggestedItems should be resolved and put on component, for
68          //make this value available when setSuggestedItemsMethodProperty is
69          //called. Note on AbstractSuggestAjax the field maxSuggestedItems
70          //the property inheritedTag = "true". In normal generation this should be defined
71          //on SuggestAjaxTag.
72          if (_maxSuggestedItems != null)
73          {
74              if (isValueReference(_maxSuggestedItems))
75              {
76                  ValueBinding vb = context.getApplication().createValueBinding(_maxSuggestedItems);
77                  comp.setValueBinding("maxSuggestedItems", vb);
78              }
79              else
80              {
81                  comp.getAttributes().put("maxSuggestedItems", Integer.valueOf(_maxSuggestedItems));
82              }
83          } 
84                  
85          AbstractSuggestAjaxTag.setSuggestedItemsMethodProperty(getFacesContext(),component,_suggestedItemsMethod);
86      }
87  
88      public static void setSuggestedItemsMethodProperty(FacesContext context,
89                                                         UIComponent component,
90                                                         String suggestedItemsMethod)
91      {
92          if (suggestedItemsMethod != null)
93          {
94              if (!(component instanceof SuggestAjax))
95              {
96                  throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no InputSuggestAjax");
97              }
98              if (isValueReference(suggestedItemsMethod))
99              {
100                 if (((SuggestAjax)component).getMaxSuggestedItems()!=null) {
101                     MethodBinding mb = context.getApplication().createMethodBinding(suggestedItemsMethod, AbstractSuggestAjaxTag.SUGGEST_ITEM_SIGNATURE);
102                     ((SuggestAjax)component).setSuggestedItemsMethod(mb);
103                 } else {
104                     MethodBinding mb = context.getApplication().createMethodBinding(suggestedItemsMethod, AbstractSuggestAjaxTag.DEFAULT_SIGNATURE);
105                     ((SuggestAjax)component).setSuggestedItemsMethod(mb);
106                 }
107             }
108             else
109             {
110                 AbstractSuggestAjaxTag.log.error("Invalid expression " + suggestedItemsMethod);
111             }
112         }
113     }
114 
115     // setter methodes to populate the components properites
116 
117     public void setSuggestedItemsMethod(String suggestedItemsMethod)
118     {
119         _suggestedItemsMethod = suggestedItemsMethod;
120     }
121     
122 }