001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: NumericCellEditor.java,v $
023   Revision 1.5  2004/05/12 18:36:41  markl
024   comment block updates
025
026   Revision 1.4  2003/01/19 09:50:53  markl
027   Javadoc & comment header updates.
028
029   Revision 1.3  2001/03/12 09:27:58  markl
030   Source code and Javadoc cleanup.
031
032   Revision 1.2  1999/07/25 13:40:54  markl
033   Minor enhancements and bug fixes.
034
035   Revision 1.1  1999/07/12 08:51:41  markl
036   Initial revision
037   ----------------------------------------------------------------------------
038*/
039
040package kiwi.ui;
041
042import java.awt.*;
043import java.text.*;
044import javax.swing.*;
045
046/** A cell editor for editing numeric fields, including integer, decimal,
047 * percentage, and currency amounts, formatted according to the rules of the
048 * current locale.
049 *
050 * @author Mark Lindner
051 *
052 * @see kiwi.text.FormatConstants
053 * @see kiwi.util.LocaleManager
054 * @see kiwi.ui.NumericField
055 * @see kiwi.ui.NumericTableCellRenderer
056 * @see kiwi.db.DomainObjectFieldAdapter
057 */
058
059public class NumericCellEditor extends DefaultCellEditor
060  {
061  private NumericField field;
062
063  /** Construct a new <code>NumericCellEditor</code> of the specified type.
064   *
065   * @param type The data type to be edited by this field; one of the
066   * constants <code>CURRENCY_FORMAT</code>, <code>DECIMAL_FORMAT</code>,
067   * <code>INTEGER_FORMAT</code< or <code>PERCENTAGE_FORMAT</code>, defined in
068   * <code>kiwi.text.FormatConstants</code>.
069   */
070  
071  public NumericCellEditor(int type)
072    {
073    this(type, 2);
074    }
075
076  /** Construct a new <code>NumericCellEditor</code> of the specified type and
077   * number of decimals displayed.
078   *
079   * @param type The data type to be edited by this field; one of the
080   * constants <code>CURRENCY_FORMAT</code>, <code>DECIMAL_FORMAT</code>,
081   * <code>INTEGER_FORMAT</code< or <code>PERCENTAGE_FORMAT</code>, defined in
082   * <code>kiwi.text.FormatConstants</code>.
083   * @param decimals The number of decimal places to be displayed (for
084   * non-integer values only).
085   */
086  
087  public NumericCellEditor(int type, int decimals)
088    {
089    super(new NumericField(1, type));
090    
091    field = (NumericField)editorComponent;
092    field.setDecimals(decimals);
093    }
094
095  /** Stop cell editing. This method stops cell editing (effectively
096   * committing the edit) only if the data entered is validated successfully.
097   *
098   * @return <code>true</code> if cell editing may stop, and <code>false</code>
099   * otherwise.
100   */
101  
102  public final boolean stopCellEditing()
103    {
104    return(validate());
105    }
106
107  /* perform the validation */
108  
109  private boolean validate()
110    {
111    boolean ok = field.validateInput();
112
113    if(ok)
114      fireEditingStopped();
115
116    return(ok);
117    }
118
119  /** Get the value currently in the cell editor.
120   *
121   * @return The current value, as a <code>Double</code>.
122   */
123   
124  public Object getCellEditorValue()
125    {
126    field.validateInput();
127    
128    return(new Double(field.getValue()));
129    }
130
131  /** Set the formatting type.
132   *
133   * @param type The data type to be edited by this cell editor. See the
134   * constructor for more information.
135   */  
136    
137  public void setType(int type)
138    {
139    field.setType(type);
140    }
141
142  /** Get the formatting type.
143   *
144   * @return The data type being edited by this cell editor.
145   */
146  
147  public int getType()
148    {
149    return(field.getType());
150    }
151
152  /** Set the number of decimal places to display for non-integer values.
153   *
154   * @param decimals The number of decimal places.
155   * @exception java.lang.IllegalArgumentException If <code>decimals</code>
156   * is less than 0.
157   */
158  
159  public void setDecimals(int decimals)
160    {
161    field.setDecimals(decimals);
162    }
163
164  /** Get the number of decimal places being displayed by this cell editor.
165   *
166   * @return The number of decimal places.
167   */
168  
169  public int getDecimals()
170    {
171    return(field.getDecimals());
172    }
173
174  /* Prepare the editor for a value. */
175  
176  private Component _prepareEditor(Object value)
177    {
178    if(value.getClass().getSuperclass() == Number.class)
179      field.setValue(((Number)value).doubleValue());
180
181    return(field);
182    }
183
184  /** Get an editor for a JTable. */
185  
186  public Component getTableCellEditorComponent(JTable table, Object value,
187                                               boolean isSelected, int row,
188                                               int column)
189    {
190    return(_prepareEditor(value));
191    }
192
193  /** Get an editor for a JTree. */
194  
195  public Component getTreeCellEditorComponent(JTree tree, Object value,
196                                              boolean isSelected,
197                                              boolean expanded, boolean leaf,
198                                              int row)
199    {
200    return(_prepareEditor(value));
201    }
202
203  }
204
205/* end of source file */