KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dwr > DWRFormatter


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

4 package dwr;
5
6 import org.wings.text.SAbstractFormatter;
7 import org.wings.SFormattedTextField;
8 import org.wings.SComponent;
9 import org.wings.script.JavaScriptListener;
10 import org.wings.resource.DefaultURLResource;
11 import org.wings.header.Script;
12
13 import java.text.ParseException JavaDoc;
14
15 /**
16  * @author hengels
17  * @version $Revision: 1.3 $
18  */

19 public abstract class DWRFormatter extends SAbstractFormatter {
20     private Script header;
21
22     public void install(SFormattedTextField field) {
23         super.install(field);
24         header = new Script("text/javascript",
25                             new DefaultURLResource("../dwr/interface/formatter_" + field.getName() + ".js"));
26         field.getParentFrame().addHeader(header);
27         CallableManager.getInstance().registerCallable("formatter_" + field.getName(), this);
28     }
29
30     public void uninstall(SFormattedTextField field) {
31         super.uninstall(field);
32         field.getParentFrame().removeHeader(header);
33         CallableManager.getInstance().unregisterCallable("formatter_" + field.getName());
34     }
35
36     public JavaScriptListener generateJavaScript(SFormattedTextField field, boolean actionListener) {
37         return new JavaScriptListener("onblur",
38                 "document.getElementById('{0}').getElementsByTagName('input')[0].style.color = 'inherit';" +
39                 "formatter_{0}.validate(callback_{0}, document.getElementById('{0}').getElementsByTagName('input')[0].value)",
40                 "function callback_{0}(data) {\n" +
41                 " if (!data && data != '') {\n" +
42                 " document.getElementById('{0}').getElementsByTagName('input')[0].focus();\n" +
43                 " document.getElementById('{0}').getElementsByTagName('input')[0].style.color = 'red';\n" +
44                 " }\n" +
45                 " else\n" +
46                 " document.getElementById('{0}').getElementsByTagName('input')[0].value = data;\n" +
47                 "}\n", new SComponent[] { field });
48     }
49
50     public abstract String JavaDoc validate(String JavaDoc text);
51 }
52
Popular Tags