KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > renderer > ImagePickerRadioRenderer


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.common.renderer;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIInput;
26 import javax.faces.context.FacesContext;
27 import javax.faces.context.ResponseWriter;
28
29 import org.alfresco.config.Config;
30 import org.alfresco.config.ConfigElement;
31 import org.alfresco.config.ConfigService;
32 import org.alfresco.web.app.Application;
33 import org.alfresco.web.ui.common.Utils;
34 import org.alfresco.web.ui.common.component.UIImagePicker;
35 import org.alfresco.web.ui.common.component.UIListItem;
36 import org.alfresco.web.ui.common.component.UIListItems;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * Renderer for the image picker component that outputs the list of images
42  * as radio buttons
43  *
44  * @author gavinc
45  */

46 public class ImagePickerRadioRenderer extends BaseRenderer
47 {
48    private static Log logger = LogFactory.getLog(ImagePickerRadioRenderer.class);
49    
50    private int columns;
51    private int position;
52    private boolean open;
53    
54    // ------------------------------------------------------------------------------
55
// Renderer implemenation
56

57    /**
58     * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
59     */

60    public void decode(FacesContext context, UIComponent component)
61    {
62       if (Utils.isComponentDisabledOrReadOnly(component))
63       {
64          return;
65       }
66       
67       String JavaDoc clientId = component.getClientId(context);
68       Map JavaDoc paramsMap = context.getExternalContext().getRequestParameterMap();
69       
70       String JavaDoc submittedValue = (String JavaDoc)paramsMap.get(clientId);
71       
72       if (logger.isDebugEnabled())
73          logger.debug("Submitted value = " + submittedValue);
74       
75       ((UIInput)component).setSubmittedValue(submittedValue);
76    }
77
78    /**
79     * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
80     */

81    public void encodeBegin(FacesContext context, UIComponent component) throws IOException JavaDoc
82    {
83       if (component.isRendered() == false)
84       {
85          return;
86       }
87
88       // setup counters
89
this.columns = 1;
90       this.position = 0;
91       this.open = false;
92       
93       ResponseWriter out = context.getResponseWriter();
94       
95       UIImagePicker imagePicker = (UIImagePicker)component;
96
97       Map JavaDoc attrs = imagePicker.getAttributes();
98       out.write("<table cellpadding='0'");
99       outputAttribute(out, attrs.get("spacing"), "cellspacing");
100       outputAttribute(out, attrs.get("styleClass"), "class");
101       outputAttribute(out, attrs.get("style"), "style");
102       out.write(">\n");
103    }
104    
105    /**
106     * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
107     */

108    @SuppressWarnings JavaDoc("unchecked")
109    public void encodeChildren(FacesContext context, UIComponent component) throws IOException JavaDoc
110    {
111       if (component.isRendered() == false)
112       {
113          return;
114       }
115       
116       UIImagePicker imagePicker = (UIImagePicker)component;
117       Map JavaDoc attrs = imagePicker.getAttributes();
118       
119       Integer JavaDoc cols = (Integer JavaDoc)attrs.get("columns");
120       if (cols != null && cols instanceof Integer JavaDoc)
121       {
122          this.columns = cols.intValue();
123       }
124       
125       // retrieve the onclick handler, if there is one
126
String JavaDoc onclick = (String JavaDoc)attrs.get("onclick");
127          
128       ResponseWriter out = context.getResponseWriter();
129       
130       // determine whether the options should be pulled from config or
131
// from the child components
132
String JavaDoc configSection = (String JavaDoc)attrs.get("configSection");
133       
134       if (configSection != null && configSection.length() > 0)
135       {
136          // render all the icons from the list that appear in the given
137
// config section
138
ConfigService cfgService = Application.getConfigService(context);
139          Config cfg = cfgService.getConfig(configSection);
140          if (cfg != null)
141          {
142             ConfigElement iconsCfg = cfg.getConfigElement("icons");
143             if (iconsCfg != null)
144             {
145                for (ConfigElement icon : iconsCfg.getChildren())
146                {
147                   String JavaDoc iconName = icon.getAttribute("name");
148                   String JavaDoc iconPath = icon.getAttribute("path");
149                   
150                   if (iconName != null && iconPath != null)
151                   {
152                      UIListItem item = new UIListItem();
153                      item.setValue(iconName);
154                      item.getAttributes().put("image", iconPath);
155                      renderItem(context, out, imagePicker, item, onclick);
156                   }
157                }
158             }
159          }
160       }
161       else
162       {
163          // get the child components
164
for (Iterator JavaDoc i = imagePicker.getChildren().iterator(); i.hasNext(); /**/)
165          {
166             UIComponent child = (UIComponent)i.next();
167             if (child instanceof UIListItems)
168             {
169                // get the value of the list items component and iterate
170
// through it's collection
171
Object JavaDoc listItems = ((UIListItems)child).getValue();
172                if (listItems instanceof Collection JavaDoc)
173                {
174                   Iterator JavaDoc iter = ((Collection JavaDoc)listItems).iterator();
175                   while (iter.hasNext())
176                   {
177                      UIListItem item = (UIListItem)iter.next();
178                      renderItem(context, out, imagePicker, item, onclick);
179                   }
180                }
181             }
182             else if (child instanceof UIListItem && child.isRendered() == true)
183             {
184                // found a valid UIListItem child to render
185
UIListItem item = (UIListItem)child;
186                renderItem(context, out, imagePicker, item, onclick);
187             }
188          }
189       }
190       
191       // if we are in the middle of a row, close it
192
if (open)
193       {
194          out.write("</tr>\n");
195       }
196    }
197
198    /**
199     * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
200     */

201    public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc
202    {
203       if (component.isRendered() == false)
204       {
205          return;
206       }
207       
208       ResponseWriter out = context.getResponseWriter();
209       out.write("</table>");
210    }
211
212    /**
213     * @see javax.faces.render.Renderer#getRendersChildren()
214     */

215    public boolean getRendersChildren()
216    {
217       return true;
218    }
219    
220    /**
221     * Renders the given item as a radio button selection choice
222     *
223     * @param context Faces context
224     * @param out ReponseWriter to write output to
225     * @param imagePicker The parent component
226     * @param item The item to render
227     * @param onclick The onClick JavaScript handler (may be null)
228     */

229    private void renderItem(FacesContext context, ResponseWriter out,
230          UIImagePicker imagePicker, UIListItem item, String JavaDoc onclick)
231          throws IOException JavaDoc
232    {
233       String JavaDoc tooltip = (String JavaDoc)item.getAttributes().get("tooltip");
234             
235       // if we are at the start of another row output "tr"
236
if ((this.position % this.columns) == 0)
237       {
238          // if we are at the end of a row, close it
239
if (this.open)
240          {
241             out.write("</tr>\n");
242             this.open = false;
243          }
244          
245          out.write("<tr>");
246          
247          // we have started the row
248
this.open = true;
249       }
250       
251       // output the next "cell" i.e. a radio button, the image and optional label
252
out.write("<td>");
253      
254       out.write("<input type='radio' name='");
255       out.write(imagePicker.getClientId(context));
256       out.write("' id='");
257       out.write(imagePicker.getClientId(context));
258       out.write("' value='");
259       // TODO: need to take into account values that may need to be converted,
260
// for now presume a string is OK
261
out.write(item.getValue().toString());
262       out.write("'");
263       
264       // determine whether this item should be selected
265
Object JavaDoc currentValue = imagePicker.getSubmittedValue();
266       if (currentValue == null)
267       {
268          currentValue = imagePicker.getValue();
269       }
270       
271       Object JavaDoc itemValue = item.getValue();
272       if (itemValue != null && itemValue.equals(currentValue))
273       {
274          out.write(" checked='true'");
275       }
276       
277       if (tooltip != null)
278       {
279          out.write(" title='");
280          out.write(Utils.encode(tooltip));
281          out.write("'");
282       }
283       
284       if (onclick != null)
285       {
286          out.write(" onclick='");
287          out.write(onclick);
288          out.write("'");
289       }
290       
291 // if (item.isDisabled())
292
// {
293
// out.write(" disabled='true'");
294
// }
295

296       out.write(">");
297       out.write("</td><td align='center'>");
298   
299       // get the image and make sure there is one!
300
String JavaDoc image = (String JavaDoc)item.getAttributes().get("image");
301       if (image == null)
302       {
303          throw new IllegalStateException JavaDoc("All child items must specify an image");
304       }
305       
306       out.write(Utils.buildImageTag(context, image, tooltip));
307       
308       String JavaDoc label = (String JavaDoc)item.getAttributes().get("label");
309       if (label != null && label.length() > 0)
310       {
311          out.write("<br/>");
312          out.write(Utils.encode(label));
313       }
314       
315       out.write("</td>");
316       
317       // we've finished the item so move the position on
318
this.position++;
319    }
320 }
321
Popular Tags