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

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

    
119
/**
120
 * @author fdiaz
121
 *
122
 */
123
@SuppressWarnings("UseSpecificCatch")
124
public class DefaultJFeaturesForm implements JFeaturesForm, Disposable {
125

    
126
    private final String STARTEDITING_ACTION = "startEditing";
127
    private final String FINISHEDITING_ACTION = "finishEditing";
128
    private final String REFRESHFORM_ACTION = "refreshForm";
129
    
130
    private class JFeaturesFormPanel extends JPanel implements PropertiesSupport {
131

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

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

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

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

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

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

    
167
        @Override
168
        public FeatureQuery getQuery() {
169
            return currentQuery;
170
        }
171

    
172
        @Override
173
        public int getSelectedsCount() {
174
            return 0;
175
        }
176

    
177
        @Override
178
        public Expression getFilterForSelecteds() {
179
            return null;
180
        }
181

    
182
        @Override
183
        public JComponent getActionButton(String actionName) {
184
            if (formset == null) {
185
                return null;
186
            }
187
            return formset.getActionButton(actionName);
188
        }
189

    
190
    }
191

    
192
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class);
193

    
194
    private static final int PAGE_SIZE = 10;
195
    private JFeaturesFormPanel panel;
196
    private JDynFormSet formset;
197
    private FeatureStore store;
198
    private boolean needrefresh;
199
    private FeaturePagingHelper features;
200
    private DynFormDefinition definition = null;
201
    private FeatureQuery currentQuery;
202
    private List<Action> otherActions;
203
    private final ActionListenerSupport actionListeners;
204
    private final PropertiesSupportHelper propertiesHelper;
205
    private boolean terminateEditingOnClose = true;
206
    private boolean ignoreTerminateEditingOnClose = false;
207

    
208
    @SuppressWarnings("LeakingThisInConstructor")
209
    public DefaultJFeaturesForm() {
210
        this.otherActions = new ArrayList<>();
211
        this.propertiesHelper = new PropertiesSupportHelper();
212
        this.propertiesHelper.setProperty("FeaturesForm", this);
213
        this.panel = new JFeaturesFormPanel(this.propertiesHelper, new BorderLayout());
214
        this.panel.addComponentListener(new ComponentAdapter() {
215
            @Override
216
            public void componentHidden(ComponentEvent e) {
217
                onClose();
218
                dispose();
219
            }
220
        });
221
        this.actionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
222
    }
223

    
224
    public static void selfRegister() {
225

    
226
    }
227

    
228
    @Override
229
    public void setPreferredSize(Dimension dimension) {
230
        panel.setPreferredSize(dimension);
231
    }
232

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

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

    
264
    @Override
265
    public void bind(FeatureStore store) {
266
        this.bind(store, null, null);
267
    }
268

    
269
    public void bind(FeatureStore store, DynClass definition) {
270
        this.bind(store, definition, null);
271
    }
272

    
273
    public void bind(FeatureStore store, DynClass definition, FeatureQuery query) {
274
        if (store == null) {
275
            throw new IllegalArgumentException("bind need a store as parameter, not a null.");
276
        }
277
        if (this.store == store) {
278
            return;
279
        }
280
        try {
281
            if(store.isEditing()){
282
                this.ignoreTerminateEditingOnClose = true;
283
            }
284
            if (definition == null) {
285
                DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager();
286
                definition = manager.featureType2DynClass(store, store.getDefaultFeatureType());
287
            }
288
            DynFormManager formManager = DynFormLocator.getDynFormManager();
289
            this.definition = formManager.getDefinition(definition);
290
            if (formset != null) {
291
                this.panel.remove(formset.asJComponent());
292
                this.panel.revalidate();
293
                this.formset = null;
294
            }
295
            this.store = store;
296
            DisposeUtils.bind(store);
297
            this.currentQuery = query;
298
            if (features != null) {
299
                this.features = null;
300
                updateForm();
301
            }            
302
        } catch (Exception ex) {
303
            throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex);
304
        }
305
    }
306

    
307
    @Override
308
    public void dispose() {
309
        if( this.formset!=null ) {
310
            Action action = this.formset.getAction(FINISHEDITING_ACTION);
311
            if( action!=null ) {
312
                DisposeUtils.disposeQuietly(action);
313
            }
314
            action = this.formset.getAction(STARTEDITING_ACTION);
315
            if( action!=null ) {
316
                DisposeUtils.disposeQuietly(action);
317
            }
318
        }
319
        if( this.panel!=null ) {
320
            this.panel.setVisible(false);
321
        }
322
        DisposeUtils.disposeQuietly(store);
323
        this.store = null;
324
        this.panel = null;
325
        this.formset = null;
326
        this.features = null;
327
        this.definition = null;
328
        this.currentQuery = null;
329
        this.otherActions = null;
330
    }
331

    
332
    public static class MyFeaturesFormContext extends DefaultFeaturesFormContext {
333

    
334
        private final DefaultJFeaturesForm featuresForm;
335
        
336
        public MyFeaturesFormContext(DefaultJFeaturesForm featuresForm) {
337
            super(featuresForm.store);
338
            this.featuresForm = featuresForm;
339
        }
340
        
341
        @Override
342
        public JFeaturesForm getFeaturesForm() {
343
            return this.featuresForm;
344
        }
345
        @Override
346
        public Feature getCurrentFeature() {    
347
            return this.featuresForm.getCurrentFeature();
348
        }
349
    }
350
    
351
    @Override
352
    public JDynFormSet getFormset() {
353
        if (this.formset == null) {
354
            DynFormManager formManager = DynFormLocator.getDynFormManager();
355
            this.formset = formManager.createJDynFormSet(
356
                    new MyFeaturesFormContext(this),
357
                    this.definition,
358
                    null
359
            );
360
            List<String> groups = this.definition.getGroups();
361
            if (groups.size() == 1 && groups.get(0) == null) {
362
                this.formset.setLayoutMode(JDynForm.USE_PLAIN);
363
            } else {
364
                this.formset.setLayoutMode(JDynForm.USE_TABS);
365
            }
366
            JComponent component = this.formset.getActionButton(JDynFormSet.ACTION_SET_CURRENT_RECORD);
367
            if( component!=null ) {
368
                I18nManager i18n = ToolsLocator.getI18nManager();
369
                component.setToolTipText(
370
                    "<html>" +
371
                    i18n.getTranslation("_Table") + ": " + this.store.getName() +"<br>\n"+
372
                    "( "+this.store.getFullName() + " )" +
373
                    "</html>"
374
                );
375
            }
376
            
377
            this.formset.setAllowNew(true);
378
            this.formset.setAllowDelete(true);
379
            this.formset.setAllowUpdate(true);
380
            this.formset.setAllowClose(true);
381
            this.formset.setAllowSearch(true);
382
            this.formset.setAutosave(true);
383

    
384
            this.formset.addAction(new StartEditingAction());
385
            this.formset.addAction(new FinishEditingAction());
386
            this.formset.addAction(new RefreshAction());
387

    
388
            FormActionContext actionContext = new FormActionContext();
389
            actionContext.set("featuresform", this);
390
            try {
391
                Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getApplicableStoreActions(actionContext);
392
                for (DALActionFactory factory : factories) {
393
                    if (StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME)) {
394
                        continue;
395
                    }
396
                    try {
397
                        Action action = factory.createAction(actionContext);
398
                        this.formset.addAction(action);
399
                    } catch (Exception ex) {
400
                        LOGGER.warn("Can't add action " + factory.getName(), ex);
401
                    }
402

    
403
                }
404
            } catch (Exception ex) {
405
                LOGGER.warn("Can't add actions", ex);
406
            }
407
            for (Action action : this.otherActions) {
408
                this.formset.addAction(action);
409
            }
410

    
411
            this.formset.addListener(new FormSetListener());
412
            this.formset.getForm().addListener(new JDynForm.JDynFormListener() {
413
                @Override
414
                public void message(String string) {
415
                }
416

    
417
                @Override
418
                public void fieldChanged(JDynFormField jdff) {
419
                    updateButtonEnabledStatus();
420
                }
421
            });
422

    
423
            ToolsSwingLocator.getToolsSwingManager().removeBorder(this.formset.asJComponent());
424
            this.panel.add(this.formset.asJComponent(), BorderLayout.CENTER);
425
            this.panel.revalidate();
426
            ToolsSwingUtils.ensureRowsCols(this.panel, 8, 70, 25, 100);
427

    
428
        }
429
        updateButtonEnabledStatus();
430
        return this.formset;
431
    }
432

    
433
    @Override
434
    public void addAction(Action action) {
435
        this.otherActions.add(action);
436
        if (this.formset != null) {
437
            this.formset.addAction(action);
438
        }
439
    }
440

    
441
    @Override
442
    public long getCurrentIndex() {
443
        if (this.formset == null) {
444
            return -1;
445
        }
446
        return this.formset.getCurrentIndex();
447
    }
448

    
449
    @Override
450
    public Feature get(long index) {
451
        if (this.formset == null || this.features == null) {
452
            return null;
453
        }
454
        try {
455
            return this.features.getFeatureAt(index);
456
        } catch (BaseException ex) {
457
            return null;
458
        }
459
    }
460

    
461
    private class RefreshAction extends AbstractAction {
462

    
463
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
464
        public RefreshAction() {
465
            I18nManager i18nManager = ToolsLocator.getI18nManager();
466
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
467

    
468
            this.putValue(NAME, null);
469
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Reload_data"));
470
            this.putValue(SMALL_ICON, iconTheme.get("common-form-refresh"));
471
            this.putValue(ACTION_COMMAND_KEY, REFRESHFORM_ACTION);
472
            this.setEnabled(!formset.isInNewState());
473
        }
474

    
475
        @Override
476
        public void actionPerformed(ActionEvent ae) {
477
            try {
478
                I18nManager i18n = ToolsLocator.getI18nManager();
479
                formset.message(i18n.getTranslation("_Form_reloaded"));
480
                int x=formset.getCurrentIndex();
481
                updateForm();
482
                formset.setCurrentIndex(x);
483
            } catch (Exception ex) {
484
                LOGGER.warn("Can't reload form", ex);
485
            }
486
        }
487
    }
488

    
489
    private class FinishEditingAction extends AbstractAction implements Observer, Disposable {
490

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

    
496
            this.putValue(NAME, null);
497
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Stop_editing"));
498
            this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
499
            this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
500

    
501
            this.setEnabled(store.isEditing());
502
            store.addObserver(this);
503
        }
504

    
505
        @Override
506
        public void actionPerformed(ActionEvent ae) {
507
            if (store == null || formset == null) {
508
                return;
509
            }
510
            if (store.isEditing()) {
511
                EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
512
                DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
513
                ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
514
                I18nManager i18nManager = ToolsLocator.getI18nManager();
515
                try {
516
                    int x = dataSwingManager.askUserStopEditing(formset.asJComponent(), store, true);
517
                    switch (x) {
518
                        case DataSwingManager.STOP_EDITING_SAVE:
519
                            EditingNotification notification = editingManager.notifyObservers(this, BEFORE_EXIT_EDITING_STORE, null, store);
520
                            if( notification.isCanceled()) {
521
                                I18nManager i18n = ToolsLocator.getI18nManager();
522
                                formset.message(i18n.getTranslation("Finish editing has been cancelled."));
523
                                return;
524
                            }                
525
                            store.finishEditing();
526
                            editingManager.notifyObservers(this, AFTER_EXIT_EDITING_STORE, null, store);
527
                            break;
528
                        case DataSwingManager.STOP_EDITING_CONTINUE:
529
                            break;
530
                        case DataSwingManager.STOP_EDITING_DISCARD:
531
                            store.cancelEditing();
532
                            int index = formset.getCurrentIndex();
533
                            try {
534
                                formset.setCurrentIndex(index);
535
                            } catch (Exception ex) {
536
                                LOGGER.warn("Can't reload form data after edit.", ex);
537
                            }
538
                            break;
539
                        case DataSwingManager.STOP_EDITING_EXPORT:
540
                            if( !((DefaultDataSwingManager)dataSwingManager).exportStore(store, MODE.DIALOG) ) {
541
                                // Mensaje de operacion no soportada
542
                            }
543
                    }
544
                } catch (Exception ex) {
545
                    LOGGER.warn("Can't finish editing in FeatureForm (" + store.getName() + ").", ex);
546
                    dialogManager.messageDialog(
547
                            i18nManager.getTranslation("_Problems_finish_table_editing") + "\n\n"
548
                            + i18nManager.getTranslation("_see_error_log_for_more_information"),
549
                            i18nManager.getTranslation("_Stop_editing"),
550
                            JOptionPane.ERROR_MESSAGE
551
                    );
552
                }
553
            }
554
            updateButtonEnabledStatus();
555
        }
556

    
557
        @Override
558
        public void update(Observable observable, Object notification) {
559
            if (store == null || formset == null) {
560
                return;
561
            }
562
            if (notification instanceof FeatureStoreNotification) {
563
                FeatureStoreNotification n = (FeatureStoreNotification) notification;
564
                switch (n.getType()) {
565
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
566
                        needrefresh = true;
567
                    case FeatureStoreNotification.AFTER_STARTEDITING:
568
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
569
                        updateButtonEnabledStatus();
570
                        break;
571
                }
572
            }
573
        }
574

    
575
        @Override
576
        public void dispose() {
577
            store.deleteObserver(this);
578
        }
579

    
580
    }
581

    
582
    private class StartEditingAction extends AbstractAction implements Observer, Disposable {
583

    
584
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
585
        public StartEditingAction() {
586
            I18nManager i18nManager = ToolsLocator.getI18nManager();
587
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
588

    
589
            this.putValue(NAME, null);
590
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Start_editing"));
591
            this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
592
            this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
593

    
594
            this.setEnabled(!store.isEditing());
595
            store.addObserver(this);
596
        }
597

    
598
        @Override
599
        public void actionPerformed(ActionEvent ae) {
600
            if (store == null || formset == null) {
601
                return;
602
            }
603
            if (!store.isEditing()) {
604
                try {
605
                    EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
606
                    EditingNotification notification = editingManager.notifyObservers(this, BEFORE_ENTER_EDITING_STORE, null, store);
607
                    if( notification.isCanceled() ) {
608
                        I18nManager i18n = ToolsLocator.getI18nManager();
609
                        formset.message(i18n.getTranslation("Enter editing has been cancelled."));
610
                        return;
611
                    }                
612
                    store.edit();
613
                    editingManager.notifyObservers(this, AFTER_ENTER_EDITING_STORE, null, store);
614
                } catch (DataException ex) {
615
                    LOGGER.warn("Can't finish editing in FeatureForm (" + store.getName() + ").", ex);
616
                }
617
            }
618
            updateButtonEnabledStatus();
619
        }
620

    
621
        @Override
622
        public void update(Observable observable, Object notification) {
623
            if (store == null || formset == null) {
624
                return;
625
            }
626
            if (notification instanceof FeatureStoreNotification) {
627
                FeatureStoreNotification n = (FeatureStoreNotification) notification;
628
                switch (n.getType()) {
629
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
630
                        needrefresh = true;
631
                    case FeatureStoreNotification.AFTER_STARTEDITING:
632
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
633
                        updateButtonEnabledStatus();
634
                        break;
635
                }
636
            }
637
        }
638

    
639
        @Override
640
        public void dispose() {
641
            store.deleteObserver(this);
642
        }
643

    
644
    }
645

    
646
    @Override
647
    public void setQuery(FeatureQuery query) {
648
        if (this.features != null) {
649
            if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) {
650
                if(this.formset.getForm().isModified()){
651
                    if (!store.isEditing()) {
652
                        try {
653
                            store.edit();
654
                        } catch (DataException e1) {
655
                            throw new RuntimeException("Can't set query", e1);
656
                        }
657
                    }
658
                    saveChanges(this.formset);
659
                }
660
            }
661
        }
662
        this.currentQuery = query;
663
        updateForm();
664
    }
665

    
666
    private FeatureQuery getCurrentQuery() {
667
        return this.currentQuery;
668
    }
669

    
670
    @Override
671
    public void showForm(MODE mode) {
672
        I18nManager i18n = ToolsLocator.getI18nManager();
673
        String title = i18n.getTranslation("_Form") + ": "+ this.definition.getLabel();
674
        showForm(title, mode);
675
    }
676

    
677
    @Override
678
    public void showForm(String title, MODE mode) {
679
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
680
        if( StringUtils.isBlank(title) ) {
681
            title = this.definition.getLabel();
682
        }
683
        winmgr.showWindow(this.asJComponent(), title, mode);
684
    }
685
    
686
    @Override
687
    public void hideForm() {
688
        DisposeUtils.dispose(this);
689
    }
690

    
691
    private void saveChanges(JDynFormSet theFormSet) {
692
        I18nManager i18n = ToolsLocator.getI18nManager();
693
        try {
694
            EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
695
            if (theFormSet.isInNewState()) {
696
                EditableFeature newFeature = store.createNewFeature(false);
697
                JDynForm form = formset.getForm();
698
                for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
699
                    String name = attr.getName();
700
                    if( !newFeature.canSetValue(name) ) {
701
                        continue;
702
                    }
703
                    JDynFormField field = form.getField(name);
704
                    if( field==null ) {
705
                        // The attribute is not in form, set default value
706
                        newFeature.set(name, attr.getDefaultValueCoerced());
707
                    } else {
708
                        try {
709
                            Object value = field.getValue();
710
                            newFeature.set(name, value);
711
                        } catch (Exception ex) {
712
                            LOGGER.warn("Can't get value of field '" + name + "'.", ex);
713
                        }
714
                    }
715
                }
716
                EditingNotification notification = editingManager.notifyObservers(this, BEFORE_INSERT_FEATURE, null, this.store, newFeature);
717
                if( notification.isCanceled() ) {
718
                    theFormSet.message(i18n.getTranslation("Data saving has been cancelled."));
719
                    return;
720
                }                
721
                if (!editingManager.canWriteFeature(newFeature)) {
722
                    theFormSet.message(i18n.getTranslation("The data cannot be saved. Check that all the required fields are filled out.."));
723
                    return;
724
                }
725
                features.insert(newFeature);
726
                editingManager.notifyObservers(this, AFTER_INSERT_FEATURE, null, this.store, newFeature);
727
                
728
                this.formset.message(i18n.getTranslation("_Record_saved"));
729
                try {
730
                    this.formset.setValues(features.asListOfDynObjects());
731
                    this.formset.setCurrentIndex((int) (features.getTotalSize()) - 1);
732
                } catch (Exception ex) {
733
                    LOGGER.warn("Can't reload form data after insert.", ex);
734
                }
735
            } else {
736
                int index = theFormSet.getCurrentIndex();
737
                DynObject currentElement = theFormSet.get(index);
738
                theFormSet.getFormValues(currentElement);
739

    
740
                EditableFeature feature = ((FacadeOfAFeature) currentElement).getEditableFeature();
741
                EditingNotification notification = editingManager.notifyObservers(this, BEFORE_UPDATE_FEATURE, null, this.store, feature);
742
                if( notification.isCanceled() ) {
743
                    theFormSet.message(i18n.getTranslation("Data saving has been cancelled."));
744
                    return;
745
                }                
746
                if (!editingManager.canWriteFeature(feature)) {
747
                    theFormSet.message(i18n.getTranslation("The data cannot be saved. Check that all the required fields are filled out.."));
748
                    return;
749
                }
750
                features.update(feature);
751
                editingManager.notifyObservers(this, AFTER_UPDATE_FEATURE, null, this.store, feature);
752

    
753
                this.formset.message(i18n.getTranslation("_Record_saved"));
754
                try {
755
                    this.formset.setCurrentIndex(index);
756
                } catch (Exception ex) {
757
                    LOGGER.warn("Can't reload form data after insert.", ex);
758
                }
759
            }
760
        } catch (Exception ex) {
761
            theFormSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
762
            throw new RuntimeException("Can't save values", ex);
763

    
764
        } finally {
765
            updateButtonEnabledStatus();
766
        }
767

    
768
    }
769

    
770
    private void deleteCurrentElement(JDynFormSet theFormSet) {
771
        I18nManager i18n = ToolsLocator.getI18nManager();
772
        try {
773
            EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
774
            int index = theFormSet.getCurrentIndex();
775
            DynObject currentElement = theFormSet.get(index);
776
            theFormSet.getFormValues(currentElement);
777
            Feature feature = ((FacadeOfAFeature) currentElement).getFeature();
778
            if (feature instanceof EditableFeature) {
779
                feature = ((EditableFeature) feature).getNotEditableCopy();
780
            }
781
            if( editingManager.notifyObservers(this, BEFORE_REMOVE_FEATURE, null, this.store, feature).isCanceled() ) {
782
                theFormSet.message(i18n.getTranslation("The delete operation has been cancelled."));
783
                return;
784
            }                
785
            features.delete(feature);
786
            editingManager.notifyObservers(this, AFTER_REMOVE_FEATURE, null, this.store, feature);
787

    
788
            this.formset.message(i18n.getTranslation("_Record_removed"));
789

    
790
            this.formset.setValues(features.asListOfDynObjects());
791
            if (features.getTotalSize() - 1 < index) {
792
                index = index - 1;
793
            }
794
            this.formset.setCurrentIndex(index);
795

    
796
        } catch (Exception ex) {
797
            theFormSet.message(i18n.getTranslation("error_removing_data_will_not_remove"));
798
            throw new RuntimeException("Can't remove values", ex);
799

    
800
        } finally {
801
            updateButtonEnabledStatus();
802
        }
803

    
804
    }
805

    
806
    @Override
807
    public void saveChanges() {
808
        if (this.formset != null && this.formset.countValues() > 0) {
809
            if (store != null && !store.isEditing()) {
810
                try {
811
                    store.edit();
812
                } catch (DataException e1) {
813
                    LOGGER.warn("Can't edit the store " + store.getName());
814
                    throw new RuntimeException("Can't save changes.", e1);
815
                }
816
            }
817
            this.saveChanges(this.formset);
818
        }
819
    }
820

    
821
    private void updateButtonEnabledStatus() {
822
        if (this.formset == null) {
823
            return;
824
        }
825
        if (this.store == null || store.isBroken() || store.isAppending() || this.features == null) {
826
            this.formset.setReadOnly(true);
827
            formset.setActionVisible(STARTEDITING_ACTION, true);
828
            formset.setActionEnabled(STARTEDITING_ACTION, true);
829
            formset.setActionVisible(FINISHEDITING_ACTION, false);
830
            formset.setActionEnabled(FINISHEDITING_ACTION, false);
831
            formset.setActionEnabled(ACTION_DELETE, false);
832
            formset.setActionEnabled(ACTION_NEW, false);
833
            formset.setActionEnabled(ACTION_CANCEL_NEW, false);
834
            formset.setActionEnabled(ACTION_SAVE, false);
835
            formset.setActionEnabled(ACTION_SEARCH, false);
836
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
837
            formset.setActionEnabled(ACTION_NAVIGATION, false);
838
            return;
839
        }
840
        if (this.needrefresh ) {
841
            this.formset.setReadOnly(true);
842
//            formset.setActionEnabled(STARTEDITING_ACTION, false);
843
//            formset.setActionEnabled(FINISHEDITING_ACTION, false);
844
            formset.setActionEnabled(ACTION_DELETE, false);
845
            formset.setActionEnabled(ACTION_NEW, false);
846
            formset.setActionEnabled(ACTION_CANCEL_NEW, false);
847
            formset.setActionEnabled(ACTION_SAVE, false);
848
            formset.setActionEnabled(ACTION_SEARCH, false);
849
            formset.setActionEnabled(ACTION_NAVIGATION, false);
850
            for (Action action : formset.getActions()) {
851
                formset.setActionEnabled((String) action.getValue(ACTION_COMMAND_KEY), false);
852
            }
853
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
854
            return;
855
        }
856
        if (store.isEditing()) {
857
            this.formset.setReadOnly(false);
858
            formset.setActionVisible(STARTEDITING_ACTION, false);
859
            formset.setActionEnabled(STARTEDITING_ACTION, false);
860
            formset.setActionVisible(FINISHEDITING_ACTION, true);
861
            formset.setActionEnabled(FINISHEDITING_ACTION, true);
862
            if (formset.isInNewState()) {
863
                formset.setActionEnabled(ACTION_DELETE, false);
864
                formset.setActionEnabled(ACTION_SEARCH, false);
865
                formset.setActionEnabled(ACTION_NEW, false);
866
                formset.setActionEnabled(ACTION_CANCEL_NEW, true);
867
                formset.setActionEnabled(ACTION_SAVE, true);
868
                formset.setActionEnabled(FINISHEDITING_ACTION, false);
869
                formset.setActionEnabled(REFRESHFORM_ACTION, false);
870
//                setEnabledUniqueFields(true);
871

    
872
            } else if (this.features != null && this.features.isEmpty()) {
873
                formset.getForm().setReadOnly(true);
874
                formset.setActionEnabled(ACTION_DELETE, false);
875
                formset.setActionEnabled(ACTION_SEARCH, false);
876
                formset.setActionEnabled(ACTION_NEW, true);
877
                formset.setActionEnabled(ACTION_CANCEL_NEW, true);
878
                formset.setActionEnabled(ACTION_SAVE, false);
879
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
880
                formset.setActionEnabled(FINISHEDITING_ACTION, true);         
881
                formset.setActionEnabled(ACTION_NAVIGATION, false);
882

    
883
            } else if (formset.getForm().isModified()) {
884
                formset.setActionEnabled(ACTION_DELETE, false);
885
                formset.setActionEnabled(ACTION_SEARCH, false);
886
                formset.setActionEnabled(ACTION_NEW, false);
887
                formset.setActionEnabled(ACTION_CANCEL_NEW, false);
888
                formset.setActionEnabled(ACTION_SAVE, true);
889
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
890
                formset.setActionEnabled(FINISHEDITING_ACTION, false);
891
                formset.setActionEnabled(ACTION_NAVIGATION, false);
892

    
893
            } else {
894
                formset.setActionEnabled(ACTION_DELETE, true);
895
                formset.setActionEnabled(ACTION_SEARCH, true);
896
                formset.setActionEnabled(ACTION_NEW, true);
897
                formset.setActionEnabled(ACTION_CANCEL_NEW, false);
898
                formset.setActionEnabled(ACTION_SAVE, false);
899
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
900
                formset.setActionEnabled(FINISHEDITING_ACTION, true);
901
                formset.setActionEnabled(ACTION_NAVIGATION, true);
902
            }
903

    
904
            return;
905
        }
906
        this.formset.setReadOnly(true);
907
        formset.setActionVisible(STARTEDITING_ACTION, true);
908
        formset.setActionEnabled(STARTEDITING_ACTION, true);
909
        formset.setActionVisible(FINISHEDITING_ACTION, false);
910
        formset.setActionEnabled(FINISHEDITING_ACTION, false);
911
        formset.setActionEnabled(ACTION_DELETE, false);
912
        formset.setActionEnabled(ACTION_NEW, false);
913
        formset.setActionEnabled(ACTION_CANCEL_NEW, false);
914
        formset.setActionEnabled(ACTION_SAVE, false);
915
        formset.setActionEnabled(ACTION_SEARCH, true);
916
        formset.setActionEnabled(REFRESHFORM_ACTION, true);
917
        for (Action action : formset.getActions()) {
918
            formset.setActionEnabled((String) action.getValue(ACTION_COMMAND_KEY), true);
919
        }
920
        
921
    }
922

    
923
    private void clearUniqueFields() {
924
        for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
925
            if ((attr.isPrimaryKey() && !attr.isAutomatic()) || (attr.isIndexed() && !attr.allowIndexDuplicateds())) {
926
                JDynFormField field = formset.getForm().getField(attr.getName());
927
                if (field != null) {
928
                    field.clear();
929
                }
930
            }
931
        }
932
    }
933

    
934
    private void setEnabledUniqueFields(boolean enabled) {
935
        for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
936
            if ((attr.isPrimaryKey() && !attr.isAutomatic()) || (attr.isIndexed() && !attr.allowIndexDuplicateds())) {
937
                JDynFormField field = formset.getForm().getField(attr.getName());
938
                if (field != null) {
939
                    field.setReadOnly(!enabled);
940
                }
941
            }
942
        }
943
    }
944

    
945
    @Override
946
    public long getDataSetSize() {
947
        if (this.features != null) {
948
            return features.getTotalSize();
949
        }
950
        return 0;
951
    }
952

    
953
    @Override
954
    public FeatureStore getFeatureStore() {
955
        return this.store;
956
    }
957
    
958
    public FeatureType getFeatureType() {
959
        if( this.store == null ) {
960
            return null;
961
        }
962
        return this.store.getDefaultFeatureTypeQuietly();
963
    }
964

    
965
    private class FormSetListener implements JDynFormSetListener {
966

    
967
        @Override
968
        public void formMessage(String message) {
969
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formMessage"));
970
        }
971

    
972
        @Override
973
        public void formClose() {
974
            panel.setVisible(false);
975
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formClose"));
976
        }
977

    
978
        @Override
979
        public void formMovedTo(int currentPosition) throws AbortActionException {
980
            LOGGER.trace("formMovedTo " + currentPosition);
981
            updateButtonEnabledStatus();
982
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formMovedTo"));
983
        }
984

    
985
        @Override
986
        public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException {
987
            LOGGER.trace("formBeforeSave");
988
            saveChanges(dynformSet);
989
            updateButtonEnabledStatus();
990
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeSave"));
991
        }
992

    
993
        @Override
994
        public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException {
995
            LOGGER.trace("formBeforeNew");
996
            clearUniqueFields();
997
            updateButtonEnabledStatus();
998
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeNew"));
999
        }
1000

    
1001
        @Override
1002
        public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException {
1003
            LOGGER.trace("formBeforeDelete");
1004
            updateButtonEnabledStatus();
1005
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeDelete"));
1006
        }
1007

    
1008
        @Override
1009
        public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException {
1010
            LOGGER.trace("formAfterSave");
1011
            updateButtonEnabledStatus();
1012
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterSave"));
1013
        }
1014

    
1015
        @Override
1016
        public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException {
1017
            LOGGER.trace("formAfterNew");
1018
            updateButtonEnabledStatus();
1019
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterNew"));
1020
        }
1021

    
1022
        @Override
1023
        public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
1024
            LOGGER.trace("formAfterDelete");
1025
            deleteCurrentElement(dynformSet);
1026
            updateButtonEnabledStatus();
1027
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterDelete"));
1028
        }
1029

    
1030
        @Override
1031
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
1032
            LOGGER.trace("formBeforeSearch");
1033
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
1034
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
1035

    
1036
            final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
1037
            searchPanel.setShowActions(false);
1038
            FeatureQuery currentQuery = getCurrentQuery();
1039
            if (currentQuery != null) {
1040
                Evaluator filter = currentQuery.getFilter();
1041
                if (filter instanceof ExpressionEvaluator) {
1042
                    Expression expression = ((ExpressionEvaluator) filter).toExpression();
1043
                    searchPanel.setFilter(expression);
1044
                    searchPanel.search();
1045
                }
1046
            }
1047
            ToolsSwingUtils.ensureRowsCols(searchPanel.asJComponent(), 20, 80, 35, 140);
1048
            I18nManager i18n = ToolsLocator.getI18nManager();
1049
            Dialog dialog = winmgr.createDialog(
1050
                    searchPanel.asJComponent(),
1051
                    i18n.getTranslation("_Filter"),
1052
                    i18n.getTranslation("_Creating_filter_for") + ": '" + store.getName() + "'",
1053
                    WindowManager_v2.BUTTONS_OK_CANCEL
1054
            );
1055
            dialog.show(WindowManager.MODE.DIALOG);
1056
            if (dialog.getAction() == WindowManager_v2.BUTTON_OK) {
1057
                SearchParameters params = searchPanel.fetch(null);
1058
                if (params.getQuery() != null) {
1059
                    FeatureQuery searchQuery = params.getQuery().getCopy();
1060

    
1061
                    Expression expresion = null;
1062
                    if (searchQuery != null) {
1063
                        expresion = searchQuery.getExpressionFilter();
1064
                    }
1065
                    try {
1066
                        FeatureQuery query = store.createFeatureQuery();
1067
                        if (ExpressionUtils.isPhraseEmpty(expresion)) {
1068
                            query.clearFilter();
1069
                        } else {
1070
                            query.setFilter(expresion);
1071
                        }
1072
                        setQuery(query);
1073
                    } catch (Exception ex) {
1074
                        LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
1075
                    }
1076
                } else {
1077
                    LOGGER.warn("FeatureQuery from parameters is null. Query is not applied to the JFeaturesFrom");
1078
                }
1079
            }
1080
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeSearch"));
1081
        }
1082

    
1083
        @Override
1084
        public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException {
1085
            LOGGER.trace("formAfterSearch");
1086
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterSearch"));
1087
        }
1088

    
1089
        @Override
1090
        public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException {
1091
            LOGGER.trace("formBeforeCancelNew");
1092
            updateButtonEnabledStatus();
1093
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeCancelNew"));
1094
        }
1095

    
1096
        @Override
1097
        public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException {
1098
            LOGGER.trace("formAfterCancelNew");
1099
            try {
1100
                int index = formset.getCurrentIndex();
1101
                if( index<0 ) {
1102
                    formset.getForm().clear();
1103
                } else {
1104
                    formset.setCurrentIndex(index);
1105
                }
1106
            } catch (Exception ex) {
1107
                LOGGER.warn("Can't reload form data after cancel new.", ex);
1108
            }            
1109
            updateButtonEnabledStatus();
1110
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterCancelNew"));
1111
        }
1112

    
1113
    }
1114

    
1115
    @Override
1116
    public void addActionListener(ActionListener listener) {
1117
        this.actionListeners.addActionListener(listener);
1118
    }
1119

    
1120
    @Override
1121
    public ActionListener[] getActionListeners() {
1122
        return this.actionListeners.getActionListeners();
1123
    }
1124

    
1125
    @Override
1126
    public void removeActionListener(ActionListener listener) {
1127
        this.actionListeners.removeActionListener(listener);
1128
    }
1129

    
1130
    @Override
1131
    public void removeAllActionListener() {
1132
        this.actionListeners.removeAllActionListener();
1133
    }
1134

    
1135
    @Override
1136
    public void fireActionEvent(ActionEvent event) {
1137
        this.actionListeners.fireActionEvent(event);
1138
    }
1139

    
1140
    @Override
1141
    public boolean hasActionListeners() {
1142
        return this.actionListeners.hasActionListeners();
1143
    }
1144

    
1145
    @Override
1146
    public Feature getCurrentFeature() {
1147
        // TODO: No tengo claro que esto sea correcto, ya que no tiene 
1148
        // en cuenta si el form se esta modificando o es nuevo para coger
1149
        // los valores del formulario.
1150
        long index = getCurrentIndex();
1151
        if( index<0 ) {
1152
            return null;
1153
        }
1154
        Feature f = get(index);
1155
        try {
1156
            DynObject adapter = f.getAsDynObject();
1157
            this.getFormset().getForm().getValues(adapter);
1158
            f = ((FacadeOfAFeature)adapter).getFeature();
1159
            return f;
1160
        } catch(Exception ex) {
1161
            return f;
1162
        }
1163
    }
1164

    
1165
    protected void callUserEvent(String name, Object...args) {
1166
//        try {
1167
            JDynForm form = this.formset.getForm();
1168
            form.callUserEvent(name, args);
1169
//        } catch(NoSuchMethodException ex) {
1170
//            LOGGER.debug("Function '"+name+"' not found.", ex);
1171
//            // Do nothing
1172
//        } catch(Exception ex) {
1173
//            LOGGER.warn("Error calling form event '"+name+"'.", ex);
1174
//        }
1175
    }
1176
    
1177
    @Override
1178
    public void setTerminateEditingOnClose(boolean b) {
1179
        terminateEditingOnClose = b;
1180
    }
1181

    
1182
    @Override
1183
    public boolean isTerminateEditingOnClose() {
1184
        return terminateEditingOnClose;
1185
    }
1186

    
1187
    private void onClose() {
1188
        if (this.store == null || this.ignoreTerminateEditingOnClose) {
1189
            return;
1190
        }
1191
        if (this.store.isEditing() && terminateEditingOnClose) {
1192
            DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
1193
            ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
1194
            I18nManager i18nManager = ToolsLocator.getI18nManager();
1195
            try {
1196
                int x = dataSwingManager.askUserStopEditing(formset.asJComponent(), store, false);
1197
                switch (x) {
1198
                    case DataSwingManager.STOP_EDITING_SAVE:
1199
                        store.finishEditing();
1200
                        break;
1201
                    case DataSwingManager.STOP_EDITING_CONTINUE:
1202
                        break;
1203
                    case DataSwingManager.STOP_EDITING_DISCARD:
1204
                        store.cancelEditing();
1205
                        break;
1206
                    case DataSwingManager.STOP_EDITING_EXPORT:
1207
                        if( !((DefaultDataSwingManager)dataSwingManager).exportStore(store, MODE.DIALOG) ) {
1208
                            // Mensaje de operacion no soportada
1209
                        }
1210
                }
1211
            } catch (Exception ex) {
1212
                LOGGER.warn("Can't finish editing in FeatureForm (" + DataStore.getNameQuietly(store) + ").", ex);
1213
                dialogManager.messageDialog(
1214
                        i18nManager.getTranslation("_Problems_finish_table_editing") + "\n\n"
1215
                        + i18nManager.getTranslation("_see_error_log_for_more_information"),
1216
                        i18nManager.getTranslation("_Stop_editing"),
1217
                        JOptionPane.ERROR_MESSAGE
1218
                );
1219
            }
1220

    
1221
        }
1222
    }
1223

    
1224

    
1225
}