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