KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > convert > SelectMultipleConverter


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.convert;
14
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.commons.beanutils.PropertyUtils;
21 import org.w3c.dom.Element JavaDoc;
22
23 import com.tonbeller.wcf.format.FormatException;
24 import com.tonbeller.wcf.format.FormatHandler;
25 import com.tonbeller.wcf.format.Formatter;
26 import com.tonbeller.wcf.ui.Item;
27 import com.tonbeller.wcf.ui.SelectMultiple;
28
29 /**
30  * sets an array bean property. The values of all selected items are collected in an
31  * array and the bean property is set to that array. If no items are selected, the
32  * bean property will contain an empty array.
33  * <p>
34  * An items value is the value attribute of the elected item.
35  * For type conversion the type, modelReference and formatString attributes
36  * will be taken from the items parent (e.g. the listBox).
37  *
38  * @author av
39  */

40 public class SelectMultipleConverter extends SelectConverterBase {
41
42   /**
43    * @see com.tonbeller.wcf.convert.SelectConverterBase#updateModelReference(Formatter, Map, Element, Object)
44    */

45   protected void updateModelReference(Formatter fmt, Element JavaDoc elem, Object JavaDoc bean)
46     throws FormatException, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
47
48     String JavaDoc model = SelectMultiple.getModelReference(elem);
49     if (model.length() == 0)
50       return;
51     
52     String JavaDoc type = SelectMultiple.getType(elem);
53     String JavaDoc formatString = SelectMultiple.getFormatString(elem);
54     FormatHandler parser = fmt.getHandler(type);
55     if (parser == null)
56       throw new FormatException("no handler found for type: " + type);
57     
58     List JavaDoc items = SelectMultiple.getSelectedItems(elem);
59     checkRequired(fmt.getLocale(), elem, items.size() == 0);
60     List JavaDoc values = new ArrayList JavaDoc();
61     
62     for (Iterator JavaDoc it = items.iterator(); it.hasNext();) {
63       Element JavaDoc item = (Element JavaDoc)it.next();
64       String JavaDoc valueString = Item.getValue(item);
65       Object JavaDoc value = parser.parse(valueString, formatString);
66       values.add(value);
67     }
68     
69     PropertyUtils.setProperty(bean, model, parser.toNativeArray(values));
70   }
71
72
73 }
74
Popular Tags