gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / dynform / src / org / gvsig / tools / dynform / impl / DefaultJDynForm.java @ 841
History | View | Annotate | Download (8.61 KB)
1 |
package org.gvsig.tools.dynform.impl; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.event.MouseAdapter; |
5 |
import java.awt.event.MouseEvent; |
6 |
import java.util.HashMap; |
7 |
import java.util.HashSet; |
8 |
import java.util.Iterator; |
9 |
import java.util.List; |
10 |
import java.util.Map; |
11 |
import java.util.Set; |
12 |
|
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.JLabel; |
15 |
import javax.swing.JOptionPane; |
16 |
import javax.swing.JPanel; |
17 |
import javax.swing.JTabbedPane; |
18 |
|
19 |
import org.gvsig.tools.dynform.api.DynFormDefinition; |
20 |
import org.gvsig.tools.dynform.api.DynFormFieldDefinition; |
21 |
import org.gvsig.tools.dynform.api.JDynForm; |
22 |
import org.gvsig.tools.dynform.api.JDynFormField; |
23 |
import org.gvsig.tools.dynform.api.JDynFormField.JDynFormFieldListener; |
24 |
import org.gvsig.tools.dynobject.DynField; |
25 |
import org.gvsig.tools.dynobject.DynObject; |
26 |
import org.gvsig.tools.service.ServiceException; |
27 |
import org.slf4j.Logger; |
28 |
import org.slf4j.LoggerFactory; |
29 |
|
30 |
import com.jgoodies.forms.builder.DefaultFormBuilder; |
31 |
import com.jgoodies.forms.layout.FormLayout; |
32 |
|
33 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
34 |
public class DefaultJDynForm implements JDynForm, JDynFormFieldListener { |
35 |
|
36 |
protected static final Logger logger = LoggerFactory |
37 |
.getLogger(DefaultJDynForm.class); |
38 |
|
39 |
private DefaultDynFormManager manager = null; |
40 |
private DynFormDefinition definition = null; |
41 |
private int layoutMode = USE_PLAIN; |
42 |
private JComponent jcomponent = null; |
43 |
private Map components = null; |
44 |
private JLabel jlabel_messages = null; |
45 |
private boolean readOnly = false; |
46 |
private Set listeners = null; |
47 |
|
48 |
public DefaultJDynForm(DefaultDynFormManager manager, DynFormDefinition definition) throws ServiceException { |
49 |
this.manager = manager;
|
50 |
this.definition = definition;
|
51 |
this.components = new HashMap(); |
52 |
this.listeners = new HashSet(); |
53 |
} |
54 |
|
55 |
public JComponent asJComponent() { |
56 |
if( this.jcomponent == null ) { |
57 |
try {
|
58 |
this.initComponents();
|
59 |
} catch (ServiceException e) {
|
60 |
throw new RuntimeException(e.getLocalizedMessage(),e); |
61 |
} |
62 |
} |
63 |
return this.jcomponent; |
64 |
} |
65 |
|
66 |
public void addListener(JDynFormListener listener) { |
67 |
this.listeners.add(listener);
|
68 |
} |
69 |
|
70 |
public void removeListener(JDynFormListener listener) { |
71 |
this.listeners.remove(listener);
|
72 |
} |
73 |
|
74 |
protected void fireMessageEvent(String message) { |
75 |
Iterator it = this.listeners.iterator(); |
76 |
while (it.hasNext()) {
|
77 |
JDynFormListener listener = (JDynFormListener) it.next(); |
78 |
try {
|
79 |
listener.message(message); |
80 |
} catch (Exception ex) { |
81 |
logger.info("Error calling listener " + listener.toString()
|
82 |
+ "(" + listener.getClass().getName() + ") from " |
83 |
+ this.toString() + "(" + this.getClass().getName() |
84 |
+ ").", ex);
|
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
|
90 |
public JLabel getMessagesJLabel() { |
91 |
if( this.jlabel_messages == null ) { |
92 |
this.jlabel_messages = new JLabel(); |
93 |
this.jlabel_messages.addMouseListener(new MouseAdapter() { |
94 |
public void mouseClicked(MouseEvent evt) { |
95 |
int count = evt.getClickCount();
|
96 |
if (count == 2) { |
97 |
JOptionPane.showMessageDialog(jcomponent,jlabel_messages.getText(),"Status",JOptionPane.INFORMATION_MESSAGE); |
98 |
} |
99 |
} |
100 |
}); |
101 |
} |
102 |
return this.jlabel_messages; |
103 |
} |
104 |
|
105 |
public void setShowMessageStatus(boolean showMessageStatus) { |
106 |
this.getMessagesJLabel().setVisible(showMessageStatus);
|
107 |
} |
108 |
|
109 |
public boolean isShowMessageStatus() { |
110 |
return this.getMessagesJLabel().isVisible(); |
111 |
} |
112 |
|
113 |
public void message() { |
114 |
this.getMessagesJLabel().setText(" "); |
115 |
} |
116 |
|
117 |
public void message(String msg) { |
118 |
this.getMessagesJLabel().setText(msg);
|
119 |
fireMessageEvent(msg); |
120 |
} |
121 |
|
122 |
private void initComponents() throws ServiceException { |
123 |
switch(this.layoutMode) { |
124 |
case USE_PLAIN:
|
125 |
default:
|
126 |
initComponentsPlain(); |
127 |
break;
|
128 |
case USE_TABS:
|
129 |
initComponentsUseTabs(); |
130 |
break;
|
131 |
case USE_SEPARATORS:
|
132 |
initComponentsUseSeparators(); |
133 |
break;
|
134 |
} |
135 |
message(); |
136 |
} |
137 |
|
138 |
private void initComponentsPlain() throws ServiceException { |
139 |
FormLayout layout = new FormLayout(
|
140 |
"right:pref, 8px, fill:80dlu:grow");
|
141 |
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
142 |
|
143 |
List fields = this.definition.getDefinitions(); |
144 |
Iterator it = fields.iterator();
|
145 |
while( it.hasNext() ) {
|
146 |
DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
147 |
JDynFormField jfield = this.manager.createJDynFormField(fieldDefinition, null); |
148 |
jfield.addListener(this);
|
149 |
if( this.readOnly ) { |
150 |
jfield.setReadOnly(readOnly); |
151 |
} |
152 |
builder.append(jfield.getJLabel(), jfield.asJComponent()); |
153 |
|
154 |
this.components.put(jfield.getName(), jfield);
|
155 |
} |
156 |
JPanel jpanel = new JPanel(); |
157 |
jpanel.setLayout(new BorderLayout()); |
158 |
jpanel.add(builder.getPanel(), BorderLayout.CENTER);
|
159 |
jpanel.add(getMessagesJLabel(), BorderLayout.PAGE_END);
|
160 |
this.jcomponent = jpanel;
|
161 |
} |
162 |
|
163 |
private void initComponentsUseSeparators() throws ServiceException { |
164 |
List groups = this.definition.getGroups(); |
165 |
|
166 |
FormLayout layout = new FormLayout(
|
167 |
"right:pref, 8px, fill:80dlu:grow");
|
168 |
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
169 |
|
170 |
for( int i=0; i<groups.size(); i++ ) { |
171 |
String group = (String) groups.get(i); |
172 |
builder.appendSeparator(group); |
173 |
List fields = this.definition.getDefinitions(group); |
174 |
Iterator it = fields.iterator();
|
175 |
while( it.hasNext() ) {
|
176 |
DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
177 |
JDynFormField jfield = this.manager.createJDynFormField(fieldDefinition, null); |
178 |
jfield.addListener(this);
|
179 |
if( this.readOnly ) { |
180 |
jfield.setReadOnly(readOnly); |
181 |
} |
182 |
|
183 |
builder.append(jfield.getJLabel(), jfield.asJComponent()); |
184 |
|
185 |
this.components.put(jfield.getName(), jfield);
|
186 |
} |
187 |
} |
188 |
JPanel jpanel = new JPanel(); |
189 |
jpanel.setLayout(new BorderLayout()); |
190 |
jpanel.add(builder.getPanel(), BorderLayout.CENTER);
|
191 |
jpanel.add(getMessagesJLabel(), BorderLayout.PAGE_END);
|
192 |
this.jcomponent = jpanel;
|
193 |
} |
194 |
|
195 |
private void initComponentsUseTabs() throws ServiceException { |
196 |
|
197 |
JTabbedPane tabbedPane = new JTabbedPane(); |
198 |
|
199 |
List groups = this.definition.getGroups(); |
200 |
|
201 |
for( int i=0; i<groups.size(); i++ ) { |
202 |
String group = (String) groups.get(i); |
203 |
|
204 |
FormLayout layout = new FormLayout(
|
205 |
"right:pref, 8px, fill:80dlu:grow");
|
206 |
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
|
207 |
|
208 |
List fields = this.definition.getDefinitions(group); |
209 |
Iterator it = fields.iterator();
|
210 |
while( it.hasNext() ) {
|
211 |
DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next(); |
212 |
JDynFormField jfield = this.manager.createJDynFormField(fieldDefinition, null); |
213 |
jfield.addListener(this);
|
214 |
if( this.readOnly ) { |
215 |
jfield.setReadOnly(readOnly); |
216 |
} |
217 |
|
218 |
builder.append(jfield.getJLabel(), jfield.asJComponent()); |
219 |
|
220 |
this.components.put(jfield.getName(), jfield);
|
221 |
} |
222 |
tabbedPane.addTab(group, builder.getPanel()); |
223 |
} |
224 |
JPanel jpanel = new JPanel(); |
225 |
jpanel.setLayout(new BorderLayout()); |
226 |
jpanel.add(tabbedPane, BorderLayout.CENTER);
|
227 |
jpanel.add(getMessagesJLabel(), BorderLayout.PAGE_END);
|
228 |
this.jcomponent = jpanel;
|
229 |
} |
230 |
|
231 |
public int getLayoutMode() { |
232 |
return this.layoutMode; |
233 |
} |
234 |
|
235 |
public void setLayoutMode(int layoutMode) { |
236 |
if( layoutMode<0 || layoutMode>USE_SEPARATORS) { |
237 |
throw new IllegalArgumentException("layoutMode ("+layoutMode+") out of range. Valid values are 0 .. "+ USE_SEPARATORS+"."); |
238 |
} |
239 |
this.layoutMode = layoutMode;
|
240 |
} |
241 |
|
242 |
public void setValues(DynObject values) { |
243 |
DynField[] fields = values.getDynClass().getDynFields();
|
244 |
for( int i=0; i<fields.length; i++ ) { |
245 |
String name = fields[i].getName();
|
246 |
JDynFormField jfield = (JDynFormField) this.components.get(name);
|
247 |
jfield.setValue(values.getDynValue(name)); |
248 |
} |
249 |
} |
250 |
|
251 |
public void getValues(DynObject values) { |
252 |
DynField[] fields = values.getDynClass().getDynFields();
|
253 |
for( int i=0; i<fields.length; i++ ) { |
254 |
String name = fields[i].getName();
|
255 |
JDynFormField jfield = (JDynFormField) this.components.get(name);
|
256 |
values.setDynValue(name,jfield.getValue()); |
257 |
} |
258 |
} |
259 |
|
260 |
public boolean haveValidValues() { |
261 |
// TODO Auto-generated method stub
|
262 |
return false; |
263 |
} |
264 |
|
265 |
public void fieldEnter(JDynFormField field) { |
266 |
message(field.getDefinition().getDescription()); |
267 |
} |
268 |
|
269 |
public void fieldExit(JDynFormField field) { |
270 |
message(); |
271 |
} |
272 |
|
273 |
public void message(JDynFormField field, String message) { |
274 |
message(message); |
275 |
} |
276 |
|
277 |
public void fieldChanged(JDynFormField field) { |
278 |
// TODO Auto-generated method stub
|
279 |
} |
280 |
|
281 |
public boolean isReadOnly() { |
282 |
return readOnly;
|
283 |
} |
284 |
|
285 |
public void setReadOnly(boolean readOnly) { |
286 |
this.readOnly = readOnly;
|
287 |
Iterator it = this.components.values().iterator(); |
288 |
while( it.hasNext() ) {
|
289 |
JDynFormField field = (JDynFormField) it.next(); |
290 |
field.setReadOnly(readOnly); |
291 |
} |
292 |
} |
293 |
} |