KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Map JavaDoc;
20
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.apache.log4j.Logger;
23 import org.w3c.dom.Element JavaDoc;
24
25 import com.tonbeller.wcf.format.FormatException;
26 import com.tonbeller.wcf.format.FormatHandler;
27 import com.tonbeller.wcf.format.Formatter;
28 import com.tonbeller.wcf.ui.Item;
29 import com.tonbeller.wcf.ui.Select;
30 import com.tonbeller.wcf.utils.DomUtils;
31
32 /**
33  * @author av
34  */

35 public abstract class SelectConverterBase extends NodeConverterBase {
36   private static Logger logger = Logger.getLogger(SelectConverterBase.class);
37
38   /**
39    * sets the selected attributes of the DOM item children of elem. calls updateModelReference
40    * if the list is valid (ie not disabled and form was submitted).
41    */

42   public void convert(Formatter fmt, Map JavaDoc params, Map JavaDoc fileSource, Element JavaDoc elem, Object JavaDoc bean)
43     throws ConvertException, FormatException, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
44
45     // disabled = true? return
46
if (Select.isDisabled(elem))
47       return;
48
49     String JavaDoc id = Select.getId(elem);
50
51     // was this form submitted
52
Object JavaDoc inputAvail = params.get(id + ".valid");
53     if (inputAvail == null)
54       return;
55
56     // get the http values
57
String JavaDoc[] values = (String JavaDoc[]) params.get(id);
58     if (values == null)
59       values = new String JavaDoc[0];
60
61     DomUtils.removeAttribute(elem, "error");
62
63     // set all items to unselected
64
List JavaDoc items = Select.getItems(elem);
65     for (Iterator JavaDoc it = items.iterator(); it.hasNext();)
66       Item.setSelected((Element JavaDoc) it.next(), false);
67
68     // select the list items
69
List JavaDoc selected = selectListItems(elem, values, items);
70
71     updateModelReference(fmt, elem, bean);
72   }
73
74   /**
75    * select the list items
76    */

77   List JavaDoc selectListItems(Element JavaDoc elem, String JavaDoc[] values, List JavaDoc items) throws ConvertException {
78     List JavaDoc selected = new ArrayList JavaDoc();
79     for (int i = 0; i < values.length; i++) {
80       String JavaDoc itemId = values[i];
81       boolean found = false;
82       search : for (Iterator JavaDoc it = items.iterator(); it.hasNext();) {
83         Element JavaDoc item = (Element JavaDoc) it.next();
84         if (Item.getId(item).equals(itemId)) {
85           Item.setSelected(item, true);
86           selected.add(item);
87           found = true;
88           break search;
89         }
90       }
91       if (!found) {
92         String JavaDoc mesg = "Item with id=\"" + itemId + "\" not found in ListBox";
93         showMissing(elem, mesg);
94       }
95     }
96     return selected;
97   }
98
99
100
101   protected abstract void updateModelReference(Formatter fmt, Element JavaDoc elem, Object JavaDoc bean)
102     throws FormatException, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc;
103
104
105   /**
106    * @see com.tonbeller.wcf.convert.NodeConverter#convert(Formatter, Object, Element)
107    */

108   public void convert(Formatter fmt, Object JavaDoc bean, Element JavaDoc elem)
109     throws ConvertException, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
110     
111     // no model reference? nothing to do
112
String JavaDoc modelReference = Select.getModelReference(elem);
113     if (modelReference.length() == 0)
114       return;
115
116     // get a formatter
117
String JavaDoc type = Select.getType(elem);
118     String JavaDoc formatString = Select.getFormatString(elem);
119     FormatHandler handler = fmt.getHandler(type);
120     if (handler == null)
121       throw new FormatException("no handler found for type: " + type);
122
123     // retrieve values from the bean property
124
Object JavaDoc o = PropertyUtils.getProperty(bean, modelReference);
125     Object JavaDoc[] values = handler.toObjectArray(o);
126     if (values == null)
127       values = new Object JavaDoc[0];
128     
129     // deselect all items
130
List JavaDoc items = Select.getItems(elem);
131     for (Iterator JavaDoc it = items.iterator(); it.hasNext(); )
132       Item.setSelected((Element JavaDoc)it.next(), false);
133     
134     // for each value, find the corresponding item and select it
135
for (int i = 0; i < values.length; i++) {
136       Object JavaDoc beanValue = values[i];
137       boolean found = false;
138       search: for (Iterator JavaDoc it = items.iterator(); it.hasNext(); ) {
139         Element JavaDoc item = (Element JavaDoc)it.next();
140         Object JavaDoc itemValue = handler.parse(Item.getValue(item), formatString);
141         if (itemValue.equals(beanValue)) {
142           Item.setSelected(item, true);
143           found = true;
144           break search;
145         }
146       }
147       if (!found) {
148         String JavaDoc mesg = "No item has a value of \"" + beanValue + "\".";
149         showMissing(elem, mesg);
150       }
151     }
152   }
153
154   private void showMissing(Element JavaDoc elem, String JavaDoc mesg) {
155     boolean soft = "true".equals(elem.getAttribute("ignore-missing"));
156     if (soft)
157       logger.warn(mesg);
158     else
159       throw new ConvertException(mesg);
160   }
161
162 }
163
Popular Tags