KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > ui > EditCtrl


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.ui;
14
15 import java.util.Date JavaDoc;
16
17 import org.w3c.dom.Document JavaDoc;
18 import org.w3c.dom.Element JavaDoc;
19
20 import com.tonbeller.wcf.format.FormatException;
21 import com.tonbeller.wcf.format.FormatHandler;
22 import com.tonbeller.wcf.format.Formatter;
23 import com.tonbeller.wcf.utils.XoplonNS;
24
25 /**
26  * has a value, type and format string. The value may be read or written into
27  * a bean via model reference.
28  */

29 public abstract class EditCtrl extends TypedCtrl {
30
31   /** Set value */
32   public static void setValue(Element JavaDoc element, String JavaDoc value) {
33     XoplonNS.setAttribute(element, "value", value);
34   }
35
36   /** Get value */
37   public static String JavaDoc getValue(Element JavaDoc element) {
38     return XoplonNS.getAttribute(element, "value");
39   }
40
41   /** Set max input length */
42   public static void setMaxLength(Element JavaDoc element, int maxLength) {
43     XoplonNS.setAttribute(element, "maxlength", String.valueOf(maxLength));
44   }
45
46   /** Get max input length */
47   public static int getMaxLength(Element JavaDoc element) {
48     return Integer.parseInt(XoplonNS.getAttribute(element, "maxlength"));
49   }
50
51   /** factory function. creates new DOM element */
52   protected static Element JavaDoc createValueHolder(Document JavaDoc doc, String JavaDoc tagName, String JavaDoc type) {
53     Element JavaDoc retVal = XoplonCtrl.createCtrl(doc, tagName);
54     XoplonNS.setAttribute(retVal, "type", type);
55     return retVal;
56   }
57
58   /** factory function. creates new DOM element */
59   protected static Element JavaDoc createValueHolder(Document JavaDoc doc, String JavaDoc tagName) {
60     return createValueHolder(doc, tagName, "string");
61   }
62
63   /** set value with type conversion */
64   public static void setValue(Formatter formatter, Element JavaDoc element, Object JavaDoc value) throws FormatException {
65     FormatHandler formatHandler = formatter.getHandler(getType(element));
66     String JavaDoc formatString = getFormatString(element);
67     String JavaDoc valueString = formatHandler.format(value, formatString);
68     XoplonNS.setAttribute(element, "value", valueString);
69   }
70
71   /** get value with type conversion */
72   public static Object JavaDoc getValue(Formatter formatter, Element JavaDoc element) throws FormatException {
73     String JavaDoc className = getType(element);
74     FormatHandler parser = formatter.getHandler(className);
75     String JavaDoc formatString = getFormatString(element);
76     return parser.parse(XoplonNS.getAttribute(element, "value"), formatString);
77   }
78   public static boolean getBoolean(Formatter fmt, Element JavaDoc elem) throws FormatException {
79     return ((Boolean JavaDoc)getValue(fmt, elem)).booleanValue();
80   }
81   public static int getInt(Formatter fmt, Element JavaDoc elem) throws FormatException {
82     return ((Number JavaDoc)getValue(fmt, elem)).intValue();
83   }
84   public static double getDouble(Formatter fmt, Element JavaDoc elem) throws FormatException {
85     return ((Number JavaDoc)getValue(fmt, elem)).doubleValue();
86   }
87   public static Date JavaDoc getDate(Formatter fmt, Element JavaDoc elem) throws FormatException {
88     return (Date JavaDoc)getValue(fmt, elem);
89   }
90   public static String JavaDoc getString(Formatter fmt, Element JavaDoc elem) throws FormatException {
91     return getValue(elem);
92   }
93
94   public static void setBoolean(Formatter fmt, Element JavaDoc elem, boolean value) throws FormatException {
95     setValue(fmt, elem, new Boolean JavaDoc(value));
96   }
97   public static void setInt(Formatter fmt, Element JavaDoc elem, int value) throws FormatException {
98     setValue(fmt, elem, new Integer JavaDoc(value));
99   }
100   public static void setDouble(Formatter fmt, Element JavaDoc elem, double value) throws FormatException {
101     setValue(fmt, elem, new Double JavaDoc(value));
102   }
103   public static void setDate(Formatter fmt, Element JavaDoc elem, Date JavaDoc value) throws FormatException {
104     setValue(fmt, elem, value);
105   }
106   public static void setString(Formatter fmt, Element JavaDoc elem, String JavaDoc value) throws FormatException {
107     setValue(elem, value);
108   }
109 }
110
Popular Tags