Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / featureform / swing / impl / DefaultJFeaturesForm.java @ 44590

History | View | Annotate | Download (33.5 KB)

1 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 42489 jjdelcerro
import java.awt.event.ActionEvent;
28 44291 jjdelcerro
import java.awt.event.ComponentAdapter;
29
import java.awt.event.ComponentEvent;
30 44531 omartinez
import java.net.URL;
31 42489 jjdelcerro
import java.util.ArrayList;
32 44281 jjdelcerro
import java.util.Collection;
33 42489 jjdelcerro
import java.util.List;
34
import javax.swing.AbstractAction;
35
import javax.swing.Action;
36 44202 jjdelcerro
import static javax.swing.Action.ACTION_COMMAND_KEY;
37
import static javax.swing.Action.NAME;
38
import static javax.swing.Action.SHORT_DESCRIPTION;
39
import static javax.swing.Action.SMALL_ICON;
40 42112 jjdelcerro
41
import javax.swing.JComponent;
42 42489 jjdelcerro
import javax.swing.JOptionPane;
43 42112 jjdelcerro
import javax.swing.JPanel;
44 44281 jjdelcerro
import org.apache.commons.lang3.StringUtils;
45 44128 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
46 44339 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
47 44239 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionUtils;
48 42112 jjdelcerro
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51
52 42775 jjdelcerro
import org.gvsig.featureform.swing.JFeaturesForm;
53 42112 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
54 44253 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
55
import org.gvsig.fmap.dal.StoresRepository;
56 42112 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
57 44239 jjdelcerro
import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator;
58 44510 omartinez
import org.gvsig.fmap.dal.feature.EditableFeature;
59 42112 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
60 42775 jjdelcerro
import org.gvsig.fmap.dal.feature.FacadeOfAFeature;
61 42112 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
62
import org.gvsig.fmap.dal.feature.FeatureStore;
63 42489 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
64 44262 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
65 42112 jjdelcerro
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
66 44281 jjdelcerro
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext;
67
import org.gvsig.fmap.dal.swing.DALActionFactory;
68 42489 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
69
import org.gvsig.fmap.dal.swing.DataSwingManager;
70 44202 jjdelcerro
import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager;
71 44281 jjdelcerro
import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory;
72 44292 jjdelcerro
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel;
73 42112 jjdelcerro
import org.gvsig.tools.ToolsLocator;
74 44291 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
75 42112 jjdelcerro
import org.gvsig.tools.dynform.AbortActionException;
76 44128 jjdelcerro
import org.gvsig.tools.dynform.DynFormDefinition;
77 42112 jjdelcerro
import org.gvsig.tools.dynform.DynFormLocator;
78
import org.gvsig.tools.dynform.DynFormManager;
79 44202 jjdelcerro
import org.gvsig.tools.dynform.JDynForm;
80 42112 jjdelcerro
import org.gvsig.tools.dynform.JDynFormSet;
81 44488 jjdelcerro
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CANCEL_NEW;
82
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_DELETE;
83
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW;
84
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE;
85
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SEARCH;
86 42112 jjdelcerro
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener;
87
import org.gvsig.tools.dynobject.DynClass;
88
import org.gvsig.tools.dynobject.DynObject;
89 44239 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
90 42112 jjdelcerro
import org.gvsig.tools.exception.BaseException;
91
import org.gvsig.tools.i18n.I18nManager;
92 42489 jjdelcerro
import org.gvsig.tools.observer.Observable;
93
import org.gvsig.tools.observer.Observer;
94 42112 jjdelcerro
import org.gvsig.tools.service.ServiceException;
95
import org.gvsig.tools.swing.api.ToolsSwingLocator;
96 42489 jjdelcerro
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
97
import org.gvsig.tools.swing.api.windowmanager.Dialog;
98 42112 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
99 42489 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
100 42112 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
101 42489 jjdelcerro
import org.gvsig.tools.swing.icontheme.IconTheme;
102 44297 jjdelcerro
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
103 44339 jjdelcerro
import org.gvsig.tools.script.ScriptManager;
104 42112 jjdelcerro
105
/**
106
 * @author fdiaz
107
 *
108
 */
109 44128 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
110 42775 jjdelcerro
public class DefaultJFeaturesForm implements JFeaturesForm {
111 42112 jjdelcerro
112 44488 jjdelcerro
    private final String STARTEDITING_ACTION = "startEditing";
113
    private final String FINISHEDITING_ACTION = "finishEditing";
114 44531 omartinez
    private final String REFRESHFORM_ACTION = "refreshForm";
115 44488 jjdelcerro
116
117 44253 jjdelcerro
    public class DefaultFeaturesFormContext implements FeaturesFormContext {
118
119
        private DefaultFeaturesFormContext() {
120
121
        }
122 44262 jjdelcerro
123 44338 jjdelcerro
        @Override
124 44262 jjdelcerro
        public FeatureStore getFeatureStore() {
125
            return store;
126
        }
127 44253 jjdelcerro
128 44281 jjdelcerro
        @Override
129 44262 jjdelcerro
        public FeatureType getFeatureType() {
130
            try {
131
                return store.getDefaultFeatureType();
132
            } catch (DataException ex) {
133
                return null;
134
            }
135
        }
136
137 44253 jjdelcerro
        @Override
138
        public ResourcesStorage getResourcesStorage() {
139 44338 jjdelcerro
            if( store == null ) {
140
                return null;
141
            }
142 44253 jjdelcerro
            return store.getResourcesStorage();
143
        }
144
145 44259 jjdelcerro
        @Override
146 44262 jjdelcerro
        public StoresRepository getStoresRepository() {
147 44338 jjdelcerro
            if( store == null ) {
148
                return null;
149
            }
150 44262 jjdelcerro
            return store.getStoresRepository();
151 44253 jjdelcerro
        }
152 44339 jjdelcerro
153
        @Override
154
        public ScriptManager getScriptManager() {
155
            return ExpressionEvaluatorLocator.getExpressionEvaluatorManager();
156
        }
157
158 44262 jjdelcerro
159 44253 jjdelcerro
    }
160
161 44291 jjdelcerro
    private class FormActionContext extends AbstractDALActionContext {
162 44281 jjdelcerro
163 44291 jjdelcerro
        public FormActionContext() {
164 44281 jjdelcerro
            super(ACTION_CONTEXT_NAME);
165
        }
166
167
        @Override
168
        public DataStore getStore() {
169 44291 jjdelcerro
            return store;
170 44281 jjdelcerro
        }
171
172
        @Override
173
        public Expression getFilter() {
174 44291 jjdelcerro
            FeatureQuery theCurrentQuery = currentQuery;
175
            if( theCurrentQuery!=null ) {
176
                Evaluator filter = theCurrentQuery.getFilter();
177 44281 jjdelcerro
                if( filter instanceof ExpressionEvaluator ) {
178
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
179
                    return expression;
180
                }
181
            }
182
            return null;
183
        }
184
185
        @Override
186
        public int getSelectedsCount() {
187
            return 0;
188
        }
189
190
        @Override
191
        public Expression getFilterForSelecteds() {
192
            return null;
193
        }
194
195
        @Override
196
        public JComponent getActionButton(String actionName) {
197
            if( formset == null ) {
198
                return null;
199
            }
200
            return formset.getActionButton(actionName);
201
        }
202
203
    }
204
205 42775 jjdelcerro
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class);
206 42112 jjdelcerro
207
    private static final int PAGE_SIZE = 500;
208 44291 jjdelcerro
    private JPanel panel;
209 42112 jjdelcerro
    private JDynFormSet formset;
210
    private FeatureStore store;
211 44488 jjdelcerro
    private FeaturePagingHelper features;
212 44128 jjdelcerro
    private DynFormDefinition definition = null;
213 42248 jjdelcerro
    private FeatureQuery currentQuery;
214 44291 jjdelcerro
    private List<Action> otherActions;
215 42714 dmartinezizquierdo
216 42775 jjdelcerro
    public DefaultJFeaturesForm() {
217 44291 jjdelcerro
        this.otherActions = new ArrayList<>();
218 42112 jjdelcerro
        this.panel = new JPanel(new BorderLayout());
219 44291 jjdelcerro
        this.panel.addComponentListener(new ComponentAdapter() {
220
            @Override
221
            public void componentHidden(ComponentEvent e) {
222
                doDispose();
223
            }
224
        });
225 42112 jjdelcerro
    }
226
227 44531 omartinez
public static void selfRegister() {
228
        String[][] iconNames = new String[][]{
229
            new String[]{"dalswing", "form-refresh-data"}
230
        };
231
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
232
        for (String[] icon : iconNames) {
233
            URL url = DefaultJFeaturesForm.class.getResource(icon[1] + ".png");
234
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
235
        }
236
237
    }
238 42489 jjdelcerro
    @Override
239 42112 jjdelcerro
    public void setPreferredSize(Dimension dimension) {
240 44253 jjdelcerro
//        panel.setPreferredSize(dimension);
241 42112 jjdelcerro
    }
242
243 42489 jjdelcerro
    private void updateForm() {
244 42473 fdiaz
        if (this.formset == null) {
245
            this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
246 42248 jjdelcerro
        }
247 42489 jjdelcerro
        try {
248 44291 jjdelcerro
            if( this.currentQuery!=null ) {
249
                this.currentQuery.retrievesAllAttributes();
250
            }
251 44488 jjdelcerro
            this.features = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE);
252
            this.formset.setValues(features.asListOfDynObjects());
253 42489 jjdelcerro
        } catch (Exception ex) {
254
            throw new RuntimeException("Can't update form", ex);
255
        }
256 44488 jjdelcerro
        updateButtonEnabledStatus();
257 42248 jjdelcerro
    }
258 42473 fdiaz
259 42489 jjdelcerro
    @Override
260 42112 jjdelcerro
    public JComponent asJComponent() {
261 44488 jjdelcerro
        if (this.features == null) {
262 42248 jjdelcerro
            try {
263
                updateForm();
264
            } catch (Exception ex) {
265
                throw new RuntimeException(ex);
266
            }
267 42473 fdiaz
        }
268 42112 jjdelcerro
        return this.panel;
269
    }
270
271 42489 jjdelcerro
    @Override
272
    public void bind(FeatureStore store) {
273
        if (store == null) {
274
            throw new IllegalArgumentException("bind need a store as parameter, not a null.");
275
        }
276
        try {
277 44202 jjdelcerro
            DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager();
278
            DynClass theDefinition = manager.featureType2DynClass(store, store.getDefaultFeatureType());
279
            this.bind(store, theDefinition);
280 42489 jjdelcerro
        } catch (Exception ex) {
281
            throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex);
282
        }
283 42112 jjdelcerro
    }
284 42473 fdiaz
285 42112 jjdelcerro
    public void bind(FeatureStore store, DynClass definition) throws ServiceException, DataException {
286
        if (this.store == store) {
287
            return;
288
        }
289 44128 jjdelcerro
        DynFormManager formManager = DynFormLocator.getDynFormManager();
290 44202 jjdelcerro
        this.definition = formManager.getDefinition(definition);
291 42112 jjdelcerro
        if (formset != null) {
292
            this.panel.remove(formset.asJComponent());
293
            this.formset = null;
294
        }
295
        this.store = store;
296 44291 jjdelcerro
        DisposeUtils.bind(store);
297 44488 jjdelcerro
        this.features = null;
298 44291 jjdelcerro
    }
299 44128 jjdelcerro
300 44291 jjdelcerro
    private void doDispose() {
301
        DisposeUtils.disposeQuietly(store);
302
        this.store = null;
303
        this.panel = null;
304
        this.formset = null;
305 44488 jjdelcerro
        this.features = null;
306 44291 jjdelcerro
        this.definition = null;
307
        this.currentQuery = null;
308
        this.otherActions = null;
309 42112 jjdelcerro
    }
310 44291 jjdelcerro
311 42489 jjdelcerro
    @Override
312
    public JDynFormSet getFormset() {
313 42473 fdiaz
        if (this.formset == null) {
314 42112 jjdelcerro
            DynFormManager formManager = DynFormLocator.getDynFormManager();
315 44253 jjdelcerro
            this.formset = formManager.createJDynFormSet(
316
                    new DefaultFeaturesFormContext(),
317
                    this.definition,
318
                    null
319
            );
320 44202 jjdelcerro
            List<String> groups = this.definition.getGroups();
321
            if( groups.size()==1 && groups.get(0)==null ) {
322
                this.formset.setLayoutMode(JDynForm.USE_PLAIN);
323
            } else {
324
                this.formset.setLayoutMode(JDynForm.USE_TABS);
325
            }
326
            this.formset.setAllowNew(true);
327
            this.formset.setAllowDelete(true);
328
            this.formset.setAllowUpdate(true);
329 42112 jjdelcerro
            this.formset.setAllowClose(true);
330 42489 jjdelcerro
            this.formset.setAllowSearch(true);
331 42112 jjdelcerro
            this.formset.setAutosave(true);
332 42714 dmartinezizquierdo
333 44202 jjdelcerro
            this.formset.addAction(new StartEditingAction());
334 42489 jjdelcerro
            this.formset.addAction(new FinishEditingAction());
335 44531 omartinez
            this.formset.addAction(new RefreshAction());
336 44281 jjdelcerro
337 44291 jjdelcerro
            FormActionContext actionContext = new FormActionContext();
338 44281 jjdelcerro
            Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
339
            for (DALActionFactory factory : factories) {
340
                if( StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME) ) {
341
                    continue;
342
                }
343
                Action action = factory.createAction(actionContext);
344
                this.formset.addAction(action);
345
            }
346 42489 jjdelcerro
            for( Action action : this.otherActions ) {
347
                this.formset.addAction(action);
348
            }
349 42714 dmartinezizquierdo
350 42489 jjdelcerro
            this.formset.addListener(new FormSetListener());
351 42112 jjdelcerro
        }
352 44488 jjdelcerro
        updateButtonEnabledStatus();
353 42112 jjdelcerro
        return this.formset;
354
    }
355
356 42489 jjdelcerro
    @Override
357
    public void addAction(Action action) {
358
        this.otherActions.add(action);
359
        if( this.formset!=null ) {
360
            this.formset.addAction(action);
361
        }
362
    }
363
364
    @Override
365
    public long getCurrentIndex() {
366
        if( this.formset==null ) {
367
            return -1;
368
        }
369
        return this.formset.getCurrentIndex();
370
    }
371
372
    @Override
373
    public Feature get(long index) {
374 44488 jjdelcerro
        if( this.formset==null || this.features==null ) {
375 42489 jjdelcerro
            return null;
376
        }
377
        try {
378 44488 jjdelcerro
            return this.features.getFeatureAt(index);
379 42489 jjdelcerro
        } catch (BaseException ex) {
380
            return null;
381
        }
382
    }
383 44531 omartinez
384
    private class RefreshAction extends AbstractAction {
385 42714 dmartinezizquierdo
386 44531 omartinez
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
387
        public RefreshAction() {
388
            I18nManager i18nManager = ToolsLocator.getI18nManager();
389
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
390
391
            this.putValue(NAME, null);
392
            this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Refresh"));
393
            this.putValue(SMALL_ICON, iconTheme.get("form-refresh-data"));
394
            this.putValue(ACTION_COMMAND_KEY, REFRESHFORM_ACTION);
395
            this.setEnabled(!formset.isInNewState());
396
        }
397
398
        @Override
399
        public void actionPerformed(ActionEvent ae) {
400
            try {
401
                I18nManager i18n = ToolsLocator.getI18nManager();
402
                formset.message(i18n.getTranslation("_Form_refreshed"));
403
                updateForm();
404
            } catch (Exception ex) {
405
                LOGGER.warn("Can't refresh form", ex);
406
            }
407
        }
408
    }
409
410
411 42489 jjdelcerro
    private class FinishEditingAction extends AbstractAction implements Observer {
412
413 44128 jjdelcerro
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
414 42489 jjdelcerro
        public FinishEditingAction() {
415 42714 dmartinezizquierdo
            I18nManager i18nManager = ToolsLocator.getI18nManager();
416 42489 jjdelcerro
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
417 42714 dmartinezizquierdo
418 42489 jjdelcerro
            this.putValue(NAME,null);
419
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing"));
420
            this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
421 44488 jjdelcerro
            this.putValue(ACTION_COMMAND_KEY, FINISHEDITING_ACTION);
422 42489 jjdelcerro
423
            this.setEnabled(store.isEditing());
424
            store.addObserver(this);
425
        }
426 42714 dmartinezizquierdo
427 42489 jjdelcerro
        @Override
428
        public void actionPerformed(ActionEvent ae) {
429
            if( store.isEditing() ) {
430
                try {
431 42714 dmartinezizquierdo
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
432 42489 jjdelcerro
                    ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
433
                    int x = dialogManager.confirmDialog(
434 44590 jjdelcerro
                            i18nManager.getTranslation("_Do_you_want_to_finish_editing_and_save_the_changes")+"\n\n"+
435
                                    i18nManager.getTranslation("_Press_cancel_to_cancel_editing_and_changes"),
436 42489 jjdelcerro
                            i18nManager.getTranslation("_Stop_editing"),
437
                            JOptionPane.YES_NO_CANCEL_OPTION,
438
                            JOptionPane.QUESTION_MESSAGE
439
                    );
440
                    switch(x) {
441
                        case JOptionPane.YES_OPTION:
442
                            store.finishEditing();
443
                            break;
444
                        case JOptionPane.NO_OPTION:
445
                            break;
446
                        case JOptionPane.CANCEL_OPTION:
447
                            store.cancelEditing();
448
                            break;
449
                    }
450
                } catch (DataException ex) {
451
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
452
                }
453
            }
454 44488 jjdelcerro
            updateButtonEnabledStatus();
455 42489 jjdelcerro
        }
456 44531 omartinez
457 42489 jjdelcerro
        @Override
458
        public void update(Observable observable, Object notification) {
459 44346 jjdelcerro
            if( store == null || formset==null ) {
460
                return;
461
            }
462 42489 jjdelcerro
            if( notification instanceof FeatureStoreNotification ) {
463
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
464
                switch( n.getType() )  {
465
                    case FeatureStoreNotification.AFTER_STARTEDITING:
466 44202 jjdelcerro
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
467
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
468 44488 jjdelcerro
                        updateButtonEnabledStatus();
469 42489 jjdelcerro
                        break;
470 44202 jjdelcerro
                }
471
            }
472
        }
473
474
    }
475 44488 jjdelcerro
476 44202 jjdelcerro
    private class StartEditingAction extends AbstractAction implements Observer {
477
478
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
479
        public StartEditingAction() {
480
            I18nManager i18nManager = ToolsLocator.getI18nManager();
481
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
482
483
            this.putValue(NAME,null);
484
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Start_editing"));
485
            this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
486 44488 jjdelcerro
            this.putValue(ACTION_COMMAND_KEY, STARTEDITING_ACTION);
487 44202 jjdelcerro
488
            this.setEnabled(!store.isEditing());
489
            store.addObserver(this);
490
        }
491
492
        @Override
493
        public void actionPerformed(ActionEvent ae) {
494
            if( !store.isEditing() ) {
495
                try {
496
                    store.edit();
497
                } catch (DataException ex) {
498
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
499
                }
500
            }
501 44488 jjdelcerro
            updateButtonEnabledStatus();
502 44202 jjdelcerro
        }
503
504
        @Override
505
        public void update(Observable observable, Object notification) {
506 44346 jjdelcerro
            if( store == null || formset==null ) {
507
                return;
508
            }
509 44202 jjdelcerro
            if( notification instanceof FeatureStoreNotification ) {
510
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
511
                switch( n.getType() )  {
512
                    case FeatureStoreNotification.AFTER_STARTEDITING:
513 42489 jjdelcerro
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
514
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
515 44488 jjdelcerro
                        updateButtonEnabledStatus();
516 42489 jjdelcerro
                        break;
517
                }
518
            }
519
        }
520 42714 dmartinezizquierdo
521 42489 jjdelcerro
    }
522
523
    @Override
524
    public void setQuery(FeatureQuery query) {
525 44488 jjdelcerro
        if (this.features != null) {
526 44239 jjdelcerro
            if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) {
527 42112 jjdelcerro
                if (!store.isEditing()) {
528
                    try {
529
                        store.edit();
530
                    } catch (DataException e1) {
531 42489 jjdelcerro
                        throw new RuntimeException("Can't set query", e1);
532 42112 jjdelcerro
                    }
533
                }
534
                saveChanges(this.formset);
535
            }
536
        }
537 42248 jjdelcerro
        this.currentQuery = query;
538
        updateForm();
539 42112 jjdelcerro
    }
540
541 44239 jjdelcerro
    private FeatureQuery getCurrentQuery() {
542
        return this.currentQuery;
543
    }
544
545 42489 jjdelcerro
    @Override
546
    public void showForm(MODE mode) {
547 42248 jjdelcerro
        this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
548
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
549 44202 jjdelcerro
        String title = this.definition.getLabel();
550 42775 jjdelcerro
        winmgr.showWindow(this.asJComponent(), title, mode);
551 42112 jjdelcerro
    }
552
553 44488 jjdelcerro
    private void saveChanges(JDynFormSet theFormSet) {
554 44346 jjdelcerro
        I18nManager i18n = ToolsLocator.getI18nManager();
555 44488 jjdelcerro
        try {
556
            if( theFormSet.isInNewState() ) {
557
                Feature newFeature = store.createNewFeature(false);
558
                DynObject newElement = newFeature.getAsDynObject();
559
                theFormSet.getFormValues(newElement);
560
                features.insert(((FacadeOfAFeature)newElement).getEditableFeature());
561
                this.formset.message(i18n.getTranslation("_Record_saved"));
562
                try {
563
                    this.formset.setValues(features.asListOfDynObjects());
564
                    this.formset.setCurrentIndex((int)(features.getTotalSize())-1);
565
                } catch(Exception ex) {
566
                    LOGGER.warn("Can't reload form data after insert.",ex);
567
                }
568
            } else {
569
                int index = theFormSet.getCurrentIndex();
570
                DynObject currentElement = theFormSet.get(index);
571
                theFormSet.getFormValues(currentElement);
572
                features.update(((FacadeOfAFeature)currentElement).getEditableFeature());
573
                this.formset.message(i18n.getTranslation("_Record_saved"));
574 42775 jjdelcerro
            }
575 44488 jjdelcerro
        } catch(Exception ex) {
576
            theFormSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
577
            throw new RuntimeException("Can't save values",ex);
578
579
        } finally {
580
            updateButtonEnabledStatus();
581 42489 jjdelcerro
        }
582 42112 jjdelcerro
583
    }
584 44510 omartinez
    private void deleteCurrentElement(JDynFormSet theFormSet) {
585
        I18nManager i18n = ToolsLocator.getI18nManager();
586
        try {
587
            int index = theFormSet.getCurrentIndex();
588
            DynObject currentElement = theFormSet.get(index);
589
            theFormSet.getFormValues(currentElement);
590
            Feature feature = ((FacadeOfAFeature) currentElement).getFeature();
591
            if (feature instanceof EditableFeature) {
592
                feature = ((EditableFeature) feature).getNotEditableCopy();
593
            }
594
            features.delete(feature);
595 44531 omartinez
596 44510 omartinez
            this.formset.message(i18n.getTranslation("_Record_removed"));
597 44531 omartinez
598
            this.formset.setValues(features.asListOfDynObjects());
599
            if (features.getTotalSize()-1<index) {
600
                index = index-1;
601
            }
602
            this.formset.setCurrentIndex(index);
603 42112 jjdelcerro
604 44510 omartinez
        } catch (Exception ex) {
605
            theFormSet.message(i18n.getTranslation("error_removing_data_will_not_remove"));
606
            throw new RuntimeException("Can't remove values", ex);
607
608
        } finally {
609
            updateButtonEnabledStatus();
610
        }
611
612
    }
613 42489 jjdelcerro
    @Override
614
    public void saveChanges() {
615
        if (this.formset != null && this.formset.countValues() > 0) {
616
            if (store != null && !store.isEditing()) {
617
                try {
618
                    store.edit();
619
                } catch (DataException e1) {
620
                    LOGGER.warn("Can't edit the store " + store.getName());
621
                    throw new RuntimeException("Can't save changes.", e1);
622
                }
623 42112 jjdelcerro
            }
624 42489 jjdelcerro
            this.saveChanges(this.formset);
625 42112 jjdelcerro
        }
626
    }
627
628 44488 jjdelcerro
    private void updateButtonEnabledStatus() {
629
        if( this.formset == null ) {
630
            return;
631
        }
632
        if( this.store == null || store.isBroken() || store.isAppending() ) {
633
            this.formset.setReadOnly(true);
634
            formset.setActionVisible(STARTEDITING_ACTION, true);
635
            formset.setActionEnabled(STARTEDITING_ACTION, true);
636
            formset.setActionVisible(FINISHEDITING_ACTION, false);
637
            formset.setActionEnabled(FINISHEDITING_ACTION, false);
638
            formset.setActionEnabled(ACTION_DELETE, false);
639
            formset.setActionEnabled(ACTION_NEW,false);
640
            formset.setActionEnabled(ACTION_CANCEL_NEW,false);
641
            formset.setActionEnabled(ACTION_SAVE,false);
642
            formset.setActionEnabled(ACTION_SEARCH,false);
643 44531 omartinez
            formset.setActionEnabled(REFRESHFORM_ACTION, true);
644 44488 jjdelcerro
            return;
645
        }
646
        if( store.isEditing() ) {
647
            this.formset.setReadOnly(false);
648
            formset.setActionVisible(STARTEDITING_ACTION, false);
649
            formset.setActionEnabled(STARTEDITING_ACTION, false);
650
            formset.setActionVisible(FINISHEDITING_ACTION, true);
651
            formset.setActionEnabled(FINISHEDITING_ACTION, true);
652
            if( formset.isInNewState() ) {
653
                formset.setActionEnabled(ACTION_DELETE, false);
654
                formset.setActionEnabled(ACTION_SEARCH,false);
655
                formset.setActionEnabled(ACTION_NEW,false);
656
                formset.setActionEnabled(ACTION_CANCEL_NEW,true);
657
                formset.setActionEnabled(ACTION_SAVE,true);
658 44531 omartinez
                formset.setActionEnabled(REFRESHFORM_ACTION, false);
659 44488 jjdelcerro
660 44510 omartinez
            } else if( this.features!=null && this.features.isEmpty() ) {
661 44488 jjdelcerro
                formset.setActionEnabled(ACTION_DELETE, false);
662
                formset.setActionEnabled(ACTION_SEARCH,false);
663
                formset.setActionEnabled(ACTION_NEW,true);
664
                formset.setActionEnabled(ACTION_CANCEL_NEW,true);
665
                formset.setActionEnabled(ACTION_SAVE,false);
666 44531 omartinez
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
667 44488 jjdelcerro
668
            } else if( formset.getForm().isModified() ) {
669
                formset.setActionEnabled(ACTION_DELETE, true);
670
                formset.setActionEnabled(ACTION_SEARCH,true);
671
                formset.setActionEnabled(ACTION_NEW,true);
672
                formset.setActionEnabled(ACTION_CANCEL_NEW,false);
673
                formset.setActionEnabled(ACTION_SAVE,true);
674 44531 omartinez
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
675 44488 jjdelcerro
676
            } else {
677
                formset.setActionEnabled(ACTION_DELETE, true);
678
                formset.setActionEnabled(ACTION_SEARCH,true);
679
                formset.setActionEnabled(ACTION_NEW,true);
680
                formset.setActionEnabled(ACTION_CANCEL_NEW,false);
681
                formset.setActionEnabled(ACTION_SAVE,true); //false);
682 44531 omartinez
                formset.setActionEnabled(REFRESHFORM_ACTION, true);
683 44488 jjdelcerro
            }
684
685
            return;
686
        }
687
        this.formset.setReadOnly(true);
688
        formset.setActionVisible(STARTEDITING_ACTION, true);
689
        formset.setActionEnabled(STARTEDITING_ACTION, true);
690
        formset.setActionVisible(FINISHEDITING_ACTION, false);
691
        formset.setActionEnabled(FINISHEDITING_ACTION, false);
692
        formset.setActionEnabled(ACTION_DELETE, false);
693
        formset.setActionEnabled(ACTION_NEW,false);
694
        formset.setActionEnabled(ACTION_CANCEL_NEW,false);
695
        formset.setActionEnabled(ACTION_SAVE,false);
696
        formset.setActionEnabled(ACTION_SEARCH,true);
697 44531 omartinez
        formset.setActionEnabled(REFRESHFORM_ACTION,true);
698 44488 jjdelcerro
    }
699
700 42489 jjdelcerro
    @Override
701
    public long getDataSetSize() {
702 44488 jjdelcerro
        if (this.features != null) {
703
            return features.getTotalSize();
704 42489 jjdelcerro
        }
705
        return 0;
706 42112 jjdelcerro
    }
707
708 42489 jjdelcerro
    @Override
709
    public FeatureStore getFeatureStore() {
710
        return this.store;
711 42112 jjdelcerro
    }
712
713 42489 jjdelcerro
    private class FormSetListener implements JDynFormSetListener {
714 42112 jjdelcerro
715 42489 jjdelcerro
        @Override
716
        public void formMessage(String message) {
717
        }
718 42112 jjdelcerro
719 42489 jjdelcerro
        @Override
720
        public void formClose() {
721
            panel.setVisible(false);
722 42112 jjdelcerro
        }
723
724 42489 jjdelcerro
        @Override
725
        public void formMovedTo(int currentPosition) throws AbortActionException {
726
            LOGGER.trace("formMovedTo " + currentPosition);
727 44488 jjdelcerro
            updateButtonEnabledStatus();
728 42489 jjdelcerro
        }
729 42112 jjdelcerro
730 42489 jjdelcerro
        @Override
731
        public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException {
732
            LOGGER.trace("formBeforeSave");
733 44488 jjdelcerro
            updateButtonEnabledStatus();
734 42489 jjdelcerro
        }
735 42112 jjdelcerro
736 44259 jjdelcerro
        @Override
737 42489 jjdelcerro
        public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException {
738
            LOGGER.trace("formBeforeNew");
739 44488 jjdelcerro
            updateButtonEnabledStatus();
740 42112 jjdelcerro
        }
741
742 44259 jjdelcerro
        @Override
743 42489 jjdelcerro
        public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException {
744
            LOGGER.trace("formBeforeDelete");
745 44488 jjdelcerro
            updateButtonEnabledStatus();
746 42489 jjdelcerro
        }
747 42112 jjdelcerro
748 42489 jjdelcerro
        @Override
749
        public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException {
750
            LOGGER.trace("formAfterSave");
751
            saveChanges(dynformSet);
752 44488 jjdelcerro
            updateButtonEnabledStatus();
753 42489 jjdelcerro
        }
754 42112 jjdelcerro
755 42489 jjdelcerro
        @Override
756
        public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException {
757
            LOGGER.trace("formAfterNew");
758 44488 jjdelcerro
            updateButtonEnabledStatus();
759 42112 jjdelcerro
        }
760
761 42489 jjdelcerro
        @Override
762
        public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
763
            LOGGER.trace("formAfterDelete");
764 44510 omartinez
            deleteCurrentElement(dynformSet);
765 44488 jjdelcerro
            updateButtonEnabledStatus();
766 42112 jjdelcerro
        }
767
768 44292 jjdelcerro
//        @Override
769
//        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
770
//            LOGGER.trace("formBeforeSearch");
771
//            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
772
//            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
773
//
774
//            JExpressionBuilder builder = dataSwingmanager.createQueryFilterExpresion(store);
775
//            FeatureQuery currentQuery = getCurrentQuery();
776
//            if( currentQuery!=null ) {
777
//                Evaluator filter = currentQuery.getFilter();
778
//                if( filter instanceof ExpressionEvaluator ) {
779
//                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
780
//                    builder.setExpression(expression);
781
//                }
782
//            }
783
//            Dialog dialog = winmgr.createDialog(
784
//                    builder.asJComponent(),
785
//                    "Filtro",
786
//                    "Creacion de filtro sobre '"+store.getName()+"'",
787
//                    WindowManager_v2.BUTTONS_OK_CANCEL
788
//            );
789
//            dialog.show(WindowManager.MODE.DIALOG);
790
//            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
791
//                Expression expresion = builder.getExpression();
792
//                try {
793
//                    FeatureQuery query = store.createFeatureQuery();
794
//                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
795
//                        query.clearFilter();
796
//                    } else {
797
//                        query.setFilter(expresion);
798
//                    }
799
//                    setQuery(query);
800
//                } catch (Exception ex) {
801
//                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
802
//                }
803
//            }
804
//        }
805
806 42489 jjdelcerro
        @Override
807
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
808
            LOGGER.trace("formBeforeSearch");
809
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
810
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
811 42714 dmartinezizquierdo
812 44292 jjdelcerro
            final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
813
            searchPanel.setShowActions(false);
814 44239 jjdelcerro
            FeatureQuery currentQuery = getCurrentQuery();
815
            if( currentQuery!=null ) {
816
                Evaluator filter = currentQuery.getFilter();
817
                if( filter instanceof ExpressionEvaluator ) {
818
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
819 44292 jjdelcerro
                    searchPanel.setFilter(expression);
820 44239 jjdelcerro
                }
821
            }
822 44292 jjdelcerro
            searchPanel.asJComponent().setPreferredSize(
823
                    new Dimension(FeatureStoreSearchPanel.DEFAULT_WIDTH, 320)
824
            );
825 44590 jjdelcerro
            I18nManager i18n = ToolsLocator.getI18nManager();
826 42489 jjdelcerro
            Dialog dialog = winmgr.createDialog(
827 44292 jjdelcerro
                    searchPanel.asJComponent(),
828 44590 jjdelcerro
                    i18n.getTranslation("_Filter"),
829
                    i18n.getTranslation("_Creating_filter_for") + ": '"+store.getName()+"'",
830 42489 jjdelcerro
                    WindowManager_v2.BUTTONS_OK_CANCEL
831
            );
832
            dialog.show(WindowManager.MODE.DIALOG);
833
            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
834 44292 jjdelcerro
                Expression expresion = searchPanel.getFilter();
835 42112 jjdelcerro
                try {
836 42489 jjdelcerro
                    FeatureQuery query = store.createFeatureQuery();
837 44239 jjdelcerro
                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
838
                        query.clearFilter();
839
                    } else {
840 44128 jjdelcerro
                        query.setFilter(expresion);
841 42489 jjdelcerro
                    }
842
                    setQuery(query);
843
                } catch (Exception ex) {
844
                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
845 42112 jjdelcerro
                }
846
            }
847
        }
848
849 42489 jjdelcerro
        @Override
850
        public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException {
851
            LOGGER.trace("formAfterSearch");
852
        }
853 42775 jjdelcerro
854
        @Override
855
        public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException {
856
            LOGGER.trace("formBeforeCancelNew");
857 44488 jjdelcerro
            updateButtonEnabledStatus();
858 42775 jjdelcerro
        }
859
860
        @Override
861
        public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException {
862
            LOGGER.trace("formAfterCancelNew");
863 44488 jjdelcerro
            updateButtonEnabledStatus();
864 42775 jjdelcerro
        }
865 42112 jjdelcerro
    }
866
867 44488 jjdelcerro
//    private static class StoreEditException extends AbortActionException {
868
//
869
//        /**
870
//         *
871
//         */
872
//        private static final long serialVersionUID = -7682017811778577130L;
873
//
874
//        public StoreEditException(Throwable cause, String storename) {
875
//            super("Can't edit the store '%(storename)'", cause, "cant_edit_the store_XstorenameX", serialVersionUID);
876
//            setValue("storename", storename);
877
//        }
878
//    }
879 42112 jjdelcerro
}