Revision 270 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

View differences:

NullableNumberSpinnerModel.java
1 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
*/
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 22
/*
23 23
 * AUTHORS (In addition to CIT):
24 24
 * 2010 Institute of New Imaging Technologies (INIT): 
......
42 42

  
43 43
/**
44 44
 * @author <a href="mailto:reinhold@uji.es">cmartin</a>
45
 *
45
 * 
46 46
 */
47
public class NullableNumberSpinnerModel extends SpinnerNumberModel implements SpinnerModel{
48
    
47
public class NullableNumberSpinnerModel extends SpinnerNumberModel implements
48
    SpinnerModel {
49

  
49 50
    private DynField field;
50 51
    private Object number;
51 52
    private boolean isFirstTime;
52
 
53

  
53 54
    /**
54 55
     * @param field
55 56
     * @param value
56 57
     */
57 58
    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);
59
        super(0, (Comparable<?>) field.getMinValue(), (Comparable<?>) field
60
            .getMaxValue(), Integer.valueOf(1));
61
        this.field = field;
62
        this.isFirstTime = true;
63
        super.setValue(0);
64
        if (value != null)
65
            setValue(value);
66 66
    }
67 67

  
68
    /* (non-Javadoc)
68
    /*
69
     * (non-Javadoc)
70
     * 
69 71
     * @see javax.swing.SpinnerModel#getNextValue()
70 72
     */
71 73
    @Override
72 74
    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;
75
        if (!isNull()) {
76
            this.number = super.getNextValue();
77
            super.setValue(this.number);
78
            return this.number;
79
        }
80
        super.setValue(0);
81
        return 0;
80 82
    }
81
    
82
    protected boolean isNull(){
83
	Object value = getValue();
84
	return ((value== null)||(value.equals("")));
83

  
84
    protected boolean isNull() {
85
        Object value = getValue();
86
        return ((value == null) || (value.equals("")));
85 87
    }
86 88

  
87
    /* (non-Javadoc)
89
    /*
90
     * (non-Javadoc)
91
     * 
88 92
     * @see javax.swing.SpinnerModel#getPreviousValue()
89 93
     */
90 94
    @Override
91 95
    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();
96
        if (isNull())
97
            return null;
98
        if (this.number.equals(this.getMinimum())) {
99
            // we are at the minimum value and we want to change to
100
            // "undetermined"
101
            this.number = null;
102
            return "";
103
        }
104
        return super.getPreviousValue();
99 105
    }
100 106

  
101
    /* (non-Javadoc)
107
    /*
108
     * (non-Javadoc)
109
     * 
102 110
     * @see javax.swing.SpinnerModel#getValue()
103 111
     */
104 112
    @Override
105 113
    public Object getValue() {
106
	 if (this.isFirstTime){
107
	     return 0;
108
	 }
109
	 if (this.number == null) 
110
	     return null;
111
	 return super.getValue();
112
	 
114
        if (this.isFirstTime) {
115
            return 0;
116
        }
117
        if (this.number == null)
118
            return null;
119
        return super.getValue();
120

  
113 121
    }
114 122

  
115
    /* (non-Javadoc)
123
    /*
124
     * (non-Javadoc)
125
     * 
116 126
     * @see javax.swing.SpinnerModel#setValue(java.lang.Object)
117 127
     */
118 128
    @Override
119 129
    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();
130
        this.number = value;
131
        this.isFirstTime = false;
132
        if (!isNull())
133
            super.setValue(value);
134
        else
135
            this.fireStateChanged();
126 136
    }
127 137
}

Also available in: Unified diff