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

History | View | Annotate | Download (33.5 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
                            i18nManager.getTranslation("_Do_you_want_to_finish_editing_and_save_the_changes")+"\n\n"+
435
                                    i18nManager.getTranslation("_Press_cancel_to_cancel_editing_and_changes"),
436
                            i18nManager.getTranslation("_Stop_editing"),
437
                            JOptionPane.YES_NO_CANCEL_OPTION,
438
                            JOptionPane.QUESTION_MESSAGE
439
                    );
440
                    switch(x) {
441
                        case JOptionPane.YES_OPTION:
442
                            store.finishEditing();
443
                            break;
444
                        case JOptionPane.NO_OPTION:
445
                            break;
446
                        case JOptionPane.CANCEL_OPTION:
447
                            store.cancelEditing();
448
                            break;
449
                    }
450
                } catch (DataException ex) {
451
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
452
                }
453
            }
454
            updateButtonEnabledStatus();
455
        }
456
    
457
        @Override
458
        public void update(Observable observable, Object notification) {
459
            if( store == null || formset==null ) {
460
                return;
461
            }
462
            if( notification instanceof FeatureStoreNotification ) {
463
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
464
                switch( n.getType() )  {
465
                    case FeatureStoreNotification.AFTER_STARTEDITING:
466
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
467
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
468
                        updateButtonEnabledStatus();
469
                        break;
470
                }
471
            }
472
        }
473

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

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

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

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

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

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

    
521
    }
522

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

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

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

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

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

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

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

    
628
    private void updateButtonEnabledStatus() {
629
        if( this.formset == null ) {
630
            return;
631
        }
632
        if( this.store == null || store.isBroken() || store.isAppending() ) {
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,false);
643
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
644
            return;
645
        }
646
        if( store.isEditing() ) {
647
            this.formset.setReadOnly(false);
648
            formset.setActionVisible(STARTEDITING_ACTION, false);
649
            formset.setActionEnabled(STARTEDITING_ACTION, false);
650
            formset.setActionVisible(FINISHEDITING_ACTION, true);
651
            formset.setActionEnabled(FINISHEDITING_ACTION, true);
652
            if( formset.isInNewState() ) {
653
                formset.setActionEnabled(ACTION_DELETE, false);
654
                formset.setActionEnabled(ACTION_SEARCH,false);
655
                formset.setActionEnabled(ACTION_NEW,false);
656
                formset.setActionEnabled(ACTION_CANCEL_NEW,true);
657
                formset.setActionEnabled(ACTION_SAVE,true);
658
                formset.setActionEnabled(REFRESHFORM_ACTION, false);
659
                
660
            } else if( this.features!=null && this.features.isEmpty() ) {
661
                formset.setActionEnabled(ACTION_DELETE, false);
662
                formset.setActionEnabled(ACTION_SEARCH,false);
663
                formset.setActionEnabled(ACTION_NEW,true);
664
                formset.setActionEnabled(ACTION_CANCEL_NEW,true);
665
                formset.setActionEnabled(ACTION_SAVE,false);
666
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
667

    
668
            } else if( formset.getForm().isModified() ) {
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);
674
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
675

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

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

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

    
713
    private class FormSetListener implements JDynFormSetListener {
714

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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