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

History | View | Annotate | Download (50.2 KB)

1 42112 jjdelcerro
/* 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 47076 jjdelcerro
import java.awt.Toolkit;
28 42489 jjdelcerro
import java.awt.event.ActionEvent;
29 45532 jjdelcerro
import java.awt.event.ActionListener;
30 44291 jjdelcerro
import java.awt.event.ComponentAdapter;
31
import java.awt.event.ComponentEvent;
32 42489 jjdelcerro
import java.util.ArrayList;
33 44281 jjdelcerro
import java.util.Collection;
34 42489 jjdelcerro
import java.util.List;
35 46010 jjdelcerro
import java.util.Map;
36 42489 jjdelcerro
import javax.swing.AbstractAction;
37
import javax.swing.Action;
38 44202 jjdelcerro
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 42112 jjdelcerro
import javax.swing.JComponent;
43 42489 jjdelcerro
import javax.swing.JOptionPane;
44 42112 jjdelcerro
import javax.swing.JPanel;
45 44281 jjdelcerro
import org.apache.commons.lang3.StringUtils;
46 44128 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
47 45781 fdiaz
import org.gvsig.expressionevaluator.ExpressionEvaluator;
48 44239 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionUtils;
49 42775 jjdelcerro
import org.gvsig.featureform.swing.JFeaturesForm;
50 42112 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
51 44253 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
52 46864 jjdelcerro
import org.gvsig.fmap.dal.EditingNotification;
53
import static org.gvsig.fmap.dal.EditingNotification.AFTER_REMOVE_FEATURE;
54
import static org.gvsig.fmap.dal.EditingNotification.AFTER_UPDATE_FEATURE;
55
import static org.gvsig.fmap.dal.EditingNotification.AFTER_INSERT_FEATURE;
56
import static org.gvsig.fmap.dal.EditingNotification.AFTER_ENTER_EDITING_STORE;
57
import static org.gvsig.fmap.dal.EditingNotification.AFTER_EXIT_EDITING_STORE;
58
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_REMOVE_FEATURE;
59
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_UPDATE_FEATURE;
60
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_INSERT_FEATURE;
61
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_ENTER_EDITING_STORE;
62
import static org.gvsig.fmap.dal.EditingNotification.BEFORE_EXIT_EDITING_STORE;
63
import org.gvsig.fmap.dal.EditingNotificationManager;
64 42112 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
65 44510 omartinez
import org.gvsig.fmap.dal.feature.EditableFeature;
66 45781 fdiaz
import org.gvsig.fmap.dal.feature.FacadeOfAFeature;
67 42112 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
68 45425 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
69 42112 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
70 46959 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureSelection;
71 42112 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
72 42489 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
73 46453 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
74 42112 jjdelcerro
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
75 44281 jjdelcerro
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext;
76
import org.gvsig.fmap.dal.swing.DALActionFactory;
77 42489 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
78
import org.gvsig.fmap.dal.swing.DataSwingManager;
79 44202 jjdelcerro
import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager;
80 44281 jjdelcerro
import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory;
81 44292 jjdelcerro
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel;
82 44829 omartinez
import org.gvsig.fmap.dal.swing.searchpanel.SearchParameters;
83 42112 jjdelcerro
import org.gvsig.tools.ToolsLocator;
84 45120 jjdelcerro
import org.gvsig.tools.dispose.Disposable;
85 44291 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
86 42112 jjdelcerro
import org.gvsig.tools.dynform.AbortActionException;
87 44128 jjdelcerro
import org.gvsig.tools.dynform.DynFormDefinition;
88 42112 jjdelcerro
import org.gvsig.tools.dynform.DynFormLocator;
89
import org.gvsig.tools.dynform.DynFormManager;
90 44202 jjdelcerro
import org.gvsig.tools.dynform.JDynForm;
91 45425 jjdelcerro
import org.gvsig.tools.dynform.JDynFormField;
92 42112 jjdelcerro
import org.gvsig.tools.dynform.JDynFormSet;
93 44488 jjdelcerro
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CANCEL_NEW;
94
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_DELETE;
95 45783 jjdelcerro
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NAVIGATION;
96 44488 jjdelcerro
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW;
97
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE;
98
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SEARCH;
99 42112 jjdelcerro
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener;
100 46980 fdiaz
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
101 42112 jjdelcerro
import org.gvsig.tools.dynobject.DynClass;
102
import org.gvsig.tools.dynobject.DynObject;
103 44239 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
104 42112 jjdelcerro
import org.gvsig.tools.exception.BaseException;
105
import org.gvsig.tools.i18n.I18nManager;
106 42489 jjdelcerro
import org.gvsig.tools.observer.Observable;
107
import org.gvsig.tools.observer.Observer;
108 45781 fdiaz
import org.gvsig.tools.swing.api.ActionListenerSupport;
109 42112 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
110 45781 fdiaz
import org.gvsig.tools.swing.api.ToolsSwingUtils;
111 42489 jjdelcerro
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
112
import org.gvsig.tools.swing.api.windowmanager.Dialog;
113 42112 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
114 45781 fdiaz
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
115 42489 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
116
import org.gvsig.tools.swing.icontheme.IconTheme;
117 46010 jjdelcerro
import org.gvsig.tools.util.PropertiesSupport;
118
import org.gvsig.tools.util.PropertiesSupportHelper;
119 45781 fdiaz
import org.slf4j.Logger;
120
import org.slf4j.LoggerFactory;
121 42112 jjdelcerro
122
/**
123
 * @author fdiaz
124
 *
125
 */
126 44128 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
127 45120 jjdelcerro
public class DefaultJFeaturesForm implements JFeaturesForm, Disposable {
128 42112 jjdelcerro
129 46010 jjdelcerro
    private class JFeaturesFormPanel extends JPanel implements PropertiesSupport {
130
131
        private final PropertiesSupport properties;
132
133
        private JFeaturesFormPanel(PropertiesSupport properties, BorderLayout layout) {
134
            super(layout);
135
            this.properties = properties;
136
        }
137
138
        @Override
139
        public Object getProperty(String name) {
140
            return this.properties.getProperty(name);
141
        }
142
143
        @Override
144
        public void setProperty(String name, Object value) {
145
            this.properties.setProperty(name, value);
146
        }
147
148
        @Override
149
        public Map<String, Object> getProperties() {
150
            return this.properties.getProperties();
151
        }
152
153
    }
154
155 44291 jjdelcerro
    private class FormActionContext extends AbstractDALActionContext {
156 44281 jjdelcerro
157 44291 jjdelcerro
        public FormActionContext() {
158 44281 jjdelcerro
            super(ACTION_CONTEXT_NAME);
159
        }
160 45618 jolivas
161 44281 jjdelcerro
        @Override
162
        public DataStore getStore() {
163 44291 jjdelcerro
            return store;
164 44281 jjdelcerro
        }
165
166
        @Override
167 44712 jjdelcerro
        public FeatureQuery getQuery() {
168 45618 jolivas
            return currentQuery;
169 44281 jjdelcerro
        }
170
171
        @Override
172
        public int getSelectedsCount() {
173
            return 0;
174
        }
175
176
        @Override
177
        public Expression getFilterForSelecteds() {
178
            return null;
179
        }
180
181
        @Override
182 46959 jjdelcerro
        public FeatureSelection getSelecteds() {
183
            try {
184
                FeatureSelection selection = getFeatureStore().createFeatureSelection();
185
                selection.select(getCurrentFeature());
186
                return selection;
187
            } catch (DataException ex) {
188
                return null;
189
            }
190
        }
191
192
        @Override
193 44281 jjdelcerro
        public JComponent getActionButton(String actionName) {
194 45618 jolivas
            if (formset == null) {
195 44281 jjdelcerro
                return null;
196
            }
197
            return formset.getActionButton(actionName);
198
        }
199 45618 jolivas
200 44281 jjdelcerro
    }
201 45618 jolivas
202 42775 jjdelcerro
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class);
203 42112 jjdelcerro
204 45100 jjdelcerro
    private static final int PAGE_SIZE = 10;
205 46010 jjdelcerro
    private JFeaturesFormPanel panel;
206 42112 jjdelcerro
    private JDynFormSet formset;
207
    private FeatureStore store;
208 45915 jjdelcerro
    private boolean needrefresh;
209 44488 jjdelcerro
    private FeaturePagingHelper features;
210 44128 jjdelcerro
    private DynFormDefinition definition = null;
211 42248 jjdelcerro
    private FeatureQuery currentQuery;
212 44291 jjdelcerro
    private List<Action> otherActions;
213 46010 jjdelcerro
    private final ActionListenerSupport actionListeners;
214
    private final PropertiesSupportHelper propertiesHelper;
215 46813 fdiaz
    private boolean terminateEditingOnClose = true;
216
    private boolean ignoreTerminateEditingOnClose = false;
217 42714 dmartinezizquierdo
218 46010 jjdelcerro
    @SuppressWarnings("LeakingThisInConstructor")
219 42775 jjdelcerro
    public DefaultJFeaturesForm() {
220 44291 jjdelcerro
        this.otherActions = new ArrayList<>();
221 46010 jjdelcerro
        this.propertiesHelper = new PropertiesSupportHelper();
222
        this.propertiesHelper.setProperty("FeaturesForm", this);
223
        this.panel = new JFeaturesFormPanel(this.propertiesHelper, new BorderLayout());
224 44291 jjdelcerro
        this.panel.addComponentListener(new ComponentAdapter() {
225
            @Override
226
            public void componentHidden(ComponentEvent e) {
227 46813 fdiaz
                onClose();
228 45120 jjdelcerro
                dispose();
229 44291 jjdelcerro
            }
230
        });
231 45532 jjdelcerro
        this.actionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
232 42112 jjdelcerro
    }
233
234 45618 jolivas
    public static void selfRegister() {
235
236 44531 omartinez
    }
237 45618 jolivas
238 42489 jjdelcerro
    @Override
239 42112 jjdelcerro
    public void setPreferredSize(Dimension dimension) {
240 45425 jjdelcerro
        panel.setPreferredSize(dimension);
241 42112 jjdelcerro
    }
242
243 42489 jjdelcerro
    private void updateForm() {
244 42473 fdiaz
        if (this.formset == null) {
245 45425 jjdelcerro
            this.getFormset();
246 42248 jjdelcerro
        }
247 42489 jjdelcerro
        try {
248 45618 jolivas
            if (this.currentQuery != null) {
249 44291 jjdelcerro
                this.currentQuery.retrievesAllAttributes();
250
            }
251 44488 jjdelcerro
            this.features = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE);
252 46467 jjdelcerro
            callUserEvent("form_beforeReload", this.formset.getForm());
253 44488 jjdelcerro
            this.formset.setValues(features.asListOfDynObjects());
254 46467 jjdelcerro
            callUserEvent("form_afterReload", this.formset.getForm());
255 42489 jjdelcerro
        } catch (Exception ex) {
256
            throw new RuntimeException("Can't update form", ex);
257
        }
258 45915 jjdelcerro
        this.needrefresh = false;
259 44488 jjdelcerro
        updateButtonEnabledStatus();
260 42248 jjdelcerro
    }
261 42473 fdiaz
262 42489 jjdelcerro
    @Override
263 42112 jjdelcerro
    public JComponent asJComponent() {
264 44488 jjdelcerro
        if (this.features == null) {
265 42248 jjdelcerro
            try {
266
                updateForm();
267
            } catch (Exception ex) {
268
                throw new RuntimeException(ex);
269
            }
270 42473 fdiaz
        }
271 42112 jjdelcerro
        return this.panel;
272
    }
273
274 42489 jjdelcerro
    @Override
275
    public void bind(FeatureStore store) {
276 45618 jolivas
        this.bind(store, null, null);
277 45532 jjdelcerro
    }
278 45618 jolivas
279
    public void bind(FeatureStore store, DynClass definition) {
280 45532 jjdelcerro
        this.bind(store, definition, null);
281
    }
282
283
    public void bind(FeatureStore store, DynClass definition, FeatureQuery query) {
284 42489 jjdelcerro
        if (store == null) {
285
            throw new IllegalArgumentException("bind need a store as parameter, not a null.");
286
        }
287 45532 jjdelcerro
        if (this.store == store) {
288
            return;
289
        }
290 42489 jjdelcerro
        try {
291 46813 fdiaz
            if(store.isEditing()){
292
                this.ignoreTerminateEditingOnClose = true;
293
            }
294 45618 jolivas
            if (definition == null) {
295 45532 jjdelcerro
                DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager();
296
                definition = manager.featureType2DynClass(store, store.getDefaultFeatureType());
297
            }
298
            DynFormManager formManager = DynFormLocator.getDynFormManager();
299
            this.definition = formManager.getDefinition(definition);
300
            if (formset != null) {
301
                this.panel.remove(formset.asJComponent());
302
                this.panel.revalidate();
303
                this.formset = null;
304
            }
305
            this.store = store;
306
            DisposeUtils.bind(store);
307
            this.currentQuery = query;
308 45618 jolivas
            if (features != null) {
309 45532 jjdelcerro
                this.features = null;
310
                updateForm();
311 46760 jjdelcerro
            }
312 42489 jjdelcerro
        } catch (Exception ex) {
313
            throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex);
314
        }
315 42112 jjdelcerro
    }
316 42473 fdiaz
317 45120 jjdelcerro
    @Override
318
    public void dispose() {
319 45915 jjdelcerro
        if( this.formset!=null ) {
320
            Action action = this.formset.getAction(FINISHEDITING_ACTION);
321
            if( action!=null ) {
322
                DisposeUtils.disposeQuietly(action);
323
            }
324
            action = this.formset.getAction(STARTEDITING_ACTION);
325
            if( action!=null ) {
326
                DisposeUtils.disposeQuietly(action);
327
            }
328
        }
329 46453 jjdelcerro
        if( this.panel!=null ) {
330
            this.panel.setVisible(false);
331
        }
332 44291 jjdelcerro
        DisposeUtils.disposeQuietly(store);
333
        this.store = null;
334
        this.panel = null;
335
        this.formset = null;
336 44488 jjdelcerro
        this.features = null;
337 44291 jjdelcerro
        this.definition = null;
338
        this.currentQuery = null;
339
        this.otherActions = null;
340 42112 jjdelcerro
    }
341 45618 jolivas
342 46010 jjdelcerro
    public static class MyFeaturesFormContext extends DefaultFeaturesFormContext {
343
344
        private final DefaultJFeaturesForm featuresForm;
345
346
        public MyFeaturesFormContext(DefaultJFeaturesForm featuresForm) {
347
            super(featuresForm.store);
348
            this.featuresForm = featuresForm;
349
        }
350
351
        @Override
352
        public JFeaturesForm getFeaturesForm() {
353
            return this.featuresForm;
354
        }
355
        @Override
356
        public Feature getCurrentFeature() {
357
            return this.featuresForm.getCurrentFeature();
358
        }
359
    }
360
361 42489 jjdelcerro
    @Override
362
    public JDynFormSet getFormset() {
363 42473 fdiaz
        if (this.formset == null) {
364 42112 jjdelcerro
            DynFormManager formManager = DynFormLocator.getDynFormManager();
365 44253 jjdelcerro
            this.formset = formManager.createJDynFormSet(
366 46010 jjdelcerro
                    new MyFeaturesFormContext(this),
367 44253 jjdelcerro
                    this.definition,
368
                    null
369
            );
370 46980 fdiaz
            if(!this.definition.getTags().has(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE)){
371
                this.formset.setLayoutMode(JDynForm.USE_TABS);
372
            }
373
374 44202 jjdelcerro
            List<String> groups = this.definition.getGroups();
375 46980 fdiaz
            if ((this.formset.getLayoutMode() == JDynForm.USE_TABS || this.formset.getLayoutMode() == JDynForm.USE_SEPARATORS) && groups.size() == 1 && groups.get(0) == null) {
376 44202 jjdelcerro
                this.formset.setLayoutMode(JDynForm.USE_PLAIN);
377
            }
378 46980 fdiaz
379 46760 jjdelcerro
            JComponent component = this.formset.getActionButton(JDynFormSet.ACTION_SET_CURRENT_RECORD);
380
            if( component!=null ) {
381
                I18nManager i18n = ToolsLocator.getI18nManager();
382
                component.setToolTipText(
383
                    "<html>" +
384
                    i18n.getTranslation("_Table") + ": " + this.store.getName() +"<br>\n"+
385
                    "( "+this.store.getFullName() + " )" +
386
                    "</html>"
387
                );
388
            }
389
390 44202 jjdelcerro
            this.formset.setAllowNew(true);
391
            this.formset.setAllowDelete(true);
392
            this.formset.setAllowUpdate(true);
393 42112 jjdelcerro
            this.formset.setAllowClose(true);
394 42489 jjdelcerro
            this.formset.setAllowSearch(true);
395 42112 jjdelcerro
            this.formset.setAutosave(true);
396 42714 dmartinezizquierdo
397 44202 jjdelcerro
            this.formset.addAction(new StartEditingAction());
398 42489 jjdelcerro
            this.formset.addAction(new FinishEditingAction());
399 44531 omartinez
            this.formset.addAction(new RefreshAction());
400 45618 jolivas
401 44291 jjdelcerro
            FormActionContext actionContext = new FormActionContext();
402 46845 jjdelcerro
            actionContext.set("featuresform", this);
403 45826 fdiaz
            try {
404 46845 jjdelcerro
                Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getApplicableStoreActions(actionContext);
405 45826 fdiaz
                for (DALActionFactory factory : factories) {
406 46845 jjdelcerro
                    if (StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME)) {
407 45826 fdiaz
                        continue;
408
                    }
409
                    try {
410 46845 jjdelcerro
                        Action action = factory.createAction(actionContext);
411
                        this.formset.addAction(action);
412 45826 fdiaz
                    } catch (Exception ex) {
413
                        LOGGER.warn("Can't add action " + factory.getName(), ex);
414
                    }
415 46051 omartinez
416 44281 jjdelcerro
                }
417 45826 fdiaz
            } catch (Exception ex) {
418
                LOGGER.warn("Can't add actions", ex);
419 45618 jolivas
            }
420
            for (Action action : this.otherActions) {
421 42489 jjdelcerro
                this.formset.addAction(action);
422
            }
423 42714 dmartinezizquierdo
424 42489 jjdelcerro
            this.formset.addListener(new FormSetListener());
425 45618 jolivas
            this.formset.getForm().addListener(new JDynForm.JDynFormListener() {
426
                @Override
427
                public void message(String string) {
428
                }
429
430
                @Override
431
                public void fieldChanged(JDynFormField jdff) {
432
                    updateButtonEnabledStatus();
433
                }
434
            });
435
436 45425 jjdelcerro
            ToolsSwingLocator.getToolsSwingManager().removeBorder(this.formset.asJComponent());
437
            this.panel.add(this.formset.asJComponent(), BorderLayout.CENTER);
438
            this.panel.revalidate();
439 47076 jjdelcerro
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
440 47081 jjdelcerro
            ToolsSwingUtils.ensureHeightWitdh(this.panel, 256, 400, screenSize.height-100, screenSize.width-60);
441 42112 jjdelcerro
        }
442 44488 jjdelcerro
        updateButtonEnabledStatus();
443 42112 jjdelcerro
        return this.formset;
444
    }
445
446 42489 jjdelcerro
    @Override
447
    public void addAction(Action action) {
448
        this.otherActions.add(action);
449 45618 jolivas
        if (this.formset != null) {
450 42489 jjdelcerro
            this.formset.addAction(action);
451
        }
452
    }
453
454
    @Override
455
    public long getCurrentIndex() {
456 45618 jolivas
        if (this.formset == null) {
457 42489 jjdelcerro
            return -1;
458
        }
459
        return this.formset.getCurrentIndex();
460
    }
461
462
    @Override
463
    public Feature get(long index) {
464 45618 jolivas
        if (this.formset == null || this.features == null) {
465 42489 jjdelcerro
            return null;
466
        }
467
        try {
468 44488 jjdelcerro
            return this.features.getFeatureAt(index);
469 42489 jjdelcerro
        } catch (BaseException ex) {
470
            return null;
471
        }
472
    }
473 45618 jolivas
474 44531 omartinez
    private class RefreshAction extends AbstractAction {
475 42714 dmartinezizquierdo
476 44531 omartinez
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
477
        public RefreshAction() {
478
            I18nManager i18nManager = ToolsLocator.getI18nManager();
479
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
480
481
            this.putValue(NAME, null);
482 45591 fdiaz
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Reload_data"));
483 46864 jjdelcerro
            this.putValue(SMALL_ICON, iconTheme.get("common-form-refresh"));
484 44531 omartinez
            this.putValue(ACTION_COMMAND_KEY, REFRESHFORM_ACTION);
485
            this.setEnabled(!formset.isInNewState());
486
        }
487
488
        @Override
489
        public void actionPerformed(ActionEvent ae) {
490
            try {
491
                I18nManager i18n = ToolsLocator.getI18nManager();
492 45591 fdiaz
                formset.message(i18n.getTranslation("_Form_reloaded"));
493 45788 jjdelcerro
                int x=formset.getCurrentIndex();
494 44531 omartinez
                updateForm();
495 45788 jjdelcerro
                formset.setCurrentIndex(x);
496 44531 omartinez
            } catch (Exception ex) {
497 45591 fdiaz
                LOGGER.warn("Can't reload form", ex);
498 44531 omartinez
            }
499
        }
500
    }
501
502 45915 jjdelcerro
    private class FinishEditingAction extends AbstractAction implements Observer, Disposable {
503 42489 jjdelcerro
504 44128 jjdelcerro
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
505 42489 jjdelcerro
        public FinishEditingAction() {
506 42714 dmartinezizquierdo
            I18nManager i18nManager = ToolsLocator.getI18nManager();
507 42489 jjdelcerro
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
508 42714 dmartinezizquierdo
509 45618 jolivas
            this.putValue(NAME, null);
510
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Stop_editing"));
511 42489 jjdelcerro
            this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
512 44488 jjdelcerro
            this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
513 42489 jjdelcerro
514
            this.setEnabled(store.isEditing());
515
            store.addObserver(this);
516
        }
517 42714 dmartinezizquierdo
518 42489 jjdelcerro
        @Override
519
        public void actionPerformed(ActionEvent ae) {
520 45915 jjdelcerro
            if (store == null || formset == null) {
521
                return;
522
            }
523 45618 jolivas
            if (store.isEditing()) {
524 46864 jjdelcerro
                EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
525 46796 jjdelcerro
                DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
526 45912 fdiaz
                ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
527
                I18nManager i18nManager = ToolsLocator.getI18nManager();
528 42489 jjdelcerro
                try {
529 46864 jjdelcerro
                    int x = dataSwingManager.askUserStopEditing(formset.asJComponent(), store, true);
530 45618 jolivas
                    switch (x) {
531 46821 jjdelcerro
                        case DataSwingManager.STOP_EDITING_SAVE:
532 46864 jjdelcerro
                            EditingNotification notification = editingManager.notifyObservers(this, BEFORE_EXIT_EDITING_STORE, null, store);
533
                            if( notification.isCanceled()) {
534
                                I18nManager i18n = ToolsLocator.getI18nManager();
535
                                formset.message(i18n.getTranslation("Finish editing has been cancelled."));
536
                                return;
537
                            }
538 42489 jjdelcerro
                            store.finishEditing();
539 46864 jjdelcerro
                            editingManager.notifyObservers(this, AFTER_EXIT_EDITING_STORE, null, store);
540 42489 jjdelcerro
                            break;
541 46821 jjdelcerro
                        case DataSwingManager.STOP_EDITING_CONTINUE:
542 42489 jjdelcerro
                            break;
543 46821 jjdelcerro
                        case DataSwingManager.STOP_EDITING_DISCARD:
544 42489 jjdelcerro
                            store.cancelEditing();
545 45618 jolivas
                            int index = formset.getCurrentIndex();
546
                            try {
547
                                formset.setCurrentIndex(index);
548
                            } catch (Exception ex) {
549
                                LOGGER.warn("Can't reload form data after edit.", ex);
550
                            }
551 42489 jjdelcerro
                            break;
552 46821 jjdelcerro
                        case DataSwingManager.STOP_EDITING_EXPORT:
553
                            if( !((DefaultDataSwingManager)dataSwingManager).exportStore(store, MODE.DIALOG) ) {
554
                                // Mensaje de operacion no soportada
555
                            }
556 42489 jjdelcerro
                    }
557 45912 fdiaz
                } catch (Exception ex) {
558 45618 jolivas
                    LOGGER.warn("Can't finish editing in FeatureForm (" + store.getName() + ").", ex);
559 45912 fdiaz
                    dialogManager.messageDialog(
560
                            i18nManager.getTranslation("_Problems_finish_table_editing") + "\n\n"
561
                            + i18nManager.getTranslation("_see_error_log_for_more_information"),
562
                            i18nManager.getTranslation("_Stop_editing"),
563
                            JOptionPane.ERROR_MESSAGE
564
                    );
565 42489 jjdelcerro
                }
566
            }
567 44488 jjdelcerro
            updateButtonEnabledStatus();
568 42489 jjdelcerro
        }
569 45618 jolivas
570 42489 jjdelcerro
        @Override
571
        public void update(Observable observable, Object notification) {
572 45618 jolivas
            if (store == null || formset == null) {
573 44346 jjdelcerro
                return;
574
            }
575 45618 jolivas
            if (notification instanceof FeatureStoreNotification) {
576
                FeatureStoreNotification n = (FeatureStoreNotification) notification;
577
                switch (n.getType()) {
578 45915 jjdelcerro
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
579
                        needrefresh = true;
580 42489 jjdelcerro
                    case FeatureStoreNotification.AFTER_STARTEDITING:
581 44202 jjdelcerro
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
582 44488 jjdelcerro
                        updateButtonEnabledStatus();
583 42489 jjdelcerro
                        break;
584 44202 jjdelcerro
                }
585
            }
586
        }
587
588 45915 jjdelcerro
        @Override
589
        public void dispose() {
590
            store.deleteObserver(this);
591
        }
592
593 44202 jjdelcerro
    }
594 45618 jolivas
595 45915 jjdelcerro
    private class StartEditingAction extends AbstractAction implements Observer, Disposable {
596 44202 jjdelcerro
597
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
598
        public StartEditingAction() {
599
            I18nManager i18nManager = ToolsLocator.getI18nManager();
600
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
601
602 45618 jolivas
            this.putValue(NAME, null);
603
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Start_editing"));
604 44202 jjdelcerro
            this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
605 44488 jjdelcerro
            this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
606 44202 jjdelcerro
607
            this.setEnabled(!store.isEditing());
608
            store.addObserver(this);
609
        }
610
611
        @Override
612
        public void actionPerformed(ActionEvent ae) {
613 45915 jjdelcerro
            if (store == null || formset == null) {
614
                return;
615
            }
616 45618 jolivas
            if (!store.isEditing()) {
617 44202 jjdelcerro
                try {
618 46864 jjdelcerro
                    EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
619
                    EditingNotification notification = editingManager.notifyObservers(this, BEFORE_ENTER_EDITING_STORE, null, store);
620
                    if( notification.isCanceled() ) {
621
                        I18nManager i18n = ToolsLocator.getI18nManager();
622
                        formset.message(i18n.getTranslation("Enter editing has been cancelled."));
623
                        return;
624
                    }
625 44202 jjdelcerro
                    store.edit();
626 46864 jjdelcerro
                    editingManager.notifyObservers(this, AFTER_ENTER_EDITING_STORE, null, store);
627 44202 jjdelcerro
                } catch (DataException ex) {
628 45618 jolivas
                    LOGGER.warn("Can't finish editing in FeatureForm (" + store.getName() + ").", ex);
629 44202 jjdelcerro
                }
630
            }
631 44488 jjdelcerro
            updateButtonEnabledStatus();
632 44202 jjdelcerro
        }
633
634
        @Override
635
        public void update(Observable observable, Object notification) {
636 45618 jolivas
            if (store == null || formset == null) {
637 44346 jjdelcerro
                return;
638
            }
639 45618 jolivas
            if (notification instanceof FeatureStoreNotification) {
640
                FeatureStoreNotification n = (FeatureStoreNotification) notification;
641
                switch (n.getType()) {
642 45915 jjdelcerro
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
643
                        needrefresh = true;
644 44202 jjdelcerro
                    case FeatureStoreNotification.AFTER_STARTEDITING:
645 42489 jjdelcerro
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
646 44488 jjdelcerro
                        updateButtonEnabledStatus();
647 42489 jjdelcerro
                        break;
648
                }
649
            }
650
        }
651 42714 dmartinezizquierdo
652 45915 jjdelcerro
        @Override
653
        public void dispose() {
654
            store.deleteObserver(this);
655
        }
656
657 42489 jjdelcerro
    }
658
659
    @Override
660
    public void setQuery(FeatureQuery query) {
661 44488 jjdelcerro
        if (this.features != null) {
662 44239 jjdelcerro
            if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) {
663 45781 fdiaz
                if(this.formset.getForm().isModified()){
664
                    if (!store.isEditing()) {
665
                        try {
666
                            store.edit();
667
                        } catch (DataException e1) {
668
                            throw new RuntimeException("Can't set query", e1);
669
                        }
670 42112 jjdelcerro
                    }
671 45781 fdiaz
                    saveChanges(this.formset);
672 42112 jjdelcerro
                }
673
            }
674
        }
675 42248 jjdelcerro
        this.currentQuery = query;
676
        updateForm();
677 42112 jjdelcerro
    }
678
679 44239 jjdelcerro
    private FeatureQuery getCurrentQuery() {
680
        return this.currentQuery;
681
    }
682 45618 jolivas
683 42489 jjdelcerro
    @Override
684
    public void showForm(MODE mode) {
685 46685 jjdelcerro
        I18nManager i18n = ToolsLocator.getI18nManager();
686
        String title = i18n.getTranslation("_Form") + ": "+ this.definition.getLabel();
687 46420 jjdelcerro
        showForm(title, mode);
688
    }
689
690 46821 jjdelcerro
    @Override
691 46420 jjdelcerro
    public void showForm(String title, MODE mode) {
692 42248 jjdelcerro
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
693 46420 jjdelcerro
        if( StringUtils.isBlank(title) ) {
694
            title = this.definition.getLabel();
695
        }
696 42775 jjdelcerro
        winmgr.showWindow(this.asJComponent(), title, mode);
697 42112 jjdelcerro
    }
698 46453 jjdelcerro
699 46821 jjdelcerro
    @Override
700 46453 jjdelcerro
    public void hideForm() {
701
        DisposeUtils.dispose(this);
702
    }
703 42112 jjdelcerro
704 44488 jjdelcerro
    private void saveChanges(JDynFormSet theFormSet) {
705 44346 jjdelcerro
        I18nManager i18n = ToolsLocator.getI18nManager();
706 44488 jjdelcerro
        try {
707 46864 jjdelcerro
            EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
708 45618 jolivas
            if (theFormSet.isInNewState()) {
709 45619 jjdelcerro
                EditableFeature newFeature = store.createNewFeature(false);
710 46024 jjdelcerro
                JDynForm form = formset.getForm();
711 45619 jjdelcerro
                for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
712 46024 jjdelcerro
                    String name = attr.getName();
713
                    if( !newFeature.canSetValue(name) ) {
714
                        continue;
715
                    }
716
                    JDynFormField field = form.getField(name);
717 45619 jjdelcerro
                    if( field==null ) {
718 46024 jjdelcerro
                        // The attribute is not in form, set default value
719
                        newFeature.set(name, attr.getDefaultValueCoerced());
720
                    } else {
721
                        try {
722
                            Object value = field.getValue();
723
                            newFeature.set(name, value);
724
                        } catch (Exception ex) {
725
                            LOGGER.warn("Can't get value of field '" + name + "'.", ex);
726
                        }
727 45619 jjdelcerro
                    }
728
                }
729 46864 jjdelcerro
                EditingNotification notification = editingManager.notifyObservers(this, BEFORE_INSERT_FEATURE, null, this.store, newFeature);
730
                if( notification.isCanceled() ) {
731
                    theFormSet.message(i18n.getTranslation("Data saving has been cancelled."));
732
                    return;
733
                }
734
                if (!editingManager.canWriteFeature(newFeature)) {
735
                    theFormSet.message(i18n.getTranslation("The data cannot be saved. Check that all the required fields are filled out.."));
736
                    return;
737
                }
738 46024 jjdelcerro
                features.insert(newFeature);
739 46864 jjdelcerro
                editingManager.notifyObservers(this, AFTER_INSERT_FEATURE, null, this.store, newFeature);
740
741 44488 jjdelcerro
                this.formset.message(i18n.getTranslation("_Record_saved"));
742
                try {
743
                    this.formset.setValues(features.asListOfDynObjects());
744 45618 jolivas
                    this.formset.setCurrentIndex((int) (features.getTotalSize()) - 1);
745
                } catch (Exception ex) {
746
                    LOGGER.warn("Can't reload form data after insert.", ex);
747 44488 jjdelcerro
                }
748
            } else {
749
                int index = theFormSet.getCurrentIndex();
750
                DynObject currentElement = theFormSet.get(index);
751
                theFormSet.getFormValues(currentElement);
752 46864 jjdelcerro
753
                EditableFeature feature = ((FacadeOfAFeature) currentElement).getEditableFeature();
754
                EditingNotification notification = editingManager.notifyObservers(this, BEFORE_UPDATE_FEATURE, null, this.store, feature);
755
                if( notification.isCanceled() ) {
756
                    theFormSet.message(i18n.getTranslation("Data saving has been cancelled."));
757
                    return;
758
                }
759
                if (!editingManager.canWriteFeature(feature)) {
760
                    theFormSet.message(i18n.getTranslation("The data cannot be saved. Check that all the required fields are filled out.."));
761
                    return;
762
                }
763
                features.update(feature);
764
                editingManager.notifyObservers(this, AFTER_UPDATE_FEATURE, null, this.store, feature);
765
766 44488 jjdelcerro
                this.formset.message(i18n.getTranslation("_Record_saved"));
767 45618 jolivas
                try {
768
                    this.formset.setCurrentIndex(index);
769
                } catch (Exception ex) {
770
                    LOGGER.warn("Can't reload form data after insert.", ex);
771
                }
772 42775 jjdelcerro
            }
773 45618 jolivas
        } catch (Exception ex) {
774 44488 jjdelcerro
            theFormSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
775 45618 jolivas
            throw new RuntimeException("Can't save values", ex);
776
777 44488 jjdelcerro
        } finally {
778
            updateButtonEnabledStatus();
779 42489 jjdelcerro
        }
780 42112 jjdelcerro
781
    }
782 45618 jolivas
783 44510 omartinez
    private void deleteCurrentElement(JDynFormSet theFormSet) {
784
        I18nManager i18n = ToolsLocator.getI18nManager();
785
        try {
786 46864 jjdelcerro
            EditingNotificationManager editingManager = DALSwingLocator.getEditingNotificationManager();
787 44510 omartinez
            int index = theFormSet.getCurrentIndex();
788
            DynObject currentElement = theFormSet.get(index);
789
            theFormSet.getFormValues(currentElement);
790
            Feature feature = ((FacadeOfAFeature) currentElement).getFeature();
791
            if (feature instanceof EditableFeature) {
792
                feature = ((EditableFeature) feature).getNotEditableCopy();
793
            }
794 46864 jjdelcerro
            if( editingManager.notifyObservers(this, BEFORE_REMOVE_FEATURE, null, this.store, feature).isCanceled() ) {
795
                theFormSet.message(i18n.getTranslation("The delete operation has been cancelled."));
796
                return;
797
            }
798 44510 omartinez
            features.delete(feature);
799 46864 jjdelcerro
            editingManager.notifyObservers(this, AFTER_REMOVE_FEATURE, null, this.store, feature);
800 45618 jolivas
801 44510 omartinez
            this.formset.message(i18n.getTranslation("_Record_removed"));
802 45618 jolivas
803 44531 omartinez
            this.formset.setValues(features.asListOfDynObjects());
804 45618 jolivas
            if (features.getTotalSize() - 1 < index) {
805
                index = index - 1;
806 44531 omartinez
            }
807
            this.formset.setCurrentIndex(index);
808 42112 jjdelcerro
809 44510 omartinez
        } catch (Exception ex) {
810
            theFormSet.message(i18n.getTranslation("error_removing_data_will_not_remove"));
811
            throw new RuntimeException("Can't remove values", ex);
812
813
        } finally {
814
            updateButtonEnabledStatus();
815
        }
816
817
    }
818 45618 jolivas
819 42489 jjdelcerro
    @Override
820
    public void saveChanges() {
821
        if (this.formset != null && this.formset.countValues() > 0) {
822
            if (store != null && !store.isEditing()) {
823
                try {
824
                    store.edit();
825
                } catch (DataException e1) {
826
                    LOGGER.warn("Can't edit the store " + store.getName());
827
                    throw new RuntimeException("Can't save changes.", e1);
828
                }
829 42112 jjdelcerro
            }
830 42489 jjdelcerro
            this.saveChanges(this.formset);
831 42112 jjdelcerro
        }
832
    }
833
834 44488 jjdelcerro
    private void updateButtonEnabledStatus() {
835 45618 jolivas
        if (this.formset == null) {
836 44488 jjdelcerro
            return;
837
        }
838 45618 jolivas
        if (this.store == null || store.isBroken() || store.isAppending() || this.features == null) {
839 44488 jjdelcerro
            this.formset.setReadOnly(true);
840
            formset.setActionVisible(STARTEDITING_ACTION, true);
841
            formset.setActionEnabled(STARTEDITING_ACTION, true);
842
            formset.setActionVisible(FINISHEDITING_ACTION, false);
843
            formset.setActionEnabled(FINISHEDITING_ACTION, false);
844
            formset.setActionEnabled(ACTION_DELETE, false);
845 45618 jolivas
            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 44531 omartinez
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
850 45783 jjdelcerro
            formset.setActionEnabled(ACTION_NAVIGATION, false);
851 44488 jjdelcerro
            return;
852
        }
853 45915 jjdelcerro
        if (this.needrefresh ) {
854
            this.formset.setReadOnly(true);
855
//            formset.setActionEnabled(STARTEDITING_ACTION, false);
856
//            formset.setActionEnabled(FINISHEDITING_ACTION, false);
857
            formset.setActionEnabled(ACTION_DELETE, false);
858
            formset.setActionEnabled(ACTION_NEW, false);
859
            formset.setActionEnabled(ACTION_CANCEL_NEW, false);
860
            formset.setActionEnabled(ACTION_SAVE, false);
861
            formset.setActionEnabled(ACTION_SEARCH, false);
862
            formset.setActionEnabled(ACTION_NAVIGATION, false);
863
            for (Action action : formset.getActions()) {
864
                formset.setActionEnabled((String) action.getValue(ACTION_COMMAND_KEY), false);
865
            }
866
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
867
            return;
868
        }
869 45618 jolivas
        if (store.isEditing()) {
870 44488 jjdelcerro
            this.formset.setReadOnly(false);
871
            formset.setActionVisible(STARTEDITING_ACTION, false);
872
            formset.setActionEnabled(STARTEDITING_ACTION, false);
873
            formset.setActionVisible(FINISHEDITING_ACTION, true);
874
            formset.setActionEnabled(FINISHEDITING_ACTION, true);
875 45618 jolivas
            if (formset.isInNewState()) {
876 44488 jjdelcerro
                formset.setActionEnabled(ACTION_DELETE, false);
877 45618 jolivas
                formset.setActionEnabled(ACTION_SEARCH, false);
878
                formset.setActionEnabled(ACTION_NEW, false);
879
                formset.setActionEnabled(ACTION_CANCEL_NEW, true);
880
                formset.setActionEnabled(ACTION_SAVE, true);
881 45585 jjdelcerro
                formset.setActionEnabled(FINISHEDITING_ACTION, false);
882 44531 omartinez
                formset.setActionEnabled(REFRESHFORM_ACTION, false);
883 45795 jjdelcerro
//                setEnabledUniqueFields(true);
884 45618 jolivas
885
            } else if (this.features != null && this.features.isEmpty()) {
886
                formset.getForm().setReadOnly(true);
887 44488 jjdelcerro
                formset.setActionEnabled(ACTION_DELETE, false);
888 45618 jolivas
                formset.setActionEnabled(ACTION_SEARCH, false);
889
                formset.setActionEnabled(ACTION_NEW, true);
890
                formset.setActionEnabled(ACTION_CANCEL_NEW, true);
891
                formset.setActionEnabled(ACTION_SAVE, false);
892 44531 omartinez
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
893 45618 jolivas
                formset.setActionEnabled(FINISHEDITING_ACTION, true);
894 45783 jjdelcerro
                formset.setActionEnabled(ACTION_NAVIGATION, false);
895 44488 jjdelcerro
896 45618 jolivas
            } else if (formset.getForm().isModified()) {
897
                formset.setActionEnabled(ACTION_DELETE, false);
898
                formset.setActionEnabled(ACTION_SEARCH, false);
899
                formset.setActionEnabled(ACTION_NEW, false);
900
                formset.setActionEnabled(ACTION_CANCEL_NEW, false);
901
                formset.setActionEnabled(ACTION_SAVE, true);
902 44531 omartinez
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
903 45591 fdiaz
                formset.setActionEnabled(FINISHEDITING_ACTION, false);
904 45783 jjdelcerro
                formset.setActionEnabled(ACTION_NAVIGATION, false);
905 44488 jjdelcerro
906
            } else {
907
                formset.setActionEnabled(ACTION_DELETE, true);
908 45618 jolivas
                formset.setActionEnabled(ACTION_SEARCH, true);
909
                formset.setActionEnabled(ACTION_NEW, true);
910
                formset.setActionEnabled(ACTION_CANCEL_NEW, false);
911 45783 jjdelcerro
                formset.setActionEnabled(ACTION_SAVE, false);
912 44531 omartinez
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
913 45591 fdiaz
                formset.setActionEnabled(FINISHEDITING_ACTION, true);
914 45783 jjdelcerro
                formset.setActionEnabled(ACTION_NAVIGATION, true);
915 44488 jjdelcerro
            }
916
917
            return;
918
        }
919
        this.formset.setReadOnly(true);
920
        formset.setActionVisible(STARTEDITING_ACTION, true);
921
        formset.setActionEnabled(STARTEDITING_ACTION, true);
922
        formset.setActionVisible(FINISHEDITING_ACTION, false);
923
        formset.setActionEnabled(FINISHEDITING_ACTION, false);
924
        formset.setActionEnabled(ACTION_DELETE, false);
925 45618 jolivas
        formset.setActionEnabled(ACTION_NEW, false);
926
        formset.setActionEnabled(ACTION_CANCEL_NEW, false);
927
        formset.setActionEnabled(ACTION_SAVE, false);
928
        formset.setActionEnabled(ACTION_SEARCH, true);
929
        formset.setActionEnabled(REFRESHFORM_ACTION, true);
930 46871 fdiaz
        for (Action action : formset.getActions()) {
931
            formset.setActionEnabled((String) action.getValue(ACTION_COMMAND_KEY), true);
932
        }
933
934 44488 jjdelcerro
    }
935 45425 jjdelcerro
936
    private void clearUniqueFields() {
937 45618 jolivas
        for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
938
            if ((attr.isPrimaryKey() && !attr.isAutomatic()) || (attr.isIndexed() && !attr.allowIndexDuplicateds())) {
939 45425 jjdelcerro
                JDynFormField field = formset.getForm().getField(attr.getName());
940 45618 jolivas
                if (field != null) {
941 45425 jjdelcerro
                    field.clear();
942
                }
943
            }
944
        }
945
    }
946 45618 jolivas
947
    private void setEnabledUniqueFields(boolean enabled) {
948
        for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) {
949
            if ((attr.isPrimaryKey() && !attr.isAutomatic()) || (attr.isIndexed() && !attr.allowIndexDuplicateds())) {
950
                JDynFormField field = formset.getForm().getField(attr.getName());
951
                if (field != null) {
952
                    field.setReadOnly(!enabled);
953
                }
954
            }
955
        }
956
    }
957
958 42489 jjdelcerro
    @Override
959
    public long getDataSetSize() {
960 44488 jjdelcerro
        if (this.features != null) {
961
            return features.getTotalSize();
962 42489 jjdelcerro
        }
963
        return 0;
964 42112 jjdelcerro
    }
965
966 42489 jjdelcerro
    @Override
967
    public FeatureStore getFeatureStore() {
968
        return this.store;
969 42112 jjdelcerro
    }
970 46453 jjdelcerro
971
    public FeatureType getFeatureType() {
972
        if( this.store == null ) {
973
            return null;
974
        }
975
        return this.store.getDefaultFeatureTypeQuietly();
976
    }
977 42112 jjdelcerro
978 42489 jjdelcerro
    private class FormSetListener implements JDynFormSetListener {
979 42112 jjdelcerro
980 42489 jjdelcerro
        @Override
981
        public void formMessage(String message) {
982 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formMessage"));
983 42489 jjdelcerro
        }
984 42112 jjdelcerro
985 42489 jjdelcerro
        @Override
986
        public void formClose() {
987
            panel.setVisible(false);
988 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formClose"));
989 42112 jjdelcerro
        }
990
991 42489 jjdelcerro
        @Override
992
        public void formMovedTo(int currentPosition) throws AbortActionException {
993
            LOGGER.trace("formMovedTo " + currentPosition);
994 44488 jjdelcerro
            updateButtonEnabledStatus();
995 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formMovedTo"));
996 42489 jjdelcerro
        }
997 42112 jjdelcerro
998 42489 jjdelcerro
        @Override
999
        public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException {
1000
            LOGGER.trace("formBeforeSave");
1001 45618 jolivas
            saveChanges(dynformSet);
1002 44488 jjdelcerro
            updateButtonEnabledStatus();
1003 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeSave"));
1004 42489 jjdelcerro
        }
1005 42112 jjdelcerro
1006 44259 jjdelcerro
        @Override
1007 42489 jjdelcerro
        public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException {
1008
            LOGGER.trace("formBeforeNew");
1009 45425 jjdelcerro
            clearUniqueFields();
1010 44488 jjdelcerro
            updateButtonEnabledStatus();
1011 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeNew"));
1012 42112 jjdelcerro
        }
1013
1014 44259 jjdelcerro
        @Override
1015 42489 jjdelcerro
        public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException {
1016
            LOGGER.trace("formBeforeDelete");
1017 44488 jjdelcerro
            updateButtonEnabledStatus();
1018 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeDelete"));
1019 42489 jjdelcerro
        }
1020 42112 jjdelcerro
1021 42489 jjdelcerro
        @Override
1022
        public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException {
1023
            LOGGER.trace("formAfterSave");
1024 44488 jjdelcerro
            updateButtonEnabledStatus();
1025 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterSave"));
1026 42489 jjdelcerro
        }
1027 42112 jjdelcerro
1028 42489 jjdelcerro
        @Override
1029
        public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException {
1030
            LOGGER.trace("formAfterNew");
1031 44488 jjdelcerro
            updateButtonEnabledStatus();
1032 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterNew"));
1033 42112 jjdelcerro
        }
1034
1035 42489 jjdelcerro
        @Override
1036
        public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
1037
            LOGGER.trace("formAfterDelete");
1038 44510 omartinez
            deleteCurrentElement(dynformSet);
1039 44488 jjdelcerro
            updateButtonEnabledStatus();
1040 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterDelete"));
1041 42112 jjdelcerro
        }
1042
1043 42489 jjdelcerro
        @Override
1044
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
1045
            LOGGER.trace("formBeforeSearch");
1046
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
1047
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
1048 42714 dmartinezizquierdo
1049 44292 jjdelcerro
            final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
1050
            searchPanel.setShowActions(false);
1051 44239 jjdelcerro
            FeatureQuery currentQuery = getCurrentQuery();
1052 45618 jolivas
            if (currentQuery != null) {
1053 44239 jjdelcerro
                Evaluator filter = currentQuery.getFilter();
1054 45618 jolivas
                if (filter instanceof ExpressionEvaluator) {
1055
                    Expression expression = ((ExpressionEvaluator) filter).toExpression();
1056 44292 jjdelcerro
                    searchPanel.setFilter(expression);
1057 44794 omartinez
                    searchPanel.search();
1058 44239 jjdelcerro
                }
1059
            }
1060 47212 jjdelcerro
            ToolsSwingUtils.ensureHeightWitdh(
1061 47213 jjdelcerro
                    searchPanel,
1062
                    ToolsSwingUtils.RELATIVE_TO_SCREEN,
1063
                    0.75f, 0.75f, 0.85f, 0.85f
1064
            );
1065
1066 44590 jjdelcerro
            I18nManager i18n = ToolsLocator.getI18nManager();
1067 42489 jjdelcerro
            Dialog dialog = winmgr.createDialog(
1068 44292 jjdelcerro
                    searchPanel.asJComponent(),
1069 44590 jjdelcerro
                    i18n.getTranslation("_Filter"),
1070 45618 jolivas
                    i18n.getTranslation("_Creating_filter_for") + ": '" + store.getName() + "'",
1071 42489 jjdelcerro
                    WindowManager_v2.BUTTONS_OK_CANCEL
1072
            );
1073
            dialog.show(WindowManager.MODE.DIALOG);
1074 45618 jolivas
            if (dialog.getAction() == WindowManager_v2.BUTTON_OK) {
1075 44829 omartinez
                SearchParameters params = searchPanel.fetch(null);
1076 45618 jolivas
                if (params.getQuery() != null) {
1077
                    FeatureQuery searchQuery = params.getQuery().getCopy();
1078
1079
                    Expression expresion = null;
1080
                    if (searchQuery != null) {
1081
                        expresion = searchQuery.getExpressionFilter();
1082 42489 jjdelcerro
                    }
1083 45618 jolivas
                    try {
1084
                        FeatureQuery query = store.createFeatureQuery();
1085
                        if (ExpressionUtils.isPhraseEmpty(expresion)) {
1086
                            query.clearFilter();
1087
                        } else {
1088
                            query.setFilter(expresion);
1089
                        }
1090
                        setQuery(query);
1091
                    } catch (Exception ex) {
1092
                        LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
1093
                    }
1094 44829 omartinez
                } else {
1095
                    LOGGER.warn("FeatureQuery from parameters is null. Query is not applied to the JFeaturesFrom");
1096
                }
1097 42112 jjdelcerro
            }
1098 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeSearch"));
1099 42112 jjdelcerro
        }
1100
1101 42489 jjdelcerro
        @Override
1102
        public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException {
1103
            LOGGER.trace("formAfterSearch");
1104 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterSearch"));
1105 42489 jjdelcerro
        }
1106 42775 jjdelcerro
1107
        @Override
1108
        public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException {
1109
            LOGGER.trace("formBeforeCancelNew");
1110 44488 jjdelcerro
            updateButtonEnabledStatus();
1111 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeCancelNew"));
1112 42775 jjdelcerro
        }
1113
1114
        @Override
1115
        public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException {
1116
            LOGGER.trace("formAfterCancelNew");
1117 45915 jjdelcerro
            try {
1118
                int index = formset.getCurrentIndex();
1119
                if( index<0 ) {
1120
                    formset.getForm().clear();
1121
                } else {
1122
                    formset.setCurrentIndex(index);
1123
                }
1124
            } catch (Exception ex) {
1125
                LOGGER.warn("Can't reload form data after cancel new.", ex);
1126
            }
1127 44488 jjdelcerro
            updateButtonEnabledStatus();
1128 45532 jjdelcerro
            actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterCancelNew"));
1129 42775 jjdelcerro
        }
1130 46813 fdiaz
1131 42112 jjdelcerro
    }
1132
1133 45532 jjdelcerro
    @Override
1134
    public void addActionListener(ActionListener listener) {
1135
        this.actionListeners.addActionListener(listener);
1136
    }
1137
1138
    @Override
1139
    public ActionListener[] getActionListeners() {
1140
        return this.actionListeners.getActionListeners();
1141
    }
1142
1143
    @Override
1144
    public void removeActionListener(ActionListener listener) {
1145
        this.actionListeners.removeActionListener(listener);
1146
    }
1147
1148
    @Override
1149
    public void removeAllActionListener() {
1150
        this.actionListeners.removeAllActionListener();
1151
    }
1152
1153
    @Override
1154
    public void fireActionEvent(ActionEvent event) {
1155
        this.actionListeners.fireActionEvent(event);
1156
    }
1157
1158
    @Override
1159
    public boolean hasActionListeners() {
1160
        return this.actionListeners.hasActionListeners();
1161
    }
1162
1163 46010 jjdelcerro
    @Override
1164
    public Feature getCurrentFeature() {
1165 46845 jjdelcerro
        // TODO: No tengo claro que esto sea correcto, ya que no tiene
1166
        // en cuenta si el form se esta modificando o es nuevo para coger
1167
        // los valores del formulario.
1168 46010 jjdelcerro
        long index = getCurrentIndex();
1169
        if( index<0 ) {
1170
            return null;
1171
        }
1172
        Feature f = get(index);
1173
        try {
1174
            DynObject adapter = f.getAsDynObject();
1175
            this.getFormset().getForm().getValues(adapter);
1176
            f = ((FacadeOfAFeature)adapter).getFeature();
1177
            return f;
1178
        } catch(Exception ex) {
1179
            return f;
1180
        }
1181
    }
1182 46467 jjdelcerro
1183
    protected void callUserEvent(String name, Object...args) {
1184 46582 fdiaz
//        try {
1185 46467 jjdelcerro
            JDynForm form = this.formset.getForm();
1186 46582 fdiaz
            form.callUserEvent(name, args);
1187
//        } catch(NoSuchMethodException ex) {
1188
//            LOGGER.debug("Function '"+name+"' not found.", ex);
1189
//            // Do nothing
1190
//        } catch(Exception ex) {
1191
//            LOGGER.warn("Error calling form event '"+name+"'.", ex);
1192
//        }
1193 46467 jjdelcerro
    }
1194 46813 fdiaz
1195
    @Override
1196
    public void setTerminateEditingOnClose(boolean b) {
1197
        terminateEditingOnClose = b;
1198
    }
1199
1200
    @Override
1201
    public boolean isTerminateEditingOnClose() {
1202
        return terminateEditingOnClose;
1203
    }
1204
1205
    private void onClose() {
1206
        if (this.store == null || this.ignoreTerminateEditingOnClose) {
1207
            return;
1208
        }
1209
        if (this.store.isEditing() && terminateEditingOnClose) {
1210
            DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager();
1211
            ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
1212
            I18nManager i18nManager = ToolsLocator.getI18nManager();
1213
            try {
1214 46864 jjdelcerro
                int x = dataSwingManager.askUserStopEditing(formset.asJComponent(), store, false);
1215 46813 fdiaz
                switch (x) {
1216 46821 jjdelcerro
                    case DataSwingManager.STOP_EDITING_SAVE:
1217 46813 fdiaz
                        store.finishEditing();
1218
                        break;
1219 46821 jjdelcerro
                    case DataSwingManager.STOP_EDITING_CONTINUE:
1220
                        break;
1221
                    case DataSwingManager.STOP_EDITING_DISCARD:
1222 46813 fdiaz
                        store.cancelEditing();
1223
                        break;
1224 46821 jjdelcerro
                    case DataSwingManager.STOP_EDITING_EXPORT:
1225
                        if( !((DefaultDataSwingManager)dataSwingManager).exportStore(store, MODE.DIALOG) ) {
1226
                            // Mensaje de operacion no soportada
1227
                        }
1228 46813 fdiaz
                }
1229
            } catch (Exception ex) {
1230 46821 jjdelcerro
                LOGGER.warn("Can't finish editing in FeatureForm (" + DataStore.getNameQuietly(store) + ").", ex);
1231 46813 fdiaz
                dialogManager.messageDialog(
1232
                        i18nManager.getTranslation("_Problems_finish_table_editing") + "\n\n"
1233
                        + i18nManager.getTranslation("_see_error_log_for_more_information"),
1234
                        i18nManager.getTranslation("_Stop_editing"),
1235
                        JOptionPane.ERROR_MESSAGE
1236
                );
1237
            }
1238
1239
        }
1240
    }
1241
1242 46845 jjdelcerro
1243 42112 jjdelcerro
}