Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / dynobject / dynfield / AbstractNDynObjectValueField.java @ 298

History | View | Annotate | Download (6.91 KB)

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
package org.gvsig.tools.swing.impl.dynobject.dynfield;
31

    
32
import java.util.ArrayList;
33
import java.util.List;
34
import java.util.Locale;
35

    
36
import javax.swing.DefaultListModel;
37
import javax.swing.event.ListDataListener;
38

    
39
import org.gvsig.tools.dynobject.DynField;
40
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
41
import org.gvsig.tools.swing.impl.dynobject.valuefield.DynFieldFormatter;
42
import org.gvsig.tools.swing.impl.dynobject.valuefield.NValueField;
43

    
44
/**
45
 * @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
46
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n&nbsp;</a> -
47
 *         gvSIG Team
48
 * @version $Id$
49
 * 
50
 */
51
public abstract class AbstractNDynObjectValueField extends ArrayList implements
52
    NValueField, List {
53

    
54
    /**
55
     * 
56
     */
57
    private static final long serialVersionUID = 2130582584413959824L;
58
    private final DynField field;
59
    private int selectedIndex;
60
    private DefaultListModel model;
61
    private ValueField parent;
62
    private DynFieldFormatter formatter;
63
    private Locale locale;
64

    
65
    /**
66
     * @param parent
67
     * @param dynField
68
     */
69
    public AbstractNDynObjectValueField(ValueField parent, DynField dynField,
70
        Locale locale) {
71
        this.parent = parent;
72
        this.field = dynField;
73
        this.locale = locale;
74
        this.selectedIndex = -1;
75
        this.model = new DefaultListModel();
76
    }
77

    
78
    /*
79
     * (non-Javadoc)
80
     * 
81
     * @see
82
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(
83
     * java.lang.Object)
84
     */
85
    public void addElement(int index, Object element) {
86
        addElement(index, element, element);
87
    }
88

    
89
    /**
90
     * @param size
91
     * @param value
92
     * @param printValue
93
     */
94
    private void addElement(int index, Object element, Object printValue) {
95
        if ((element == null) || (element.equals(""))) {
96
            return;
97
        }
98

    
99
        if (printValue == null) {
100
            printValue = element;
101
        }
102
        int ind = this.model.size();
103

    
104
        this.add(ind, element);
105

    
106
        if (getFormatter().isDate() || getFormatter().isNumber()) {
107
            String printText = getFormatter().format(element);
108
            this.model.add(ind, printText);
109
        } else {
110
            this.model.add(ind, printValue);
111
        }
112
    }
113

    
114
    public void addElement(Object value) {
115
        addElement(this.size(), value);
116
    }
117

    
118
    public void addElement(Object value, Object printValue) {
119
        addElement(this.size(), value, printValue);
120
    }
121

    
122
    /*
123
     * (non-Javadoc)
124
     * 
125
     * @seejavax.swing.ListModel#addListDataListener(javax.swing.event.
126
     * ListDataListener)
127
     */
128
    public void addListDataListener(ListDataListener l) {
129
        this.model.addListDataListener(l);
130
    }
131

    
132
    /**
133
     * @return
134
     */
135
    public Object firstElement() {
136
        return this.model.firstElement();
137
    }
138

    
139
    public Object getDefaultFieldValue() {
140
        return getDynField().getDefaultValue();
141
    }
142

    
143
    public DynField getDynField() {
144
        return this.field.getElementsType();
145
    }
146

    
147
    /*
148
     * (non-Javadoc)
149
     * 
150
     * @see javax.swing.ListModel#getElementAt(int)
151
     */
152
    public Object getElementAt(int index) {
153
        return this.get(index);
154
    }
155

    
156
    public Object getFieldValue() {
157
        return this.getValue();
158
    }
159

    
160
    private DynFieldFormatter getFormatter() {
161
        if (this.formatter == null) {
162
            formatter = DynFieldFormatter.getInstance(this.field, getLocale());
163
        }
164
        return this.formatter;
165
    }
166

    
167
    protected Locale getLocale() {
168
        return locale;
169
    }
170

    
171
    /*
172
     * (non-Javadoc)
173
     * 
174
     * @see javax.swing.ListModel#getSize()
175
     */
176
    public int getSize() {
177
        return this.model.getSize();
178
    }
179

    
180
    /*
181
     * (non-Javadoc)
182
     * 
183
     * @see
184
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getValue()
185
     */
186
    public Object getValue() {
187
        if (this.isEmpty()) {
188
            return null;
189
        }
190
        if ((selectedIndex < 0) || (selectedIndex >= this.size())) {
191
            return null;
192
        }
193
        return this.get(selectedIndex);
194
    }
195

    
196
    public void removeElement(int index) {
197
        this.remove(index);
198
        this.model.remove(index);
199
    }
200

    
201
    /*
202
     * (non-Javadoc)
203
     * 
204
     * @seejavax.swing.ListModel#removeListDataListener(javax.swing.event.
205
     * ListDataListener)
206
     */
207
    public void removeListDataListener(ListDataListener l) {
208
        this.model.removeListDataListener(l);
209
    }
210

    
211
    public void setFieldValue(Object value) {
212
        parent.setFieldValue(value);
213
    }
214

    
215
    public void setSelectedIndex(int index) {
216
        selectedIndex = index;
217
    }
218

    
219
    public void setValue(int index, Object element, Object printValue) {
220
        if ((element == null) || (element.equals(""))) {
221
            return;
222
        }
223

    
224
        if (printValue == null) {
225
            printValue = element;
226
        }
227

    
228
        this.set(index, element);
229

    
230
        if (getFormatter().isDate() || getFormatter().isNumber()) {
231
            Object printText = getFormatter().format(element);
232
            this.model.set(index, printText);
233
        } else {
234
            this.model.set(index, printValue);
235
        }
236
    }
237

    
238
    /*
239
     * (non-Javadoc)
240
     * 
241
     * @see
242
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(
243
     * java.lang.Object)
244
     */
245
    public void setValue(Object value) {
246
        if (value == null) {
247
            return;
248
        }
249
        if (value instanceof List) {
250
            for (Object elem : (List) value) {
251
                addElement(elem);
252
            }
253
        } else {
254
            addElement(value);
255
        }
256
    }
257
}