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 @ 931
History | View | Annotate | Download (6.52 KB)
1 |
package org.gvsig.tools.dynform.spi.dynformfield; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
5 |
import java.util.HashSet; |
6 |
import java.util.Iterator; |
7 |
import java.util.Set; |
8 |
|
9 |
import javax.swing.Action; |
10 |
import javax.swing.JComponent; |
11 |
import javax.swing.JLabel; |
12 |
import javax.swing.JPanel; |
13 |
|
14 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
15 |
import org.gvsig.tools.dynform.spi.dynformfield.JProblemIndicator; |
16 |
import org.gvsig.tools.dynform.spi.dynformfield.SupportPopupMenu; |
17 |
import org.gvsig.tools.dynforms.DynFormFieldDefinition; |
18 |
import org.gvsig.tools.dynforms.JDynFormField; |
19 |
import org.gvsig.tools.dynobject.DynObject; |
20 |
import org.gvsig.tools.service.Manager; |
21 |
import org.gvsig.tools.service.spi.ServiceManager; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
@SuppressWarnings({"rawtypes", "unchecked"}) |
26 |
public abstract class AbstractJDynFormField implements JDynFormField { |
27 |
|
28 |
protected static final Logger logger = LoggerFactory |
29 |
.getLogger(AbstractJDynFormField.class); |
30 |
|
31 |
private DynFormSPIManager manager = null; |
32 |
private DynFormFieldDefinition definition = null; |
33 |
private JLabel jlabel = null; |
34 |
private JPanel jlabelpanel = null; |
35 |
private Set listeners = null; |
36 |
private JProblemIndicator problemIndicator = null; |
37 |
|
38 |
protected DynObject parameters = null; |
39 |
protected JComponent contents = null; |
40 |
|
41 |
private boolean readOnly = false; |
42 |
|
43 |
|
44 |
public AbstractJDynFormField(DynObject parameters,
|
45 |
ServiceManager serviceManager) { |
46 |
this.parameters = parameters;
|
47 |
this.manager = (DynFormSPIManager) serviceManager;
|
48 |
this.definition = this.getDefinition(); |
49 |
this.listeners = new HashSet(); |
50 |
this.readOnly = this.definition.isReadOnly(); |
51 |
this.problemIndicator = this.manager.createProblemIndicator(this); |
52 |
} |
53 |
|
54 |
public abstract void initComponent(); |
55 |
public abstract Object getAssignedValue(); |
56 |
|
57 |
public Object getParameterValue() { |
58 |
return this.parameters.getDynValue(DynFormSPIManager.FIELD_VALUE); |
59 |
} |
60 |
|
61 |
public JComponent asJComponent() { |
62 |
if (this.contents == null) { |
63 |
this.initComponent();
|
64 |
if( this.readOnly ) { |
65 |
this.setReadOnly(readOnly);
|
66 |
} |
67 |
} |
68 |
return this.contents; |
69 |
} |
70 |
|
71 |
public String getName() { |
72 |
return this.definition.getName(); |
73 |
} |
74 |
|
75 |
public String getLabel() { |
76 |
return definition.getLabel();
|
77 |
} |
78 |
|
79 |
public JComponent getJLabel() { |
80 |
if (this.jlabel == null) { |
81 |
this.jlabel = new JLabel(this.getLabel()); |
82 |
this.jlabel.setLabelFor(this.contents); |
83 |
if( this.getDefinition().isMandatory() ) { |
84 |
this.jlabel.setForeground(Color.red.darker()); |
85 |
} |
86 |
this.jlabelpanel = new JPanel(); |
87 |
this.jlabelpanel.setLayout(new BorderLayout()); |
88 |
this.jlabelpanel.add(jlabel,BorderLayout.CENTER); |
89 |
this.jlabelpanel.add(problemIndicator.asJComponent(), BorderLayout.LINE_END); |
90 |
} |
91 |
return this.jlabelpanel; |
92 |
} |
93 |
|
94 |
public DynFormFieldDefinition getDefinition() {
|
95 |
return (DynFormFieldDefinition) this.parameters |
96 |
.getDynValue(DynFormSPIManager.FIELD_FIELDDEFINITION); |
97 |
} |
98 |
|
99 |
public Manager getManager() {
|
100 |
return this.manager; |
101 |
} |
102 |
|
103 |
public void addListener(JDynFormFieldListener listener) { |
104 |
this.listeners.add(listener);
|
105 |
} |
106 |
|
107 |
public void removeListener(JDynFormFieldListener listener) { |
108 |
this.listeners.remove(listener);
|
109 |
} |
110 |
|
111 |
protected void fireFieldChangedEvent() { |
112 |
Iterator it = this.listeners.iterator(); |
113 |
while (it.hasNext()) {
|
114 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
115 |
try {
|
116 |
listener.fieldChanged(this);
|
117 |
} catch (Exception ex) { |
118 |
logger.info("Error calling listener " + listener.toString()
|
119 |
+ "(" + listener.getClass().getName() + ") from " |
120 |
+ this.toString() + "(" + this.getClass().getName() |
121 |
+ ").", ex);
|
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
protected void fireFieldEnterEvent() { |
127 |
Iterator it = this.listeners.iterator(); |
128 |
while (it.hasNext()) {
|
129 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
130 |
try {
|
131 |
listener.fieldEnter(this);
|
132 |
} catch (Exception ex) { |
133 |
logger.info("Error calling listener " + listener.toString()
|
134 |
+ "(" + listener.getClass().getName() + ") from " |
135 |
+ this.toString() + "(" + this.getClass().getName() |
136 |
+ ").", ex);
|
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
protected void fireFieldExitEvent() { |
142 |
Iterator it = this.listeners.iterator(); |
143 |
while (it.hasNext()) {
|
144 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
145 |
try {
|
146 |
listener.fieldExit(this);
|
147 |
} catch (Exception ex) { |
148 |
logger.info("Error calling listener " + listener.toString()
|
149 |
+ "(" + listener.getClass().getName() + ") from " |
150 |
+ this.toString() + "(" + this.getClass().getName() |
151 |
+ ").", ex);
|
152 |
} |
153 |
} |
154 |
} |
155 |
|
156 |
public void fireMessageEvent(String message) { |
157 |
Iterator it = this.listeners.iterator(); |
158 |
while (it.hasNext()) {
|
159 |
JDynFormFieldListener listener = (JDynFormFieldListener) it.next(); |
160 |
try {
|
161 |
listener.message(this, message);
|
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 |
public boolean isReadOnly() { |
172 |
return this.readOnly ; |
173 |
} |
174 |
|
175 |
public void setReadOnly(boolean readonly) { |
176 |
// FIXME: Implememtacion por defecto, sobreescribirla en las subclases
|
177 |
// segun convenga para cada componente.
|
178 |
|
179 |
this.readOnly = readonly;
|
180 |
if( this.contents != null ) { |
181 |
this.contents.setEnabled(!readOnly);
|
182 |
} |
183 |
} |
184 |
|
185 |
public JProblemIndicator problemIndicator() {
|
186 |
return this.problemIndicator; |
187 |
} |
188 |
|
189 |
public class IllegalFieldValue extends RuntimeException { |
190 |
|
191 |
/**
|
192 |
*
|
193 |
*/
|
194 |
private static final long serialVersionUID = -4409236610055983438L; |
195 |
|
196 |
public IllegalFieldValue(JDynFormField field, String message) { |
197 |
super("The value of field '"+field.getLabel()+"' is not valid. " + message); |
198 |
} |
199 |
} |
200 |
|
201 |
public String toString() { |
202 |
return super.toString() + "{" + this.getName() + "}"; |
203 |
} |
204 |
|
205 |
public boolean isModified() { |
206 |
boolean modified = false; |
207 |
try {
|
208 |
if( !this.isReadOnly() ) { |
209 |
Object value = this.getValue(); |
210 |
if( value == null ) { |
211 |
if( value != this.getAssignedValue() ) { |
212 |
modified = true;
|
213 |
} |
214 |
} else {
|
215 |
if( ! value.equals(this.getAssignedValue()) ) { |
216 |
modified = true;
|
217 |
} |
218 |
} |
219 |
} |
220 |
} catch(IllegalFieldValue ex) {
|
221 |
// Si es incorrecto el valor del campo decimos a capom que esta modificado.
|
222 |
modified = true;
|
223 |
} |
224 |
return modified;
|
225 |
} |
226 |
|
227 |
public void addActionToPopupMenu(String name, Action action) { |
228 |
if( contents instanceof SupportPopupMenu ) { |
229 |
((SupportPopupMenu) this.contents).addActionToPopupMenu(name, action);
|
230 |
} |
231 |
} |
232 |
|
233 |
public void addSeparatorToPopupMenu() { |
234 |
if( contents instanceof SupportPopupMenu ) { |
235 |
((SupportPopupMenu) this.contents).addSeparatorToPopupMenu();
|
236 |
} |
237 |
} |
238 |
} |