Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynformset / subform / SubformJDynFormSet.java @ 1405

History | View | Annotate | Download (21.7 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.tools.dynform.services.dynformset.subform;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.Color;
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
import javax.swing.Action;
33
import javax.swing.BorderFactory;
34

    
35
import javax.swing.JComponent;
36
import javax.swing.JLabel;
37
import javax.swing.JOptionPane;
38
import javax.swing.JPanel;
39
import org.gvsig.tools.ToolsLocator;
40

    
41
import org.gvsig.tools.dynform.AbortActionException;
42
import org.gvsig.tools.dynform.DynFormDefinition;
43
import org.gvsig.tools.dynform.JDynForm;
44
import org.gvsig.tools.dynform.JDynForm.JDynFormListener;
45
import org.gvsig.tools.dynform.JDynFormField;
46
import org.gvsig.tools.dynform.services.dynformset.subform.FormSetButtonBar.FormSetListener;
47
import org.gvsig.tools.dynform.spi.AbstractJDynFormSet;
48
import org.gvsig.tools.dynform.spi.ActionStore;
49
import org.gvsig.tools.dynobject.DynObject;
50
import org.gvsig.tools.dynobject.DynObjectSet;
51
import org.gvsig.tools.exception.BaseException;
52
import org.gvsig.tools.service.ServiceException;
53
import org.gvsig.tools.service.spi.ServiceManager;
54
import org.gvsig.tools.visitor.VisitCanceledException;
55
import org.gvsig.tools.visitor.Visitor;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

    
59
public class SubformJDynFormSet extends AbstractJDynFormSet implements FormSetListener, JDynFormListener {
60

    
61
    private static final Logger logger = LoggerFactory.getLogger(SubformJDynFormSet.class);
62

    
63
    private JLabel jlabel_messages = null;
64
    private JDynForm form = null;
65
    private FormSetButtonBar buttonBar = null;
66
    private int current = 0;
67

    
68
    public SubformJDynFormSet(ServiceManager manager, DynFormDefinition definition) throws ServiceException {
69
        super(manager, definition);
70
    }
71

    
72
    @Override
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
        this.fireFormMovedToEvent(current);
82
        return this.contents;
83
    }
84

    
85
    public JLabel getMessagesJLabel() {
86
        if (this.jlabel_messages == null) {
87
            this.jlabel_messages = new JLabel();
88
//                        this.jlabel_messages.setBorder( BorderFactory.createLoweredBevelBorder());
89
            this.jlabel_messages.setText(" ");
90
            this.jlabel_messages.addMouseListener(new MouseAdapter() {
91
                @Override
92
                public void mouseClicked(MouseEvent evt) {
93
                    int count = evt.getClickCount();
94
                    if (count == 2) {
95
                        JOptionPane.showMessageDialog(contents, jlabel_messages.getText(), "Status", JOptionPane.INFORMATION_MESSAGE);
96
                    }
97
                }
98
            });
99
        }
100
        return this.jlabel_messages;
101
    }
102

    
103
    @Override
104
    public void message() {
105
        this.getMessagesJLabel().setText(" ");
106
    }
107

    
108
    @Override
109
    public void message(String msg) {
110
        this.getMessagesJLabel().setText(msg);
111
    }
112

    
113
    private FormSetButtonBar getButtonBar() throws ServiceException {
114
        if (this.buttonBar == null) {
115
            initComponents();
116
        }
117
        return this.buttonBar;
118
    }
119

    
120
    private void initComponents() throws ServiceException {
121
        this.contents = new JPanel();
122
        this.contents.setBorder(BorderFactory.createLineBorder(Color.GRAY));
123
        this.contents.setLayout(new BorderLayout());
124

    
125
        this.form = this.manager.getDynFormManager().createJDynForm(definition);
126
        this.form.setUseScrollBars(this.getUseScrollBars());
127
        this.form.addListener(this);
128

    
129
        if (!actionsBuffer.isEmpty()) {
130
            for (ActionStore actStore : actionsBuffer) {
131
                if (actStore.isSeparator()) {
132
                    form.addSeparatorToPopupMenu(
133
                            actStore.getDataType());
134
                } else {
135
                    form.addActionToPopupMenu(
136
                            actStore.getDataType(),
137
                            actStore.getActionName(),
138
                            actStore.getAction());
139
                }
140
            }
141
        }
142
        if (this.getUseScrollBars()) {
143
            if (this.formHeight > -1 && this.formWidth > -1) {
144
                this.setFormSize(this.formWidth, this.formHeight);
145
            }
146
        }
147
        this.form.setShowMessageStatus(false);
148
        //this.form.setLayoutMode(this.layoutMode);
149
        this.form.addListener(this);
150

    
151
        this.buttonBar = new FormSetButtonBar();
152
        this.buttonBar.addListener(this);
153
        this.buttonBar.setActionActive(FormSetButtonBar.ActionDelete, this.allowDelete());
154
        this.buttonBar.setActionActive(FormSetButtonBar.ActionNew, this.allowNew());
155
        this.buttonBar.setActionActive(FormSetButtonBar.ActionCancelNew, this.allowNew());
156
        this.buttonBar.setActionActive(FormSetButtonBar.ActionSave, this.allowUpdate());
157
        this.buttonBar.setActionActive(FormSetButtonBar.ActionSearch, this.allowSearch());
158
        this.buttonBar.setActionActive(FormSetButtonBar.ActionClose, this.allowClose());
159
        this.buttonBar.setVisible(FormSetButtonBar.ActionCancelNew, false);
160
        this.contents.add(this.buttonBar.asJComponent(), BorderLayout.NORTH);
161
        this.contents.add(this.form.asJComponent(), BorderLayout.CENTER);
162
        this.contents.add(this.getMessagesJLabel(), BorderLayout.SOUTH);
163
        doChangeValues();
164
    }
165

    
166
    @Override
167
    public void setAllowDelete(boolean allowDelete) {
168
        super.setAllowDelete(allowDelete);
169
        if( this.buttonBar!=null ) {
170
            this.buttonBar.setActionActive(FormSetButtonBar.ActionDelete, this.allowDelete());
171
        }
172
    }
173

    
174
    @Override
175
    public void setAllowNew(boolean allowNew) {
176
        super.setAllowNew(allowNew);
177
        if( this.buttonBar!=null ) {
178
            this.buttonBar.setActionActive(FormSetButtonBar.ActionNew, this.allowNew());
179
        }
180
    }
181
    
182
    private void doChangeValues() throws ServiceException {
183
        this.current = 0;
184
        if (this.values == null || this.values.isEmpty()) {
185
            this.form.setReadOnly(true);
186
            this.getButtonBar().setEnabled(FormSetButtonBar.ActionDelete, false);
187
            this.getButtonBar().setEnabled(FormSetButtonBar.ActionSave, false);
188
            this.getButtonBar().setEnabled(FormSetButtonBar.ActionNew,
189
                    this.allowNew() && !this.isReadOnly());
190
            this.getButtonBar().setNumrecords(0);
191
            this.form.clear();
192
        } else {
193
            this.form.setValues((DynObject) this.values.get(this.current));
194
            this.getButtonBar().setEnabled(FormSetButtonBar.ActionDelete,
195
                    this.allowDelete() && !this.isReadOnly());
196
            this.getButtonBar().setEnabled(FormSetButtonBar.ActionSave,
197
                    this.allowUpdate() && !this.isReadOnly());
198
            this.getButtonBar().setNumrecords(this.values.size());
199
        }
200
        this.getButtonBar().setCurrent(this.current);
201
    }
202

    
203
    @Override
204
    public void setValues(List values) throws ServiceException {
205
        super.setValues(values);
206
        if (this.contents != null) {
207
            doChangeValues();
208
        }
209
    }
210

    
211
    @Override
212
    public void setValues(DynObjectSet values) throws ServiceException {
213
        super.setValues(values);
214
        if (this.contents != null) {
215
            doChangeValues();
216
        }
217
    }
218

    
219
    @Override
220
    public void setReadOnly(boolean readOnly) {
221
        super.setReadOnly(readOnly);
222
        if (this.buttonBar != null) {
223
            this.buttonBar.setReadOnly(readOnly);
224
        }
225
        if (this.form != null) {
226
            this.form.setReadOnly(readOnly);
227
        }
228
    }
229

    
230
    @Override
231
    public boolean doActionFirst() {
232
        if (this.values.isEmpty()) {
233
            return false;
234
        }
235

    
236
        if (autosave) {
237
            if (this.form.isModified() || this.isInNewState() ) {
238
                if (!doActionSave()) {
239
                    return false;
240
                }
241
            }
242
        }
243
        this.current = 0;
244
        this.form.setValues((DynObject) this.values.get(this.current));
245
        this.buttonBar.setCurrent(this.current);
246
        try {
247
            this.getButtonBar().setEnabled(FormSetButtonBar.ActionDelete,
248
                    this.allowDelete() && !this.isReadOnly());
249
            this.getButtonBar().setEnabled(FormSetButtonBar.ActionSave,
250
                    this.allowUpdate() && !this.isReadOnly());
251
        } catch (Exception ex) {
252
            logger.warn("Can't enable/disable delete and save buttons",ex);
253
        }
254
        this.fireFormMovedToEvent(this.current);
255
        return true;
256
    }
257

    
258
    @Override
259
    public boolean doActionPrevious() {
260
        if (this.values.isEmpty()) {
261
            return false;
262
        }
263

    
264
        if (autosave) {
265
            if (this.form.isModified() || this.isInNewState() ) {
266
                if (!doActionSave()) {
267
                    return false;
268
                }
269
            }
270
        }
271
        if (this.current > 0) {
272
            this.current--;
273
        }
274
        this.form.setValues((DynObject) this.values.get(this.current));
275
        this.buttonBar.setCurrent(this.current);
276
        this.fireFormMovedToEvent(this.current);
277
        return true;
278
    }
279

    
280
    @Override
281
    public boolean doActionNext() {
282
        if (this.values.isEmpty()) {
283
            return false;
284
        }
285

    
286
        if (autosave) {
287
            if (this.form.isModified() || this.isInNewState() ) {
288
                if (!doActionSave()) {
289
                    return false;
290
                }
291
            }
292
        }
293
        if (this.current < this.values.size() - 1) {
294
            this.current++;
295
        }
296
        this.form.setValues((DynObject) this.values.get(this.current));
297
        this.buttonBar.setCurrent(this.current);
298
        this.fireFormMovedToEvent(this.current);
299
        return true;
300
    }
301

    
302
    @Override
303
    public boolean doActionLast() {
304
        if (this.values.isEmpty()) {
305
            return false;
306
        }
307

    
308
        if (autosave) {
309
            if (this.form.isModified() || this.isInNewState() ) {
310
                if (!doActionSave()) {
311
                    return false;
312
                }
313
            }
314
        }
315
        this.current = this.values.size() - 1;
316
        this.form.setValues((DynObject) this.values.get(this.current));
317
        this.buttonBar.setCurrent(this.current);
318
        this.fireFormMovedToEvent(this.current);
319
        return true;
320
    }
321

    
322
    @Override
323
    public boolean doActionSave() {
324
        if ( !this.isInNewState() && this.values.isEmpty()) {
325
            return false;
326
        }
327
        try {
328
            this.listeners.accept(new Visitor() {
329
                @Override
330
                public void visit(Object listener) throws VisitCanceledException, BaseException {
331
                    ((JDynFormSetListener) listener).formBeforeSave(SubformJDynFormSet.this);
332
                }
333
            });
334
        } catch (AbortActionException e) {
335
            return false;
336
        } catch (BaseException e) {
337
            logger.warn("Can't notify to listeners before save.",e);
338
            return false;
339
        }
340

    
341
        List<String> fieldsName = new ArrayList<>();
342
        if (!this.form.hasValidValues(fieldsName)) {
343
            String errores = "";
344
            Iterator<String> it = fieldsName.iterator();
345
            while (it.hasNext()) {
346
                String name = (String) it.next();
347
                errores = errores + "     - " + name + "\n";
348
            }
349
            int r = confirmDialog(
350
                    "There are incorrect data in the following fields:\n" + errores + "\nContinuing the incorrect values ​will not be saved. Do you want to continue?", "warning",
351
                    JOptionPane.WARNING_MESSAGE,
352
                    JOptionPane.YES_NO_OPTION);
353
            if (r != JOptionPane.YES_OPTION) {
354
                return false;
355
            }
356
        }
357
        if( !this.isInNewState() ) {
358
            if (this.current >= 0 && this.current < this.values.size()) {
359
                this.form.getValues((DynObject) this.values.get(this.current));
360
            } else {
361
                logger.warn("current out of range.");
362
            }
363
        }
364
        try {
365
            this.listeners.accept(new Visitor() {
366
                @Override
367
                public void visit(Object listener) throws VisitCanceledException, BaseException {
368
                    ((JDynFormSetListener) listener).formAfterSave(SubformJDynFormSet.this);
369
                }
370
            });
371
        } catch (AbortActionException e) {
372
            return false;
373
        } catch (BaseException e) {
374
            logger.warn("Can't notify to listeners after save.",e);
375
            return false;
376
        }
377
        this.leaveTheNewState();
378
        return true;
379
    }
380

    
381
    @Override
382
    public boolean doActionNew() {
383
        try {
384
            this.listeners.accept(new Visitor() {
385
                @Override
386
                public void visit(Object listener) throws VisitCanceledException, BaseException {
387
                    ((JDynFormSetListener) listener).formBeforeNew(SubformJDynFormSet.this);
388
                }
389
            });
390
        } catch (BaseException e) {
391
            return true;
392
        }
393
        this.enterStateNew();
394
        try {
395
            this.listeners.accept(new Visitor() {
396
                @Override
397
                public void visit(Object listener) throws VisitCanceledException, BaseException {
398
                    ((JDynFormSetListener) listener).formAfterNew(SubformJDynFormSet.this);
399
                }
400
            });
401
        } catch (BaseException e) {
402
            return true;
403
        }
404
        return true;
405
    }
406

    
407
    @Override
408
    public boolean doActionCancelNew() {
409
        try {
410
            this.listeners.accept(new Visitor() {
411
                @Override
412
                public void visit(Object listener) throws VisitCanceledException, BaseException {
413
                    ((JDynFormSetListener) listener).formBeforeCancelNew(SubformJDynFormSet.this);
414
                }
415
            });
416
        } catch (BaseException e) {
417
            return true;
418
        }
419
        this.leaveTheNewState();
420
        try {
421
            this.listeners.accept(new Visitor() {
422
                @Override
423
                public void visit(Object listener) throws VisitCanceledException, BaseException {
424
                    ((JDynFormSetListener) listener).formAfterCancelNew(SubformJDynFormSet.this);
425
                }
426
            });
427
        } catch (BaseException e) {
428
            return true;
429
        }
430
        return true;
431
    }
432

    
433
    @Override
434
    protected void enterStateNew() {
435
        if( this.isInNewState() ) {
436
            return;
437
        }
438
        try {
439
            FormSetButtonBar bar = this.getButtonBar();
440
            super.enterStateNew();
441
            bar.setCurrentLabel(ToolsLocator.getI18nManager().getTranslation("nuevo"));
442
            bar.setNumrecords(this.countValues());
443

    
444
            bar.setVisible(FormSetButtonBar.ActionCancelNew, true);
445
            bar.setVisible(FormSetButtonBar.ActionNew, false);
446
            
447
            bar.setEnabled(FormSetButtonBar.ActionSave,true);
448
            bar.setEnabled(FormSetButtonBar.ActionCancelNew,true);
449
            bar.setEnabled(FormSetButtonBar.ActionDelete,false);
450
            bar.setEnabled(FormSetButtonBar.ActionFirst,false);
451
            bar.setEnabled(FormSetButtonBar.ActionNext,false);
452
            bar.setEnabled(FormSetButtonBar.ActionPrevious,false);
453
            bar.setEnabled(FormSetButtonBar.ActionLast,false);
454
            bar.setEnabled(FormSetButtonBar.ActionSearch,false);
455
        } catch (Exception ex) {
456
            logger.warn("Can't enable/disable delete and save buttons",ex);
457
        }
458
    }
459

    
460
    @Override
461
    protected void leaveTheNewState() {
462
        if( !(this.isInNewState()) ) {
463
            return;
464
        }
465
        super.leaveTheNewState();
466
        try {
467
            FormSetButtonBar bar = this.getButtonBar();
468

    
469
            bar.setVisible(FormSetButtonBar.ActionCancelNew, false);
470
            bar.setVisible(FormSetButtonBar.ActionNew, true);
471
            
472
            bar.setEnabled(FormSetButtonBar.ActionSave,true);
473
            bar.setEnabled(FormSetButtonBar.ActionNew,true);
474
            bar.setEnabled(FormSetButtonBar.ActionDelete,true);
475
            bar.setEnabled(FormSetButtonBar.ActionSearch,true);
476

    
477
            bar.setCurrentLabel(null);
478
            bar.setNumrecords(this.countValues());
479
            bar.setCurrent(this.current);
480
        } catch (Exception ex) {
481
            logger.warn("Can't enable/disable delete and save buttons",ex);
482
        }
483
    }
484
    
485
    @Override
486
    public boolean doActionDelete() {
487
        if (this.values.isEmpty()) {
488
            return false;
489
        }
490

    
491
        if (this.countValues() < 1) {
492
            return true;
493
        }
494
        try {
495
            this.listeners.accept(new Visitor() {
496
                @Override
497
                public void visit(Object listener) throws VisitCanceledException, BaseException {
498
                    ((JDynFormSetListener) listener).formBeforeDelete(SubformJDynFormSet.this);
499
                }
500
            });
501
        } catch (BaseException e) {
502
            return true;
503
        }
504

    
505
        if (this.current >= countValues()) {
506
            this.current = countValues() - 1;
507
        }
508
        if (this.current > -1) {
509
            this.form.setValues((DynObject) this.values.get(this.current));
510
        }
511
        this.buttonBar.setNumrecords(this.countValues());
512
        this.buttonBar.setCurrent(this.current);
513

    
514
        try {
515
            this.listeners.accept(new Visitor() {
516
                @Override
517
                public void visit(Object listener) throws VisitCanceledException, BaseException {
518
                    ((JDynFormSetListener) listener).formAfterDelete(SubformJDynFormSet.this);
519
                }
520
            });
521
        } catch (BaseException e) {
522
            return true;
523
        }
524
        return true;
525
    }
526

    
527
    @Override
528
    public boolean doActionSearch() {
529
        if (this.values.isEmpty()) {
530
            return false;
531
        }
532

    
533
        try {
534
            this.listeners.accept(new Visitor() {
535
                @Override
536
                public void visit(Object listener) throws VisitCanceledException, BaseException {
537
                    ((JDynFormSetListener) listener).formBeforeSearch(SubformJDynFormSet.this);
538
                }
539
            });
540
        } catch (BaseException e) {
541
            return true;
542
        }
543
        // Do search
544
        try {
545
            this.listeners.accept(new Visitor() {
546
                @Override
547
                public void visit(Object listener) throws VisitCanceledException, BaseException {
548
                    ((JDynFormSetListener) listener).formAfterSearch(SubformJDynFormSet.this);
549
                }
550
            });
551
        } catch (BaseException e) {
552
            return true;
553
        }
554
        return true;
555
    }
556

    
557
    @Override
558
    public boolean doActionClose() {
559
        fireCloseEvent();
560
        return true;
561
    }
562

    
563
    @Override
564
    public void doSetCurrentRecord(int index) {
565
        this.setCurrentIndex(index);
566
    }
567
    
568
    @Override
569
    public void setFormSize(int width, int height) {
570
        super.setFormSize(width, height);
571
        if (this.form != null) {
572
            this.form.setFormSize(width, height);
573
        }
574
    }
575

    
576
    @Override
577
    public int getCurrentIndex() {
578
        return this.current;
579
    }
580

    
581
    @Override
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(this.current);
590
    }
591

    
592
    @Override
593
    public void fieldChanged(JDynFormField field) {
594
        logger.debug("Ha cambiado el field: " + field.getName() + " con el valor => " + field.getValue());
595
    }
596

    
597
    @Override
598
    public DynObject get(int position) {
599
        logger.debug("Recojo el field: " + position);
600
        return (DynObject) this.values.get(position);
601
    }
602

    
603
    @Override
604
    public List getValues() {
605
        if (this.current >= 0 && this.current < this.values.size()) {
606
            this.form.getValues((DynObject) this.values.get(this.current));
607
        }
608
        return this.values;
609
    }
610

    
611
    @Override
612
    public boolean hasValidValues() {
613
        return this.form.hasValidValues();
614
    }
615

    
616
    @Override
617
    public boolean hasValidValues(List<String> fieldsName) {
618
        return this.form.hasValidValues(fieldsName);
619
    }
620

    
621
    public void clear() {
622
        if (this.form != null) {
623
            this.form.clear();
624
        }
625
    }
626

    
627
    @Override
628
    public void getFormValues(DynObject values) {
629
        this.form.getValues(values);
630
    }
631

    
632
    @Override
633
    public void addAction(Action action) {
634
        try {
635
            this.getButtonBar().addAction(action);
636
        } catch (ServiceException ex) {
637
            throw new RuntimeException("Can't add action to JDymFormSet", ex);
638
        }
639
    }
640
}