gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.spi / src / main / java / org / gvsig / tools / dynform / spi / dynformfield / AbstractJDynFormFieldWithValueList.java @ 1998
History | View | Annotate | Download (12.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.dynform.spi.dynformfield; |
25 |
|
26 |
import java.awt.event.FocusEvent; |
27 |
import java.awt.event.FocusListener; |
28 |
import java.awt.event.KeyAdapter; |
29 |
import java.awt.event.KeyEvent; |
30 |
import java.util.Objects; |
31 |
import javax.swing.BorderFactory; |
32 |
import javax.swing.ComboBoxModel; |
33 |
import javax.swing.DefaultComboBoxModel; |
34 |
|
35 |
import javax.swing.JComboBox; |
36 |
import javax.swing.JComponent; |
37 |
import javax.swing.JLabel; |
38 |
import javax.swing.JSpinner; |
39 |
import javax.swing.JTextField; |
40 |
import javax.swing.UIManager; |
41 |
import javax.swing.text.JTextComponent; |
42 |
import org.apache.commons.lang3.StringUtils; |
43 |
|
44 |
import org.gvsig.tools.dataTypes.CoercionException; |
45 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
46 |
import org.gvsig.tools.dynform.JDynFormField; |
47 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
48 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager.ComponentsFactory; |
49 |
import static org.gvsig.tools.dynform.spi.DynFormSPIManager.TAG_DYNFORM_DROPDOWN; |
50 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField.IllegalFieldValue; |
51 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
52 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
53 |
import org.gvsig.tools.swing.api.DropDown; |
54 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
55 |
|
56 |
public abstract class AbstractJDynFormFieldWithValueList extends AbstractJDynFormField implements JDynFormField, FocusListener { |
57 |
|
58 |
protected Object assignedValue = null; |
59 |
protected DropDown dropDown = null; |
60 |
|
61 |
public AbstractJDynFormFieldWithValueList(
|
62 |
DynFormSPIManager serviceManager, |
63 |
ComponentsFactory componentsFactory, |
64 |
JDynFormFieldFactory factory, |
65 |
DynFormFieldDefinition definition, |
66 |
Object value
|
67 |
) { |
68 |
super(serviceManager, componentsFactory, factory, definition, value);
|
69 |
this.assignedValue = value;
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
public Object getAssignedValue() { |
74 |
return this.assignedValue; |
75 |
} |
76 |
|
77 |
protected JTextField getJTextField() { |
78 |
if (this.contents instanceof JTextField) { |
79 |
return (JTextField) this.contents; |
80 |
} |
81 |
return null; |
82 |
} |
83 |
|
84 |
protected JSpinner getJSpinnerField() { |
85 |
if (this.contents instanceof JSpinner) { |
86 |
return (JSpinner) this.contents; |
87 |
} |
88 |
return null; |
89 |
} |
90 |
|
91 |
protected DropDown getDropDown() {
|
92 |
return this.dropDown; |
93 |
} |
94 |
|
95 |
protected DynObjectValueItem[] getAvailableValues() { |
96 |
return this.getDefinition().getAvailableValues(); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void initComponent() { |
101 |
DynObjectValueItem[] availableValues = this.getAvailableValues(); |
102 |
ComponentsFactory theFactory = this.getComponentsFactory();
|
103 |
if (theFactory.containsJComboBox(this.getDefinition(), null)) { |
104 |
JComboBox combo = this.getComponentsFactory().getJComboBox(this.getDefinition(), null); |
105 |
this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(combo);
|
106 |
this.dropDown.setModel(new DefaultComboBoxModel(availableValues)); |
107 |
this.contents = combo;
|
108 |
} else if (theFactory.containsJLabel(this.getDefinition(), "Ddn")) { |
109 |
JLabel label = this.getComponentsFactory().getJLabel(this.getDefinition(), "Ddn"); |
110 |
this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(label);
|
111 |
this.dropDown.setModel(new DefaultComboBoxModel(availableValues)); |
112 |
this.dropDown.setVisibleDropdownArrow(true); |
113 |
this.contents = label;
|
114 |
} else if (theFactory.containsJSpinner(this.getDefinition(), null)) { |
115 |
this.contents = this.getComponentsFactory().getJSpinner(this.getDefinition(), null); |
116 |
this.dropDown = null; |
117 |
} else if (theFactory.containsJTextField(this.getDefinition(), null)) { |
118 |
this.contents = theFactory.getJTextField(this.getDefinition(), null); |
119 |
this.dropDown = null; |
120 |
} else {
|
121 |
if (availableValues == null) { |
122 |
this.dropDown = null; |
123 |
this.contents = this.getComponentsFactory().getJTextField(this.getDefinition(), null); |
124 |
} else {
|
125 |
String dropdownType = this.getTagValueAsString(TAG_DYNFORM_DROPDOWN, |
126 |
availableValues.length <= 20 ? "label" : "combo" |
127 |
).toLowerCase().trim(); |
128 |
switch (dropdownType) {
|
129 |
case "label": { |
130 |
JLabel label = this.getComponentsFactory().getJLabel(this.getDefinition(), "Ddn"); |
131 |
this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(label);
|
132 |
this.dropDown.setModel(new DefaultComboBoxModel(availableValues)); |
133 |
this.dropDown.setVisibleDropdownArrow(true); |
134 |
label.setBorder(BorderFactory.createLineBorder(UIManager.getColor("TextField.darkShadow"))); |
135 |
label.setForeground(UIManager.getColor("TextField.foreground")); |
136 |
label.setBackground(UIManager.getColor("TextField.background")); |
137 |
label.setOpaque(true);
|
138 |
this.contents = label;
|
139 |
break;
|
140 |
} |
141 |
default:
|
142 |
case "combo": { |
143 |
JComboBox combo = this.getComponentsFactory().getJComboBox(this.getDefinition(), null); |
144 |
this.dropDown = ToolsSwingLocator.getToolsSwingManager().createDropDown(combo);
|
145 |
this.dropDown.setModel(new DefaultComboBoxModel(availableValues)); |
146 |
this.contents = combo;
|
147 |
break;
|
148 |
} |
149 |
} |
150 |
} |
151 |
} |
152 |
this.contents.addFocusListener(this); |
153 |
this.setReadOnly(readOnly); // Forzamos a actualiza rel estado del component |
154 |
//component dont have support to set null values
|
155 |
if (theFactory.containsJSpinner(this.getDefinition(), null) && this.assignedValue==null) { |
156 |
this.setValue(0); |
157 |
} else {
|
158 |
this.setValue(this.assignedValue); |
159 |
} |
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void setReadOnly(boolean readonly) { |
164 |
if (!readonly && this.isForcedReadOnly()) { |
165 |
readonly = true;
|
166 |
} |
167 |
if (this.dropDown == null) { |
168 |
super.setReadOnly(readonly);
|
169 |
} else {
|
170 |
this.readOnly = readonly;
|
171 |
if (jlabel != null) { |
172 |
this.jlabel.setEnabled(!readonly);
|
173 |
} |
174 |
this.dropDown.setReadOnly(readonly);
|
175 |
} |
176 |
} |
177 |
|
178 |
protected String getValueFromJComponent() { |
179 |
String s;
|
180 |
if (this.dropDown == null) { |
181 |
JTextField jtext = this.getJTextField(); |
182 |
if (jtext == null) { |
183 |
s = null;
|
184 |
} else {
|
185 |
s = jtext.getText(); |
186 |
} |
187 |
} else {
|
188 |
DynObjectValueItem value = (DynObjectValueItem) this.dropDown.getSelectedItem();
|
189 |
if (value == null) { |
190 |
s = null;
|
191 |
} else {
|
192 |
s = Objects.toString(value.getValue(), null);
|
193 |
} |
194 |
} |
195 |
if (StringUtils.isBlank(s) && this.translateEmptyToNull()) { |
196 |
return null; |
197 |
} |
198 |
return s;
|
199 |
} |
200 |
|
201 |
@Override
|
202 |
public void setValue(Object value) { |
203 |
if (this.dropDown == null) { |
204 |
JTextField jtext = this.getJTextField(); |
205 |
if (jtext != null) { |
206 |
String s;
|
207 |
if (value == null) { |
208 |
this.setTranslateEmptyToNull(true); |
209 |
value = this.getDefinition().getDefaultValue();
|
210 |
if (value == null) { |
211 |
s = this.getDefaultValue();
|
212 |
} else {
|
213 |
s = value.toString(); |
214 |
} |
215 |
} else {
|
216 |
this.setTranslateEmptyToNull(false); |
217 |
s = value.toString(); |
218 |
try {
|
219 |
this.getDefinition().validate(value);
|
220 |
this.problemIndicator().clear();
|
221 |
} catch (DynFieldValidateException e) {
|
222 |
this.problemIndicator().set(e.getLocalizedMessage());
|
223 |
} |
224 |
} |
225 |
jtext.setText(s); |
226 |
} else if (this.contents instanceof JSpinner) { |
227 |
JSpinner jspinner = this.getJSpinnerField(); |
228 |
if (value == null) { |
229 |
this.setTranslateEmptyToNull(true); |
230 |
value = this.getDefinition().getDefaultValue();
|
231 |
if (value != null) { |
232 |
jspinner.setValue(value); |
233 |
} else {
|
234 |
jspinner.setValue(121);
|
235 |
} |
236 |
} else {
|
237 |
this.setTranslateEmptyToNull(false); |
238 |
} |
239 |
jspinner.setValue(value); |
240 |
} |
241 |
} else {
|
242 |
if (value == null) { |
243 |
this.dropDown.setSelectedIndex(-1); |
244 |
} else {
|
245 |
ComboBoxModel model = this.dropDown.getModel(); |
246 |
if (model != null) { |
247 |
for (int i = 0; i < model.getSize(); i++) { |
248 |
DynObjectValueItem item = (DynObjectValueItem) model.getElementAt(i); |
249 |
if (item != null && Objects.equals(item.getValue(), value)) { |
250 |
this.dropDown.setSelectedIndex(i);
|
251 |
break;
|
252 |
} |
253 |
} |
254 |
} |
255 |
} |
256 |
} |
257 |
this.assignedValue = value;
|
258 |
} |
259 |
|
260 |
/*
|
261 |
* M?todos espec?ficos de cada tipo de datos
|
262 |
*/
|
263 |
protected String getDefaultValue() { |
264 |
return ""; |
265 |
} |
266 |
|
267 |
@Override
|
268 |
public Object getValue() { |
269 |
Object value = null; |
270 |
String s = this.getValueFromJComponent(); |
271 |
try {
|
272 |
value = this.getDefinition().coerce(s);
|
273 |
} catch (CoercionException e) {
|
274 |
throw new IllegalFieldValue(this, "Can't convert value '" + s + "' to '" + this.getDefinition().getDataType().getName() + "'."); |
275 |
} |
276 |
this.problemIndicator().clear();
|
277 |
return value;
|
278 |
} |
279 |
|
280 |
@SuppressWarnings("unused") |
281 |
@Override
|
282 |
public boolean hasValidValue() { |
283 |
try {
|
284 |
Object value = this.getValue(); |
285 |
} catch (Exception e) { |
286 |
return false; |
287 |
} |
288 |
return true; |
289 |
} |
290 |
|
291 |
@Override
|
292 |
public void focusGained(FocusEvent arg0) { |
293 |
fireFieldEnterEvent(); |
294 |
this.problemIndicator().restore();
|
295 |
} |
296 |
|
297 |
@Override
|
298 |
public void focusLost(FocusEvent arg0) { |
299 |
if (this.hasValidValue()) { |
300 |
this.problemIndicator().clear();
|
301 |
} else {
|
302 |
try {
|
303 |
Object value = this.getValue(); |
304 |
} catch (Exception e) { |
305 |
this.problemIndicator().set(e.getLocalizedMessage());
|
306 |
} |
307 |
} |
308 |
fireFieldExitEvent(); |
309 |
} |
310 |
|
311 |
/**
|
312 |
* Este m?todo es por si se quiere a?adir una expresi?n regular al
|
313 |
* JTextField Por defecto, se puede escribir cualquier cosa, pero se puede
|
314 |
* sobreescribir este m?todo en la clase que lo requiera para a?adir
|
315 |
* restricciones (p.ej: en los num?ricos)
|
316 |
*
|
317 |
* @return expresi?n regular que filtra el contenido del JTextField
|
318 |
*/
|
319 |
public String getJTextFieldRegex() { |
320 |
return "[*]"; |
321 |
} |
322 |
|
323 |
public class KeyAdapterRegEx extends KeyAdapter { |
324 |
|
325 |
/**
|
326 |
* Key released on field.
|
327 |
*
|
328 |
* @param e
|
329 |
*/
|
330 |
@Override
|
331 |
public void keyReleased(KeyEvent e) { |
332 |
String curText = ((JTextComponent) e.getSource()).getText(); |
333 |
curText = curText.replaceAll(getJTextFieldRegex(), "");
|
334 |
|
335 |
((JTextComponent) e.getSource()).setText(curText);
|
336 |
} |
337 |
} |
338 |
} |