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