001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.properties.editors;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025:
026: import javax.swing.JPanel;
027: import javax.swing.JTextField;
028:
029: import com.jeta.swingbuilder.gui.components.FloatDocument;
030: import com.jeta.swingbuilder.gui.components.IntegerDocument;
031: import com.jeta.swingbuilder.gui.properties.JETAPropertyEditor;
032: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
033:
034: public class NumericEditor extends JETAPropertyEditor {
035: /**
036: * Panel that is used to hold our editor
037: */
038: private JPanel m_panel;
039:
040: /**
041: * Text field that accepts only characters that are valid for floating point
042: * values (i.e. digit, ., - )
043: */
044: private JTextField m_field = new JTextField();
045:
046: /**
047: * The type of value to expect (Integer.class, Long.class, Float.class,
048: * Short.class, or Double.class )
049: */
050: private Class m_number_class;
051:
052: /**
053: * ctor
054: *
055: * @param c
056: * the type of value to expect (Integer.class, Long.class,
057: * Float.class, Short.class, or Double.class )
058: */
059: public NumericEditor(Class c) {
060: m_number_class = c;
061: m_panel = new JPanel();
062: m_panel.setLayout(new BorderLayout());
063: m_panel.add(m_field, BorderLayout.CENTER);
064:
065: m_panel.setBackground(javax.swing.UIManager
066: .getColor("Table.background"));
067:
068: m_field.addActionListener(new ActionListener() {
069: public void actionPerformed(ActionEvent evt) {
070: setValue(m_field.getText());
071: }
072: });
073:
074: if (isIntegral())
075: m_field.setDocument(new IntegerDocument());
076: else
077: m_field.setDocument(new FloatDocument());
078: }
079:
080: public Object convertValue(Object value) {
081: if (value instanceof Byte)
082: return value;
083: else if (value instanceof Short)
084: return value;
085: else if (value instanceof Integer)
086: return value;
087: else if (value instanceof Long)
088: return value;
089: else if (value instanceof Float)
090: return value;
091: else if (value instanceof Double)
092: return value;
093: else if (value instanceof String) {
094: if (isIntegral()) {
095: return new Long(toLong((String) value));
096: } else {
097: return new Double(toDouble((String) value));
098: }
099: } else
100: return "0";
101: }
102:
103: /**
104: * @return the custom editor
105: */
106: public Component getCustomEditor() {
107: return m_panel;
108: }
109:
110: /**
111: * @return true if this editor supports custom editing inline in the
112: * property table. Property types such as the Java primitives and
113: * Strings support inline editing.
114: */
115: public boolean supportsInlineEditing() {
116: return true;
117: }
118:
119: /**
120: * Sets the value
121: */
122: public void setValue(Object value) {
123: value = convertValue(value);
124: super .setValue(value);
125: m_field.setText(value.toString());
126: }
127:
128: /**
129: * @return the value represented by this field
130: */
131: public Object getValue() {
132: String field_txt = FormDesignerUtils
133: .fastTrim(m_field.getText());
134: if (isByte())
135: return new Byte((byte) toLong(field_txt));
136: else if (isShort())
137: return new Short((short) toLong(field_txt));
138: else if (isInteger())
139: return new Integer((int) toLong(field_txt));
140: else if (isLong())
141: return new Long(toLong(field_txt));
142: else if (isFloat())
143: return new Float((float) toDouble(field_txt));
144: else if (isDouble())
145: return new Double(toDouble(field_txt));
146: else {
147: assert (false);
148: return null;
149: }
150: }
151:
152: private boolean isByte() {
153: return (Byte.class == m_number_class);
154: }
155:
156: private boolean isInteger() {
157: return (Integer.class == m_number_class);
158: }
159:
160: private boolean isShort() {
161: return (Short.class == m_number_class);
162: }
163:
164: private boolean isLong() {
165: return (Long.class == m_number_class);
166: }
167:
168: private boolean isIntegral() {
169: return (isByte() || isInteger() || isShort() || isLong());
170: }
171:
172: private boolean isFloat() {
173: return (Float.class == m_number_class);
174: }
175:
176: private boolean isDouble() {
177: return (Double.class == m_number_class);
178: }
179:
180: /**
181: * @return the given string converted to a double. If the string is not
182: * valid, 0 is returned.
183: */
184: private double toDouble(String sval) {
185: try {
186: return Double.parseDouble(sval);
187: } catch (Exception e) {
188: return 0.0;
189: }
190: }
191:
192: /**
193: * @return the given string converted to a long. If the string is not valid,
194: * 0 is returned.
195: */
196: private long toLong(String sval) {
197: try {
198: return Long.parseLong(sval);
199: } catch (Exception e) {
200: return 0L;
201: }
202: }
203:
204: public static class IntegerEditor extends NumericEditor {
205: public IntegerEditor() {
206: super (Integer.class);
207: }
208: }
209:
210: public static class ByteEditor extends NumericEditor {
211: public ByteEditor() {
212: super (Byte.class);
213: }
214: }
215:
216: public static class ShortEditor extends NumericEditor {
217: public ShortEditor() {
218: super (Short.class);
219: }
220: }
221:
222: public static class LongEditor extends NumericEditor {
223: public LongEditor() {
224: super (Long.class);
225: }
226: }
227:
228: public static class FloatEditor extends NumericEditor {
229: public FloatEditor() {
230: super (Float.class);
231: }
232: }
233:
234: public static class DoubleEditor extends NumericEditor {
235: public DoubleEditor() {
236: super (Double.class);
237: }
238: }
239: }
|