KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SFormattedTextField


1 /*
2  * SFormattedTextField.java
3  *
4  * Created on 9. September 2003, 09:05
5  */

6
7 /*
8  * $Id: SFormattedTextField.java,v 1.6 2005/02/17 13:12:07 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;
21
22 import org.wings.text.SAbstractFormatter;
23 import org.wings.text.SDefaultFormatter;
24 import org.wings.script.JavaScriptListener;
25
26 import java.awt.event.ActionListener JavaDoc;
27 import java.text.ParseException JavaDoc;
28
29
30 /*
31  * @author theresia
32  */

33 public class SFormattedTextField
34     extends STextField
35 {
36     private SAbstractFormatter formatter = null;
37     private static final SAbstractFormatter NO_FORMATTER = new SAbstractFormatter() {
38         public JavaScriptListener generateJavaScript(SFormattedTextField field, boolean actionListener) {
39             return null;
40         }
41
42         public Object JavaDoc stringToValue(String JavaDoc text) throws ParseException JavaDoc {
43             return null;
44         }
45
46         public String JavaDoc valueToString(Object JavaDoc value) throws ParseException JavaDoc {
47             return null;
48         }
49     };
50
51     public SFormattedTextField() {
52         this(NO_FORMATTER);
53     }
54
55     public SFormattedTextField(SAbstractFormatter formatter) {
56         this.formatter = formatter;
57     }
58
59     public void setValue(Object JavaDoc object) {
60         String JavaDoc string = null;
61         if (formatter != null)
62             try {
63                 string = this.formatter.valueToString(object);
64             }
65             catch (ParseException JavaDoc e) {
66                 e.printStackTrace();
67             }
68         super.setText(string);
69     }
70
71     public Object JavaDoc getValue() {
72         Object JavaDoc returnValue = null;
73         try {
74             returnValue = this.formatter.stringToValue(this.getText());
75         }
76         catch (ParseException JavaDoc e) {
77             e.printStackTrace();
78         }
79         return returnValue;
80     }
81
82     // brauch man das?? So wohl nicht...
83
public SFormattedTextField(Object JavaDoc value) {
84         setValue(value);
85     }
86
87     public SAbstractFormatter getFormatter() {
88         return formatter;
89     }
90
91     public void setFormatter(SAbstractFormatter formatter) {
92         if (this.formatter != null) {
93             this.formatter.uninstall(this);
94         }
95         this.formatter = formatter;
96         formatter.install(this);
97     }
98
99     /*
100      * When an ActionListener is added, the Formatter must be updated.
101      */

102     public void addActionListener(ActionListener JavaDoc listener) {
103         super.addActionListener(listener);
104         if (formatter == null) {
105             formatter = new SDefaultFormatter();
106             formatter.install(this);
107         } else {
108             formatter.updateFormatter();
109         }
110     }
111
112     protected void setParentFrame(SFrame parentFrame) {
113         if (this.parentFrame != null)
114             formatter.uninstall(this);
115         super.setParentFrame(parentFrame);
116         if (this.parentFrame != null)
117             formatter.install(this);
118     }
119 }
Popular Tags