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