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 @ 647
History | View | Annotate | Download (7.16 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 |
|
35 |
import javax.swing.DefaultListModel; |
36 |
import javax.swing.event.ListDataListener; |
37 |
|
38 |
import org.gvsig.tools.dynobject.DynField; |
39 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
40 |
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField; |
41 |
import org.gvsig.tools.swing.impl.dynobject.dynfield.model.DynFieldModelFactory; |
42 |
import org.gvsig.tools.swing.impl.dynobject.valuefield.DynFieldFormatter; |
43 |
import org.gvsig.tools.swing.impl.dynobject.valuefield.NValueField; |
44 |
|
45 |
/**
|
46 |
* @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
|
47 |
* @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n </a> -
|
48 |
* gvSIG Team
|
49 |
* @version $Id$
|
50 |
*
|
51 |
*/
|
52 |
public abstract class AbstractNDynObjectValueField extends ArrayList implements |
53 |
NValueField, List {
|
54 |
|
55 |
/**
|
56 |
*
|
57 |
*/
|
58 |
private static final long serialVersionUID = 2130582584413959824L; |
59 |
|
60 |
private DynField field;
|
61 |
private int selectedIndex; |
62 |
private DefaultListModel model; |
63 |
private ValueField parent;
|
64 |
private DynFieldFormatter formatter;
|
65 |
|
66 |
public AbstractNDynObjectValueField(ValueField parent, DynField dynField) {
|
67 |
this.parent = parent;
|
68 |
this.selectedIndex = -1; |
69 |
initDynField(dynField); |
70 |
} |
71 |
|
72 |
private void initDynField(DynField dynField) { |
73 |
|
74 |
this.field = dynField;
|
75 |
DynField field = dynField.getElementsType(); |
76 |
if (field != null) { |
77 |
this.field = field;
|
78 |
return;
|
79 |
} |
80 |
if (this.field == null) { |
81 |
this.field = this.parent.getDynField(); |
82 |
} |
83 |
} |
84 |
|
85 |
public DynField getParentDynField() {
|
86 |
return parent.getDynField();
|
87 |
} |
88 |
|
89 |
public DefaultListModel getModel() { |
90 |
if (model == null) { |
91 |
this.model = DynFieldModelFactory.getModel(getDynField());
|
92 |
} |
93 |
return this.model; |
94 |
} |
95 |
|
96 |
public void addElement(Object value) { |
97 |
addElement(getSize(), value); |
98 |
} |
99 |
|
100 |
public void addElement(Object value, Object printValue) { |
101 |
addElement(getSize(), value, printValue); |
102 |
} |
103 |
|
104 |
public String getValidationMessage() { |
105 |
try {
|
106 |
this.getDynField().validate(this.getValue()); |
107 |
return ""; |
108 |
} catch (DynFieldValidateException e) {
|
109 |
return e.getMessage(3); |
110 |
} |
111 |
} |
112 |
|
113 |
/*
|
114 |
* (non-Javadoc)
|
115 |
*
|
116 |
* @see
|
117 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(
|
118 |
* java.lang.Object)
|
119 |
*/
|
120 |
public void addElement(int index, Object element) { |
121 |
addElement(index, element, element); |
122 |
} |
123 |
|
124 |
/**
|
125 |
* @param size
|
126 |
* @param value
|
127 |
* @param printValue
|
128 |
*/
|
129 |
private void addElement(int index, Object element, Object printValue) { |
130 |
String printText = null; |
131 |
if (printValue != null) { |
132 |
printText = getFormatter().format(printValue); |
133 |
} else {
|
134 |
printText = getFormatter().format(element); |
135 |
} |
136 |
if ((printText != null) && (!printText.equals(""))) { |
137 |
this.add(index, element);
|
138 |
getModel().add(index, printText); |
139 |
} |
140 |
} |
141 |
|
142 |
/*
|
143 |
* (non-Javadoc)
|
144 |
*
|
145 |
* @seejavax.swing.ListModel#addListDataListener(javax.swing.event.
|
146 |
* ListDataListener)
|
147 |
*/
|
148 |
public void addListDataListener(ListDataListener l) { |
149 |
getModel().addListDataListener(l); |
150 |
} |
151 |
|
152 |
/**
|
153 |
* @return
|
154 |
*/
|
155 |
public Object firstElement() { |
156 |
return getModel().firstElement();
|
157 |
} |
158 |
|
159 |
public Object getDefaultFieldValue() { |
160 |
return getDynField().getDefaultValue();
|
161 |
} |
162 |
|
163 |
public DynField getDynField() {
|
164 |
return this.field; |
165 |
} |
166 |
|
167 |
/*
|
168 |
* (non-Javadoc)
|
169 |
*
|
170 |
* @see javax.swing.ListModel#getElementAt(int)
|
171 |
*/
|
172 |
public Object getElementAt(int index) { |
173 |
return this.get(index); |
174 |
} |
175 |
|
176 |
public Object getFieldValue() { |
177 |
return this.getValue(); |
178 |
} |
179 |
|
180 |
private DynFieldFormatter getFormatter() {
|
181 |
if (this.formatter == null) { |
182 |
formatter = DynFieldFormatter.getInstance(this.getDynField());
|
183 |
} |
184 |
return this.formatter; |
185 |
} |
186 |
|
187 |
/*
|
188 |
* (non-Javadoc)
|
189 |
*
|
190 |
* @see javax.swing.ListModel#getSize()
|
191 |
*/
|
192 |
public int getSize() { |
193 |
return getModel().getSize();
|
194 |
} |
195 |
|
196 |
/*
|
197 |
* (non-Javadoc)
|
198 |
*
|
199 |
* @see
|
200 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getValue()
|
201 |
*/
|
202 |
public Object getValue() { |
203 |
if (this.isEmpty()) { |
204 |
return null; |
205 |
} |
206 |
if ((selectedIndex < 0) || (selectedIndex >= this.size())) { |
207 |
return null; |
208 |
} |
209 |
return this.get(selectedIndex); |
210 |
} |
211 |
|
212 |
public void removeElement(int index) { |
213 |
this.remove(index);
|
214 |
getModel().remove(index); |
215 |
} |
216 |
|
217 |
public void setFieldValue(Object value) { |
218 |
parent.setFieldValue(value); |
219 |
} |
220 |
|
221 |
public void setSelectedIndex(int index) { |
222 |
selectedIndex = index; |
223 |
} |
224 |
|
225 |
public void setValue(int index, Object element, Object printValue) { |
226 |
|
227 |
String printText = null; |
228 |
if (printValue != null) { |
229 |
printText = printValue.toString(); |
230 |
} else {
|
231 |
printText = getFormatter().format(element); |
232 |
} |
233 |
|
234 |
if (printText != null) { |
235 |
this.set(index, printText);
|
236 |
getModel().set(index, printText); |
237 |
} |
238 |
} |
239 |
|
240 |
/*
|
241 |
* (non-Javadoc)
|
242 |
*
|
243 |
* @see
|
244 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(
|
245 |
* java.lang.Object)
|
246 |
*/
|
247 |
public void setValue(Object value) { |
248 |
if (value == null) { |
249 |
return;
|
250 |
} |
251 |
if (value instanceof List) { |
252 |
for (Object elem : (List) value) { |
253 |
addElement(elem); |
254 |
} |
255 |
} else {
|
256 |
addElement(value); |
257 |
} |
258 |
} |
259 |
|
260 |
public void removeListDataListener(ListDataListener l) { |
261 |
|
262 |
} |
263 |
} |