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

History | View | Annotate | Download (51.9 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.Toolkit;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.awt.event.ComponentAdapter;
31
import java.awt.event.ComponentEvent;
32
import java.util.ArrayList;
33
import java.util.Collection;
34
import java.util.List;
35
import java.util.Map;
36
import javax.swing.AbstractAction;
37
import javax.swing.Action;
38
import static javax.swing.Action.ACTION_COMMAND_KEY;
39
import static javax.swing.Action.NAME;
40
import static javax.swing.Action.SHORT_DESCRIPTION;
41
import static javax.swing.Action.SMALL_ICON;
42
import javax.swing.JComponent;
43
import javax.swing.JOptionPane;
44
import javax.swing.JPanel;
45
import org.apache.commons.lang3.StringUtils;
46
import org.gvsig.expressionevaluator.Expression;
47
import org.gvsig.expressionevaluator.ExpressionEvaluator;
48
import org.gvsig.expressionevaluator.ExpressionUtils;
49
import org.gvsig.featureform.swing.JFeaturesForm;
50
import org.gvsig.fmap.dal.DALLocator;
51
import org.gvsig.fmap.dal.DataQuery;
52
import org.gvsig.fmap.dal.DataStore;
53
import org.gvsig.fmap.dal.EditingNotification;
54
import static org.gvsig.fmap.dal.EditingNotification.AFTER_REMOVE_FEATURE;
55
import static org.gvsig.fmap.dal.EditingNotification.AFTER_UPDATE_FEATURE;
56
import static org.gvsig.fmap.dal.EditingNotification.AFTER_INSERT_FEATURE;
57
import static org.gvsig.fmap.dal.EditingNotification.AFTER_ENTER_EDITING_STORE;
58
import static org.gvsig.fmap.dal.EditingNotification.AFTER_EXIT_EDITING_STORE;
59
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_REMOVE_FEATURE;
60
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_UPDATE_FEATURE;
61
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_INSERT_FEATURE;
62
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_ENTER_EDITING_STORE;
63
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_EXIT_EDITING_STORE;
64
import org.gvsig.fmap.dal.EditingNotificationManager;
65
import org.gvsig.fmap.dal.exception.DataException;
66
import org.gvsig.fmap.dal.feature.EditableFeature;
67
import org.gvsig.fmap.dal.feature.FacadeOfAFeature;
68
import org.gvsig.fmap.dal.feature.Feature;
69
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
70
import org.gvsig.fmap.dal.feature.FeatureQuery;
71
import org.gvsig.fmap.dal.feature.FeatureSelection;
72
import org.gvsig.fmap.dal.feature.FeatureStore;
73
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
74
import org.gvsig.fmap.dal.feature.FeatureType;
75
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
76
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
77
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext;
78
import org.gvsig.fmap.dal.swing.DALActionFactory;
79
import org.gvsig.fmap.dal.swing.DALSwingLocator;
80
import org.gvsig.fmap.dal.swing.DataSwingManager;
81
import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager;
82
import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory;
83
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel;
84
import org.gvsig.fmap.dal.swing.searchpanel.SearchParameters;
85
import org.gvsig.tools.ToolsLocator;
86
import org.gvsig.tools.dispose.Disposable;
87
import org.gvsig.tools.dispose.DisposeUtils;
88
import org.gvsig.tools.dynform.AbortActionException;
89
import org.gvsig.tools.dynform.DynFormDefinition;
90
import org.gvsig.tools.dynform.DynFormLocator;
91
import org.gvsig.tools.dynform.DynFormManager;
92
import org.gvsig.tools.dynform.JDynForm;
93
import org.gvsig.tools.dynform.JDynFormField;
94
import org.gvsig.tools.dynform.JDynFormSet;
95
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CANCEL_NEW;
96
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_DELETE;
97
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NAVIGATION;
98
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW;
99
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE;
100
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SEARCH;
101
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener;
102
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
103
import org.gvsig.tools.dynobject.DynClass;
104
import org.gvsig.tools.dynobject.DynObject;
105
import org.gvsig.tools.evaluator.Evaluator;
106
import org.gvsig.tools.exception.BaseException;
107
import org.gvsig.tools.i18n.I18nManager;
108
import org.gvsig.tools.observer.Observable;
109
import org.gvsig.tools.observer.Observer;
110
import org.gvsig.tools.swing.api.ActionListenerSupport;
111
import org.gvsig.tools.swing.api.ToolsSwingLocator;
112
import org.gvsig.tools.swing.api.ToolsSwingUtils;
113
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
114
import org.gvsig.tools.swing.api.windowmanager.Dialog;
115
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
116
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
117
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
118
import org.gvsig.tools.swing.icontheme.IconTheme;
119
import org.gvsig.tools.util.PropertiesSupport;
120
import org.gvsig.tools.util.PropertiesSupportHelper;
121
import org.slf4j.Logger;
122
import org.slf4j.LoggerFactory;
123

    
124
/**
125
 * @author fdiaz
126
 *
127
 */
128
@SuppressWarnings("UseSpecificCatch")
129
public class DefaultJFeaturesForm implements JFeaturesForm, Disposable {
130

    
131
    private class JFeaturesFormPanel extends JPanel implements PropertiesSupport {
132

    
133
        private final PropertiesSupport properties;
134
        
135
        private JFeaturesFormPanel(PropertiesSupport properties, BorderLayout layout) {
136
            super(layout);
137
            this.properties = properties;
138
        }
139

    
140
        @Override
141
        public Object getProperty(String name) {
142
            return this.properties.getProperty(name);
143
        }
144

    
145
        @Override
146
        public void setProperty(String name, Object value) {
147
            this.properties.setProperty(name, value);
148
        }
149

    
150
        @Override
151
        public Map<String, Object> getProperties() {
152
            return this.properties.getProperties();
153
        }
154
        
155
    }
156
    
157
    private class FormActionContext extends AbstractDALActionContext {
158

    
159
        public FormActionContext() {
160
            super(ACTION_CONTEXT_NAME);
161
        }
162

    
163
        @Override
164
        public DataStore getStore() {
165
            return store;
166
        }
167

    
168
        @Override
169
        public FeatureQuery getQuery() {
170
            if(currentQuery == null) {
171
                Feature f = getCurrentFeature();
172
                if(f!=null){
173
                    FeatureQuery query = store.createFeatureQuery(f.createFilter());
174
                    return query;
175
                }
176
            }
177
            return currentQuery;
178
        }
179

    
180
        @Override
181
        public int getSelectedsCount() {
182
            return getCurrentFeature()==null?0:1;
183
        }
184

    
185
        @Override
186
        public Expression getFilterForSelecteds() {
187
            Feature f = getCurrentFeature();
188
            if(f!=null){
189
                Expression filter = f.createFilter();
190
                return filter;
191
            }
192
            return null;
193
        }
194

    
195
        @Override
196
        public FeatureSelection getSelecteds() {
197
            try {
198
                FeatureSelection selection = getFeatureStore().createFeatureSelection();
199
                selection.select(getCurrentFeature());
200
                return selection;
201
            } catch (DataException ex) {
202
                return null;
203
            }
204
        }
205

    
206
        @Override
207
        public JComponent getActionButton(String actionName) {
208
            if (formset == null) {
209
                return null;
210
            }
211
            return formset.getActionButton(actionName);
212
        }
213

    
214
    }
215

    
216
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class);
217

    
218
    private static final int PAGE_SIZE = 10;
219
    private JFeaturesFormPanel panel;
220
    private JDynFormSet formset;
221
    private FeatureStore store;
222
    private boolean needrefresh;
223
    private FeaturePagingHelper features;
224
    private DynFormDefinition definition = null;
225
    private FeatureQuery currentQuery;
226
    private List<Action> otherActions;
227
    private final ActionListenerSupport actionListeners;
228
    private final PropertiesSupportHelper propertiesHelper;
229
    private boolean terminateEditingOnClose = true;
230
    private boolean ignoreTerminateEditingOnClose = false;
231

    
232
    @SuppressWarnings("LeakingThisInConstructor")
233
    public DefaultJFeaturesForm() {
234
        this.otherActions = new ArrayList<>();
235
        this.propertiesHelper = new PropertiesSupportHelper();
236
        this.propertiesHelper.setProperty("FeaturesForm", this);
237
        this.panel = new JFeaturesFormPanel(this.propertiesHelper, new BorderLayout());
238
        this.panel.addComponentListener(new ComponentAdapter() {
239
            @Override
240
            public void componentHidden(ComponentEvent e) {
241
                onClose();
242
                dispose();
243
            }
244
        });
245
        this.actionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
246
    }
247

    
248
    public static void selfRegister() {
249

    
250
    }
251

    
252
    @Override
253
    public void setPreferredSize(Dimension dimension) {
254
        panel.setPreferredSize(dimension);
255
    }
256

    
257
    private void updateForm() {
258
        if (this.formset == null) {
259
            this.getFormset();
260
        }
261
        try {
262
            if (this.currentQuery != null) {
263
                this.currentQuery.retrievesAllAttributes();
264
            }
265
            this.features = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE);
266
            callUserEvent("form_beforeReload", this.formset.getForm());
267
            this.formset.setValues(features.asListOfDynObjects());
268
            callUserEvent("form_afterReload", this.formset.getForm());
269
        } catch (Exception ex) {
270
            throw new RuntimeException("Can't update form", ex);
271
        }
272
        this.needrefresh = false;
273
        updateButtonEnabledStatus();
274
    }
275

    
276
    @Override
277
    public JComponent asJComponent() {
278
        if (this.features == null) {
279
            try {
280
                updateForm();
281
            } catch (Exception ex) {
282
                throw new RuntimeException(ex);
283
            }
284
        }
285
        return this.panel;
286
    }
287

    
288
    @Override
289
    public void bind(FeatureStore store) {
290
        this.bind(store, null, null);
291
    }
292

    
293
    public void bind(FeatureStore store, DynClass definition) {
294
        this.bind(store, definition, null);
295
    }
296

    
297
    public void bind(FeatureStore store, DynClass definition, FeatureQuery query) {
298
        if (store == null) {
299
            throw new IllegalArgumentException("bind need a store as parameter, not a null.");
300
        }
301
        if (this.store == store) {
302
            return;
303
        }
304
        try {
305
            if(store.isEditing()){
306
                this.ignoreTerminateEditingOnClose = true;
307
            }
308
            if (definition == null) {
309
                DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager();
310
                definition = manager.featureType2DynClass(store, store.getDefaultFeatureType());
311
            }
312
            DynFormManager formManager = DynFormLocator.getDynFormManager();
313
            this.definition = formManager.getDefinition(definition);
314
            if (formset != null) {
315
                this.panel.remove(formset.asJComponent());
316
                this.panel.revalidate();
317
                this.formset = null;
318
            }
319
            this.store = store;
320
            DisposeUtils.bind(store);
321
            this.currentQuery = query;
322
            if (features != null) {
323
                this.features = null;
324
                updateForm();
325
            }            
326
        } catch (Exception ex) {
327
            throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex);
328
        }
329
    }
330

    
331
    @Override
332
    public void dispose() {
333
        if( this.formset!=null ) {
334
            Action action = this.formset.getAction(FINISHEDITING_ACTION);
335
            if( action!=null ) {
336
                DisposeUtils.disposeQuietly(action);
337
            }
338
            action = this.formset.getAction(STARTEDITING_ACTION);
339
            if( action!=null ) {
340
                DisposeUtils.disposeQuietly(action);
341
            }
342
        }
343
        if( this.panel!=null ) {
344
            this.panel.setVisible(false);
345
        }
346
        DisposeUtils.disposeQuietly(store);
347
        this.store = null;
348
        this.panel = null;
349
        this.formset = null;
350
        this.features = null;
351
        this.definition = null;
352
        this.currentQuery = null;
353
        this.otherActions = null;
354
    }
355

    
356
    public static class MyFeaturesFormContext extends DefaultFeaturesFormContext {
357

    
358
        private final DefaultJFeaturesForm featuresForm;
359
        
360
        public MyFeaturesFormContext(DefaultJFeaturesForm featuresForm) {
361
            super(featuresForm.store);
362
            this.featuresForm = featuresForm;
363
        }
364
        
365
        @Override
366
        public JFeaturesForm getFeaturesForm() {
367
            return this.featuresForm;
368
        }
369
        @Override
370
        public Feature getCurrentFeature() {    
371
            return this.featuresForm.getCurrentFeature();
372
        }
373
    }
374
    
375
    @Override
376
    public JDynFormSet getFormset() {
377
        if (this.formset == null) {
378
            DynFormManager formManager = DynFormLocator.getDynFormManager();
379
            this.formset = formManager.createJDynFormSet(
380
                    new MyFeaturesFormContext(this),
381
                    this.definition,
382
                    null
383
            );
384
            if(!this.definition.getTags().has(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE)){
385
                this.formset.setLayoutMode(JDynForm.USE_TABS);
386
            }
387
            
388
            List<String> groups = this.definition.getGroups();
389
            if ((this.formset.getLayoutMode() == JDynForm.USE_TABS || this.formset.getLayoutMode() == JDynForm.USE_SEPARATORS) && groups.size() == 1 && groups.get(0) == null) {
390
                this.formset.setLayoutMode(JDynForm.USE_PLAIN);
391
            }
392
            
393
            JComponent component = this.formset.getActionButton(JDynFormSet.ACTION_SET_CURRENT_RECORD);
394
            if( component!=null ) {
395
                I18nManager i18n = ToolsLocator.getI18nManager();
396
                component.setToolTipText(
397
                    "<html>" +
398
                    i18n.getTranslation("_Table") + ": " + this.store.getName() +"<br>\n"+
399
                    "( "+this.store.getFullName() + " )" +
400
                    "</html>"
401
                );
402
            }
403
            
404
            this.formset.setAllowNew(true);
405
            this.formset.setAllowDelete(true);
406
            this.formset.setAllowUpdate(true);
407
            this.formset.setAllowClose(true);
408
            this.formset.setAllowSearch(true);
409
            this.formset.setAutosave(true);
410

    
411
            this.formset.addAction(new StartEditingAction());
412
            this.formset.addAction(new FinishEditingAction());
413
            this.formset.addAction(new RefreshAction());
414

    
415
            FormActionContext actionContext = new FormActionContext();
416
            actionContext.set("featuresform", this);
417
            try {
418
                Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getApplicableStoreActions(actionContext);
419
                for (DALActionFactory factory : factories) {
420
                    if (StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME)) {
421
                        continue;
422
                    }
423
                    try {
424
                        Action action = factory.createAction(actionContext);
425
                        this.formset.addAction(action);
426
                    } catch (Exception ex) {
427
                        LOGGER.warn("Can't add action " + factory.getName(), ex);
428
                    }
429

    
430
                }
431
            } catch (Exception ex) {
432
                LOGGER.warn("Can't add actions", ex);
433
            }
434
            for (Action action : this.otherActions) {
435
                this.formset.addAction(action);
436
            }
437

    
438
            this.formset.addListener(new FormSetListener());
439
            this.formset.getForm().addListener(new JDynForm.JDynFormListener() {
440
                @Override
441
                public void message(String string) {
442
                }
443

    
444
                @Override
445
                public void fieldChanged(JDynFormField jdff) {
446
                    updateButtonEnabledStatus();
447
                }
448
            });
449

    
450
            ToolsSwingLocator.getToolsSwingManager().removeBorder(this.formset.asJComponent());
451
            this.panel.add(this.formset.asJComponent(), BorderLayout.CENTER);
452
            this.panel.revalidate();
453
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
454
            ToolsSwingUtils.ensureHeightWitdh(this.panel, 256, 400, screenSize.height-100, screenSize.width-60);
455
        }
456
        updateButtonEnabledStatus();
457
        return this.formset;
458
    }
459

    
460
    @Override
461
    public void addAction(Action action) {
462
        this.otherActions.add(action);
463
        if (this.formset != null) {
464
            this.formset.addAction(action);
465
        }
466
    }
467

    
468
    @Override
469
    public long getCurrentIndex() {
470
        if (this.formset == null) {
471
            return -1;
472
        }
473
        return this.formset.getCurrentIndex();
474
    }
475

    
476
    @Override
477
    public Feature get(long index) {
478
        if (this.formset == null || this.features == null) {
479
            return null;
480
        }
481
        try {
482
            return this.features.getFeatureAt(index);
483
        } catch (BaseException ex) {
484
            return null;
485
        }
486
    }
487

    
488
    private class RefreshAction extends AbstractAction {
489

    
490
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
491
        public RefreshAction() {
492
            I18nManager i18nManager = ToolsLocator.getI18nManager();
493
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
494

    
495
            this.putValue(NAME, null);
496
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Reload_data"));
497
            this.putValue(SMALL_ICON, iconTheme.get("common-form-refresh"));
498
            this.putValue(ACTION_COMMAND_KEY, REFRESHFORM_ACTION);
499
            this.setEnabled(!formset.isInNewState());
500
        }
501

    
502
        @Override
503
        public void actionPerformed(ActionEvent ae) {
504
            try {
505
                I18nManager i18n = ToolsLocator.getI18nManager();
506
                formset.message(i18n.getTranslation("_Form_reloaded"));
507
                int x=formset.getCurrentIndex();
508
                updateForm();
509
                formset.setCurrentIndex(x);
510
            } catch (Exception ex) {
511
                LOGGER.warn("Can't reload form", ex);
512
            }
513
        }
514
    }
515

    
516
    private class FinishEditingAction extends AbstractAction implements Observer, Disposable {
517

    
518
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
519
        public FinishEditingAction() {
520
            I18nManager i18nManager = ToolsLocator.getI18nManager();
521
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
522

    
523
            this.putValue(NAME, null);
524
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Stop_editing"));
525
            this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
526
            this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
527

    
528
            this.setEnabled(store.isEditing());
529
            store.addObserver(this);
530
        }
531

    
532
        @Override
533
        public void actionPerformed(ActionEvent ae) {
534
            if (store == null || formset == null) {
535
                return;
536
            }
537
            if (store.isEditing()) {
538
                EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
539
                DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
540
                ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
541
                I18nManager i18nManager = ToolsLocator.getI18nManager();
542
                try {
543
                    int index;
544
                    int x = dataSwingManager.askUserStopEditing(formset.asJComponent(), store, true);
545
                    switch (x) {
546
                        case DataSwingManager.STOP_EDITING_SAVE:
547
                            EditingNotification notification = editingManager.notifyObservers(this, BEFORE_EXIT_EDITING_STORE, null, store);
548
                            if( notification.isCanceled() || notification.isAborted()) {
549
                                I18nManager i18n = ToolsLocator.getI18nManager();
550
                                formset.message(i18n.getTranslation("Finish editing has been cancelled."));
551
                                return;
552
                            }                
553
                            store.finishEditing();
554
                            editingManager.notifyObservers(this, AFTER_EXIT_EDITING_STORE, null, store);
555
                            index = formset.getCurrentIndex();
556
                            try {
557
                                updateForm();
558
                                formset.setCurrentIndex(index);
559
                            } catch (Exception ex) {
560
                                LOGGER.warn("Can't reload form data after edit.", ex);
561
                            }
562
                            break;
563
                        case DataSwingManager.STOP_EDITING_CONTINUE:
564
                            break;
565
                        case DataSwingManager.STOP_EDITING_DISCARD:
566
                            store.cancelEditing();
567
                            index = formset.getCurrentIndex();
568
                            try {
569
                                updateForm();
570
                                formset.setCurrentIndex(index);                                
571
                            } catch (IndexOutOfBoundsException ex) {
572
                                LOGGER.debug("Can't reload form data after discard edit.",ex);
573
                            } catch (Exception ex) {
574
                                LOGGER.warn("Can't reload form data after discard edit.", ex);
575
                            }
576
                            break;
577
                        case DataSwingManager.STOP_EDITING_EXPORT:
578
                            if( !((DefaultDataSwingManager)dataSwingManager).exportStore(store, MODE.DIALOG) ) {
579
                                // Mensaje de operacion no soportada
580
                            }
581
                    }
582
                } catch (Exception ex) {
583
                    LOGGER.warn("Can't finish editing in FeatureForm (" + store.getName() + ").", ex);
584
                    dialogManager.messageDialog(
585
                            i18nManager.getTranslation("_Problems_finish_table_editing") + "\n\n"
586
                            + i18nManager.getTranslation("_see_error_log_for_more_information"),
587
                            i18nManager.getTranslation("_Stop_editing"),
588
                            JOptionPane.ERROR_MESSAGE
589
                    );
590
                }
591
            }
592
            updateButtonEnabledStatus();
593
        }
594

    
595
        @Override
596
        public void update(Observable observable, Object notification) {
597
            if (store == null || formset == null) {
598
                return;
599
            }
600
            if (notification instanceof FeatureStoreNotification) {
601
                FeatureStoreNotification n = (FeatureStoreNotification) notification;
602
                switch (n.getType()) {
603
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
604
                        needrefresh = true;
605
                    case FeatureStoreNotification.AFTER_STARTEDITING:
606
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
607
                        updateButtonEnabledStatus();
608
                        break;
609
                }
610
            }
611
        }
612

    
613
        @Override
614
        public void dispose() {
615
            store.deleteObserver(this);
616
        }
617

    
618
    }
619

    
620
    private class StartEditingAction extends AbstractAction implements Observer, Disposable {
621

    
622
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
623
        public StartEditingAction() {
624
            I18nManager i18nManager = ToolsLocator.getI18nManager();
625
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
626

    
627
            this.putValue(NAME, null);
628
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Start_editing"));
629
            this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
630
            this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
631

    
632
            this.setEnabled(!store.isEditing());
633
            store.addObserver(this);
634
        }
635

    
636
        @Override
637
        public void actionPerformed(ActionEvent ae) {
638
            if (store == null || formset == null) {
639
                return;
640
            }
641
            if (!store.isEditing()) {
642
                try {
643
                    EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
644
                    EditingNotification notification = editingManager.notifyObservers(this, BEFORE_ENTER_EDITING_STORE, null, store);
645
                    if( notification.isCanceled() || notification.isAborted() ) {
646
                        I18nManager i18n = ToolsLocator.getI18nManager();
647
                        formset.message(i18n.getTranslation("Enter editing has been cancelled."));
648
                        return;
649
                    }                
650
                    store.edit();
651
                    editingManager.notifyObservers(this, AFTER_ENTER_EDITING_STORE, null, store);
652
                } catch (DataException ex) {
653
                    LOGGER.warn("Can't finish editing in FeatureForm (" + store.getName() + ").", ex);
654
                }
655
            }
656
            updateButtonEnabledStatus();
657
        }
658

    
659
        @Override
660
        public void update(Observable observable, Object notification) {
661
            if (store == null || formset == null) {
662
                return;
663
            }
664
            if (notification instanceof FeatureStoreNotification) {
665
                FeatureStoreNotification n = (FeatureStoreNotification) notification;
666
                switch (n.getType()) {
667
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
668
                        needrefresh = true;
669
                    case FeatureStoreNotification.AFTER_STARTEDITING:
670
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
671
                        updateButtonEnabledStatus();
672
                        break;
673
                }
674
            }
675
        }
676

    
677
        @Override
678
        public void dispose() {
679
            store.deleteObserver(this);
680
        }
681

    
682
    }
683

    
684
    @Override
685
    public void setQuery(FeatureQuery query) {
686
        if (this.features != null) {
687
            if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) {
688
                if(this.formset.getForm().isModified()){
689
                    if (!store.isEditing()) {
690
                        try {
691
                            store.edit();
692
                        } catch (DataException e1) {
693
                            throw new RuntimeException("Can't set query", e1);
694
                        }
695
                    }
696
                    saveChanges(this.formset);
697
                }
698
            }
699
        }
700
        this.currentQuery = query;
701
        updateForm();
702
    }
703

    
704
    private FeatureQuery getCurrentQuery() {
705
        return this.currentQuery;
706
    }
707

    
708
    @Override
709
    public void showForm(MODE mode) {
710
        I18nManager i18n = ToolsLocator.getI18nManager();
711
        String title = i18n.getTranslation("_Form") + ": "+ this.definition.getLabel();
712
        showForm(title, mode);
713
    }
714

    
715
    @Override
716
    public void showForm(String title, MODE mode) {
717
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
718
        if( StringUtils.isBlank(title) ) {
719
            title = this.definition.getLabel();
720
        }
721
        winmgr.showWindow(this.asJComponent(), title, mode);
722
    }
723
    
724
    @Override
725
    public void hideForm() {
726
        DisposeUtils.dispose(this);
727
    }
728

    
729
    private void saveChanges(JDynFormSet theFormSet) {
730
        I18nManager i18n = ToolsLocator.getI18nManager();
731
        try {
732
            EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
733
            if (theFormSet.isInNewState()) {
734
                EditableFeature newFeature = store.createNewFeature(false);
735
                JDynForm form = formset.getForm();
736
                for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
737
                    String name = attr.getName();
738
                    if( !newFeature.canSetValue(name) ) {
739
                        continue;
740
                    }
741
                    JDynFormField field = form.getField(name);
742
                    if( field==null ) {
743
                        // The attribute is not in form, set default value
744
                        newFeature.set(name, attr.getDefaultValueCoerced());
745
                    } else {
746
                        try {
747
                            Object value = field.getValue();
748
                            newFeature.set(name, value);
749
                        } catch (Exception ex) {
750
                            LOGGER.warn("Can't get value of field '" + name + "'.", ex);
751
                        }
752
                    }
753
                }
754
                EditingNotification notification = editingManager.notifyObservers(this, BEFORE_INSERT_FEATURE, null, this.store, newFeature);
755
                if( notification.isAborted()) {
756
                    theFormSet.message(i18n.getTranslation("Problems occurred while saving the data."));
757
                    return;
758
                }                
759
                if( notification.isCanceled() ) {
760
                    theFormSet.message(i18n.getTranslation("Data saving has been cancelled."));
761
                    return;
762
                }                
763
                if (!editingManager.validateFeature(newFeature)) {
764
                    theFormSet.message(i18n.getTranslation("The data cannot be saved. Check that all the required fields are filled out.."));
765
                    return;
766
                }
767
                features.insert(newFeature);
768
                editingManager.notifyObservers(this, AFTER_INSERT_FEATURE, null, this.store, newFeature);
769
                
770
                this.formset.message(i18n.getTranslation("_Record_saved"));
771
                try {
772
                    this.formset.setValues(features.asListOfDynObjects());
773
                    this.formset.setCurrentIndex((int) (features.getTotalSize()) - 1);
774
                } catch (Exception ex) {
775
                    LOGGER.warn("Can't reload form data after insert.", ex);
776
                }
777
            } else {
778
                int index = theFormSet.getCurrentIndex();
779
                DynObject currentElement = theFormSet.get(index);
780
                theFormSet.getFormValues(currentElement);
781

    
782
                EditableFeature feature = ((FacadeOfAFeature) currentElement).getEditableFeature();
783
                EditingNotification notification = editingManager.notifyObservers(this, BEFORE_UPDATE_FEATURE, null, this.store, feature);
784
                if( notification.isAborted()) {
785
                    theFormSet.message(i18n.getTranslation("Problems occurred while saving the data."));
786
                    return;
787
                }                
788
                if( notification.isCanceled() ) {
789
                    theFormSet.message(i18n.getTranslation("Data saving has been cancelled."));
790
                    return;
791
                }              
792
                if (!editingManager.validateFeature(feature)) {
793
                    theFormSet.message(i18n.getTranslation("The data cannot be saved. Check that all the required fields are filled out.."));
794
                    return;
795
                }
796
                features.update(feature);
797
                editingManager.notifyObservers(this, AFTER_UPDATE_FEATURE, null, this.store, feature);
798

    
799
                this.formset.message(i18n.getTranslation("_Record_saved"));
800
                try {
801
                    this.formset.setCurrentIndex(index);
802
                } catch (Exception ex) {
803
                    LOGGER.warn("Can't reload form data after insert.", ex);
804
                }
805
            }
806
        } catch (Exception ex) {
807
            theFormSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
808
            throw new RuntimeException("Can't save values", ex);
809

    
810
        } finally {
811
            updateButtonEnabledStatus();
812
        }
813

    
814
    }
815

    
816
    private void deleteCurrentElement(JDynFormSet theFormSet) {
817
        I18nManager i18n = ToolsLocator.getI18nManager();
818
        try {
819
            EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
820
            int index = theFormSet.getCurrentIndex();
821
            DynObject currentElement = theFormSet.get(index);
822
            theFormSet.getFormValues(currentElement);
823
            Feature feature = ((FacadeOfAFeature) currentElement).getFeature();
824
            if (feature instanceof EditableFeature) {
825
                feature = ((EditableFeature) feature).getNotEditableCopy();
826
            }
827
            if( editingManager.notifyObservers(this, BEFORE_REMOVE_FEATURE, null, this.store, feature).isCanceled() ) {
828
                theFormSet.message(i18n.getTranslation("The delete operation has been cancelled."));
829
                return;
830
            }                
831
            features.delete(feature);
832
            editingManager.notifyObservers(this, AFTER_REMOVE_FEATURE, null, this.store, feature);
833

    
834
            this.formset.message(i18n.getTranslation("_Record_removed"));
835

    
836
            this.formset.setValues(features.asListOfDynObjects());
837
            if (features.getTotalSize() - 1 < index) {
838
                index = index - 1;
839
            }
840
            this.formset.setCurrentIndex(index);
841

    
842
        } catch (Exception ex) {
843
            theFormSet.message(i18n.getTranslation("error_removing_data_will_not_remove"));
844
            throw new RuntimeException("Can't remove values", ex);
845

    
846
        } finally {
847
            updateButtonEnabledStatus();
848
        }
849

    
850
    }
851

    
852
    @Override
853
    public void saveChanges() {
854
        if (this.formset != null && this.formset.countValues() > 0) {
855
            if (store != null && !store.isEditing()) {
856
                try {
857
                    store.edit();
858
                } catch (DataException e1) {
859
                    LOGGER.warn("Can't edit the store " + store.getName());
860
                    throw new RuntimeException("Can't save changes.", e1);
861
                }
862
            }
863
            this.saveChanges(this.formset);
864
        }
865
    }
866

    
867
    private void updateButtonEnabledStatus() {
868
        if (this.formset == null) {
869
            return;
870
        }
871
        if (this.store == null || store.isBroken() || store.isAppending() || this.features == null) {
872
            this.formset.setReadOnly(true);
873
            formset.setActionVisible(STARTEDITING_ACTION, true);
874
            formset.setActionEnabled(STARTEDITING_ACTION, true);
875
            formset.setActionVisible(FINISHEDITING_ACTION, false);
876
            formset.setActionEnabled(FINISHEDITING_ACTION, false);
877
            formset.setActionEnabled(ACTION_DELETE, false);
878
            formset.setActionEnabled(ACTION_NEW, false);
879
            formset.setActionEnabled(ACTION_CANCEL_NEW, false);
880
            formset.setActionEnabled(ACTION_SAVE, false);
881
            formset.setActionEnabled(ACTION_SEARCH, false);
882
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
883
            formset.setActionEnabled(ACTION_NAVIGATION, false);
884
            return;
885
        }
886
        if (this.needrefresh ) {
887
            this.formset.setReadOnly(true);
888
//            formset.setActionEnabled(STARTEDITING_ACTION, false);
889
//            formset.setActionEnabled(FINISHEDITING_ACTION, false);
890
            formset.setActionEnabled(ACTION_DELETE, false);
891
            formset.setActionEnabled(ACTION_NEW, false);
892
            formset.setActionEnabled(ACTION_CANCEL_NEW, false);
893
            formset.setActionEnabled(ACTION_SAVE, false);
894
            formset.setActionEnabled(ACTION_SEARCH, false);
895
            formset.setActionEnabled(ACTION_NAVIGATION, false);
896
            for (Action action : formset.getActions()) {
897
                formset.setActionEnabled((String) action.getValue(ACTION_COMMAND_KEY), false);
898
            }
899
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
900
            return;
901
        }
902
        if (store.isEditing()) {
903
            this.formset.setReadOnly(false);
904
            formset.setActionVisible(STARTEDITING_ACTION, false);
905
            formset.setActionEnabled(STARTEDITING_ACTION, false);
906
            formset.setActionVisible(FINISHEDITING_ACTION, true);
907
            formset.setActionEnabled(FINISHEDITING_ACTION, true);
908
            if (formset.isInNewState()) {
909
                formset.setActionEnabled(ACTION_DELETE, false);
910
                formset.setActionEnabled(ACTION_SEARCH, false);
911
                formset.setActionEnabled(ACTION_NEW, false);
912
                formset.setActionEnabled(ACTION_CANCEL_NEW, true);
913
                formset.setActionEnabled(ACTION_SAVE, true);
914
                formset.setActionEnabled(FINISHEDITING_ACTION, false);
915
                formset.setActionEnabled(REFRESHFORM_ACTION, false);
916
//                setEnabledUniqueFields(true);
917

    
918
            } else if (this.features != null && this.features.isEmpty()) {
919
                formset.getForm().setReadOnly(true);
920
                formset.setActionEnabled(ACTION_DELETE, false);
921
                formset.setActionEnabled(ACTION_SEARCH, false);
922
                formset.setActionEnabled(ACTION_NEW, true);
923
                formset.setActionEnabled(ACTION_CANCEL_NEW, true);
924
                formset.setActionEnabled(ACTION_SAVE, false);
925
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
926
                formset.setActionEnabled(FINISHEDITING_ACTION, true);         
927
                formset.setActionEnabled(ACTION_NAVIGATION, false);
928

    
929
            } else if (formset.getForm().isModified()) {
930
                formset.setActionEnabled(ACTION_DELETE, false);
931
                formset.setActionEnabled(ACTION_SEARCH, false);
932
                formset.setActionEnabled(ACTION_NEW, false);
933
                formset.setActionEnabled(ACTION_CANCEL_NEW, false);
934
                formset.setActionEnabled(ACTION_SAVE, true);
935
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
936
                formset.setActionEnabled(FINISHEDITING_ACTION, false);
937
                formset.setActionEnabled(ACTION_NAVIGATION, false);
938

    
939
            } else {
940
                formset.setActionEnabled(ACTION_DELETE, true);
941
                formset.setActionEnabled(ACTION_SEARCH, true);
942
                formset.setActionEnabled(ACTION_NEW, true);
943
                formset.setActionEnabled(ACTION_CANCEL_NEW, false);
944
                formset.setActionEnabled(ACTION_SAVE, false);
945
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
946
                formset.setActionEnabled(FINISHEDITING_ACTION, true);
947
                formset.setActionEnabled(ACTION_NAVIGATION, true);
948
            }
949

    
950
            return;
951
        }
952
        this.formset.setReadOnly(true);
953
        formset.setActionVisible(STARTEDITING_ACTION, true);
954
        formset.setActionEnabled(STARTEDITING_ACTION, true);
955
        formset.setActionVisible(FINISHEDITING_ACTION, false);
956
        formset.setActionEnabled(FINISHEDITING_ACTION, false);
957
        formset.setActionEnabled(ACTION_DELETE, false);
958
        formset.setActionEnabled(ACTION_NEW, false);
959
        formset.setActionEnabled(ACTION_CANCEL_NEW, false);
960
        formset.setActionEnabled(ACTION_SAVE, false);
961
        formset.setActionEnabled(ACTION_SEARCH, true);
962
        formset.setActionEnabled(REFRESHFORM_ACTION, true);
963
        for (Action action : formset.getActions()) {
964
            formset.setActionEnabled((String) action.getValue(ACTION_COMMAND_KEY), true);
965
        }
966
        
967
    }
968

    
969
    private void clearUniqueFields() {
970
        for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
971
            if ((attr.isPrimaryKey() && !attr.isAutomatic()) || (attr.isIndexed() && !attr.allowIndexDuplicateds())) {
972
                JDynFormField field = formset.getForm().getField(attr.getName());
973
                if (field != null) {
974
                    field.clear();
975
                }
976
            }
977
        }
978
    }
979

    
980
    private void setEnabledUniqueFields(boolean enabled) {
981
        for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
982
            if ((attr.isPrimaryKey() && !attr.isAutomatic()) || (attr.isIndexed() && !attr.allowIndexDuplicateds())) {
983
                JDynFormField field = formset.getForm().getField(attr.getName());
984
                if (field != null) {
985
                    field.setReadOnly(!enabled);
986
                }
987
            }
988
        }
989
    }
990

    
991
    @Override
992
    public long getDataSetSize() {
993
        if (this.features != null) {
994
            return features.getTotalSize();
995
        }
996
        return 0;
997
    }
998

    
999
    @Override
1000
    public FeatureStore getFeatureStore() {
1001
        return this.store;
1002
    }
1003
    
1004
    public FeatureType getFeatureType() {
1005
        if( this.store == null ) {
1006
            return null;
1007
        }
1008
        return this.store.getDefaultFeatureTypeQuietly();
1009
    }
1010

    
1011
    private class FormSetListener implements JDynFormSetListener {
1012

    
1013
        @Override
1014
        public void formMessage(String message) {
1015
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formMessage"));
1016
        }
1017

    
1018
        @Override
1019
        public void formClose() {
1020
            panel.setVisible(false);
1021
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formClose"));
1022
        }
1023

    
1024
        @Override
1025
        public void formMovedTo(int currentPosition) throws AbortActionException {
1026
            LOGGER.trace("formMovedTo " + currentPosition);
1027
            updateButtonEnabledStatus();
1028
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formMovedTo"));
1029
        }
1030

    
1031
        @Override
1032
        public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException {
1033
            LOGGER.trace("formBeforeSave");
1034
            saveChanges(dynformSet);
1035
            updateButtonEnabledStatus();
1036
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeSave"));
1037
        }
1038

    
1039
        @Override
1040
        public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException {
1041
            LOGGER.trace("formBeforeNew");
1042
            clearUniqueFields();
1043
            updateButtonEnabledStatus();
1044
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeNew"));
1045
        }
1046

    
1047
        @Override
1048
        public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException {
1049
            LOGGER.trace("formBeforeDelete");
1050
            updateButtonEnabledStatus();
1051
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeDelete"));
1052
        }
1053

    
1054
        @Override
1055
        public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException {
1056
            LOGGER.trace("formAfterSave");
1057
            updateButtonEnabledStatus();
1058
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterSave"));
1059
        }
1060

    
1061
        @Override
1062
        public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException {
1063
            LOGGER.trace("formAfterNew");
1064
            updateButtonEnabledStatus();
1065
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterNew"));
1066
        }
1067

    
1068
        @Override
1069
        public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
1070
            LOGGER.trace("formAfterDelete");
1071
            deleteCurrentElement(dynformSet);
1072
            updateButtonEnabledStatus();
1073
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterDelete"));
1074
        }
1075

    
1076
        @Override
1077
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
1078
            LOGGER.trace("formBeforeSearch");
1079
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
1080
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
1081

    
1082
            final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
1083
            searchPanel.setShowActions(false);
1084
            FeatureQuery currentQuery = getCurrentQuery();
1085
            if (currentQuery != null) {
1086
                Evaluator filter = currentQuery.getFilter();
1087
                if (filter instanceof ExpressionEvaluator) {
1088
                    Expression expression = ((ExpressionEvaluator) filter).toExpression();
1089
                    searchPanel.setFilter(expression);
1090
                    searchPanel.search();
1091
                }
1092
            }
1093
            ToolsSwingUtils.ensureHeightWitdh(
1094
                    searchPanel, 
1095
                    ToolsSwingUtils.RELATIVE_TO_SCREEN,
1096
                    0.75f, 0.75f, 0.85f, 0.85f
1097
            );          
1098
                             
1099
            I18nManager i18n = ToolsLocator.getI18nManager();
1100
            Dialog dialog = winmgr.createDialog(
1101
                    searchPanel.asJComponent(),
1102
                    i18n.getTranslation("_Filter"),
1103
                    i18n.getTranslation("_Creating_filter_for") + ": '" + store.getName() + "'",
1104
                    WindowManager_v2.BUTTONS_OK_CANCEL
1105
            );
1106
            dialog.show(WindowManager.MODE.DIALOG);
1107
            if (dialog.getAction() == WindowManager_v2.BUTTON_OK) {
1108
                SearchParameters params = searchPanel.fetch(null);
1109
                if (params.getQuery() != null) {
1110
                    FeatureQuery searchQuery = params.getQuery().getCopy();
1111

    
1112
                    Expression expresion = null;
1113
                    if (searchQuery != null) {
1114
                        expresion = searchQuery.getExpressionFilter();
1115
                    }
1116
                    try {
1117
                        FeatureQuery query = store.createFeatureQuery();
1118
                        if (ExpressionUtils.isPhraseEmpty(expresion)) {
1119
                            query.clearFilter();
1120
                        } else {
1121
                            query.setFilter(expresion);
1122
                        }
1123
                        setQuery(query);
1124
                    } catch (Exception ex) {
1125
                        LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
1126
                    }
1127
                } else {
1128
                    LOGGER.warn("FeatureQuery from parameters is null. Query is not applied to the JFeaturesFrom");
1129
                }
1130
            }
1131
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeSearch"));
1132
        }
1133

    
1134
        @Override
1135
        public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException {
1136
            LOGGER.trace("formAfterSearch");
1137
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterSearch"));
1138
        }
1139

    
1140
        @Override
1141
        public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException {
1142
            LOGGER.trace("formBeforeCancelNew");
1143
            updateButtonEnabledStatus();
1144
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeCancelNew"));
1145
        }
1146

    
1147
        @Override
1148
        public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException {
1149
            LOGGER.trace("formAfterCancelNew");
1150
            try {
1151
                int index = formset.getCurrentIndex();
1152
                if( index<0 ) {
1153
                    formset.getForm().clear();
1154
                } else {
1155
                    formset.setCurrentIndex(index);
1156
                }
1157
            } catch (Exception ex) {
1158
                LOGGER.warn("Can't reload form data after cancel new.", ex);
1159
            }            
1160
            updateButtonEnabledStatus();
1161
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterCancelNew"));
1162
        }
1163

    
1164
    }
1165

    
1166
    @Override
1167
    public void addActionListener(ActionListener listener) {
1168
        this.actionListeners.addActionListener(listener);
1169
    }
1170

    
1171
    @Override
1172
    public ActionListener[] getActionListeners() {
1173
        return this.actionListeners.getActionListeners();
1174
    }
1175

    
1176
    @Override
1177
    public void removeActionListener(ActionListener listener) {
1178
        this.actionListeners.removeActionListener(listener);
1179
    }
1180

    
1181
    @Override
1182
    public void removeAllActionListener() {
1183
        this.actionListeners.removeAllActionListener();
1184
    }
1185

    
1186
    @Override
1187
    public void fireActionEvent(ActionEvent event) {
1188
        this.actionListeners.fireActionEvent(event);
1189
    }
1190

    
1191
    @Override
1192
    public boolean hasActionListeners() {
1193
        return this.actionListeners.hasActionListeners();
1194
    }
1195

    
1196
    @Override
1197
    public Feature getCurrentFeature() {
1198
        // TODO: No tengo claro que esto sea correcto, ya que no tiene 
1199
        // en cuenta si el form se esta modificando o es nuevo para coger
1200
        // los valores del formulario.
1201
        long index = getCurrentIndex();
1202
        if( index<0 ) {
1203
            return null;
1204
        }
1205
        Feature f = get(index);
1206
        try {
1207
            DynObject adapter = f.getAsDynObject();
1208
            this.getFormset().getForm().getValues(adapter);
1209
            f = ((FacadeOfAFeature)adapter).getFeature();
1210
            return f;
1211
        } catch(Exception ex) {
1212
            return f;
1213
        }
1214
    }
1215

    
1216
    protected void callUserEvent(String name, Object...args) {
1217
//        try {
1218
            JDynForm form = this.formset.getForm();
1219
            form.callUserEvent(name, args);
1220
//        } catch(NoSuchMethodException ex) {
1221
//            LOGGER.debug("Function '"+name+"' not found.", ex);
1222
//            // Do nothing
1223
//        } catch(Exception ex) {
1224
//            LOGGER.warn("Error calling form event '"+name+"'.", ex);
1225
//        }
1226
    }
1227
    
1228
    @Override
1229
    public void setTerminateEditingOnClose(boolean b) {
1230
        terminateEditingOnClose = b;
1231
    }
1232

    
1233
    @Override
1234
    public boolean isTerminateEditingOnClose() {
1235
        return terminateEditingOnClose;
1236
    }
1237

    
1238
    private void onClose() {
1239
        if (this.store == null || this.ignoreTerminateEditingOnClose) {
1240
            return;
1241
        }
1242
        if (this.store.isEditing() && terminateEditingOnClose) {
1243
            DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
1244
            ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
1245
            I18nManager i18nManager = ToolsLocator.getI18nManager();
1246
            try {
1247
                int x = dataSwingManager.askUserStopEditing(formset.asJComponent(), store, false);
1248
                switch (x) {
1249
                    case DataSwingManager.STOP_EDITING_SAVE:
1250
                        store.finishEditing();
1251
                        break;
1252
                    case DataSwingManager.STOP_EDITING_CONTINUE:
1253
                        break;
1254
                    case DataSwingManager.STOP_EDITING_DISCARD:
1255
                        store.cancelEditing();
1256
                        break;
1257
                    case DataSwingManager.STOP_EDITING_EXPORT:
1258
                        if( !((DefaultDataSwingManager)dataSwingManager).exportStore(store, MODE.DIALOG) ) {
1259
                            // Mensaje de operacion no soportada
1260
                        }
1261
                }
1262
            } catch (Exception ex) {
1263
                LOGGER.warn("Can't finish editing in FeatureForm (" + DataStore.getNameQuietly(store) + ").", ex);
1264
                dialogManager.messageDialog(
1265
                        i18nManager.getTranslation("_Problems_finish_table_editing") + "\n\n"
1266
                        + i18nManager.getTranslation("_see_error_log_for_more_information"),
1267
                        i18nManager.getTranslation("_Stop_editing"),
1268
                        JOptionPane.ERROR_MESSAGE
1269
                );
1270
            }
1271

    
1272
        }
1273
    }
1274

    
1275

    
1276
}