Revision 298 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
AbstractNDynObjectValueField.java | ||
---|---|---|
34 | 34 |
import java.util.Locale; |
35 | 35 |
|
36 | 36 |
import javax.swing.DefaultListModel; |
37 |
import javax.swing.JFormattedTextField; |
|
38 | 37 |
import javax.swing.event.ListDataListener; |
39 | 38 |
|
40 | 39 |
import org.gvsig.tools.dynobject.DynField; |
41 | 40 |
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField; |
42 | 41 |
import org.gvsig.tools.swing.impl.dynobject.valuefield.DynFieldFormatter; |
43 |
import org.gvsig.tools.swing.impl.dynobject.valuefield.DynFieldFormatterFactory; |
|
44 | 42 |
import org.gvsig.tools.swing.impl.dynobject.valuefield.NValueField; |
45 | 43 |
|
46 | 44 |
/** |
... | ... | |
53 | 51 |
public abstract class AbstractNDynObjectValueField extends ArrayList implements |
54 | 52 |
NValueField, List { |
55 | 53 |
|
54 |
/** |
|
55 |
* |
|
56 |
*/ |
|
57 |
private static final long serialVersionUID = 2130582584413959824L; |
|
56 | 58 |
private final DynField field; |
57 | 59 |
private int selectedIndex; |
58 | 60 |
private DefaultListModel model; |
59 | 61 |
private ValueField parent; |
60 |
private DynFieldFormatter formatter;
|
|
62 |
private DynFieldFormatter formatter;
|
|
61 | 63 |
private Locale locale; |
62 | 64 |
|
63 | 65 |
/** |
64 | 66 |
* @param parent |
65 | 67 |
* @param dynField |
66 | 68 |
*/ |
67 |
public AbstractNDynObjectValueField(ValueField parent, DynField dynField, Locale locale) { |
|
69 |
public AbstractNDynObjectValueField(ValueField parent, DynField dynField, |
|
70 |
Locale locale) { |
|
68 | 71 |
this.parent = parent; |
69 | 72 |
this.field = dynField; |
70 | 73 |
this.locale = locale; |
... | ... | |
72 | 75 |
this.model = new DefaultListModel(); |
73 | 76 |
} |
74 | 77 |
|
75 |
public Object getDefaultFieldValue() { |
|
76 |
return getDynField().getDefaultValue(); |
|
77 |
} |
|
78 |
|
|
79 |
public Object getFieldValue() { |
|
80 |
return this.getValue(); |
|
81 |
} |
|
82 |
|
|
83 |
public void setFieldValue(Object value) { |
|
84 |
parent.setFieldValue(value); |
|
85 |
} |
|
86 |
|
|
87 | 78 |
/* |
88 | 79 |
* (non-Javadoc) |
89 | 80 |
* |
90 | 81 |
* @see |
91 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#getValue() |
|
92 |
*/ |
|
93 |
public Object getValue() { |
|
94 |
if (this.isEmpty()) |
|
95 |
return null; |
|
96 |
if ((selectedIndex < 0) || (selectedIndex >= this.size())) |
|
97 |
return null; |
|
98 |
return this.get(selectedIndex); |
|
99 |
} |
|
100 |
|
|
101 |
public void setSelectedIndex(int index) { |
|
102 |
selectedIndex = index; |
|
103 |
} |
|
104 |
|
|
105 |
/* |
|
106 |
* (non-Javadoc) |
|
107 |
* |
|
108 |
* @see |
|
109 | 82 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue( |
110 | 83 |
* java.lang.Object) |
111 | 84 |
*/ |
... | ... | |
113 | 86 |
addElement(index, element, element); |
114 | 87 |
} |
115 | 88 |
|
116 |
public void addElement(Object value) { |
|
117 |
addElement(this.size(), value); |
|
118 |
} |
|
119 |
|
|
120 |
public void addElement(Object value, Object printValue) { |
|
121 |
addElement(this.size(), value, printValue); |
|
122 |
} |
|
123 |
|
|
124 | 89 |
/** |
125 | 90 |
* @param size |
126 | 91 |
* @param value |
127 | 92 |
* @param printValue |
128 | 93 |
*/ |
129 | 94 |
private void addElement(int index, Object element, Object printValue) { |
130 |
if ((element == null)||(element.equals(""))){ |
|
131 |
return; |
|
132 |
} |
|
133 |
|
|
134 |
if (printValue == null) |
|
135 |
printValue = element; |
|
136 |
int ind = this.model.size(); |
|
137 |
|
|
138 |
this.add(ind, element); |
|
139 |
|
|
140 |
if (getFormatter().isDate()||getFormatter().isNumber()){ |
|
141 |
String printText = getFormatter().format(element); |
|
142 |
this.model.add(ind, printText); |
|
143 |
}else |
|
144 |
this.model.add(ind, printValue); |
|
145 |
} |
|
146 |
|
|
147 |
private DynFieldFormatter getFormatter(){ |
|
148 |
if (this.formatter == null) |
|
149 |
formatter = DynFieldFormatter.getInstance(this.field, getLocale()); |
|
150 |
return this.formatter; |
|
151 |
} |
|
95 |
if ((element == null) || (element.equals(""))) { |
|
96 |
return; |
|
97 |
} |
|
152 | 98 |
|
153 |
protected Locale getLocale(){ |
|
154 |
return locale; |
|
155 |
} |
|
99 |
if (printValue == null) { |
|
100 |
printValue = element; |
|
101 |
} |
|
102 |
int ind = this.model.size(); |
|
156 | 103 |
|
157 |
public void setValue(int index, Object element, Object printValue) { |
|
158 |
if ((element == null)||(element.equals(""))){ |
|
159 |
return; |
|
160 |
} |
|
161 |
|
|
162 |
if (printValue == null) |
|
163 |
printValue = element; |
|
164 |
|
|
165 |
this.set(index, element); |
|
166 |
|
|
167 |
if (getFormatter().isDate()||getFormatter().isNumber()){ |
|
168 |
Object printText = getFormatter().format(element); |
|
169 |
this.model.set(index, printText); |
|
170 |
}else |
|
171 |
this.model.set(index, printValue); |
|
172 |
} |
|
104 |
this.add(ind, element); |
|
173 | 105 |
|
174 |
/* |
|
175 |
* (non-Javadoc) |
|
176 |
* |
|
177 |
* @see |
|
178 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue( |
|
179 |
* java.lang.Object) |
|
180 |
*/ |
|
181 |
public void setValue(Object value) { |
|
182 |
if (value == null) |
|
183 |
return; |
|
184 |
if (value instanceof List) { |
|
185 |
for (Object elem : (List) value) |
|
186 |
addElement(elem); |
|
106 |
if (getFormatter().isDate() || getFormatter().isNumber()) { |
|
107 |
String printText = getFormatter().format(element); |
|
108 |
this.model.add(ind, printText); |
|
187 | 109 |
} else { |
188 |
addElement(value);
|
|
110 |
this.model.add(ind, printValue);
|
|
189 | 111 |
} |
190 | 112 |
} |
191 | 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 |
|
|
192 | 122 |
/* |
193 | 123 |
* (non-Javadoc) |
194 | 124 |
* |
... | ... | |
199 | 129 |
this.model.addListDataListener(l); |
200 | 130 |
} |
201 | 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 |
|
|
202 | 147 |
/* |
203 | 148 |
* (non-Javadoc) |
204 | 149 |
* |
... | ... | |
208 | 153 |
return this.get(index); |
209 | 154 |
} |
210 | 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 |
|
|
211 | 171 |
/* |
212 | 172 |
* (non-Javadoc) |
213 | 173 |
* |
... | ... | |
220 | 180 |
/* |
221 | 181 |
* (non-Javadoc) |
222 | 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 |
* |
|
223 | 204 |
* @seejavax.swing.ListModel#removeListDataListener(javax.swing.event. |
224 | 205 |
* ListDataListener) |
225 | 206 |
*/ |
... | ... | |
227 | 208 |
this.model.removeListDataListener(l); |
228 | 209 |
} |
229 | 210 |
|
230 |
/** |
|
231 |
* @return |
|
232 |
*/ |
|
233 |
public Object firstElement() { |
|
234 |
return this.model.firstElement(); |
|
211 |
public void setFieldValue(Object value) { |
|
212 |
parent.setFieldValue(value); |
|
235 | 213 |
} |
236 | 214 |
|
237 |
public void removeElement(int index) { |
|
238 |
this.remove(index); |
|
239 |
this.model.remove(index); |
|
215 |
public void setSelectedIndex(int index) { |
|
216 |
selectedIndex = index; |
|
240 | 217 |
} |
241 | 218 |
|
242 |
public DynField getDynField() { |
|
243 |
return this.field.getElementsType(); |
|
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 |
} |
|
244 | 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 |
} |
|
245 | 257 |
} |
Also available in: Unified diff