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

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 Expression getFilter() {
174
            FeatureQuery theCurrentQuery = currentQuery;
175
            if( theCurrentQuery!=null ) {
176
                Evaluator filter = theCurrentQuery.getFilter();
177
                if( filter instanceof ExpressionEvaluator ) {
178
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
179
                    return expression;
180
                }
181
            }
182
            return null;
183
        }
184

    
185
        @Override
186
        public int getSelectedsCount() {
187
            return 0;
188
        }
189

    
190
        @Override
191
        public Expression getFilterForSelecteds() {
192
            return null;
193
        }
194

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

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

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

    
227
public static void selfRegister() {
228
        String[][] iconNames = new String[][]{
229
            new String[]{"dalswing", "form-refresh-data"}
230
        };
231
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
232
        for (String[] icon : iconNames) {
233
            URL url = DefaultJFeaturesForm.class.getResource(icon[1] + ".png");
234
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
235
        }
236
        
237
    }
238
    @Override
239
    public void setPreferredSize(Dimension dimension) {
240
//        panel.setPreferredSize(dimension);
241
    }
242

    
243
    private void updateForm() {
244
        if (this.formset == null) {
245
            this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
246
        }
247
        try {
248
            if( this.currentQuery!=null ) {
249
                this.currentQuery.retrievesAllAttributes();
250
            }
251
            this.features = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE);
252
            this.formset.setValues(features.asListOfDynObjects());
253
        } catch (Exception ex) {
254
            throw new RuntimeException("Can't update form", ex);
255
        }
256
        updateButtonEnabledStatus();
257
    }
258

    
259
    @Override
260
    public JComponent asJComponent() {
261
        if (this.features == null) {
262
            try {
263
                updateForm();
264
            } catch (Exception ex) {
265
                throw new RuntimeException(ex);
266
            }
267
        }
268
        return this.panel;
269
    }
270

    
271
    @Override
272
    public void bind(FeatureStore store) {
273
        if (store == null) {
274
            throw new IllegalArgumentException("bind need a store as parameter, not a null.");
275
        }
276
        try {
277
            DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager();
278
            DynClass theDefinition = manager.featureType2DynClass(store, store.getDefaultFeatureType());
279
            this.bind(store, theDefinition);
280
        } catch (Exception ex) {
281
            throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex);
282
        }
283
    }
284

    
285
    public void bind(FeatureStore store, DynClass definition) throws ServiceException, DataException {
286
        if (this.store == store) {
287
            return;
288
        }
289
        DynFormManager formManager = DynFormLocator.getDynFormManager();
290
        this.definition = formManager.getDefinition(definition);
291
        if (formset != null) {
292
            this.panel.remove(formset.asJComponent());
293
            this.formset = null;
294
        }
295
        this.store = store;
296
        DisposeUtils.bind(store);
297
        this.features = null;
298
    }
299

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

    
333
            this.formset.addAction(new StartEditingAction());
334
            this.formset.addAction(new FinishEditingAction());
335
            this.formset.addAction(new RefreshAction());
336
            
337
            FormActionContext actionContext = new FormActionContext();
338
            Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
339
            for (DALActionFactory factory : factories) {
340
                if( StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME) ) {
341
                    continue;
342
                }
343
                Action action = factory.createAction(actionContext);
344
                this.formset.addAction(action);
345
            }            
346
            for( Action action : this.otherActions ) {
347
                this.formset.addAction(action);
348
            }
349

    
350
            this.formset.addListener(new FormSetListener());
351
        }
352
        updateButtonEnabledStatus();
353
        return this.formset;
354
    }
355

    
356
    @Override
357
    public void addAction(Action action) {
358
        this.otherActions.add(action);
359
        if( this.formset!=null ) {
360
            this.formset.addAction(action);
361
        }
362
    }
363

    
364
    @Override
365
    public long getCurrentIndex() {
366
        if( this.formset==null ) {
367
            return -1;
368
        }
369
        return this.formset.getCurrentIndex();
370
    }
371

    
372
    @Override
373
    public Feature get(long index) {
374
        if( this.formset==null || this.features==null ) {
375
            return null;
376
        }
377
        try {
378
            return this.features.getFeatureAt(index);
379
        } catch (BaseException ex) {
380
            return null;
381
        }
382
    }
383
    
384
    private class RefreshAction extends AbstractAction {
385

    
386
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
387
        public RefreshAction() {
388
            I18nManager i18nManager = ToolsLocator.getI18nManager();
389
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
390

    
391
            this.putValue(NAME, null);
392
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Refresh"));
393
            this.putValue(SMALL_ICON, iconTheme.get("form-refresh-data"));
394
            this.putValue(ACTION_COMMAND_KEY, REFRESHFORM_ACTION);
395
            this.setEnabled(!formset.isInNewState());
396
        }
397

    
398
        @Override
399
        public void actionPerformed(ActionEvent ae) {
400
            try {
401
                I18nManager i18n = ToolsLocator.getI18nManager();
402
                formset.message(i18n.getTranslation("_Form_refreshed"));
403
                updateForm();
404
            } catch (Exception ex) {
405
                LOGGER.warn("Can't refresh form", ex);
406
            }
407
        }
408
    }
409

    
410
       
411
    private class FinishEditingAction extends AbstractAction implements Observer {
412

    
413
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
414
        public FinishEditingAction() {
415
            I18nManager i18nManager = ToolsLocator.getI18nManager();
416
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
417

    
418
            this.putValue(NAME,null);
419
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing"));
420
            this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
421
            this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
422

    
423
            this.setEnabled(store.isEditing());
424
            store.addObserver(this);
425
        }
426

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

    
473
    }
474
    
475
    private class StartEditingAction extends AbstractAction implements Observer {
476

    
477
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
478
        public StartEditingAction() {
479
            I18nManager i18nManager = ToolsLocator.getI18nManager();
480
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
481

    
482
            this.putValue(NAME,null);
483
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Start_editing"));
484
            this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
485
            this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
486

    
487
            this.setEnabled(!store.isEditing());
488
            store.addObserver(this);
489
        }
490

    
491
        @Override
492
        public void actionPerformed(ActionEvent ae) {
493
            if( !store.isEditing() ) {
494
                try {
495
                    store.edit();
496
                } catch (DataException ex) {
497
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
498
                }
499
            }
500
            updateButtonEnabledStatus();
501
        }
502

    
503
        @Override
504
        public void update(Observable observable, Object notification) {
505
            if( store == null || formset==null ) {
506
                return;
507
            }
508
            if( notification instanceof FeatureStoreNotification ) {
509
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
510
                switch( n.getType() )  {
511
                    case FeatureStoreNotification.AFTER_STARTEDITING:
512
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
513
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
514
                        updateButtonEnabledStatus();
515
                        break;
516
                }
517
            }
518
        }
519

    
520
    }
521

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

    
540
    private FeatureQuery getCurrentQuery() {
541
        return this.currentQuery;
542
    }
543
    
544
    @Override
545
    public void showForm(MODE mode) {
546
        this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
547
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
548
        String title = this.definition.getLabel();
549
        winmgr.showWindow(this.asJComponent(), title, mode);
550
    }
551

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

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

    
603
        } catch (Exception ex) {
604
            theFormSet.message(i18n.getTranslation("error_removing_data_will_not_remove"));
605
            throw new RuntimeException("Can't remove values", ex);
606

    
607
        } finally {
608
            updateButtonEnabledStatus();
609
        }
610

    
611
    }
612
    @Override
613
    public void saveChanges() {
614
        if (this.formset != null && this.formset.countValues() > 0) {
615
            if (store != null && !store.isEditing()) {
616
                try {
617
                    store.edit();
618
                } catch (DataException e1) {
619
                    LOGGER.warn("Can't edit the store " + store.getName());
620
                    throw new RuntimeException("Can't save changes.", e1);
621
                }
622
            }
623
            this.saveChanges(this.formset);
624
        }
625
    }
626

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

    
667
            } else if( formset.getForm().isModified() ) {
668
                formset.setActionEnabled(ACTION_DELETE, true);
669
                formset.setActionEnabled(ACTION_SEARCH,true);
670
                formset.setActionEnabled(ACTION_NEW,true);
671
                formset.setActionEnabled(ACTION_CANCEL_NEW,false);
672
                formset.setActionEnabled(ACTION_SAVE,true);
673
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
674

    
675
            } else {
676
                formset.setActionEnabled(ACTION_DELETE, true);
677
                formset.setActionEnabled(ACTION_SEARCH,true);
678
                formset.setActionEnabled(ACTION_NEW,true);
679
                formset.setActionEnabled(ACTION_CANCEL_NEW,false);
680
                formset.setActionEnabled(ACTION_SAVE,true); //false);
681
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
682
            }
683

    
684
            return;
685
        }
686
        this.formset.setReadOnly(true);
687
        formset.setActionVisible(STARTEDITING_ACTION, true);
688
        formset.setActionEnabled(STARTEDITING_ACTION, true);
689
        formset.setActionVisible(FINISHEDITING_ACTION, false);
690
        formset.setActionEnabled(FINISHEDITING_ACTION, false);
691
        formset.setActionEnabled(ACTION_DELETE, false);
692
        formset.setActionEnabled(ACTION_NEW,false);
693
        formset.setActionEnabled(ACTION_CANCEL_NEW,false);
694
        formset.setActionEnabled(ACTION_SAVE,false);
695
        formset.setActionEnabled(ACTION_SEARCH,true);
696
        formset.setActionEnabled(REFRESHFORM_ACTION,true);
697
    }
698
        
699
    @Override
700
    public long getDataSetSize() {
701
        if (this.features != null) {
702
            return features.getTotalSize();
703
        }
704
        return 0;
705
    }
706

    
707
    @Override
708
    public FeatureStore getFeatureStore() {
709
        return this.store;
710
    }
711

    
712
    private class FormSetListener implements JDynFormSetListener {
713

    
714
        @Override
715
        public void formMessage(String message) {
716
        }
717

    
718
        @Override
719
        public void formClose() {
720
            panel.setVisible(false);
721
        }
722

    
723
        @Override
724
        public void formMovedTo(int currentPosition) throws AbortActionException {
725
            LOGGER.trace("formMovedTo " + currentPosition);
726
            updateButtonEnabledStatus();
727
        }
728

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

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

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

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

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

    
760
        @Override
761
        public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
762
            LOGGER.trace("formAfterDelete");
763
            deleteCurrentElement(dynformSet);
764
            updateButtonEnabledStatus();
765
        }
766

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

    
805
        @Override
806
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
807
            LOGGER.trace("formBeforeSearch");
808
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
809
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
810

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

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

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

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

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