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