Revision 205
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.serv/org.gvsig.tools.swing.serv.field/src/main/java/org/gvsig/tools/swing/serv/field/component/spinner/NullNumberEditor.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program 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 this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
/* |
|
23 |
* AUTHORS (In addition to CIT): |
|
24 |
* 2010 Institute of New Imaging Technologies (INIT): |
|
25 |
* http://www.init.uji.es |
|
26 |
* Geographic Information research group: |
|
27 |
* http://www.geoinfo.uji.es |
|
28 |
* Universitat Jaume I, Spain |
|
29 |
*/ |
|
30 |
|
|
31 |
/** |
|
32 |
* |
|
33 |
*/ |
|
34 |
package org.gvsig.tools.swing.serv.field.component.spinner; |
|
35 |
|
|
36 |
import java.util.Date; |
|
37 |
|
|
38 |
import javax.swing.JSpinner; |
|
39 |
import javax.swing.SpinnerModel; |
|
40 |
import javax.swing.event.ChangeEvent; |
|
41 |
|
|
42 |
/** |
|
43 |
* @author <a href="mailto:reinhold@uji.es">cmartin</a> |
|
44 |
* |
|
45 |
*/ |
|
46 |
public class NullNumberEditor extends JSpinner.NumberEditor { |
|
47 |
/** |
|
48 |
* @param arg0 |
|
49 |
*/ |
|
50 |
public NullNumberEditor(JSpinner spinner) { |
|
51 |
super(spinner); |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* @param arg0 |
|
56 |
* @param arg1 |
|
57 |
*/ |
|
58 |
public NullNumberEditor(JSpinner spinner, String dateFormatPattern) { |
|
59 |
super(spinner, dateFormatPattern); |
|
60 |
} |
|
61 |
|
|
62 |
public void stateChanged(ChangeEvent e) { |
|
63 |
// super.stateChanged(e); |
|
64 |
Object value = getSpinner().getValue(); |
|
65 |
String text = ""; |
|
66 |
// String text = "(Add a value)"; |
|
67 |
if (value != null) { |
|
68 |
// text = getFormat().format((Number) value); |
|
69 |
super.stateChanged(e); |
|
70 |
}else{ |
|
71 |
SpinnerModel model = getSpinner().getModel(); |
|
72 |
model.setValue(0); |
|
73 |
getTextField().setText(text); |
|
74 |
getTextField().repaint(); |
|
75 |
} |
|
76 |
} |
|
77 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.serv/org.gvsig.tools.swing.serv.field/src/main/java/org/gvsig/tools/swing/serv/field/component/spinner/NullableNumberSpinnerModel.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program 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 this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
/* |
|
23 |
* AUTHORS (In addition to CIT): |
|
24 |
* 2010 Institute of New Imaging Technologies (INIT): |
|
25 |
* http://www.init.uji.es |
|
26 |
* Geographic Information research group: |
|
27 |
* http://www.geoinfo.uji.es |
|
28 |
* Universitat Jaume I, Spain |
|
29 |
*/ |
|
30 |
|
|
31 |
/** |
|
32 |
* |
|
33 |
*/ |
|
34 |
package org.gvsig.tools.swing.serv.field.component.spinner; |
|
35 |
|
|
36 |
import javax.swing.AbstractSpinnerModel; |
|
37 |
import javax.swing.SpinnerModel; |
|
38 |
import javax.swing.SpinnerNumberModel; |
|
39 |
import javax.swing.event.ChangeListener; |
|
40 |
|
|
41 |
import org.gvsig.tools.dynobject.DynField; |
|
42 |
|
|
43 |
/** |
|
44 |
* @author <a href="mailto:reinhold@uji.es">cmartin</a> |
|
45 |
* |
|
46 |
*/ |
|
47 |
public class NullableNumberSpinnerModel extends SpinnerNumberModel implements SpinnerModel{ |
|
48 |
|
|
49 |
private DynField field; |
|
50 |
private Object number; |
|
51 |
private boolean isFirstTime; |
|
52 |
|
|
53 |
/** |
|
54 |
* @param field |
|
55 |
* @param value |
|
56 |
*/ |
|
57 |
public NullableNumberSpinnerModel(DynField field, Number value) { |
|
58 |
super(0, (Comparable<?>) field |
|
59 |
.getMinValue(), (Comparable<?>) field.getMaxValue(), Integer |
|
60 |
.valueOf(1)); |
|
61 |
this.field = field; |
|
62 |
this.isFirstTime = true; |
|
63 |
super.setValue(0); |
|
64 |
if (value!=null) |
|
65 |
setValue(value); |
|
66 |
} |
|
67 |
|
|
68 |
/* (non-Javadoc) |
|
69 |
* @see javax.swing.SpinnerModel#getNextValue() |
|
70 |
*/ |
|
71 |
@Override |
|
72 |
public Object getNextValue() { |
|
73 |
if (!isNull()) { |
|
74 |
this.number = super.getNextValue(); |
|
75 |
super.setValue(this.number); |
|
76 |
return this.number; |
|
77 |
} |
|
78 |
super.setValue(0); |
|
79 |
return 0; |
|
80 |
} |
|
81 |
|
|
82 |
protected boolean isNull(){ |
|
83 |
Object value = getValue(); |
|
84 |
return ((value== null)||(value.equals(""))); |
|
85 |
} |
|
86 |
|
|
87 |
/* (non-Javadoc) |
|
88 |
* @see javax.swing.SpinnerModel#getPreviousValue() |
|
89 |
*/ |
|
90 |
@Override |
|
91 |
public Object getPreviousValue() { |
|
92 |
if (isNull()) return null; |
|
93 |
if (this.number.equals(this.getMinimum())){ |
|
94 |
// we are at the minimum value and we want to change to "undetermined" |
|
95 |
this.number = null; |
|
96 |
return ""; |
|
97 |
} |
|
98 |
return super.getPreviousValue(); |
|
99 |
} |
|
100 |
|
|
101 |
/* (non-Javadoc) |
|
102 |
* @see javax.swing.SpinnerModel#getValue() |
|
103 |
*/ |
|
104 |
@Override |
|
105 |
public Object getValue() { |
|
106 |
if (this.isFirstTime){ |
|
107 |
return 0; |
|
108 |
} |
|
109 |
if (this.number == null) |
|
110 |
return null; |
|
111 |
return super.getValue(); |
|
112 |
|
|
113 |
} |
|
114 |
|
|
115 |
/* (non-Javadoc) |
|
116 |
* @see javax.swing.SpinnerModel#setValue(java.lang.Object) |
|
117 |
*/ |
|
118 |
@Override |
|
119 |
public void setValue(Object value) { |
|
120 |
this.number = value; |
|
121 |
this.isFirstTime = false; |
|
122 |
if (!isNull()) |
|
123 |
super.setValue(value); |
|
124 |
else |
|
125 |
this.fireStateChanged(); |
|
126 |
} |
|
127 |
} |
Also available in: Unified diff