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

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