KickJava   Java API By Example, From Geeks To Geeks.

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


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.w3c.dom.Element JavaDoc;
20
21 import com.tonbeller.wcf.format.FormatException;
22 import com.tonbeller.wcf.format.Formatter;
23 import com.tonbeller.wcf.ui.Item;
24 import com.tonbeller.wcf.utils.DomUtils;
25
26 /**
27  * @author av
28  * Converter for RadioButton and CheckBox.
29  */

30
31 public abstract class BooleanConverter extends NodeConverterBase {
32   public static final int UNKNOWN = 1;
33   public static final int TRUE = 2;
34   public static final int FALSE = 3;
35
36   /**
37    * sets the selected attribute of the DOM element. If a modelReference
38    * is specified, it must point to a boolean bean-property that will be updated.
39    *
40    * @param fmt Formatter for i18n string-object conversion
41    * @param params parameters of http request
42    * @param elem the target element.
43    * @param bean the target bean
44    */

45   public void convert(Formatter fmt, Map JavaDoc params, Map JavaDoc fileParams, Element JavaDoc elem, Object JavaDoc bean)
46     throws FormatException, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
47
48     int state = isSelected(elem, params);
49     if (state == UNKNOWN)
50       return;
51
52     // was the checkbox checked?
53
Boolean JavaDoc value = new Boolean JavaDoc(state == TRUE);
54
55     // set into elem and bean
56
DomUtils.removeAttribute(elem, "error");
57     Item.setSelected(elem, value.booleanValue());
58
59     // update bean
60
String JavaDoc modelReference = Item.getModelReference(elem);
61     if (bean != null && modelReference.length() > 0)
62       PropertyUtils.setProperty(bean, Item.getModelReference(elem), value);
63   }
64
65   /**
66    * evaluates the http paramters. Does not evaluate the selected attribute of the DOM Element.
67    * @return one of TRUE, FALSE, UNKNOWN
68    */

69   public abstract int isSelected(Element JavaDoc elem, Map JavaDoc params);
70
71   /**
72    * sets the selected attribute of the checkbox from the bean
73    */

74   public void convert(Formatter fmt, Object JavaDoc bean, Element JavaDoc elem)
75     throws IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
76
77     String JavaDoc modelReference = Item.getModelReference(elem);
78     if (bean == null || modelReference.length() == 0)
79       return;
80
81     Boolean JavaDoc value = (Boolean JavaDoc) PropertyUtils.getProperty(bean, Item.getModelReference(elem));
82     boolean b = (value == null) ? false : value.booleanValue();
83     Item.setSelected(elem, b);
84   }
85
86 }
87
Popular Tags