1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
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
67
68
69
70
71
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
116
117 public void setSuggestedItemsMethod(String suggestedItemsMethod)
118 {
119 _suggestedItemsMethod = suggestedItemsMethod;
120 }
121
122 }