KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > text > SAbstractFormatter


1 /*
2  * SAbstractFormatter.java
3  *
4  * Created on 11. September 2003, 11:29
5  */

6
7 /*
8  * $Id: SAbstractFormatter.java,v 1.4 2005/02/16 07:40:55 hengels Exp $
9  * Copyright 2000,2005 wingS development team.
10  *
11  * This file is part of wingS (http://www.j-wings.org).
12  *
13  * wingS is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License
15  * as published by the Free Software Foundation; either version 2.1
16  * of the License, or (at your option) any later version.
17  *
18  * Please see COPYING for the complete licence.
19  */

20 package org.wings.text;
21
22 import org.wings.SFormattedTextField;
23 import org.wings.script.JavaScriptListener;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.text.ParseException JavaDoc;
28
29 /**
30  * @author theresia
31  */

32 public abstract class SAbstractFormatter {
33
34     private Vector JavaDoc textFields = null;
35     private Vector JavaDoc jScripts = null;
36
37
38     public SAbstractFormatter() {
39         textFields = new Vector JavaDoc();
40         jScripts = new Vector JavaDoc();
41     }
42
43
44     /*
45      * Installs a SFormattedTextField to the Formatter. This is important, because the
46      * Formatter must create JavaScript for the FormattedTextField everytime the Formatter
47      * is changed.
48      **/

49     public void install(SFormattedTextField textField) {
50         boolean b = false;
51         if (textField != null) {
52             for (Iterator JavaDoc i = textFields.iterator(); i.hasNext();) {
53                 if (i.next() == textField) {
54                     return;
55                 }
56             }//for
57
textFields.add(textField);
58             if (textField.getActionListeners().length > 0) {
59                 b = true;
60             }
61
62             JavaScriptListener jScriptListener = generateJavaScript(textField, b);
63
64             textField.addScriptListener(jScriptListener);
65
66             jScripts.add(jScriptListener);
67         }
68     }
69
70     /*
71      * removes the Formatter
72      */

73     public void uninstall(SFormattedTextField textField) {
74         if (textField != null) {
75             for (int i = 0; i < textFields.size(); i++) {
76                 if (textFields.get(i) == textField) {
77                     textField.removeScriptListener((JavaScriptListener) jScripts.get(i));
78                     jScripts.remove(i);
79                     textFields.remove(i);
80                 }
81             }
82         }
83     }
84
85
86     /*
87      * Generates the new JavaScript for all registered SFormattedTextFields.
88      * New code must be created if the Format of the formatter changes or if
89      * a new ActionListener is registered.
90      */

91     public void updateFormatter() {
92         if (textFields.size() > 0) {
93             Vector JavaDoc tempJScripts = new Vector JavaDoc();
94             for (int i = 0; i < textFields.size(); i++) {
95                 SFormattedTextField tf = (SFormattedTextField) textFields.get(i);
96                 tf.removeScriptListener((JavaScriptListener) jScripts.get(i));
97                 boolean b = false;
98                 if (tf.getActionListeners().length > 0) {
99                     b = true;
100                 }
101
102                 JavaScriptListener jl = generateJavaScript(tf, b);
103                 //JavaScriptListener jl = generateJavaScript(tf.getEncodedLowLevelEventId(), b);
104
tf.addScriptListener(jl);
105                 tempJScripts.add(jl);
106
107             }
108             jScripts = tempJScripts;
109         }
110
111         /* if(javaScriptListener != null){
112              textField.removeScriptListener(javaScriptListener);
113          }
114          textField.addScriptListener(generateJavaScript(textField.getEncodedLowLevelEventId()));*/

115     }
116
117     public abstract JavaScriptListener generateJavaScript(SFormattedTextField field, boolean actionListener);
118
119     /**
120      * @param text String to convert
121      * @return Object representation of text
122      */

123     public abstract Object JavaDoc stringToValue(String JavaDoc text) throws ParseException JavaDoc;
124
125     /**
126      * @param value Value to convert
127      * @return String representation of value
128      */

129     public abstract String JavaDoc valueToString(Object JavaDoc value) throws ParseException JavaDoc;
130 }
131
Popular Tags