KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dwr > DWRExample


1 /*
2  * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3  */

4 package dwr;
5
6 import org.wings.session.Session;
7 import org.wings.session.SessionManager;
8 import org.wings.externalizer.ExternalizedResource;
9 import org.wings.event.SRequestEvent;
10 import org.wings.event.SRequestListener;
11 import org.wings.*;
12 import org.wings.resource.DefaultURLResource;
13 import org.wings.header.Script;
14 import org.wings.script.JavaScriptListener;
15 import org.wings.text.SAbstractFormatter;
16
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import java.text.ParseException JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.NumberFormat JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * @author hengels
26  * @version $Revision: 1.3 $
27  */

28 public class DWRExample
29 {
30     private SFormattedTextField dateTextField;
31     private SFormattedTextField numberTextField;
32     private SButton button;
33     private SFrame frame;
34
35     public DWRExample() throws IOException JavaDoc {
36         dateTextField = new SFormattedTextField(new DateFormatter());
37         dateTextField.setName("dateTextField");
38
39         numberTextField = new SFormattedTextField(new NumberFormatter());
40         numberTextField.setName("numberTextField");
41
42         button = new SButton("submit");
43
44         SForm form = new SForm();
45         form.setLayout(new STemplateLayout(SessionManager.getSession().getServletContext().getRealPath("/template/content.thtml")));
46         form.add(dateTextField, "dateTextField");
47         form.add(numberTextField, "numberTextField");
48         form.add(button, "submit");
49
50         frame = new SFrame("DWRExample example");
51         frame.getContentPane().add(form);
52         frame.show();
53         frame.addHeader(new Script("text/javascript", new DefaultURLResource("../dwr/engine.js")));
54     }
55
56     public static class DateFormatter extends DWRFormatter {
57         DateFormat JavaDoc format = DateFormat.getDateInstance(DateFormat.SHORT, SessionManager.getSession().getLocale());
58
59         public Object JavaDoc stringToValue(String JavaDoc text) throws ParseException JavaDoc {
60             System.out.println("text = '" + text + "'");
61             if (text == null || text.trim().length() == 0)
62                 return null;
63             else
64                 return format.parse(text.trim());
65         }
66
67         public String JavaDoc valueToString(Object JavaDoc value) throws ParseException JavaDoc {
68             if (value == null)
69                 return "";
70             else
71                 return format.format(value);
72         }
73
74         public String JavaDoc validate(String JavaDoc text) {
75             try {
76                 String JavaDoc s = valueToString(stringToValue(text));
77                 System.out.println("return = '" + s + "'");
78                 return s;
79             }
80             catch (ParseException JavaDoc e) {
81                 return null;
82             }
83         }
84     }
85
86     public static class NumberFormatter extends DWRFormatter {
87         NumberFormat JavaDoc format = NumberFormat.getNumberInstance(SessionManager.getSession().getLocale());
88
89         public Object JavaDoc stringToValue(String JavaDoc text) throws ParseException JavaDoc {
90             if (text == null || text.trim().length() == 0)
91                 return null;
92             else
93                 return format.parse(text.trim());
94         }
95
96         public String JavaDoc valueToString(Object JavaDoc value) throws ParseException JavaDoc {
97             if (value == null)
98                 return "";
99             else
100                 return format.format(value);
101         }
102
103         public String JavaDoc validate(String JavaDoc text) {
104             try {
105                 return valueToString(stringToValue(text));
106             }
107             catch (ParseException JavaDoc e) {
108                 return null;
109             }
110         }
111     }
112 }
113
Popular Tags