KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > swing > GenericCellEditor


1 // ============================================================================
2
// $Id: GenericCellEditor.java,v 1.13 2005/12/17 04:45:03 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.swing;
34
35 import java.awt.Color JavaDoc;
36 import java.text.Format JavaDoc;
37 import java.text.NumberFormat JavaDoc;
38 import javax.swing.DefaultCellEditor JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JTextField JavaDoc;
42 import javax.swing.border.LineBorder JavaDoc;
43 import net.sf.jga.fn.UnaryFunctor;
44 import net.sf.jga.fn.arithmetic.Arithmetic;
45 import net.sf.jga.fn.arithmetic.ArithmeticFactory;
46 import net.sf.jga.fn.property.ConstructUnary;
47 import net.sf.jga.fn.string.DefaultFormat;
48 import net.sf.jga.fn.string.FormatValue;
49 import net.sf.jga.fn.string.ParseFormat;
50 import net.sf.jga.fn.EvaluationException;
51
52 /**
53  * CellEditor that uses a pair of functors to parse/format a value. Empty
54  * strings, or strings containing only whitespace will correspond to null
55  * values of type T.
56  * <p>
57  * Copyright &copy; 2003-2005 David A. Hall
58  *
59  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
60  */

61
62 public class GenericCellEditor<T> extends DefaultCellEditor JavaDoc {
63
64     static final long serialVersionUID = -6265710748312156901L;
65
66     // correctly typed reference to the editor component, saving excess casts
67
protected JTextField JavaDoc _component;
68
69     // default value returned when the string is empty.
70
private T _defaultValue;
71
72     
73     /**
74      * Builds a GenericCellEdior using default format for the given class. The
75      * resulting editor can support any class that defines toString() and also
76      * has a one argument constructor that takes a String.
77      */

78     public GenericCellEditor (Class JavaDoc<T> type) {
79         this(new DefaultFormat<T>(), new ConstructUnary<String JavaDoc,T>(String JavaDoc.class, type));
80     }
81     
82     /**
83      * Builds the GenericCellEditor using the given functor pair.
84      */

85     public GenericCellEditor (UnaryFunctor<T,String JavaDoc> formatter, UnaryFunctor<String JavaDoc,T> parser) {
86         this(formatter, parser, null);
87     }
88
89     /**
90      * Builds the GenericCellEditor using the given functor pair and default
91      * value for empty strings.
92      */

93     public GenericCellEditor (UnaryFunctor<T,String JavaDoc> formatter, UnaryFunctor<String JavaDoc,T> parser,
94                               T defaultValue)
95     {
96         super(new JTextField JavaDoc());
97         if (formatter == null) {
98             String JavaDoc msg = "Formatter (functor) is required";
99             throw new IllegalArgumentException JavaDoc(msg);
100         }
101         
102         if (parser == null) {
103             String JavaDoc msg = "Parser (functor) is required";
104             throw new IllegalArgumentException JavaDoc(msg);
105         }
106         
107         _component = (JTextField JavaDoc) editorComponent;
108         _defaultValue = defaultValue;
109
110         // overrides the protected delegate
111
delegate = new FormattedDelegate(formatter, parser);
112     }
113
114     /**
115      * Retrieves the value from the editor component, giving a visual clue when
116      * the value is invalid
117      */

118     
119     public boolean stopCellEditing() {
120         try {
121             // @SuppressException
122
// The FormattedDelegate class provides the implementation of
123
// getCellEditorValue, and it is known to always return a T
124
T value = (T) getCellEditorValue();
125         }
126         catch (Exception JavaDoc e) {
127             _component.setBorder(new LineBorder JavaDoc(Color.red));
128             return false;
129         }
130         
131         return super.stopCellEditing();
132     }
133
134     /**
135      * Returns the horizontal alignment: one of the values defined by JTextField.
136      */

137     public int getHorizontalAlignment() {
138         return _component.getHorizontalAlignment();
139     }
140     
141     /**
142      * Sets the horizontal alignment to one of the values defined by JTextField.
143      */

144     public void setHorizontalAlignment(int alignment) {
145         _component.setHorizontalAlignment(alignment);
146     }
147
148     public String JavaDoc toString() {
149         return "GenericTableCellEditor["+delegate+"]";
150     }
151     
152     //
153
// Overrides the base class nested EditorDelegate class, using the given
154
// functor pair to fill in and interpret the value of the component.
155
//
156

157     protected class FormattedDelegate extends EditorDelegate {
158
159         static final long serialVersionUID = -3670848351305705031L;
160         
161         private UnaryFunctor<T,String JavaDoc> _formatter;
162         private UnaryFunctor<String JavaDoc,T> _parser;
163         
164         public FormattedDelegate(UnaryFunctor<T,String JavaDoc> formatter,
165                                  UnaryFunctor<String JavaDoc,T> parser)
166         {
167             _formatter = formatter;
168             _parser = parser;
169         }
170         
171         /**
172          * Sets the text in the editor component by formatting the given value.
173          * Will use the default value if one is available when given a null,
174          * otherwise will use an empty string.
175          */

176         public void setValue(Object JavaDoc value) {
177             if (value != null) {
178                 // @SuppressWarnings
179
// ClassCastException is explicitely caught and sort-of dealt with
180
try {
181                     _component.setText(_formatter.fn((T)value));
182                     return;
183                 }
184                 catch (ClassCastException JavaDoc x) {
185                     // The value we're passed is not of the correct type, so
186
// fall through to one of the defaults.
187
}
188             }
189             
190             if (_defaultValue != null) {
191                 _component.setText(_formatter.fn(_defaultValue));
192                 return;
193             }
194             
195             _component.setText("");
196         }
197         
198         /**
199          * Retrieves the value in the component. If the component contains an
200          * empty string (or one with only whitespace), or if an exception
201          * occurs, then returns the default value (which may be null).
202          */

203         public Object JavaDoc getCellEditorValue() {
204             String JavaDoc txt = _component.getText();
205             if (txt == null || txt.trim().length() == 0) {
206                 return _defaultValue;
207             }
208
209             try {
210                 Object JavaDoc val = _parser.fn(txt);
211                 return val;
212             }
213             catch (EvaluationException x) {
214                 return _defaultValue;
215             }
216         }
217
218         public String JavaDoc toString() { return "FormattedDelegate["+_formatter+","+_parser+"]"; }
219     }
220 }
221
Popular Tags