KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > FormatEditor


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.Dimension JavaDoc;
22 import java.text.ParsePosition JavaDoc;
23 import org.objectweb.jac.aspects.gui.FieldEditor;
24 import org.objectweb.jac.aspects.gui.Format;
25 import org.objectweb.jac.core.rtti.FieldItem;
26 import org.objectweb.jac.util.WrappedThrowableException;
27
28 public abstract class FormatEditor extends TextFieldEditor
29     implements FieldEditor
30 {
31     /** Stores the default format of the date */
32     protected Format format;
33
34     /**
35     * Constructs a new date editor. */

36
37     public FormatEditor(Object JavaDoc substance, FieldItem field) {
38         super(substance,field);
39         textField = new JTextField(10);
40         textField.addFocusListener(this);
41         add(textField);
42         initFormat(field);
43     }
44
45     protected abstract void initFormat(FieldItem field);
46
47     public void setField(FieldItem field) {
48         super.setField(field);
49         initFormat(field);
50     }
51
52     public Object JavaDoc getValue() {
53         try {
54             if (textField.getText().equals("")) {
55                 return null;
56             } else {
57                 return parse(textField.getText());
58             }
59         } catch (Exception JavaDoc e) {
60             throw new WrappedThrowableException(e);
61         }
62     }
63
64     protected Object JavaDoc parse(String JavaDoc s) {
65         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
66         return format.parse(s,pos);
67     }
68
69     public void setValue(Object JavaDoc value) {
70         super.setValue(value);
71         if( value != null )
72             textField.setText(format.format(value));
73     }
74 }
75
Popular Tags