1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
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
53
54
55
56
57 public String getDescription() {
58 return null;
59 }
60
61
62
63
64 public PropertyEditor getEditor() {
65 return this;
66 }
67
68
69
70
71 public Object getPropertyValue() {
72 return getValue();
73 }
74
75
76
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
91
92 public void setDescription(String description) {
93
94 }
95
96
97
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
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 }