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 @ 44794

History | View | Annotate | Download (33.3 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.net.URL;
31
import java.util.ArrayList;
32
import java.util.Collection;
33
import java.util.List;
34
import javax.swing.AbstractAction;
35
import javax.swing.Action;
36
import static javax.swing.Action.ACTION_COMMAND_KEY;
37
import static javax.swing.Action.NAME;
38
import static javax.swing.Action.SHORT_DESCRIPTION;
39
import static javax.swing.Action.SMALL_ICON;
40

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

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

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

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

    
112
    private final String STARTEDITING_ACTION = "startEditing";
113
    private final String FINISHEDITING_ACTION = "finishEditing";
114
    private final String REFRESHFORM_ACTION = "refreshForm";
115

    
116

    
117
    public class DefaultFeaturesFormContext implements FeaturesFormContext {
118

    
119
        private DefaultFeaturesFormContext() {
120
            
121
        }
122

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

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

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

    
158
        
159
    }
160
    
161
    private class FormActionContext extends AbstractDALActionContext {
162

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

    
172
        @Override
173
        public FeatureQuery getQuery() {
174
          return currentQuery;
175
        }
176

    
177
        @Override
178
        public int getSelectedsCount() {
179
            return 0;
180
        }
181

    
182
        @Override
183
        public Expression getFilterForSelecteds() {
184
            return null;
185
        }
186

    
187
        @Override
188
        public JComponent getActionButton(String actionName) {
189
            if( formset == null ) {
190
                return null;
191
            }
192
            return formset.getActionButton(actionName);
193
        }
194
        
195
    }
196
    
197
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class);
198

    
199
    private static final int PAGE_SIZE = 500;
200
    private JPanel panel;
201
    private JDynFormSet formset;
202
    private FeatureStore store;
203
    private FeaturePagingHelper features;
204
    private DynFormDefinition definition = null;
205
    private FeatureQuery currentQuery;
206
    private List<Action> otherActions;
207

    
208
    public DefaultJFeaturesForm() {
209
        this.otherActions = new ArrayList<>();
210
        this.panel = new JPanel(new BorderLayout());
211
        this.panel.addComponentListener(new ComponentAdapter() {
212
            @Override
213
            public void componentHidden(ComponentEvent e) {
214
                doDispose();
215
            }
216
        });
217
    }
218

    
219
public static void selfRegister() {
220
        String[][] iconNames = new String[][]{
221
            new String[]{"dalswing", "form-refresh-data"}
222
        };
223
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
224
        for (String[] icon : iconNames) {
225
            URL url = DefaultJFeaturesForm.class.getResource(icon[1] + ".png");
226
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
227
        }
228
        
229
    }
230
    @Override
231
    public void setPreferredSize(Dimension dimension) {
232
//        panel.setPreferredSize(dimension);
233
    }
234

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

    
251
    @Override
252
    public JComponent asJComponent() {
253
        if (this.features == null) {
254
            try {
255
                updateForm();
256
            } catch (Exception ex) {
257
                throw new RuntimeException(ex);
258
            }
259
        }
260
        return this.panel;
261
    }
262

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

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

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

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

    
342
            this.formset.addListener(new FormSetListener());
343
        }
344
        updateButtonEnabledStatus();
345
        return this.formset;
346
    }
347

    
348
    @Override
349
    public void addAction(Action action) {
350
        this.otherActions.add(action);
351
        if( this.formset!=null ) {
352
            this.formset.addAction(action);
353
        }
354
    }
355

    
356
    @Override
357
    public long getCurrentIndex() {
358
        if( this.formset==null ) {
359
            return -1;
360
        }
361
        return this.formset.getCurrentIndex();
362
    }
363

    
364
    @Override
365
    public Feature get(long index) {
366
        if( this.formset==null || this.features==null ) {
367
            return null;
368
        }
369
        try {
370
            return this.features.getFeatureAt(index);
371
        } catch (BaseException ex) {
372
            return null;
373
        }
374
    }
375
    
376
    private class RefreshAction extends AbstractAction {
377

    
378
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
379
        public RefreshAction() {
380
            I18nManager i18nManager = ToolsLocator.getI18nManager();
381
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
382

    
383
            this.putValue(NAME, null);
384
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Refresh"));
385
            this.putValue(SMALL_ICON, iconTheme.get("form-refresh-data"));
386
            this.putValue(ACTION_COMMAND_KEY, REFRESHFORM_ACTION);
387
            this.setEnabled(!formset.isInNewState());
388
        }
389

    
390
        @Override
391
        public void actionPerformed(ActionEvent ae) {
392
            try {
393
                I18nManager i18n = ToolsLocator.getI18nManager();
394
                formset.message(i18n.getTranslation("_Form_refreshed"));
395
                updateForm();
396
            } catch (Exception ex) {
397
                LOGGER.warn("Can't refresh form", ex);
398
            }
399
        }
400
    }
401

    
402
       
403
    private class FinishEditingAction extends AbstractAction implements Observer {
404

    
405
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
406
        public FinishEditingAction() {
407
            I18nManager i18nManager = ToolsLocator.getI18nManager();
408
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
409

    
410
            this.putValue(NAME,null);
411
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing"));
412
            this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
413
            this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
414

    
415
            this.setEnabled(store.isEditing());
416
            store.addObserver(this);
417
        }
418

    
419
        @Override
420
        public void actionPerformed(ActionEvent ae) {
421
            if( store.isEditing() ) {
422
                try {
423
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
424
                    ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
425
                    int x = dialogManager.confirmDialog(
426
                            i18nManager.getTranslation("_Do_you_want_to_finish_editing_and_save_the_changes")+"\n\n"+
427
                                    i18nManager.getTranslation("_Press_cancel_to_cancel_editing_and_changes"),
428
                            i18nManager.getTranslation("_Stop_editing"),
429
                            JOptionPane.YES_NO_CANCEL_OPTION,
430
                            JOptionPane.QUESTION_MESSAGE
431
                    );
432
                    switch(x) {
433
                        case JOptionPane.YES_OPTION:
434
                            store.finishEditing();
435
                            break;
436
                        case JOptionPane.NO_OPTION:
437
                            break;
438
                        case JOptionPane.CANCEL_OPTION:
439
                            store.cancelEditing();
440
                            break;
441
                    }
442
                } catch (DataException ex) {
443
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
444
                }
445
            }
446
            updateButtonEnabledStatus();
447
        }
448
    
449
        @Override
450
        public void update(Observable observable, Object notification) {
451
            if( store == null || formset==null ) {
452
                return;
453
            }
454
            if( notification instanceof FeatureStoreNotification ) {
455
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
456
                switch( n.getType() )  {
457
                    case FeatureStoreNotification.AFTER_STARTEDITING:
458
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
459
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
460
                        updateButtonEnabledStatus();
461
                        break;
462
                }
463
            }
464
        }
465

    
466
    }
467
    
468
    private class StartEditingAction extends AbstractAction implements Observer {
469

    
470
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
471
        public StartEditingAction() {
472
            I18nManager i18nManager = ToolsLocator.getI18nManager();
473
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
474

    
475
            this.putValue(NAME,null);
476
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Start_editing"));
477
            this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
478
            this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
479

    
480
            this.setEnabled(!store.isEditing());
481
            store.addObserver(this);
482
        }
483

    
484
        @Override
485
        public void actionPerformed(ActionEvent ae) {
486
            if( !store.isEditing() ) {
487
                try {
488
                    store.edit();
489
                } catch (DataException ex) {
490
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
491
                }
492
            }
493
            updateButtonEnabledStatus();
494
        }
495

    
496
        @Override
497
        public void update(Observable observable, Object notification) {
498
            if( store == null || formset==null ) {
499
                return;
500
            }
501
            if( notification instanceof FeatureStoreNotification ) {
502
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
503
                switch( n.getType() )  {
504
                    case FeatureStoreNotification.AFTER_STARTEDITING:
505
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
506
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
507
                        updateButtonEnabledStatus();
508
                        break;
509
                }
510
            }
511
        }
512

    
513
    }
514

    
515
    @Override
516
    public void setQuery(FeatureQuery query) {
517
        if (this.features != null) {
518
            if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) {
519
                if (!store.isEditing()) {
520
                    try {
521
                        store.edit();
522
                    } catch (DataException e1) {
523
                        throw new RuntimeException("Can't set query", e1);
524
                    }
525
                }
526
                saveChanges(this.formset);
527
            }
528
        }
529
        this.currentQuery = query;
530
        updateForm();
531
    }
532

    
533
    private FeatureQuery getCurrentQuery() {
534
        return this.currentQuery;
535
    }
536
    
537
    @Override
538
    public void showForm(MODE mode) {
539
        this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
540
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
541
        String title = this.definition.getLabel();
542
        winmgr.showWindow(this.asJComponent(), title, mode);
543
    }
544

    
545
    private void saveChanges(JDynFormSet theFormSet) {
546
        I18nManager i18n = ToolsLocator.getI18nManager();
547
        try {
548
            if( theFormSet.isInNewState() ) {
549
                Feature newFeature = store.createNewFeature(false);
550
                DynObject newElement = newFeature.getAsDynObject();
551
                theFormSet.getFormValues(newElement);
552
                features.insert(((FacadeOfAFeature)newElement).getEditableFeature());
553
                this.formset.message(i18n.getTranslation("_Record_saved"));
554
                try {
555
                    this.formset.setValues(features.asListOfDynObjects());
556
                    this.formset.setCurrentIndex((int)(features.getTotalSize())-1);
557
                } catch(Exception ex) {
558
                    LOGGER.warn("Can't reload form data after insert.",ex);
559
                }
560
            } else {
561
                int index = theFormSet.getCurrentIndex();
562
                DynObject currentElement = theFormSet.get(index);
563
                theFormSet.getFormValues(currentElement);
564
                features.update(((FacadeOfAFeature)currentElement).getEditableFeature());
565
                this.formset.message(i18n.getTranslation("_Record_saved"));
566
            }
567
        } catch(Exception ex) {
568
            theFormSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
569
            throw new RuntimeException("Can't save values",ex);
570
            
571
        } finally {
572
            updateButtonEnabledStatus();
573
        }
574

    
575
    }
576
    private void deleteCurrentElement(JDynFormSet theFormSet) {
577
        I18nManager i18n = ToolsLocator.getI18nManager();
578
        try {
579
            int index = theFormSet.getCurrentIndex();
580
            DynObject currentElement = theFormSet.get(index);
581
            theFormSet.getFormValues(currentElement);
582
            Feature feature = ((FacadeOfAFeature) currentElement).getFeature();
583
            if (feature instanceof EditableFeature) {
584
                feature = ((EditableFeature) feature).getNotEditableCopy();
585
            }
586
            features.delete(feature);
587
            
588
            this.formset.message(i18n.getTranslation("_Record_removed"));
589
            
590
            this.formset.setValues(features.asListOfDynObjects());
591
            if (features.getTotalSize()-1<index) {
592
                index = index-1;
593
            }
594
            this.formset.setCurrentIndex(index);
595

    
596
        } catch (Exception ex) {
597
            theFormSet.message(i18n.getTranslation("error_removing_data_will_not_remove"));
598
            throw new RuntimeException("Can't remove values", ex);
599

    
600
        } finally {
601
            updateButtonEnabledStatus();
602
        }
603

    
604
    }
605
    @Override
606
    public void saveChanges() {
607
        if (this.formset != null && this.formset.countValues() > 0) {
608
            if (store != null && !store.isEditing()) {
609
                try {
610
                    store.edit();
611
                } catch (DataException e1) {
612
                    LOGGER.warn("Can't edit the store " + store.getName());
613
                    throw new RuntimeException("Can't save changes.", e1);
614
                }
615
            }
616
            this.saveChanges(this.formset);
617
        }
618
    }
619

    
620
    private void updateButtonEnabledStatus() {
621
        if( this.formset == null ) {
622
            return;
623
        }
624
        if( this.store == null || store.isBroken() || store.isAppending() ) {
625
            this.formset.setReadOnly(true);
626
            formset.setActionVisible(STARTEDITING_ACTION, true);
627
            formset.setActionEnabled(STARTEDITING_ACTION, true);
628
            formset.setActionVisible(FINISHEDITING_ACTION, false);
629
            formset.setActionEnabled(FINISHEDITING_ACTION, false);
630
            formset.setActionEnabled(ACTION_DELETE, false);
631
            formset.setActionEnabled(ACTION_NEW,false);
632
            formset.setActionEnabled(ACTION_CANCEL_NEW,false);
633
            formset.setActionEnabled(ACTION_SAVE,false);
634
            formset.setActionEnabled(ACTION_SEARCH,false);
635
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
636
            return;
637
        }
638
        if( store.isEditing() ) {
639
            this.formset.setReadOnly(false);
640
            formset.setActionVisible(STARTEDITING_ACTION, false);
641
            formset.setActionEnabled(STARTEDITING_ACTION, false);
642
            formset.setActionVisible(FINISHEDITING_ACTION, true);
643
            formset.setActionEnabled(FINISHEDITING_ACTION, true);
644
            if( formset.isInNewState() ) {
645
                formset.setActionEnabled(ACTION_DELETE, false);
646
                formset.setActionEnabled(ACTION_SEARCH,false);
647
                formset.setActionEnabled(ACTION_NEW,false);
648
                formset.setActionEnabled(ACTION_CANCEL_NEW,true);
649
                formset.setActionEnabled(ACTION_SAVE,true);
650
                formset.setActionEnabled(REFRESHFORM_ACTION, false);
651
                
652
            } else if( this.features!=null && this.features.isEmpty() ) {
653
                formset.setActionEnabled(ACTION_DELETE, false);
654
                formset.setActionEnabled(ACTION_SEARCH,false);
655
                formset.setActionEnabled(ACTION_NEW,true);
656
                formset.setActionEnabled(ACTION_CANCEL_NEW,true);
657
                formset.setActionEnabled(ACTION_SAVE,false);
658
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
659

    
660
            } else if( formset.getForm().isModified() ) {
661
                formset.setActionEnabled(ACTION_DELETE, true);
662
                formset.setActionEnabled(ACTION_SEARCH,true);
663
                formset.setActionEnabled(ACTION_NEW,true);
664
                formset.setActionEnabled(ACTION_CANCEL_NEW,false);
665
                formset.setActionEnabled(ACTION_SAVE,true);
666
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
667

    
668
            } else {
669
                formset.setActionEnabled(ACTION_DELETE, true);
670
                formset.setActionEnabled(ACTION_SEARCH,true);
671
                formset.setActionEnabled(ACTION_NEW,true);
672
                formset.setActionEnabled(ACTION_CANCEL_NEW,false);
673
                formset.setActionEnabled(ACTION_SAVE,true); //false);
674
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
675
            }
676

    
677
            return;
678
        }
679
        this.formset.setReadOnly(true);
680
        formset.setActionVisible(STARTEDITING_ACTION, true);
681
        formset.setActionEnabled(STARTEDITING_ACTION, true);
682
        formset.setActionVisible(FINISHEDITING_ACTION, false);
683
        formset.setActionEnabled(FINISHEDITING_ACTION, false);
684
        formset.setActionEnabled(ACTION_DELETE, false);
685
        formset.setActionEnabled(ACTION_NEW,false);
686
        formset.setActionEnabled(ACTION_CANCEL_NEW,false);
687
        formset.setActionEnabled(ACTION_SAVE,false);
688
        formset.setActionEnabled(ACTION_SEARCH,true);
689
        formset.setActionEnabled(REFRESHFORM_ACTION,true);
690
    }
691
        
692
    @Override
693
    public long getDataSetSize() {
694
        if (this.features != null) {
695
            return features.getTotalSize();
696
        }
697
        return 0;
698
    }
699

    
700
    @Override
701
    public FeatureStore getFeatureStore() {
702
        return this.store;
703
    }
704

    
705
    private class FormSetListener implements JDynFormSetListener {
706

    
707
        @Override
708
        public void formMessage(String message) {
709
        }
710

    
711
        @Override
712
        public void formClose() {
713
            panel.setVisible(false);
714
        }
715

    
716
        @Override
717
        public void formMovedTo(int currentPosition) throws AbortActionException {
718
            LOGGER.trace("formMovedTo " + currentPosition);
719
            updateButtonEnabledStatus();
720
        }
721

    
722
        @Override
723
        public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException {
724
            LOGGER.trace("formBeforeSave");
725
            updateButtonEnabledStatus();
726
        }
727

    
728
        @Override
729
        public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException {
730
            LOGGER.trace("formBeforeNew");
731
            updateButtonEnabledStatus();
732
        }
733

    
734
        @Override
735
        public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException {
736
            LOGGER.trace("formBeforeDelete");
737
            updateButtonEnabledStatus();
738
        }
739

    
740
        @Override
741
        public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException {
742
            LOGGER.trace("formAfterSave");
743
            saveChanges(dynformSet);
744
            updateButtonEnabledStatus();
745
        }
746

    
747
        @Override
748
        public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException {
749
            LOGGER.trace("formAfterNew");
750
            updateButtonEnabledStatus();
751
        }
752

    
753
        @Override
754
        public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
755
            LOGGER.trace("formAfterDelete");
756
            deleteCurrentElement(dynformSet);
757
            updateButtonEnabledStatus();
758
        }
759

    
760
//        @Override
761
//        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
762
//            LOGGER.trace("formBeforeSearch");
763
//            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
764
//            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
765
//
766
//            JExpressionBuilder builder = dataSwingmanager.createQueryFilterExpresion(store);
767
//            FeatureQuery currentQuery = getCurrentQuery();
768
//            if( currentQuery!=null ) {
769
//                Evaluator filter = currentQuery.getFilter();
770
//                if( filter instanceof ExpressionEvaluator ) {
771
//                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
772
//                    builder.setExpression(expression);
773
//                }
774
//            }
775
//            Dialog dialog = winmgr.createDialog(
776
//                    builder.asJComponent(),
777
//                    "Filtro",
778
//                    "Creacion de filtro sobre '"+store.getName()+"'",
779
//                    WindowManager_v2.BUTTONS_OK_CANCEL
780
//            );
781
//            dialog.show(WindowManager.MODE.DIALOG);
782
//            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
783
//                Expression expresion = builder.getExpression();
784
//                try {
785
//                    FeatureQuery query = store.createFeatureQuery();
786
//                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
787
//                        query.clearFilter();
788
//                    } else {
789
//                        query.setFilter(expresion);
790
//                    }
791
//                    setQuery(query);
792
//                } catch (Exception ex) {
793
//                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
794
//                }
795
//            }
796
//        }
797

    
798
        @Override
799
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
800
            LOGGER.trace("formBeforeSearch");
801
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
802
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
803

    
804
            final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
805
            searchPanel.setShowActions(false);
806
            FeatureQuery currentQuery = getCurrentQuery();
807
            if( currentQuery!=null ) {
808
                Evaluator filter = currentQuery.getFilter();
809
                if( filter instanceof ExpressionEvaluator ) {
810
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
811
                    searchPanel.setFilter(expression);
812
                    searchPanel.search();
813
                }
814
            }
815
            searchPanel.asJComponent().setPreferredSize(
816
                    new Dimension(FeatureStoreSearchPanel.DEFAULT_WIDTH, 320)
817
            );
818
            I18nManager i18n = ToolsLocator.getI18nManager();
819
            Dialog dialog = winmgr.createDialog(
820
                    searchPanel.asJComponent(),
821
                    i18n.getTranslation("_Filter"),
822
                    i18n.getTranslation("_Creating_filter_for") + ": '"+store.getName()+"'",
823
                    WindowManager_v2.BUTTONS_OK_CANCEL
824
            );
825
            dialog.show(WindowManager.MODE.DIALOG);
826
            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
827
                FeatureQuery searchQuery = searchPanel.getQuery();
828
                Expression expresion = null;
829
                if( searchQuery!=null ) {
830
                  expresion = searchQuery.getExpressionFilter();
831
                }
832
                try {
833
                    FeatureQuery query = store.createFeatureQuery();
834
                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
835
                        query.clearFilter();
836
                    } else {
837
                        query.setFilter(expresion);
838
                    }
839
                    setQuery(query);
840
                } catch (Exception ex) {
841
                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
842
                }
843
            }
844
        }
845

    
846
        @Override
847
        public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException {
848
            LOGGER.trace("formAfterSearch");
849
        }
850

    
851
        @Override
852
        public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException {
853
            LOGGER.trace("formBeforeCancelNew");
854
            updateButtonEnabledStatus();
855
        }
856

    
857
        @Override
858
        public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException {
859
            LOGGER.trace("formAfterCancelNew");
860
            updateButtonEnabledStatus();
861
        }
862
    }
863

    
864
//    private static class StoreEditException extends AbortActionException {
865
//
866
//        /**
867
//         *
868
//         */
869
//        private static final long serialVersionUID = -7682017811778577130L;
870
//
871
//        public StoreEditException(Throwable cause, String storename) {
872
//            super("Can't edit the store '%(storename)'", cause, "cant_edit_the store_XstorenameX", serialVersionUID);
873
//            setValue("storename", storename);
874
//        }
875
//    }
876
}