View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.property.editors;
21  
22  import java.awt.Dimension;
23  
24  import javax.swing.JTextField;
25  
26  import com.cosylab.gui.components.numberfield.AbstractNumberDocument;
27  import com.cosylab.gui.components.numberfield.DoubleDocument;
28  
29  /**
30   * description
31   * 
32   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a> 
33   * @version $id$
34   */
35  public class NumberEditor extends JTextField implements PropertyEditor {
36      private boolean allowNull=false;
37      /**
38       * 
39       */
40  	public NumberEditor() {
41  		this(new DoubleDocument());
42  		setValue(new Double(0));
43  	}
44  
45  	public NumberEditor(AbstractNumberDocument doc) {
46  		super();
47  		setDocument(doc);
48  		setPreferredSize(new Dimension(120, 20));
49  	}
50  	
51  	/**
52  	 * This implementation return null, to indicate the editor is unable to display 
53  	 * the description.
54  	 * 
55  	 * @see PropertyEditor#getDescription()
56  	 */
57  	public String getDescription() {
58  		return null;
59  	}
60  
61  	/**
62  	 * @see PropertyEditor#getEditor()
63  	 */
64  	public PropertyEditor getEditor() {
65  		return this;
66  	}
67  
68  	/**
69  	 * @see PropertyEditor#getValue()
70  	 */
71  	public Object getPropertyValue() {
72  		return getValue();
73  	}
74  
75  	/**
76  	 * @see PropertyEditor#setValue(Object)
77  	 */
78  	public boolean setPropertyValue(Object value) {
79  		if (value instanceof Number) {
80  			setValue((Number)value);
81  			return true;
82  		} else if (value==null && allowNull) {
83              setValue(null);
84  		    return true;
85          }
86  		return false;
87  	}
88  
89  	/**
90  	 * @see PropertyEditor#setDescription(String)
91  	 */
92  	public void setDescription(String description) {
93  		// we can't handle this
94  	}
95  	
96  	/* (non-Javadoc)
97  	 * @see com.cosylab.gui.components.NumberField#setValue(java.lang.Number)
98  	 */
99  	public void setValue(Number newValue) {
100 		Number oldValue=getValue();
101 		setText(newValue==null?"":String.valueOf(newValue));
102 		revalidate();
103 		if (newValue!=oldValue && (newValue==null || !newValue.equals(oldValue))) {
104 			firePropertyChange(PROPERTY_VALUE_NAME,oldValue,newValue);
105 		}
106 	}
107     
108     public void setMaxValue(Number maxValue) {
109         ((AbstractNumberDocument)getDocument()).setMaxValue(maxValue);
110     }
111     
112     public void setMinValue(Number minValue) {
113         ((AbstractNumberDocument)getDocument()).setMinValue(minValue);
114     }
115 
116 	/**
117 	 * @return
118 	 */
119 	public Number getValue() {
120 		Number ret=((AbstractNumberDocument)getDocument()).getValue();
121 		if (ret==null && !allowNull) {
122 			ret=new Double(0);
123 		}
124 		return ret;
125 	}
126     public boolean isNullAllowed() {
127         return allowNull;
128     }
129     public void setNullAllowed(boolean allow) {
130         this.allowNull=allow;
131     }
132 
133 }