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 org.apache.myfaces.custom.ajax.api.AjaxDecodePhaseListener;
22  import org.apache.myfaces.custom.ajax.api.AjaxSuggestRenderer;
23  import org.apache.myfaces.renderkit.html.ext.HtmlTextRenderer;
24  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.el.MethodBinding;
29  import javax.faces.el.MethodNotFoundException;
30  import java.util.Collection;
31  import java.util.List;
32  
33  /**
34   * @author Gerald Muellan
35   * @author Martin Marinschek
36   * @version $Revision: 177984 $ $Date: 2005-05-23 19:39:37 +0200 (Mon, 23 May 2005) $
37   */
38  public class SuggestAjaxRenderer extends HtmlTextRenderer implements AjaxSuggestRenderer
39  {
40      public static final int DEFAULT_MAX_SUGGESTED_ITEMS = 200;
41  
42      public Collection getSuggestedItems(FacesContext context, UIComponent uiComponent)
43      {
44          RendererUtils.checkParamValidity(context, uiComponent, SuggestAjax.class);
45  
46          SuggestAjax suggestAjax = (SuggestAjax) uiComponent;
47  
48          //getting the suggested items
49          MethodBinding mb = suggestAjax.getSuggestedItemsMethod();
50          Integer maxSuggestedCount = suggestAjax.getMaxSuggestedItems();
51  
52          Collection suggesteds;
53  
54          if (maxSuggestedCount != null
55                  && maxSuggestedCount.intValue() > 0)
56          {
57              try
58              {
59                  suggesteds = (Collection) mb.invoke(context,new Object[]{
60                          AjaxDecodePhaseListener.getValueForComponent(context, uiComponent),
61                          maxSuggestedCount});
62              }
63              catch(MethodNotFoundException dummy)
64              {
65                  suggesteds = (List) mb.invoke(context,new Object[]{
66                          AjaxDecodePhaseListener.getValueForComponent(context, uiComponent)});
67              }
68          }
69          else
70          {
71              try
72              {
73                  suggesteds = (List) mb.invoke(context,new Object[]{
74                          AjaxDecodePhaseListener.getValueForComponent(context, uiComponent)});
75              }
76              catch(MethodNotFoundException dummy)
77              {
78                  suggesteds = (Collection) mb.invoke(context,new Object[]{
79                          AjaxDecodePhaseListener.getValueForComponent(context, uiComponent),
80                          new Integer( DEFAULT_MAX_SUGGESTED_ITEMS )});
81              }
82          }
83  
84          return suggesteds;
85      }
86  
87      public void decode(FacesContext facesContext, UIComponent component)
88      {
89          super.decode(facesContext, component);
90      }
91  
92       protected String addQueryString(String url, String queryString)
93       {        
94             return url + (url.indexOf("?") > 0 ? "&" : "?") + queryString;
95       }
96  }