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 / AbstractJDynFormField.java @ 1225
History | View | Annotate | Download (13.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.BorderLayout; |
27 |
import java.awt.Color; |
28 |
import java.util.ArrayList; |
29 |
import java.util.HashSet; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
import java.util.Set; |
33 |
|
34 |
import javax.swing.Action; |
35 |
import javax.swing.JComboBox; |
36 |
import javax.swing.JComponent; |
37 |
import javax.swing.JLabel; |
38 |
import javax.swing.JList; |
39 |
import javax.swing.JPanel; |
40 |
import javax.swing.JScrollPane; |
41 |
import javax.swing.JSpinner; |
42 |
import javax.swing.JTextField; |
43 |
import javax.swing.JViewport; |
44 |
import javax.swing.text.JTextComponent; |
45 |
import org.gvsig.tools.dataTypes.CoercionException; |
46 |
import org.gvsig.tools.dataTypes.DataTypes; |
47 |
|
48 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
49 |
import org.gvsig.tools.dynform.JDynForm; |
50 |
import org.gvsig.tools.dynform.JDynFormField; |
51 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
52 |
import org.gvsig.tools.dynobject.DynField_v2; |
53 |
import org.gvsig.tools.dynobject.DynObject; |
54 |
import org.gvsig.tools.service.Manager; |
55 |
import org.gvsig.tools.service.spi.ServiceManager; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
@SuppressWarnings({"rawtypes", "unchecked"}) |
60 |
public abstract class AbstractJDynFormField implements JDynFormField{ |
61 |
|
62 |
protected static final Logger logger = LoggerFactory |
63 |
.getLogger(AbstractJDynFormField.class); |
64 |
|
65 |
private DynFormSPIManager manager = null; |
66 |
private DynFormFieldDefinition definition = null; |
67 |
private JLabel jlabel = null; |
68 |
private JPanel jlabelpanel = null; |
69 |
private Set listeners = null; |
70 |
private JProblemIndicator problemIndicator = null; |
71 |
|
72 |
protected DynObject parameters = null; |
73 |
protected JComponent contents = null; |
74 |
|
75 |
private boolean readOnly = false; |
76 |
private List customActions; |
77 |
protected boolean emptyToNull = false; |
78 |
private JDynForm form;
|
79 |
|
80 |
|
81 |
public AbstractJDynFormField(DynObject parameters,
|
82 |
ServiceManager serviceManager) { |
83 |
this.parameters = parameters;
|
84 |
this.manager = (DynFormSPIManager) serviceManager;
|
85 |
this.definition = this.getDefinition(); |
86 |
this.listeners = new HashSet(); |
87 |
this.readOnly = this.definition.isReadOnly(); |
88 |
this.problemIndicator = this.manager.createProblemIndicator(this); |
89 |
this.customActions = new ArrayList<Action>(); |
90 |
} |
91 |
|
92 |
public abstract void initComponent(); |
93 |
public abstract Object getAssignedValue(); |
94 |
|
95 |
public Object getParameterValue() { |
96 |
return this.parameters.getDynValue(DynFormSPIManager.FIELD_VALUE); |
97 |
} |
98 |
|
99 |
public JComponent asJComponent() { |
100 |
if (this.contents == null) { |
101 |
this.initComponent();
|
102 |
if(!this.customActions.isEmpty()){ |
103 |
addPopupComponents(); |
104 |
} |
105 |
if( this.readOnly ) { |
106 |
this.setReadOnly(readOnly);
|
107 |
} |
108 |
} |
109 |
return this.contents; |
110 |
} |
111 |
|
112 |
public String getName() { |
113 |
return this.definition.getName(); |
114 |
} |
115 |
|
116 |
public String getLabel() { |
117 |
if(definition.getLabel() != null) |
118 |
return definition.getLabel();
|
119 |
else
|
120 |
return this.getName(); |
121 |
} |
122 |
|
123 |
public JComponent getJLabel() { |
124 |
if (this.jlabel == null) { |
125 |
if( getTagValueAsBoolean("dynform.label.empty", false) ) { |
126 |
this.jlabel = new JLabel(""); |
127 |
} else {
|
128 |
this.jlabel = new JLabel(this.getLabel()); |
129 |
} |
130 |
this.jlabel.setLabelFor(this.contents); |
131 |
if( this.getDefinition().isMandatory() ) { |
132 |
this.jlabel.setForeground(Color.red.darker()); |
133 |
} |
134 |
this.jlabelpanel = new JPanel(); |
135 |
this.jlabelpanel.setLayout(new BorderLayout()); |
136 |
this.jlabelpanel.add(jlabel,BorderLayout.CENTER); |
137 |
this.jlabelpanel.add(problemIndicator.asJComponent(), BorderLayout.LINE_END); |
138 |
} |
139 |
return this.jlabelpanel; |
140 |
} |
141 |
|
142 |
public DynFormFieldDefinition getDefinition() {
|
143 |
return (DynFormFieldDefinition) this.parameters |
144 |
.getDynValue(DynFormSPIManager.FIELD_FIELDDEFINITION); |
145 |
} |
146 |
|
147 |
public Manager getManager() {
|
148 |
return this.manager; |
149 |
} |
150 |
|
151 |
public DynFormSPIManager getServiceManager(){
|
152 |
return this.manager; |
153 |
} |
154 |
|
155 |
public void addListener(JDynFormFieldListener listener) { |
156 |
this.listeners.add(listener);
|
157 |
} |
158 |
|
159 |
public void removeListener(JDynFormFieldListener listener) { |
160 |
this.listeners.remove(listener);
|
161 |
} |
162 |
|
163 |
protected void fireFieldChangedEvent() { |
164 |
Iterator it = this.listeners.iterator(); |
165 |
while (it.hasNext()) {
|
166 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
167 |
try {
|
168 |
listener.fieldChanged(this);
|
169 |
} catch (Exception ex) { |
170 |
logger.info("Error calling listener " + listener.toString()
|
171 |
+ "(" + listener.getClass().getName() + ") from " |
172 |
+ this.toString() + "(" + this.getClass().getName() |
173 |
+ ").", ex);
|
174 |
} |
175 |
} |
176 |
} |
177 |
|
178 |
protected void fireFieldEnterEvent() { |
179 |
Iterator it = this.listeners.iterator(); |
180 |
while (it.hasNext()) {
|
181 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
182 |
try {
|
183 |
listener.fieldEnter(this);
|
184 |
} catch (Exception ex) { |
185 |
logger.info("Error calling listener " + listener.toString()
|
186 |
+ "(" + listener.getClass().getName() + ") from " |
187 |
+ this.toString() + "(" + this.getClass().getName() |
188 |
+ ").", ex);
|
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
protected void fireFieldExitEvent() { |
194 |
Iterator it = this.listeners.iterator(); |
195 |
while (it.hasNext()) {
|
196 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
197 |
try {
|
198 |
listener.fieldExit(this);
|
199 |
} catch (Exception ex) { |
200 |
logger.info("Error calling listener " + listener.toString()
|
201 |
+ "(" + listener.getClass().getName() + ") from " |
202 |
+ this.toString() + "(" + this.getClass().getName() |
203 |
+ ").", ex);
|
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
public void fireMessageEvent(String message) { |
209 |
Iterator it = this.listeners.iterator(); |
210 |
while (it.hasNext()) {
|
211 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
212 |
try {
|
213 |
listener.message(this, message);
|
214 |
} catch (Exception ex) { |
215 |
logger.info("Error calling listener " + listener.toString()
|
216 |
+ "(" + listener.getClass().getName() + ") from " |
217 |
+ this.toString() + "(" + this.getClass().getName() |
218 |
+ ").", ex);
|
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
public boolean isReadOnly() { |
224 |
return this.readOnly ; |
225 |
} |
226 |
|
227 |
public void setReadOnly(boolean readonly) { |
228 |
// FIXME: Implememtacion por defecto, sobreescribirla en las subclases
|
229 |
// segun convenga para cada componente.
|
230 |
JTextComponent x = null; |
231 |
|
232 |
this.readOnly = readonly;
|
233 |
if(jlabel != null){ |
234 |
this.jlabel.setEnabled(!readonly);
|
235 |
} |
236 |
if( this.contents != null ) { |
237 |
if( this.contents instanceof JTextComponent ) { |
238 |
x = (JTextComponent) this.contents; |
239 |
x.setEditable(!readonly); |
240 |
} else if( this.contents instanceof JScrollPane ) { |
241 |
try {
|
242 |
JViewport port = ((JScrollPane)this.contents).getViewport(); |
243 |
x = (JTextComponent) port.getView();
|
244 |
x.setEditable(!readonly); |
245 |
} catch(Exception ex) { |
246 |
this.contents.setEnabled(!readOnly);
|
247 |
} |
248 |
} else {
|
249 |
this.contents.setEnabled(!readOnly);
|
250 |
} |
251 |
} |
252 |
} |
253 |
|
254 |
public JProblemIndicator problemIndicator() {
|
255 |
return this.problemIndicator; |
256 |
} |
257 |
|
258 |
public class IllegalFieldValue extends RuntimeException { |
259 |
|
260 |
/**
|
261 |
*
|
262 |
*/
|
263 |
private static final long serialVersionUID = -4409236610055983438L; |
264 |
|
265 |
public IllegalFieldValue(JDynFormField field, String message) { |
266 |
super("The value of field '"+field.getLabel()+"' is not valid. " + message); |
267 |
} |
268 |
} |
269 |
|
270 |
public String toString() { |
271 |
return super.toString() + "{" + this.getName() + "}"; |
272 |
} |
273 |
|
274 |
public boolean isModified() { |
275 |
boolean modified = false; |
276 |
try {
|
277 |
if( !this.isReadOnly() ) { |
278 |
Object value = this.getValue(); |
279 |
if( value == null ) { |
280 |
if( value != this.getAssignedValue() ) { |
281 |
modified = true;
|
282 |
} |
283 |
} else {
|
284 |
if( ! value.equals(this.getAssignedValue()) ) { |
285 |
modified = true;
|
286 |
} |
287 |
} |
288 |
} |
289 |
} catch(IllegalFieldValue ex) {
|
290 |
// Si es incorrecto el valor del campo decimos a capom que esta modificado.
|
291 |
modified = true;
|
292 |
} |
293 |
return modified;
|
294 |
} |
295 |
|
296 |
private void addPopupComponents(){ |
297 |
Iterator it = this.customActions.iterator(); |
298 |
while(it.hasNext()){
|
299 |
Object obj = it.next();
|
300 |
if(obj!=null){ |
301 |
Action act = (Action) obj; |
302 |
if(contents instanceof SupportPopupMenu) { |
303 |
DynFormFieldAction sact = new DynFormFieldAction(this, act); |
304 |
((SupportPopupMenu) this.contents).addActionToPopupMenu(
|
305 |
sact.getName(), |
306 |
sact); |
307 |
} |
308 |
}else{
|
309 |
if(contents instanceof SupportPopupMenu) { |
310 |
((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
|
311 |
} |
312 |
} |
313 |
|
314 |
} |
315 |
} |
316 |
|
317 |
public void addActionToPopupMenu(String name, Action action) { |
318 |
if(contents != null){ |
319 |
if(contents instanceof SupportPopupMenu ) { |
320 |
DynFormFieldAction sact = new DynFormFieldAction(this,action); |
321 |
((SupportPopupMenu) this.contents).addActionToPopupMenu(
|
322 |
sact.getName(), |
323 |
sact); |
324 |
} |
325 |
}else{
|
326 |
this.customActions.add(action);
|
327 |
} |
328 |
} |
329 |
|
330 |
public void addSeparatorToPopupMenu() { |
331 |
if(contents != null){ |
332 |
if( contents instanceof SupportPopupMenu ) { |
333 |
((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
|
334 |
} |
335 |
}else{
|
336 |
this.customActions.add(null); |
337 |
} |
338 |
} |
339 |
|
340 |
public void setTranslateEmptyToNull(boolean emptyToNull) { |
341 |
this.emptyToNull = emptyToNull;
|
342 |
} |
343 |
|
344 |
public boolean translateEmptyToNull() { |
345 |
return this.emptyToNull; |
346 |
} |
347 |
|
348 |
public void clear() { |
349 |
if( this.contents == null ) { |
350 |
return;
|
351 |
} |
352 |
if( this.contents instanceof JTextField ) { |
353 |
Object value = this.getDefinition().getDefaultValue(); |
354 |
if( value != null ) { |
355 |
value = value.toString(); |
356 |
} else {
|
357 |
value = "";
|
358 |
} |
359 |
((JTextField)this.contents).setText((String)value); |
360 |
return;
|
361 |
} |
362 |
if( this.contents instanceof JSpinner ) { |
363 |
Object value = this.getDefinition().getDefaultValue(); |
364 |
if( value != null ) { |
365 |
value = value.toString(); |
366 |
} |
367 |
((JSpinner)this.contents).setValue(value); |
368 |
return;
|
369 |
} |
370 |
if( this.contents instanceof JList ) { |
371 |
((JList)this.contents).setSelectedIndex(-1); |
372 |
return;
|
373 |
} |
374 |
if( this.contents instanceof JComboBox ) { |
375 |
((JComboBox)this.contents).setSelectedIndex(-1); |
376 |
return;
|
377 |
} |
378 |
} |
379 |
|
380 |
public void setForm(JDynForm form) { |
381 |
this.form = form;
|
382 |
} |
383 |
|
384 |
public JDynForm getForm() {
|
385 |
return this.form; |
386 |
} |
387 |
|
388 |
protected int getTagValueAsInt(String tagname, int defaultVaue) { |
389 |
return getTagValueAsInt(tagname, null, defaultVaue); |
390 |
} |
391 |
|
392 |
protected int getTagValueAsInt(String tagname1, String tagname2, int defaultVaue) { |
393 |
DynField_v2 fielddef = (DynField_v2)this.getDefinition();
|
394 |
String tagname = null; |
395 |
if( fielddef.getTags().has(tagname1) ) {
|
396 |
tagname = tagname1; |
397 |
} else if( fielddef.getTags().has(tagname2) ) { |
398 |
tagname = tagname2; |
399 |
} else {
|
400 |
return defaultVaue;
|
401 |
} |
402 |
try {
|
403 |
int value = fielddef.getTags().getInt(tagname);
|
404 |
return value;
|
405 |
} catch (CoercionException ex) {
|
406 |
logger.warn("Can't parse tag '"+tagname+"' as int for field '"+fielddef.getName()+"'.",ex); |
407 |
} |
408 |
return defaultVaue;
|
409 |
} |
410 |
|
411 |
protected boolean getTagValueAsBoolean(String tagname, boolean defaultVaue) { |
412 |
return getTagValueAsBoolean(tagname, null, defaultVaue); |
413 |
} |
414 |
|
415 |
protected boolean getTagValueAsBoolean(String tagname1, String tagname2, boolean defaultVaue) { |
416 |
DynField_v2 fielddef = (DynField_v2)this.getDefinition();
|
417 |
String tagname = null; |
418 |
if( fielddef.getTags().has(tagname1) ) {
|
419 |
tagname = tagname1; |
420 |
} else if( fielddef.getTags().has(tagname2) ) { |
421 |
tagname = tagname2; |
422 |
} else {
|
423 |
return defaultVaue;
|
424 |
} |
425 |
try {
|
426 |
boolean value = fielddef.getTags().getBoolean(tagname);
|
427 |
return value;
|
428 |
} catch (CoercionException ex) {
|
429 |
logger.warn("Can't parse tag '"+tagname+"' as boolean for field '"+fielddef.getName()+"'.",ex); |
430 |
} |
431 |
return defaultVaue;
|
432 |
} |
433 |
|
434 |
protected String getTagValueAsString(String tagname, String defaultVaue) { |
435 |
return getTagValueAsString(tagname, null, defaultVaue); |
436 |
} |
437 |
|
438 |
protected String getTagValueAsString(String tagname1, String tagname2, String defaultVaue) { |
439 |
DynField_v2 fielddef = (DynField_v2)this.getDefinition();
|
440 |
String tagname = null; |
441 |
if( fielddef.getTags().has(tagname1) ) {
|
442 |
tagname = tagname1; |
443 |
} else if( fielddef.getTags().has(tagname2) ) { |
444 |
tagname = tagname2; |
445 |
} else {
|
446 |
return defaultVaue;
|
447 |
} |
448 |
try {
|
449 |
String value = (String) fielddef.getTags().get(tagname,DataTypes.STRING); |
450 |
return value;
|
451 |
} catch (CoercionException ex) {
|
452 |
logger.warn("Can't parse tag '"+tagname+"' as string for field '"+fielddef.getName()+"'.",ex); |
453 |
} |
454 |
return defaultVaue;
|
455 |
} |
456 |
|
457 |
} |