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