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 / DefaultJDynFormSet.java @ 1031

History | View | Annotate | Download (17.6 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.impl;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.event.MouseAdapter;
28
import java.awt.event.MouseEvent;
29
import java.util.ArrayList;
30
import java.util.Iterator;
31
import java.util.List;
32

    
33
import javax.swing.Action;
34
import javax.swing.JComponent;
35
import javax.swing.JLabel;
36
import javax.swing.JOptionPane;
37
import javax.swing.JPanel;
38

    
39
import org.gvsig.tools.lang.Cloneable;
40
import org.gvsig.tools.dataTypes.DataType;
41
import org.gvsig.tools.dispose.DisposableIterator;
42
import org.gvsig.tools.dynform.AbortActionException;
43
import org.gvsig.tools.dynform.DynFormDefinition;
44
import org.gvsig.tools.dynform.JDynForm;
45
import org.gvsig.tools.dynform.JDynForm.JDynFormListener;
46
import org.gvsig.tools.dynform.JDynFormSet;
47
import org.gvsig.tools.dynform.impl.FormSetButtonBar.FormSetListener;
48
import org.gvsig.tools.dynobject.DynObject;
49
import org.gvsig.tools.dynobject.DynObjectSet;
50
import org.gvsig.tools.exception.BaseException;
51
import org.gvsig.tools.service.ServiceException;
52
import org.gvsig.tools.visitor.VisitCanceledException;
53
import org.gvsig.tools.visitor.Visitor;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

    
57
public class DefaultJDynFormSet implements JDynFormSet, FormSetListener, JDynFormListener {
58

    
59
        private static final Logger logger = LoggerFactory.getLogger(DefaultJDynFormSet.class);
60
        
61
        private DefaultDynFormManager manager = null;
62
        private DynFormDefinition definition = null;
63
        private int layoutMode = USE_PLAIN;
64
        private JLabel jlabel_messages = null;
65
        private boolean readOnly = false;
66
        private JDynForm form = null;
67
        private FormSetButtonBar buttonBar = null;
68
        private JPanel contents = null;
69
        private List values=null;
70
        private int current=0;
71
        private DefaultVisitableSet listeners = null;
72
        private boolean autosave = true;
73

    
74
        private boolean hasActionDelete = true;
75
        private boolean hasActionNew = true;
76
        private boolean hasActionUpdate = true;
77
        private boolean hasActionSearch = true;
78
        
79
        private int formWidth = -1;
80
        private int formHeight = -1;
81
        
82
        private List<ActionStore> actionsBuffer = new ArrayList<ActionStore>();
83

    
84
        private boolean useScrollBars = true;
85
        
86
        public DefaultJDynFormSet(DefaultDynFormManager manager, DynFormDefinition definition) throws ServiceException {
87
                this.manager = manager;
88
                this.definition = definition;
89
                this.listeners = new DefaultVisitableSet();
90
        }
91
        
92
        public JComponent asJComponent() {
93
                if( this.contents == null ) {
94
                        try {
95
                                this.initComponents();
96
                        } catch (ServiceException e) {
97
                                throw new RuntimeException(e.getLocalizedMessage(),e);
98
                        }
99
                }
100
                this.fireFormMovedToEvent();
101
                return this.contents;
102
        }
103
        
104
        public void addListener(JDynFormSetListener listener) {
105
                this.listeners.add(listener);
106
        }
107

    
108
        public void removeListener(JDynFormSetListener listener) {
109
                this.listeners.remove(listener);
110
        }
111
        
112
        protected void fireMessageEvent(final String message) {
113
                try {
114
                        this.listeners.accept(new Visitor() {
115
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
116
                                        ((JDynFormSetListener)listener).formMessage(message);
117
                                }
118
                        });
119
                } catch (AbortActionException e) {
120
                        // Do nothing 
121
                } catch (Exception e) {
122
                        logger.info("Error calling to the form message event.",e);
123
                }
124
        }
125

    
126
        protected void fireCloseEvent() {
127
                try {
128
                        this.listeners.accept(new Visitor() {
129
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
130
                                        ((JDynFormSetListener)listener).formClose();
131
                                }
132
                        });
133
                } catch (AbortActionException e) {
134
                        // Do nothing 
135
                } catch (Exception e) {
136
                        logger.info("Error calling to the form close event.",e);
137
                }
138
        }
139
        
140
        protected void fireFormMovedToEvent() {
141
                try {
142
                        this.listeners.accept(new Visitor() {
143
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
144
                                        ((JDynFormSetListener)listener).formMovedTo(current);
145
                                }
146
                        });
147
                } catch (AbortActionException e) {
148
                        // Do nothing 
149
                } catch (Exception e) {
150
                        logger.info("Error calling to the form moved to event.",e);
151
                }
152
        }
153
        
154
        public JLabel getMessagesJLabel() {
155
                if( this.jlabel_messages == null ) {
156
                        this.jlabel_messages = new JLabel();
157
//                        this.jlabel_messages.setBorder( BorderFactory.createLoweredBevelBorder());
158
                        this.jlabel_messages.setText(" ");
159
                        this.jlabel_messages.addMouseListener(new MouseAdapter()  {
160
                    public void mouseClicked(MouseEvent evt) {
161
                        int count = evt.getClickCount();
162
                        if (count == 2) {
163
                            JOptionPane.showMessageDialog(contents,jlabel_messages.getText(),"Status",JOptionPane.INFORMATION_MESSAGE);
164
                        }
165
                    }
166
                });
167
                }
168
                return this.jlabel_messages;
169
        }
170
        
171
        public void message() {
172
                this.getMessagesJLabel().setText(" ");
173
        }
174
        
175
        public void message(String msg) {
176
                this.getMessagesJLabel().setText(msg);
177
        }
178
        
179
        private FormSetButtonBar getButtonBar() throws ServiceException {
180
                if( this.buttonBar == null ) {
181
                        initComponents();
182
                }
183
                return this.buttonBar;
184
        }
185
        
186
        private void initComponents() throws ServiceException {
187
                this.contents = new JPanel();
188
                this.contents.setLayout(new BorderLayout());
189
                
190
                this.form = this.manager.createJDynForm(definition);
191
                this.form.setUseScrollBars(this.useScrollBars);
192
                
193
                if(!actionsBuffer.isEmpty()){
194
                        Iterator it = actionsBuffer.iterator();
195
                        while(it.hasNext()){
196
                                ActionStore actStore = (ActionStore) it.next();
197
                                if(actStore.isSeparator()){
198
                                        form.addSeparatorToPopupMenu(
199
                                                        actStore.getDataType());
200
                                }else{
201
                                        form.addActionToPopupMenu(
202
                                                        actStore.getDataType(), 
203
                                                        actStore.getActionName(), 
204
                                                        actStore.getAction());
205
                                }
206
                        }
207
                }
208
                if( this.useScrollBars ) {
209
                        if(this.formHeight > -1 && this.formWidth > -1){
210
                                this.setFormSize(this.formWidth, this.formHeight);
211
                        }
212
                }
213
                this.form.setShowMessageStatus(false);
214
                this.form.setLayoutMode(this.layoutMode);
215
                this.form.addListener(this);
216

    
217
                this.buttonBar = new FormSetButtonBar();
218
                this.buttonBar.addListener(this);
219
                this.buttonBar.setActionActive(this.buttonBar.ActionDelete, hasActionDelete);
220
                this.buttonBar.setActionActive(this.buttonBar.ActionNew, hasActionNew);
221
                this.buttonBar.setActionActive(this.buttonBar.ActionSave, hasActionUpdate);
222
                this.buttonBar.setActionActive(this.buttonBar.ActionSearch, hasActionSearch);
223
                
224
                this.contents.add(this.buttonBar.asJComponent(), BorderLayout.NORTH);
225
                this.contents.add(this.form.asJComponent(), BorderLayout.CENTER);
226
                this.contents.add(this.getMessagesJLabel(), BorderLayout.SOUTH);
227
                if( this.values != null ) {
228
                        doChangeValue();
229
                }
230
                this.form.setReadOnly(this.readOnly);
231
        }
232
        
233
        public int getLayoutMode() {
234
                return this.layoutMode;
235
        }
236
        
237
        public void setLayoutMode(int layoutMode) {
238
                if( layoutMode<0 || layoutMode>USE_SEPARATORS) {
239
                        throw new IllegalArgumentException("layoutMode ("+layoutMode+") out of range. Valid values are 0 .. "+ USE_SEPARATORS+".");
240
                }
241
                this.layoutMode = layoutMode;
242
        }
243

    
244
        private void doChangeValue()  throws ServiceException  {
245
                this.current = 0;
246
                this.form.setValues((DynObject) this.values.get(this.current));
247

    
248
                this.getButtonBar().setNumrecords(this.values.size());
249
                this.getButtonBar().setCurrent(this.current);
250
        }
251
        
252
        public void setValues(List values) throws ServiceException {
253
                List x = new ArrayList();
254
                x.addAll(values);
255
                this.values = x;
256
                if( this.contents != null ) {
257
                        doChangeValue();
258
                        this.getButtonBar().setEnabled(FormSetButtonBar.ActionDelete, true);
259
                        this.getButtonBar().setEnabled(FormSetButtonBar.ActionSave, true);
260
                        this.getButtonBar().setEnabled(FormSetButtonBar.ActionNew, true);
261
                }
262
        }
263

    
264
        public void setValues(DynObjectSet values) throws ServiceException {
265
                List x = new ArrayList();
266
                DisposableIterator it;
267
                try {
268
                        it = values.iterator();
269
                } catch (BaseException e) {
270
                        logger.info("Uf! o se que hacer con este error, lo relanzo sin mas.",e);
271
                        throw new RuntimeException(e);
272
                }
273
                while(it.hasNext()) {
274
                        DynObject obj = (DynObject) it.next();
275
                        if( obj instanceof Cloneable ) {
276
                                try {
277
                                        obj = (DynObject) ((Cloneable)obj).clone();
278
                                } catch (CloneNotSupportedException e) {
279
                                        // Do nothing
280
                                }
281
                        }
282
                        x.add(obj);
283
                }
284
                this.values = x;
285
                if( this.contents != null ) {
286
                        doChangeValue();
287
                        this.getButtonBar().setEnabled(FormSetButtonBar.ActionDelete, values.isDeleteEnabled());
288
                        this.getButtonBar().setEnabled(FormSetButtonBar.ActionSave, values.isUpdateEnabled());
289
                        this.getButtonBar().setEnabled(FormSetButtonBar.ActionNew, values.isUpdateEnabled());
290
                }
291
        }
292

    
293
        public boolean isReadOnly() {
294
                return readOnly;
295
        }
296
        
297
        public void setReadOnly(boolean readOnly) {
298
                this.readOnly = readOnly;
299
                if( this.form != null ) { 
300
                        this.form.setReadOnly(readOnly);
301
                }
302
        }
303

    
304
        public boolean doActionFirst() {
305
                if( autosave ) {
306
                        if( !doActionSave() ) {
307
                                return false;
308
                        }
309
                }
310
                this.current = 0;
311
                this.form.setValues((DynObject) this.values.get(this.current));
312
                this.buttonBar.setCurrent(this.current);
313
                this.fireFormMovedToEvent();
314
                return true;
315
        }
316

    
317
        public boolean doActionPrevious() {
318
                if( autosave ) {
319
                        if( !doActionSave() ) {
320
                                return false;
321
                        }
322
                }
323
                if( this.current > 0 ) {
324
                        this.current--;
325
                }
326
                this.form.setValues((DynObject) this.values.get(this.current));
327
                this.buttonBar.setCurrent(this.current);
328
                this.fireFormMovedToEvent();
329
                return true;
330
        }
331

    
332
        public boolean doActionNext() {
333
                if( autosave ) {
334
                        if( !doActionSave() ) {
335
                                return false;
336
                        }
337
                }
338
                if( this.current < this.values.size() ) {
339
                        this.current++;
340
                }
341
                this.form.setValues((DynObject) this.values.get(this.current));
342
                this.buttonBar.setCurrent(this.current);
343
                this.fireFormMovedToEvent();
344
                return true;
345
        }
346

    
347
        public boolean doActionLast() {
348
                if( autosave ) {
349
                        if( !doActionSave() ) {
350
                                return false;
351
                        }
352
                }
353
                this.current = this.values.size()-1;
354
                this.form.setValues((DynObject) this.values.get(this.current));
355
                this.buttonBar.setCurrent(this.current);
356
                this.fireFormMovedToEvent();
357
                return true;
358
        }
359

    
360
        public boolean doActionSave() {
361
                if( !this.form.isModified() ) {
362
                        return true;
363
                }
364
                try {
365
                        this.listeners.accept(new Visitor() {
366
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
367
                                        ((JDynFormSetListener)listener).formBeforeSave();
368
                                }
369
                        });
370
                } catch (BaseException e) {
371
                        return true;
372
                }
373
                
374
                List<String> fieldsName = new ArrayList<String>();
375
                if( !this.form.haveValidValues(fieldsName) ) {
376
                        String errores = "";
377
                        Iterator<String> it= fieldsName.iterator();
378
                        while(it.hasNext()){
379
                                String name = (String)it.next();
380
                                errores = errores +"     - "+ name + "\n";
381
                        }
382
                        int r = confirmDialog(
383
                                        "There are incorrect data in the following fields:\n" +errores+"\nContinuing the incorrect values ​will not be saved. Do you want to continue?", "warning", 
384
                                        JOptionPane.WARNING_MESSAGE, 
385
                                        JOptionPane.YES_NO_OPTION);
386
                        if(  r != JOptionPane.YES_OPTION ) {
387
                                return false;
388
                        }
389
                }
390
                if( this.current >=0 && this.current<this.values.size() ) {
391
                        this.form.getValues((DynObject) this.values.get(this.current));
392
                } else {
393
                        logger.warn("current out of range.");
394
                }
395
                        
396
                
397
                try {
398
                        this.listeners.accept(new Visitor() {
399
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
400
                                        ((JDynFormSetListener)listener).formAfterSave();
401
                                }
402
                        });
403
                } catch (BaseException e) {
404
                        return true;
405
                }
406
                return true;
407
        }
408

    
409
        public boolean doActionNew() {
410
                try {
411
                        this.listeners.accept(new Visitor() {
412
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
413
                                        ((JDynFormSetListener)listener).formBeforeNew();
414
                                }
415
                        });
416
                } catch (BaseException e) {
417
                        return true;
418
                }
419
                // Do new Actions
420
                try {
421
                        this.listeners.accept(new Visitor() {
422
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
423
                                        ((JDynFormSetListener)listener).formAfterNew();
424
                                }
425
                        });
426
                } catch (BaseException e) {
427
                        return true;
428
                }
429
                return true;
430
        }
431

    
432
        public boolean doActionDelete() {
433
                try {
434
                        this.listeners.accept(new Visitor() {
435
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
436
                                        ((JDynFormSetListener)listener).formBeforeDelete();
437
                                }
438
                        });
439
                } catch (BaseException e) {
440
                        return true;
441
                }
442
                // Do new Actions
443
                try {
444
                        this.listeners.accept(new Visitor() {
445
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
446
                                        ((JDynFormSetListener)listener).formAfterDelete();
447
                                }
448
                        });
449
                } catch (BaseException e) {
450
                        return true;
451
                }                return true;
452
        }
453

    
454
        public boolean doActionSearch() {
455
                try {
456
                        this.listeners.accept(new Visitor() {
457
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
458
                                        ((JDynFormSetListener)listener).formBeforeSearch();
459
                                }
460
                        });
461
                } catch (BaseException e) {
462
                        return true;
463
                }
464
                // Do search
465
                try {
466
                        this.listeners.accept(new Visitor() {
467
                                public void visit(Object listener) throws VisitCanceledException, BaseException {
468
                                        ((JDynFormSetListener)listener).formBeforeSearch();
469
                                }
470
                        });
471
                } catch (BaseException e) {
472
                        return true;
473
                }
474
                return true;
475
        }
476

    
477
        public boolean doActionClose() {
478
                fireCloseEvent();
479
                return true;
480
        }
481
        
482
        public boolean isAutosave() {
483
                return this.autosave;
484
        }
485
        
486
        public void setAutosave(boolean autosave) {
487
                this.autosave = autosave;
488
        }
489
        
490
        public int confirmDialog(final String message, final String title, final int optionType,
491
                        final int messageType) {
492
                return JOptionPane.showConfirmDialog(
493
                                this.contents, message,title, optionType, messageType);
494
        }
495

    
496
        public boolean allowUpdate() {
497
                try {
498
                        return this.getButtonBar().isActionActive(this.getButtonBar().ActionSave);
499
                } catch (ServiceException e) {
500
                        logger.info("Button bar isn't initializated");
501
                }
502
                return false;
503
        }
504

    
505
        public boolean allowDelete() {
506
                try {
507
                        return this.getButtonBar().isActionActive(this.getButtonBar().ActionDelete);
508
                } catch (ServiceException e) {
509
                        logger.info("Button bar isn't initializated");
510
                }
511
                return false;
512
        }
513

    
514
        public boolean allowNew() {
515
                try {
516
                        return this.getButtonBar().isActionActive(this.getButtonBar().ActionNew);
517
                } catch (ServiceException e) {
518
                        logger.info("Button bar isn't initializated");
519
                }
520
                return false;
521
        }
522

    
523
        public boolean allowSearch() {
524
                try {
525
                        return this.getButtonBar().isActionActive(this.getButtonBar().ActionSearch);
526
                } catch (ServiceException e) {
527
                        logger.info("Button bar isn't initializated");
528
                }
529
                return false;
530
        }
531

    
532
        public void setAllowUpdate(boolean allowUpdate) {
533
                hasActionUpdate = allowUpdate;
534
//                try {
535
//                        this.getButtonBar().setActionActive(this.getButtonBar().ActionSave, allowUpdate);
536
//                } catch (ServiceException e) {
537
//                        // TODO Auto-generated catch block
538
//                        e.printStackTrace();
539
//                }
540
        }
541

    
542
        public void setAllowDelete(boolean allowDelete) {
543
                hasActionDelete = allowDelete;
544
//                try {
545
//                        this.getButtonBar().setActionActive(this.getButtonBar().ActionDelete, allowDelete);
546
//                } catch (ServiceException e) {
547
//                        // TODO Auto-generated catch block
548
//                        e.printStackTrace();
549
//                }
550
        }
551

    
552
        public void setAllowNew(boolean allowNew) {
553
                hasActionNew = allowNew;
554
        }
555

    
556
        public void setAllowSearch(boolean allowSearch) {
557
                hasActionSearch = allowSearch;
558
//                try {
559
//                        this.getButtonBar().setActionActive(this.getButtonBar().ActionSearch, allowSearch);
560
//                } catch (ServiceException e) {
561
//                        // TODO Auto-generated catch block
562
//                        e.printStackTrace();
563
//                }
564
        }
565

    
566
        public void setFormSize(int width, int height) {
567
                this.formHeight = height;
568
                this.formWidth = width;
569
                if(this.form != null){
570
                        this.form.setFormSize(width, height);
571
                }
572
        }
573

    
574
        public int getCurrentIndex() {
575
                return this.current;
576
        }
577

    
578
        public int countValues() {
579
                return this.values.size();
580
        }
581

    
582
        public void setCurrentIndex(int index) {
583
                if( index <0 || index > countValues() ) {
584
                        throw new IllegalArgumentException("Index ("+index+") out of range [0.."+countValues()+"].");
585
                }
586
                this.current = index;
587
                this.form.setValues((DynObject) this.values.get(this.current));
588
                this.buttonBar.setCurrent(this.current);
589
                this.fireFormMovedToEvent();
590
        }
591

    
592
        public DynObject get(int position) {
593
                return (DynObject) this.values.get(position);
594
        }
595

    
596
        public void addActionToPopupMenu(DataType tipo, String name, Action action) {
597
                try {
598
                        if(this.form==null){
599
                                this.actionsBuffer.add(new ActionStore(tipo, name, action));
600
                        }else{
601
                                this.form.addActionToPopupMenu(tipo, name, action);
602
                        }
603
                } catch( Exception ex) {
604
                        String s = (tipo==null)? "(null)" : tipo.getName();
605
                        logger.warn("Can't add popup menu '"+name+"' to the fields of type '"+s+"' of the form.", ex);
606
                }
607
        }
608

    
609
        public void addSeparatorToPopupMenu(DataType tipo) {
610
                try {
611
                        if(this.form==null){
612
                                this.actionsBuffer.add(new ActionStore(tipo));
613
                        }else{
614
                                this.form.addSeparatorToPopupMenu(tipo);
615
                        }
616
                } catch( Exception ex) {
617
                        String s = (tipo==null)? "(null)" : tipo.getName();
618
                        logger.warn("Can't add separator to the popup menu to the fields of type '"+s+"' of the form.", ex);
619
                }
620

    
621
        }                
622
        
623
        public void setUseScrollBars(boolean usesScrolls) {
624
                this.useScrollBars  = usesScrolls;
625
        }
626

    
627
        public boolean getUseScrollBars() {
628
                return useScrollBars;
629
        }
630
}