1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.inputAjax;
20
21 import java.io.IOException;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 import javax.faces.context.FacesContext;
27 import javax.faces.render.Renderer;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.myfaces.component.html.ext.HtmlSelectManyCheckbox;
32 import org.apache.myfaces.custom.ajax.AjaxCallbacks;
33 import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
34 import org.apache.myfaces.custom.ajax.api.DeprecatedAjaxComponent;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public abstract class AbstractHtmlSelectManyCheckboxAjax extends HtmlSelectManyCheckbox implements DeprecatedAjaxComponent, AjaxCallbacks
51 {
52 private static final Log log = LogFactory.getLog(AbstractHtmlSelectManyCheckboxAjax.class);
53 public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlSelectManyCheckboxAjax";
54 public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.CheckboxAjax";
55
56 public AbstractHtmlSelectManyCheckboxAjax()
57 {
58 super();
59 setRendererType(DEFAULT_RENDERER_TYPE);
60 }
61
62
63
64
65
66
67 public void decodeAjax(FacesContext context)
68 {
69 log.debug("entering HtmlSelectManyCheckboxAjax.decodeAjax");
70
71
72
73 Map requestParams = context.getExternalContext().getRequestParameterMap();
74
75 String elname = (String) requestParams.get("elname");
76 String elvalue = (String) requestParams.get("elvalue");
77 String checkedStr = (String) requestParams.get("checked");
78
79 boolean checked = Boolean.valueOf(checkedStr).booleanValue();
80
81
82 Object valOb = this.getValue();
83
84 if(valOb instanceof Collection){
85
86
87 log.debug("valOb is collection");
88 Collection c = (Collection) valOb;
89 updateChosenValue(c, elname, elvalue, checked);
90 } else {
91 log.error("Invalid chosen values type in HtmlSelectManyCheckbox");
92 }
93
94
95 processValidators(context);
96 processUpdates(context);
97
98 }
99
100
101
102
103
104
105
106
107 public void updateChosenValue(Collection c, String elname, String elvalue, boolean checked) {
108 boolean found = false;
109 if (c != null) {
110 Iterator iter = c.iterator();
111 while (iter.hasNext())
112 {
113 String value = (String) iter.next();
114 if (value.equals(elvalue)) {
115 found = true;
116 if (!checked) {
117
118 log.debug("Removing elvalue: " + elvalue);
119 iter.remove();
120 } else {
121
122 }
123 break;
124 }
125 }
126 if (!found && checked) {
127
128 log.debug("Adding elvalue: " + elvalue);
129 c.add(elvalue);
130 }
131 } else {
132 log.error("LIST IS NULL!!!");
133 }
134 }
135
136 public void encodeAjax(FacesContext context) throws IOException
137 {
138
139 if (context == null) throw new NullPointerException("context");
140 if (!isRendered()) return;
141 Renderer renderer = getRenderer(context);
142
143 if (renderer != null && renderer instanceof AjaxRenderer)
144 {
145 ((AjaxRenderer) renderer).encodeAjax(context, this);
146
147 }
148 }
149
150
151
152
153
154
155 public abstract String getOnSuccess();
156
157
158
159
160
161
162 public abstract String getOnFailure();
163
164
165
166
167
168
169 public abstract String getOnStart();
170
171 }