Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / featureform / swing / impl / DefaultJFeaturesForm.java @ 44510

History | View | Annotate | Download (31 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.featureform.swing.impl;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.Dimension;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ComponentAdapter;
29
import java.awt.event.ComponentEvent;
30
import java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.List;
33
import javax.swing.AbstractAction;
34
import javax.swing.Action;
35
import static javax.swing.Action.ACTION_COMMAND_KEY;
36
import static javax.swing.Action.NAME;
37
import static javax.swing.Action.SHORT_DESCRIPTION;
38
import static javax.swing.Action.SMALL_ICON;
39

    
40
import javax.swing.JComponent;
41
import javax.swing.JOptionPane;
42
import javax.swing.JPanel;
43
import org.apache.commons.lang3.StringUtils;
44
import org.gvsig.expressionevaluator.Expression;
45
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
46
import org.gvsig.expressionevaluator.ExpressionUtils;
47

    
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
import org.gvsig.featureform.swing.JFeaturesForm;
52
import org.gvsig.fmap.dal.DALLocator;
53
import org.gvsig.fmap.dal.DataStore;
54
import org.gvsig.fmap.dal.StoresRepository;
55
import org.gvsig.fmap.dal.exception.DataException;
56
import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator;
57
import org.gvsig.fmap.dal.feature.EditableFeature;
58
import org.gvsig.fmap.dal.feature.Feature;
59
import org.gvsig.fmap.dal.feature.FacadeOfAFeature;
60
import org.gvsig.fmap.dal.feature.FeatureQuery;
61
import org.gvsig.fmap.dal.feature.FeatureStore;
62
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
63
import org.gvsig.fmap.dal.feature.FeatureType;
64
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
65
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext;
66
import org.gvsig.fmap.dal.swing.DALActionFactory;
67
import org.gvsig.fmap.dal.swing.DALSwingLocator;
68
import org.gvsig.fmap.dal.swing.DataSwingManager;
69
import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager;
70
import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory;
71
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel;
72
import org.gvsig.tools.ToolsLocator;
73
import org.gvsig.tools.dispose.DisposeUtils;
74
import org.gvsig.tools.dynform.AbortActionException;
75
import org.gvsig.tools.dynform.DynFormDefinition;
76
import org.gvsig.tools.dynform.DynFormLocator;
77
import org.gvsig.tools.dynform.DynFormManager;
78
import org.gvsig.tools.dynform.JDynForm;
79
import org.gvsig.tools.dynform.JDynFormSet;
80
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CANCEL_NEW;
81
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_DELETE;
82
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW;
83
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE;
84
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SEARCH;
85
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener;
86
import org.gvsig.tools.dynobject.DynClass;
87
import org.gvsig.tools.dynobject.DynObject;
88
import org.gvsig.tools.evaluator.Evaluator;
89
import org.gvsig.tools.exception.BaseException;
90
import org.gvsig.tools.i18n.I18nManager;
91
import org.gvsig.tools.observer.Observable;
92
import org.gvsig.tools.observer.Observer;
93
import org.gvsig.tools.service.ServiceException;
94
import org.gvsig.tools.swing.api.ToolsSwingLocator;
95
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
96
import org.gvsig.tools.swing.api.windowmanager.Dialog;
97
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
98
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
99
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
100
import org.gvsig.tools.swing.icontheme.IconTheme;
101
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
102
import org.gvsig.tools.script.ScriptManager;
103

    
104
/**
105
 * @author fdiaz
106
 *
107
 */
108
@SuppressWarnings("UseSpecificCatch")
109
public class DefaultJFeaturesForm implements JFeaturesForm {
110

    
111
    private final String STARTEDITING_ACTION = "startEditing";
112
    private final String FINISHEDITING_ACTION = "finishEditing";
113

    
114

    
115
    public class DefaultFeaturesFormContext implements FeaturesFormContext {
116

    
117
        private DefaultFeaturesFormContext() {
118
            
119
        }
120

    
121
        @Override
122
        public FeatureStore getFeatureStore() {
123
            return store;
124
        }
125
        
126
        @Override
127
        public FeatureType getFeatureType() {
128
            try {
129
                return store.getDefaultFeatureType();
130
            } catch (DataException ex) {
131
                return null;
132
            }
133
        }
134
        
135
        @Override
136
        public ResourcesStorage getResourcesStorage() {
137
            if( store == null ) {
138
                return null;
139
            }
140
            return store.getResourcesStorage();
141
        }
142

    
143
        @Override
144
        public StoresRepository getStoresRepository() {
145
            if( store == null ) {
146
                return null;
147
            }
148
            return store.getStoresRepository();
149
        }
150

    
151
        @Override
152
        public ScriptManager getScriptManager() {
153
            return ExpressionEvaluatorLocator.getExpressionEvaluatorManager();
154
        }
155

    
156
        
157
    }
158
    
159
    private class FormActionContext extends AbstractDALActionContext {
160

    
161
        public FormActionContext() {
162
            super(ACTION_CONTEXT_NAME);
163
        }
164
        
165
        @Override
166
        public DataStore getStore() {
167
            return store;
168
        }
169

    
170
        @Override
171
        public Expression getFilter() {
172
            FeatureQuery theCurrentQuery = currentQuery;
173
            if( theCurrentQuery!=null ) {
174
                Evaluator filter = theCurrentQuery.getFilter();
175
                if( filter instanceof ExpressionEvaluator ) {
176
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
177
                    return expression;
178
                }
179
            }
180
            return null;
181
        }
182

    
183
        @Override
184
        public int getSelectedsCount() {
185
            return 0;
186
        }
187

    
188
        @Override
189
        public Expression getFilterForSelecteds() {
190
            return null;
191
        }
192

    
193
        @Override
194
        public JComponent getActionButton(String actionName) {
195
            if( formset == null ) {
196
                return null;
197
            }
198
            return formset.getActionButton(actionName);
199
        }
200
        
201
    }
202
    
203
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class);
204

    
205
    private static final int PAGE_SIZE = 500;
206
    private JPanel panel;
207
    private JDynFormSet formset;
208
    private FeatureStore store;
209
    private FeaturePagingHelper features;
210
    private DynFormDefinition definition = null;
211
    private FeatureQuery currentQuery;
212
    private List<Action> otherActions;
213

    
214
    public DefaultJFeaturesForm() {
215
        this.otherActions = new ArrayList<>();
216
        this.panel = new JPanel(new BorderLayout());
217
        this.panel.addComponentListener(new ComponentAdapter() {
218
            @Override
219
            public void componentHidden(ComponentEvent e) {
220
                doDispose();
221
            }
222
        });
223
    }
224

    
225
    @Override
226
    public void setPreferredSize(Dimension dimension) {
227
//        panel.setPreferredSize(dimension);
228
    }
229

    
230
    private void updateForm() {
231
        if (this.formset == null) {
232
            this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
233
        }
234
        try {
235
            if( this.currentQuery!=null ) {
236
                this.currentQuery.retrievesAllAttributes();
237
            }
238
            this.features = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE);
239
            this.formset.setValues(features.asListOfDynObjects());
240
        } catch (Exception ex) {
241
            throw new RuntimeException("Can't update form", ex);
242
        }
243
        updateButtonEnabledStatus();
244
    }
245

    
246
    @Override
247
    public JComponent asJComponent() {
248
        if (this.features == null) {
249
            try {
250
                updateForm();
251
            } catch (Exception ex) {
252
                throw new RuntimeException(ex);
253
            }
254
        }
255
        return this.panel;
256
    }
257

    
258
    @Override
259
    public void bind(FeatureStore store) {
260
        if (store == null) {
261
            throw new IllegalArgumentException("bind need a store as parameter, not a null.");
262
        }
263
        try {
264
            DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager();
265
            DynClass theDefinition = manager.featureType2DynClass(store, store.getDefaultFeatureType());
266
            this.bind(store, theDefinition);
267
        } catch (Exception ex) {
268
            throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex);
269
        }
270
    }
271

    
272
    public void bind(FeatureStore store, DynClass definition) throws ServiceException, DataException {
273
        if (this.store == store) {
274
            return;
275
        }
276
        DynFormManager formManager = DynFormLocator.getDynFormManager();
277
        this.definition = formManager.getDefinition(definition);
278
        if (formset != null) {
279
            this.panel.remove(formset.asJComponent());
280
            this.formset = null;
281
        }
282
        this.store = store;
283
        DisposeUtils.bind(store);
284
        this.features = null;
285
    }
286

    
287
    private void doDispose() {
288
        DisposeUtils.disposeQuietly(store);
289
        this.store = null;
290
        this.panel = null;
291
        this.formset = null;
292
        this.features = null;
293
        this.definition = null;
294
        this.currentQuery = null;
295
        this.otherActions = null;
296
    }
297
    
298
    @Override
299
    public JDynFormSet getFormset() {
300
        if (this.formset == null) {
301
            DynFormManager formManager = DynFormLocator.getDynFormManager();
302
            this.formset = formManager.createJDynFormSet(
303
                    new DefaultFeaturesFormContext(),
304
                    this.definition,
305
                    null
306
            );
307
            List<String> groups = this.definition.getGroups();
308
            if( groups.size()==1 && groups.get(0)==null ) {
309
                this.formset.setLayoutMode(JDynForm.USE_PLAIN);
310
            } else {
311
                this.formset.setLayoutMode(JDynForm.USE_TABS);
312
            }
313
            this.formset.setAllowNew(true);
314
            this.formset.setAllowDelete(true);
315
            this.formset.setAllowUpdate(true);
316
            this.formset.setAllowClose(true);
317
            this.formset.setAllowSearch(true);
318
            this.formset.setAutosave(true);
319

    
320
            this.formset.addAction(new StartEditingAction());
321
            this.formset.addAction(new FinishEditingAction());
322
            
323
            FormActionContext actionContext = new FormActionContext();
324
            Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
325
            for (DALActionFactory factory : factories) {
326
                if( StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME) ) {
327
                    continue;
328
                }
329
                Action action = factory.createAction(actionContext);
330
                this.formset.addAction(action);
331
            }            
332
            for( Action action : this.otherActions ) {
333
                this.formset.addAction(action);
334
            }
335

    
336
            this.formset.addListener(new FormSetListener());
337
        }
338
        updateButtonEnabledStatus();
339
        return this.formset;
340
    }
341

    
342
    @Override
343
    public void addAction(Action action) {
344
        this.otherActions.add(action);
345
        if( this.formset!=null ) {
346
            this.formset.addAction(action);
347
        }
348
    }
349

    
350
    @Override
351
    public long getCurrentIndex() {
352
        if( this.formset==null ) {
353
            return -1;
354
        }
355
        return this.formset.getCurrentIndex();
356
    }
357

    
358
    @Override
359
    public Feature get(long index) {
360
        if( this.formset==null || this.features==null ) {
361
            return null;
362
        }
363
        try {
364
            return this.features.getFeatureAt(index);
365
        } catch (BaseException ex) {
366
            return null;
367
        }
368
    }
369

    
370
    private class FinishEditingAction extends AbstractAction implements Observer {
371

    
372
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
373
        public FinishEditingAction() {
374
            I18nManager i18nManager = ToolsLocator.getI18nManager();
375
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
376

    
377
            this.putValue(NAME,null);
378
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing"));
379
            this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
380
            this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
381

    
382
            this.setEnabled(store.isEditing());
383
            store.addObserver(this);
384
        }
385

    
386
        @Override
387
        public void actionPerformed(ActionEvent ae) {
388
            if( store.isEditing() ) {
389
                try {
390
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
391
                    ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
392
                    int x = dialogManager.confirmDialog(
393
                            "? Desea terminar edicion y guardar los cambios ?\n\nPulse cancelar para cancelar la edicion y los cambios.",
394
                            i18nManager.getTranslation("_Stop_editing"),
395
                            JOptionPane.YES_NO_CANCEL_OPTION,
396
                            JOptionPane.QUESTION_MESSAGE
397
                    );
398
                    switch(x) {
399
                        case JOptionPane.YES_OPTION:
400
                            store.finishEditing();
401
                            break;
402
                        case JOptionPane.NO_OPTION:
403
                            break;
404
                        case JOptionPane.CANCEL_OPTION:
405
                            store.cancelEditing();
406
                            break;
407
                    }
408
                } catch (DataException ex) {
409
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
410
                }
411
            }
412
            updateButtonEnabledStatus();
413
        }
414

    
415
        @Override
416
        public void update(Observable observable, Object notification) {
417
            if( store == null || formset==null ) {
418
                return;
419
            }
420
            if( notification instanceof FeatureStoreNotification ) {
421
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
422
                switch( n.getType() )  {
423
                    case FeatureStoreNotification.AFTER_STARTEDITING:
424
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
425
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
426
                        updateButtonEnabledStatus();
427
                        break;
428
                }
429
            }
430
        }
431

    
432
    }
433
    
434
    private class StartEditingAction extends AbstractAction implements Observer {
435

    
436
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
437
        public StartEditingAction() {
438
            I18nManager i18nManager = ToolsLocator.getI18nManager();
439
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
440

    
441
            this.putValue(NAME,null);
442
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Start_editing"));
443
            this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
444
            this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
445

    
446
            this.setEnabled(!store.isEditing());
447
            store.addObserver(this);
448
        }
449

    
450
        @Override
451
        public void actionPerformed(ActionEvent ae) {
452
            if( !store.isEditing() ) {
453
                try {
454
                    store.edit();
455
                } catch (DataException ex) {
456
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
457
                }
458
            }
459
            updateButtonEnabledStatus();
460
        }
461

    
462
        @Override
463
        public void update(Observable observable, Object notification) {
464
            if( store == null || formset==null ) {
465
                return;
466
            }
467
            if( notification instanceof FeatureStoreNotification ) {
468
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
469
                switch( n.getType() )  {
470
                    case FeatureStoreNotification.AFTER_STARTEDITING:
471
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
472
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
473
                        updateButtonEnabledStatus();
474
                        break;
475
                }
476
            }
477
        }
478

    
479
    }
480

    
481
    @Override
482
    public void setQuery(FeatureQuery query) {
483
        if (this.features != null) {
484
            if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) {
485
                if (!store.isEditing()) {
486
                    try {
487
                        store.edit();
488
                    } catch (DataException e1) {
489
                        throw new RuntimeException("Can't set query", e1);
490
                    }
491
                }
492
                saveChanges(this.formset);
493
            }
494
        }
495
        this.currentQuery = query;
496
        updateForm();
497
    }
498

    
499
    private FeatureQuery getCurrentQuery() {
500
        return this.currentQuery;
501
    }
502
    
503
    @Override
504
    public void showForm(MODE mode) {
505
        this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
506
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
507
        String title = this.definition.getLabel();
508
        winmgr.showWindow(this.asJComponent(), title, mode);
509
    }
510

    
511
    private void saveChanges(JDynFormSet theFormSet) {
512
        I18nManager i18n = ToolsLocator.getI18nManager();
513
        try {
514
            if( theFormSet.isInNewState() ) {
515
                Feature newFeature = store.createNewFeature(false);
516
                DynObject newElement = newFeature.getAsDynObject();
517
                theFormSet.getFormValues(newElement);
518
                features.insert(((FacadeOfAFeature)newElement).getEditableFeature());
519
                this.formset.message(i18n.getTranslation("_Record_saved"));
520
                try {
521
                    this.formset.setValues(features.asListOfDynObjects());
522
                    this.formset.setCurrentIndex((int)(features.getTotalSize())-1);
523
                } catch(Exception ex) {
524
                    LOGGER.warn("Can't reload form data after insert.",ex);
525
                }
526
            } else {
527
                int index = theFormSet.getCurrentIndex();
528
                DynObject currentElement = theFormSet.get(index);
529
                theFormSet.getFormValues(currentElement);
530
                features.update(((FacadeOfAFeature)currentElement).getEditableFeature());
531
                this.formset.message(i18n.getTranslation("_Record_saved"));
532
            }
533
        } catch(Exception ex) {
534
            theFormSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
535
            throw new RuntimeException("Can't save values",ex);
536
            
537
        } finally {
538
            updateButtonEnabledStatus();
539
        }
540

    
541
    }
542
    private void deleteCurrentElement(JDynFormSet theFormSet) {
543
        I18nManager i18n = ToolsLocator.getI18nManager();
544
        try {
545
            int index = theFormSet.getCurrentIndex();
546
            DynObject currentElement = theFormSet.get(index);
547
            theFormSet.getFormValues(currentElement);
548
            Feature feature = ((FacadeOfAFeature) currentElement).getFeature();
549
            if (feature instanceof EditableFeature) {
550
                feature = ((EditableFeature) feature).getNotEditableCopy();
551
            }
552
            features.delete(feature);
553
            this.formset.message(i18n.getTranslation("_Record_removed"));
554

    
555
        } catch (Exception ex) {
556
            theFormSet.message(i18n.getTranslation("error_removing_data_will_not_remove"));
557
            throw new RuntimeException("Can't remove values", ex);
558

    
559
        } finally {
560
            updateButtonEnabledStatus();
561
        }
562

    
563
    }
564
    @Override
565
    public void saveChanges() {
566
        if (this.formset != null && this.formset.countValues() > 0) {
567
            if (store != null && !store.isEditing()) {
568
                try {
569
                    store.edit();
570
                } catch (DataException e1) {
571
                    LOGGER.warn("Can't edit the store " + store.getName());
572
                    throw new RuntimeException("Can't save changes.", e1);
573
                }
574
            }
575
            this.saveChanges(this.formset);
576
        }
577
    }
578

    
579
    private void updateButtonEnabledStatus() {
580
        if( this.formset == null ) {
581
            return;
582
        }
583
        if( this.store == null || store.isBroken() || store.isAppending() ) {
584
            this.formset.setReadOnly(true);
585
            formset.setActionVisible(STARTEDITING_ACTION, true);
586
            formset.setActionEnabled(STARTEDITING_ACTION, true);
587
            formset.setActionVisible(FINISHEDITING_ACTION, false);
588
            formset.setActionEnabled(FINISHEDITING_ACTION, false);
589
            formset.setActionEnabled(ACTION_DELETE, false);
590
            formset.setActionEnabled(ACTION_NEW,false);
591
            formset.setActionEnabled(ACTION_CANCEL_NEW,false);
592
            formset.setActionEnabled(ACTION_SAVE,false);
593
            formset.setActionEnabled(ACTION_SEARCH,false);
594
            return;
595
        }
596
        if( store.isEditing() ) {
597
            this.formset.setReadOnly(false);
598
            formset.setActionVisible(STARTEDITING_ACTION, false);
599
            formset.setActionEnabled(STARTEDITING_ACTION, false);
600
            formset.setActionVisible(FINISHEDITING_ACTION, true);
601
            formset.setActionEnabled(FINISHEDITING_ACTION, true);
602
            if( formset.isInNewState() ) {
603
                formset.setActionEnabled(ACTION_DELETE, false);
604
                formset.setActionEnabled(ACTION_SEARCH,false);
605
                formset.setActionEnabled(ACTION_NEW,false);
606
                formset.setActionEnabled(ACTION_CANCEL_NEW,true);
607
                formset.setActionEnabled(ACTION_SAVE,true);
608
                
609
            } else if( this.features!=null && this.features.isEmpty() ) {
610
                formset.setActionEnabled(ACTION_DELETE, false);
611
                formset.setActionEnabled(ACTION_SEARCH,false);
612
                formset.setActionEnabled(ACTION_NEW,true);
613
                formset.setActionEnabled(ACTION_CANCEL_NEW,true);
614
                formset.setActionEnabled(ACTION_SAVE,false);
615

    
616
            } else if( formset.getForm().isModified() ) {
617
                formset.setActionEnabled(ACTION_DELETE, true);
618
                formset.setActionEnabled(ACTION_SEARCH,true);
619
                formset.setActionEnabled(ACTION_NEW,true);
620
                formset.setActionEnabled(ACTION_CANCEL_NEW,false);
621
                formset.setActionEnabled(ACTION_SAVE,true);
622

    
623
            } else {
624
                formset.setActionEnabled(ACTION_DELETE, true);
625
                formset.setActionEnabled(ACTION_SEARCH,true);
626
                formset.setActionEnabled(ACTION_NEW,true);
627
                formset.setActionEnabled(ACTION_CANCEL_NEW,false);
628
                formset.setActionEnabled(ACTION_SAVE,true); //false);
629
            }
630

    
631
            return;
632
        }
633
        this.formset.setReadOnly(true);
634
        formset.setActionVisible(STARTEDITING_ACTION, true);
635
        formset.setActionEnabled(STARTEDITING_ACTION, true);
636
        formset.setActionVisible(FINISHEDITING_ACTION, false);
637
        formset.setActionEnabled(FINISHEDITING_ACTION, false);
638
        formset.setActionEnabled(ACTION_DELETE, false);
639
        formset.setActionEnabled(ACTION_NEW,false);
640
        formset.setActionEnabled(ACTION_CANCEL_NEW,false);
641
        formset.setActionEnabled(ACTION_SAVE,false);
642
        formset.setActionEnabled(ACTION_SEARCH,true);
643
    }
644
        
645
    @Override
646
    public long getDataSetSize() {
647
        if (this.features != null) {
648
            return features.getTotalSize();
649
        }
650
        return 0;
651
    }
652

    
653
    @Override
654
    public FeatureStore getFeatureStore() {
655
        return this.store;
656
    }
657

    
658
    private class FormSetListener implements JDynFormSetListener {
659

    
660
        @Override
661
        public void formMessage(String message) {
662
        }
663

    
664
        @Override
665
        public void formClose() {
666
            panel.setVisible(false);
667
        }
668

    
669
        @Override
670
        public void formMovedTo(int currentPosition) throws AbortActionException {
671
            LOGGER.trace("formMovedTo " + currentPosition);
672
            updateButtonEnabledStatus();
673
        }
674

    
675
        @Override
676
        public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException {
677
            LOGGER.trace("formBeforeSave");
678
            updateButtonEnabledStatus();
679
        }
680

    
681
        @Override
682
        public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException {
683
            LOGGER.trace("formBeforeNew");
684
            updateButtonEnabledStatus();
685
        }
686

    
687
        @Override
688
        public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException {
689
            LOGGER.trace("formBeforeDelete");
690
            updateButtonEnabledStatus();
691
        }
692

    
693
        @Override
694
        public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException {
695
            LOGGER.trace("formAfterSave");
696
            saveChanges(dynformSet);
697
            updateButtonEnabledStatus();
698
        }
699

    
700
        @Override
701
        public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException {
702
            LOGGER.trace("formAfterNew");
703
            updateButtonEnabledStatus();
704
        }
705

    
706
        @Override
707
        public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
708
            LOGGER.trace("formAfterDelete");
709
            deleteCurrentElement(dynformSet);
710
            updateButtonEnabledStatus();
711
        }
712

    
713
//        @Override
714
//        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
715
//            LOGGER.trace("formBeforeSearch");
716
//            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
717
//            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
718
//
719
//            JExpressionBuilder builder = dataSwingmanager.createQueryFilterExpresion(store);
720
//            FeatureQuery currentQuery = getCurrentQuery();
721
//            if( currentQuery!=null ) {
722
//                Evaluator filter = currentQuery.getFilter();
723
//                if( filter instanceof ExpressionEvaluator ) {
724
//                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
725
//                    builder.setExpression(expression);
726
//                }
727
//            }
728
//            Dialog dialog = winmgr.createDialog(
729
//                    builder.asJComponent(),
730
//                    "Filtro",
731
//                    "Creacion de filtro sobre '"+store.getName()+"'",
732
//                    WindowManager_v2.BUTTONS_OK_CANCEL
733
//            );
734
//            dialog.show(WindowManager.MODE.DIALOG);
735
//            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
736
//                Expression expresion = builder.getExpression();
737
//                try {
738
//                    FeatureQuery query = store.createFeatureQuery();
739
//                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
740
//                        query.clearFilter();
741
//                    } else {
742
//                        query.setFilter(expresion);
743
//                    }
744
//                    setQuery(query);
745
//                } catch (Exception ex) {
746
//                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
747
//                }
748
//            }
749
//        }
750

    
751
        @Override
752
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
753
            LOGGER.trace("formBeforeSearch");
754
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
755
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
756

    
757
            final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
758
            searchPanel.setShowActions(false);
759
            FeatureQuery currentQuery = getCurrentQuery();
760
            if( currentQuery!=null ) {
761
                Evaluator filter = currentQuery.getFilter();
762
                if( filter instanceof ExpressionEvaluator ) {
763
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
764
                    searchPanel.setFilter(expression);
765
                }
766
            }
767
            searchPanel.asJComponent().setPreferredSize(
768
                    new Dimension(FeatureStoreSearchPanel.DEFAULT_WIDTH, 320)
769
            );
770
            Dialog dialog = winmgr.createDialog(
771
                    searchPanel.asJComponent(),
772
                    "Filtro",
773
                    "Creacion de filtro sobre '"+store.getName()+"'",
774
                    WindowManager_v2.BUTTONS_OK_CANCEL
775
            );
776
            dialog.show(WindowManager.MODE.DIALOG);
777
            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
778
                Expression expresion = searchPanel.getFilter();
779
                try {
780
                    FeatureQuery query = store.createFeatureQuery();
781
                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
782
                        query.clearFilter();
783
                    } else {
784
                        query.setFilter(expresion);
785
                    }
786
                    setQuery(query);
787
                } catch (Exception ex) {
788
                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
789
                }
790
            }
791
        }
792

    
793
        @Override
794
        public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException {
795
            LOGGER.trace("formAfterSearch");
796
        }
797

    
798
        @Override
799
        public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException {
800
            LOGGER.trace("formBeforeCancelNew");
801
            updateButtonEnabledStatus();
802
        }
803

    
804
        @Override
805
        public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException {
806
            LOGGER.trace("formAfterCancelNew");
807
            updateButtonEnabledStatus();
808
        }
809
    }
810

    
811
//    private static class StoreEditException extends AbortActionException {
812
//
813
//        /**
814
//         *
815
//         */
816
//        private static final long serialVersionUID = -7682017811778577130L;
817
//
818
//        public StoreEditException(Throwable cause, String storename) {
819
//            super("Can't edit the store '%(storename)'", cause, "cant_edit_the store_XstorenameX", serialVersionUID);
820
//            setValue("storename", storename);
821
//        }
822
//    }
823
}