1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.renderkit.html;
20
21 import org.apache.myfaces.shared.renderkit.JSFAttr;
22 import org.apache.myfaces.shared.renderkit.RendererUtils;
23 import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
24
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.UISelectMany;
27 import javax.faces.component.UISelectOne;
28 import javax.faces.component.behavior.ClientBehavior;
29 import javax.faces.component.behavior.ClientBehaviorHolder;
30 import javax.faces.component.html.HtmlSelectManyListbox;
31 import javax.faces.component.html.HtmlSelectOneListbox;
32 import javax.faces.context.FacesContext;
33 import javax.faces.convert.Converter;
34 import javax.faces.convert.ConverterException;
35 import java.io.IOException;
36 import java.util.List;
37 import java.util.Map;
38
39
40
41
42
43
44
45 public class HtmlListboxRendererBase
46 extends HtmlSelectableRendererBase
47 {
48 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
49 throws IOException
50 {
51 org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null);
52
53 Map<String, List<ClientBehavior>> behaviors = null;
54 if (uiComponent instanceof ClientBehaviorHolder)
55 {
56 behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
57 if (!behaviors.isEmpty())
58 {
59 ResourceUtils.renderDefaultJsfJsInlineIfNecessary(
60 facesContext, facesContext.getResponseWriter());
61 }
62 }
63
64 Integer size = (Integer)uiComponent.getAttributes().get(JSFAttr.SIZE_ATTR);
65 if (size == null)
66 {
67 size = Integer.MIN_VALUE;
68 }
69
70 if (uiComponent instanceof UISelectMany)
71 {
72 renderListbox(facesContext,
73 (UISelectMany)uiComponent,
74 isDisabled(facesContext, uiComponent),
75 size, getConverter(facesContext, uiComponent));
76 }
77 else if (uiComponent instanceof HtmlSelectOneListbox)
78 {
79 renderListbox(facesContext,
80 (UISelectOne)uiComponent,
81 isDisabled(facesContext, uiComponent),
82 size, getConverter(facesContext, uiComponent));
83 }
84 else
85 {
86 throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
87 }
88 }
89
90 protected void renderListbox(FacesContext facesContext,
91 UISelectOne selectOne, boolean disabled, int size,
92 Converter converter) throws IOException
93 {
94 internalRenderSelect(facesContext, selectOne, disabled, size, false,
95 converter);
96 }
97
98 protected void renderListbox(FacesContext facesContext,
99 UISelectMany selectMany, boolean disabled, int size,
100 Converter converter) throws IOException
101 {
102 internalRenderSelect(facesContext, selectMany, disabled, size, true,
103 converter);
104 }
105
106 protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
107 {
108
109 if (uiComponent instanceof HtmlSelectManyListbox)
110 {
111 return ((HtmlSelectManyListbox)uiComponent).isDisabled();
112 }
113 else if (uiComponent instanceof HtmlSelectOneListbox)
114 {
115 return ((HtmlSelectOneListbox)uiComponent).isDisabled();
116 }
117 else
118 {
119 return org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(uiComponent,
120 org.apache.myfaces.shared.renderkit.html.HTML.DISABLED_ATTR, false);
121 }
122 }
123
124
125 public void decode(FacesContext facesContext, UIComponent uiComponent)
126 {
127 RendererUtils.checkParamValidity(facesContext, uiComponent, null);
128
129 if (uiComponent instanceof UISelectMany)
130 {
131 HtmlRendererUtils.decodeUISelectMany(facesContext, uiComponent);
132 }
133 else if (uiComponent instanceof UISelectOne)
134 {
135 HtmlRendererUtils.decodeUISelectOne(facesContext, uiComponent);
136 }
137 else
138 {
139 throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
140 }
141 if (uiComponent instanceof ClientBehaviorHolder &&
142 !HtmlRendererUtils.isDisabled(uiComponent))
143 {
144 HtmlRendererUtils.decodeClientBehaviors(facesContext, uiComponent);
145 }
146 }
147
148 public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue)
149 throws ConverterException
150 {
151 org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null);
152
153 if (uiComponent instanceof UISelectMany)
154 {
155 return org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUISelectManyValue(facesContext,
156 (UISelectMany)uiComponent,
157 submittedValue);
158 }
159 else if (uiComponent instanceof UISelectOne)
160 {
161 return RendererUtils.getConvertedUISelectOneValue(facesContext,
162 (UISelectOne)uiComponent,
163 submittedValue);
164 }
165 else
166 {
167 throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
168 }
169 }
170
171
172
173
174
175
176
177 protected Converter getConverter(FacesContext facesContext,
178 UIComponent component)
179 {
180 if (component instanceof UISelectMany)
181 {
182 return HtmlRendererUtils.findUISelectManyConverterFailsafe(facesContext,
183 (UISelectMany) component);
184 }
185 else if (component instanceof UISelectOne)
186 {
187 return HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, component);
188 }
189 return null;
190 }
191
192 }