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