KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
17
18 import org.apache.commons.beanutils.PropertyUtils;
19 import org.apache.log4j.Logger;
20 import org.w3c.dom.Element JavaDoc;
21
22 import com.tonbeller.wcf.format.FormatException;
23 import com.tonbeller.wcf.format.FormatHandler;
24 import com.tonbeller.wcf.format.Formatter;
25 import com.tonbeller.wcf.ui.EditCtrl;
26 import com.tonbeller.wcf.ui.TypedCtrl;
27 import com.tonbeller.wcf.utils.XoplonNS;
28
29 /**
30  * converts user input for a Edit control
31  * @see com.tonbeller.wcf.ui.Edit
32  * @author av
33  */

34 public class EditCtrlConverter extends NodeConverterBase {
35   private static Logger logger = Logger.getLogger(EditCtrlConverter.class);
36
37   /**
38    * updates a bean property as well as the value attribute of element
39    */

40   public void convert(Formatter formatter, Map JavaDoc param, Map JavaDoc fileSource, Element JavaDoc element, Object JavaDoc bean)
41     throws FormatException, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
42
43     // disabled = true? return
44
if (TypedCtrl.isDisabled(element))
45       return;
46
47     String JavaDoc id = EditCtrl.getId(element);
48     String JavaDoc[] inputValue = (String JavaDoc[]) param.get(id);
49
50     // input available
51
if (inputValue != null && inputValue.length > 0) {
52
53       XoplonNS.removeAttribute(element, "error");
54
55       // parse input
56
String JavaDoc formatString = EditCtrl.getFormatString(element);
57       try {
58
59         checkRequired(formatter.getLocale(), element, inputValue[0].trim().length() == 0);
60
61         String JavaDoc type = EditCtrl.getType(element);
62         FormatHandler handler = formatter.getHandler(type);
63         if (handler == null)
64           throw new FormatException("no handler found for type: " + type);
65
66         Object JavaDoc newValue = handler.parse(inputValue[0], formatString);
67         String JavaDoc strValue = handler.format(newValue, formatString);
68         EditCtrl.setValue(element, strValue);
69
70         String JavaDoc model = EditCtrl.getModelReference(element);
71         if (bean != null && model.length() > 0) {
72           PropertyUtils.setProperty(bean, model, newValue);
73         }
74
75       } catch (IllegalAccessException JavaDoc e) {
76         logger.info("exception caught", e);
77         XoplonNS.setAttribute(element, "error", e.getMessage());
78         XoplonNS.setAttribute(element, "value", inputValue[0]);
79         throw e;
80       } catch (NoSuchMethodException JavaDoc e) {
81         logger.info("exception caught", e);
82         XoplonNS.setAttribute(element, "error", e.getMessage());
83         XoplonNS.setAttribute(element, "value", inputValue[0]);
84         throw e;
85       } catch (InvocationTargetException JavaDoc e) {
86         logger.info("exception caught", e);
87         XoplonNS.setAttribute(element, "error", e.getMessage());
88         XoplonNS.setAttribute(element, "value", inputValue[0]);
89         throw e;
90       } catch (FormatException e) {
91         logger.info("invalid user input: " + e.getMessage());
92         XoplonNS.setAttribute(element, "error", e.getMessage());
93         XoplonNS.setAttribute(element, "value", inputValue[0]);
94         throw e;
95       }
96     }
97   }
98
99   /**
100    * initializes the value attribute of element from a bean property.
101    */

102   public void convert(Formatter formatter, Object JavaDoc bean, Element JavaDoc element)
103     throws IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
104
105     try {
106       String JavaDoc model = EditCtrl.getModelReference(element);
107       if (model.length() == 0)
108         return;
109
110       String JavaDoc type = EditCtrl.getType(element);
111       FormatHandler handler = formatter.getHandler(type);
112       if (handler == null)
113         throw new FormatException("no handler found for type: " + type);
114
115       Object JavaDoc value = PropertyUtils.getProperty(bean, model);
116       String JavaDoc pattern = EditCtrl.getFormatString(element);
117       String JavaDoc strValue = handler.format(value, pattern);
118       EditCtrl.setValue(element, strValue);
119
120     } catch (IllegalAccessException JavaDoc e) {
121       XoplonNS.setAttribute(element, "error", e.getMessage());
122       throw e;
123     } catch (NoSuchMethodException JavaDoc e) {
124       XoplonNS.setAttribute(element, "error", e.getMessage());
125       throw e;
126     } catch (InvocationTargetException JavaDoc e) {
127       XoplonNS.setAttribute(element, "error", e.getMessage());
128       throw e;
129     } catch (FormatException e) {
130       XoplonNS.setAttribute(element, "error", e.getMessage());
131       throw e;
132     }
133   }
134
135 }
136
Popular Tags