Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.impl / src / main / java / org / gvsig / tools / dynform / impl / DefaultJDynForm.java @ 931

History | View | Annotate | Download (12.6 KB)

1
package org.gvsig.tools.dynform.impl;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Component;
5
import java.awt.Dimension;
6
import java.awt.event.MouseAdapter;
7
import java.awt.event.MouseEvent;
8
import java.util.ArrayList;
9
import java.util.HashMap;
10
import java.util.HashSet;
11
import java.util.Iterator;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Set;
15

    
16
import javax.swing.Action;
17
import javax.swing.JComponent;
18
import javax.swing.JLabel;
19
import javax.swing.JOptionPane;
20
import javax.swing.JPanel;
21
import javax.swing.JScrollPane;
22
import javax.swing.JTabbedPane;
23
import javax.swing.JViewport;
24

    
25
import org.gvsig.tools.dataTypes.CoercionException;
26
import org.gvsig.tools.dataTypes.DataType;
27
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
28
import org.gvsig.tools.dynforms.DynFormDefinition;
29
import org.gvsig.tools.dynforms.DynFormFieldDefinition;
30
import org.gvsig.tools.dynforms.JDynForm;
31
import org.gvsig.tools.dynforms.JDynFormField;
32
import org.gvsig.tools.dynforms.JDynForm.JDynFormListener;
33
import org.gvsig.tools.dynforms.JDynFormField.JDynFormFieldListener;
34
import org.gvsig.tools.dynobject.DynField;
35
import org.gvsig.tools.dynobject.DynObject;
36
import org.gvsig.tools.service.ServiceException;
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
import com.jgoodies.forms.builder.DefaultFormBuilder;
41
import com.jgoodies.forms.layout.FormLayout;
42
import com.jgoodies.forms.layout.RowSpec;
43

    
44
@SuppressWarnings({ "unchecked", "rawtypes" })
45
public class DefaultJDynForm implements JDynForm, JDynFormFieldListener {
46

    
47
        protected static final Logger logger = LoggerFactory
48
                        .getLogger(DefaultJDynForm.class);
49
        
50
        private int formWidth= 320;
51
        private int formHeight = 540;
52
        
53
        private DefaultDynFormManager manager = null;
54
        private DynFormDefinition definition = null;
55
        private int layoutMode = USE_PLAIN;
56
        private JComponent contents = null;
57
        private Map components = null;
58
        private JLabel jlabel_messages = null;
59
        private boolean readOnly = false;
60
        private Set listeners = null;
61

    
62
        public DefaultJDynForm(DefaultDynFormManager manager, DynFormDefinition definition) throws ServiceException {
63
                this.manager = manager;
64
                this.definition = definition;
65
                this.components = new HashMap();
66
                this.listeners = new HashSet();
67
        }
68

    
69
        public DynFormSPIManager getServiceManager() {
70
                return this.manager.getServiceManager();
71
        }
72
        
73
        public JComponent asJComponent() {
74
                if( this.contents == null ) {
75
                        try {
76
                                this.initComponents();
77
                        } catch (ServiceException e) {
78
                                throw new RuntimeException(e.getLocalizedMessage(),e);
79
                        }
80
                }
81
                return this.contents;
82
        }
83
        
84
        public void addListener(JDynFormListener listener) {
85
                this.listeners.add(listener);
86
        }
87

    
88
        public void removeListener(JDynFormListener listener) {
89
                this.listeners.remove(listener);
90
        }
91
        
92
        protected void fireMessageEvent(String message) {
93
                Iterator it = this.listeners.iterator();
94
                while (it.hasNext()) {
95
                        JDynFormListener listener = (JDynFormListener) it.next();
96
                        try {
97
                        listener.message(message);
98
                        } catch (Exception ex) {
99
                                logger.info("Error calling listener " + listener.toString()
100
                                                + "(" + listener.getClass().getName() + ") from "
101
                                                + this.toString() + "(" + this.getClass().getName()
102
                                                + ").", ex);
103
                        }
104
                }
105
        }
106

    
107
        
108
        public JLabel getMessagesJLabel() {
109
                if( this.jlabel_messages == null ) {
110
                        this.jlabel_messages = new JLabel();
111
                        this.jlabel_messages.addMouseListener(new MouseAdapter()  {
112
                    public void mouseClicked(MouseEvent evt) {
113
                        int count = evt.getClickCount();
114
                        if (count == 2) {
115
                            JOptionPane.showMessageDialog(contents,jlabel_messages.getText(),"Status",JOptionPane.INFORMATION_MESSAGE);
116
                        }
117
                    }
118
                });
119
                }
120
                return this.jlabel_messages;
121
        }
122
        
123
        public void setShowMessageStatus(boolean showMessageStatus) {
124
                this.getMessagesJLabel().setVisible(showMessageStatus);
125
        }
126
        
127
        public boolean isShowMessageStatus() {
128
                return this.getMessagesJLabel().isVisible();
129
        }
130
        
131
        public void message() {
132
                this.getMessagesJLabel().setText(" ");
133
        }
134
        
135
        public void message(String msg) {
136
                this.getMessagesJLabel().setText(msg);
137
                fireMessageEvent(msg);
138
        }
139
        
140
        private void initComponents() throws ServiceException {
141
                switch(this.layoutMode) {
142
                case USE_PLAIN:
143
                default:
144
                        initComponentsPlain();
145
                        break;
146
                case USE_TABS:
147
                        initComponentsUseTabs();
148
                        break;
149
                case USE_SEPARATORS:
150
                        initComponentsUseSeparators();
151
                        break;
152
                }
153
                message();
154
        }
155
        
156
        private void initComponentsPlain() throws ServiceException {
157
                 FormLayout layout = new FormLayout(
158
                              "right:pref,  8px,  fill:80dlu:grow");
159
            DefaultFormBuilder builder = new DefaultFormBuilder(layout);
160
            
161
                List fields = this.definition.getDefinitions();
162
                Iterator it = fields.iterator();
163
                while( it.hasNext() ) {
164
                        DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next();
165
                        JDynFormField jfield = getServiceManager().createJDynFormField(fieldDefinition, null);
166
                        jfield.setReadOnly(this.readOnly);
167
                        jfield.addListener(this);
168
                        if( this.readOnly ) {
169
                                jfield.setReadOnly(readOnly);
170
                        }
171
                        builder.append(jfield.getJLabel(),   jfield.asJComponent());
172
                        
173
                        this.components.put(jfield.getName(), jfield);
174
                }
175
                JPanel jpanel = new JPanel();
176
                jpanel.setLayout(new BorderLayout());
177
                jpanel.add(builder.getPanel(), BorderLayout.CENTER);
178
                jpanel.add(getMessagesJLabel(), BorderLayout.PAGE_END);
179
                
180
                JScrollPane scroll = new JScrollPane(jpanel);
181
                scroll.setPreferredSize(new Dimension(formWidth, formHeight));
182
                this.contents = scroll;
183
        }
184

    
185
        private void initComponentsUseSeparators() throws ServiceException {
186
                List groups = this.definition.getGroups();
187
                
188
                FormLayout layout = new FormLayout(
189
                              "left:pref,  8px,  fill:80dlu:grow", "pref");
190
                
191
            DefaultFormBuilder builder = new DefaultFormBuilder(layout);
192
            builder.setDefaultRowSpec(new RowSpec(
193
                                RowSpec.TOP,
194
                                builder.getLayout().getRowSpec(1).getSize(),
195
                                builder.getLayout().getRowSpec(1).getResizeWeight()));
196
            
197
            for( int i=0; i<groups.size(); i++ ) {
198
                    String group = (String) groups.get(i);
199
                    builder.appendSeparator(group);
200
                        List fields = this.definition.getDefinitions(group);
201
                        Iterator it = fields.iterator();
202
                        while( it.hasNext() ) {
203
                                DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next();
204
                                JDynFormField jfield = getServiceManager().createJDynFormField(fieldDefinition, null);
205
                                jfield.setReadOnly(this.readOnly);
206
                                jfield.addListener(this);
207
                                if( this.readOnly ) {
208
                                        jfield.setReadOnly(readOnly);
209
                                }
210
                                
211
                                builder.append(jfield.getJLabel(),   jfield.asJComponent());
212
                                
213
                                this.components.put(jfield.getName(), jfield);
214
                        }
215
            }
216
                JPanel jpanel = new JPanel();
217
                jpanel.setLayout(new BorderLayout());
218
                jpanel.add(builder.getPanel(), BorderLayout.CENTER);
219
                jpanel.add(getMessagesJLabel(), BorderLayout.PAGE_END);
220
                
221
                JScrollPane scroll = new JScrollPane(jpanel);
222
                scroll.setPreferredSize(new Dimension(formWidth, formHeight));
223
                this.contents = scroll;
224
        }
225

    
226
        private void initComponentsUseTabs() throws ServiceException {
227

    
228
                JTabbedPane tabbedPane = new JTabbedPane();
229
                
230
                List groups = this.definition.getGroups();
231
                
232
            for( int i=0; i<groups.size(); i++ ) {
233
                    String group = (String) groups.get(i);
234
                    
235
                        FormLayout layout = new FormLayout(
236
                                      "left:pref,  8px,  fill:80dlu:grow", "pref");
237
                        
238
                    DefaultFormBuilder builder = new DefaultFormBuilder(layout);
239
                        builder.setDefaultRowSpec(new RowSpec(
240
                                        RowSpec.TOP,
241
                                        builder.getLayout().getRowSpec(1).getSize(),
242
                                        builder.getLayout().getRowSpec(1).getResizeWeight()));
243
                        
244
                        List fields = this.definition.getDefinitions(group);
245
                        Iterator it = fields.iterator();
246
                        while( it.hasNext() ) {
247
                                DynFormFieldDefinition fieldDefinition = (DynFormFieldDefinition) it.next();
248
                                JDynFormField jfield = getServiceManager().createJDynFormField(fieldDefinition, null);
249
                                jfield.setReadOnly(this.readOnly);
250
                                jfield.addListener(this);
251
                                if( this.readOnly ) {
252
                                        jfield.setReadOnly(readOnly);
253
                                }
254
                                
255
                                builder.append(jfield.getJLabel(),   jfield.asJComponent());
256
                                
257
                                this.components.put(jfield.getName(), jfield);
258
                        }
259
                    tabbedPane.addTab(group, builder.getPanel());
260
            }
261
                JPanel jpanel = new JPanel();
262
                jpanel.setLayout(new BorderLayout());
263
                jpanel.add(tabbedPane, BorderLayout.CENTER);
264
                jpanel.add(getMessagesJLabel(), BorderLayout.PAGE_END);
265
                
266
                JScrollPane scroll = new JScrollPane(jpanel);
267
                scroll.setPreferredSize(new Dimension(formWidth, formHeight));
268
                this.contents = scroll;
269
        }
270

    
271
        public int getLayoutMode() {
272
                return this.layoutMode;
273
        }
274
        
275
        public void setLayoutMode(int layoutMode) {
276
                if( layoutMode<0 || layoutMode>USE_SEPARATORS) {
277
                        throw new IllegalArgumentException("layoutMode ("+layoutMode+") out of range. Valid values are 0 .. "+ USE_SEPARATORS+".");
278
                }
279
                this.layoutMode = layoutMode;
280
        }
281
        
282
        public void setValues(DynObject values) {
283
                DynField[] fields = values.getDynClass().getDynFields();
284
                for( int i=0; i<fields.length; i++ ) {
285
                        String name = fields[i].getName();
286
                        JDynFormField jfield = (JDynFormField) this.components.get(name);
287
                        jfield.setValue(values.getDynValue(name));
288
                }
289
        }
290

    
291
        public void getValues(DynObject values) {
292
                DynField[] fields = values.getDynClass().getDynFields();
293
                for( int i=0; i<fields.length; i++ ) {
294
                        String name = fields[i].getName();
295
                        JDynFormField jfield = (JDynFormField) this.components.get(name);
296
                        try {
297
                                Object value = jfield.getValue();
298
                                values.setDynValue(name,value);
299
                                
300
                        } catch(Exception ex) {
301
                                logger.info(ex.getLocalizedMessage(), ex);
302
                        }
303
                }
304
        }
305

    
306
        public boolean haveValidValues() {
307
                Iterator it = this.components.values().iterator();
308
                while( it.hasNext() ) {
309
                        JDynFormField jfield = (JDynFormField) it.next();
310
                        if( ! jfield.hasValidValue() ) {
311
                                return false;
312
                        }
313
                }
314
                
315
                return true;
316
        }
317
        
318
        public boolean haveValidValues(List<String> fieldsName) {
319
                if(fieldsName == null){
320
                        fieldsName = new ArrayList<String>();
321
                }
322
                
323
                Iterator it = this.components.values().iterator();
324
                while( it.hasNext() ) {
325
                        JDynFormField jfield = (JDynFormField) it.next();
326
                        if( ! jfield.hasValidValue() ) {
327
                                fieldsName.add(jfield.getName());
328
                        }
329
                }
330
                
331
                if(!fieldsName.isEmpty()){
332
                        return false;
333
                }else
334
                        return true;
335
        }
336
        
337
        public boolean isModified() {
338
                Iterator it = this.components.values().iterator();
339
                while( it.hasNext() ) {
340
                        JDynFormField jfield = (JDynFormField) it.next();
341
                        if( jfield.isModified() ) {
342
                                return true;
343
                        }
344
                }
345
                return false;
346
        }
347

    
348
        public void fieldEnter(JDynFormField field) {
349
                message(field.getDefinition().getDescription());
350
        }
351

    
352
        public void fieldExit(JDynFormField field) {
353
                message();
354
        }
355

    
356
        public void message(JDynFormField field, String message) {
357
                message(message);
358
        }
359

    
360
        public void fieldChanged(JDynFormField field) {
361
                // TODO Auto-generated method stub
362
        }
363

    
364
        public boolean isReadOnly() {
365
                return readOnly;
366
        }
367
        
368
        public void setReadOnly(boolean readOnly) {
369
                this.readOnly = readOnly;
370
                if( this.contents != null ) {
371
                        Iterator it = this.components.values().iterator();
372
                        while( it.hasNext() ) {
373
                                JDynFormField field = (JDynFormField) it.next();
374
                                field.setReadOnly(readOnly);
375
                        }
376
                }
377
        }
378

    
379
        public int getFormWidth() {
380
                if(this.contents == null){
381
                        return this.formWidth;
382
                }else{
383
                        JViewport port = ((JScrollPane)this.asJComponent()).getViewport();
384
                        Component comp = (Component) port.getView();
385
                        return comp.getWidth();
386
                }
387
        }
388

    
389
        public int getFormHeight() {
390
                if(this.contents == null){
391
                        return this.formHeight;
392
                }else{
393
                        JViewport port = ((JScrollPane)this.asJComponent()).getViewport();
394
                        Component comp = (Component) port.getView();
395
                        return comp.getHeight();
396
                }
397
        }
398

    
399
        public void setFormSize(int width, int height) {
400
                this.formHeight = height;
401
                this.formWidth = width;
402
                if( this.contents != null ){
403
                        this.asJComponent().setPreferredSize(new Dimension(width, height));
404
                }
405
        }
406

    
407
        public void addActionToPopupMenu(DataType tipo, String name, Action action) {
408
                // TODO Auto-generated method stub
409
                
410
        }
411

    
412
        public void addSeparatorToPopupMenu(DataType tipo) {
413
                // TODO Auto-generated method stub
414
                
415
        }
416
        
417
        public Object getValue(String fieldName) {
418
                JDynFormField field = (JDynFormField) this.components.get(fieldName);
419
                return field.getValue();
420
        }
421
        
422
        public void setValue(String fieldName, Object value) {
423
                JDynFormField field = (JDynFormField) this.components.get(fieldName);
424
                try {
425
                        value = field.getDefinition().getDataType().coerce(value);
426
                } catch (CoercionException e) {
427
                        String msg = "Invalid value '"+((value==null)?"(null)":value.toString())+"' for field '"+fieldName+"'.";
428
                        logger.warn(msg,e);
429
                        throw new RuntimeException(msg, e);
430
                }
431
                field.setValue(value);
432
        }
433
        
434
        public JDynFormField getField(String fieldName) {
435
                JDynFormField field = (JDynFormField) this.components.get(fieldName);
436
                return field;
437
        }
438
}