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

History | View | Annotate | Download (27.5 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.featureform.swing.impl;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.Dimension;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ComponentAdapter;
29
import java.awt.event.ComponentEvent;
30
import java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.List;
33
import javax.swing.AbstractAction;
34
import javax.swing.Action;
35
import static javax.swing.Action.ACTION_COMMAND_KEY;
36
import static javax.swing.Action.NAME;
37
import static javax.swing.Action.SHORT_DESCRIPTION;
38
import static javax.swing.Action.SMALL_ICON;
39

    
40
import javax.swing.JComponent;
41
import javax.swing.JOptionPane;
42
import javax.swing.JPanel;
43
import org.apache.commons.lang3.StringUtils;
44
import org.gvsig.expressionevaluator.Expression;
45
import org.gvsig.expressionevaluator.ExpressionUtils;
46
import org.gvsig.expressionevaluator.swing.JExpressionBuilder;
47

    
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
import org.gvsig.featureform.swing.JFeaturesForm;
52
import org.gvsig.fmap.dal.DALLocator;
53
import org.gvsig.fmap.dal.DataStore;
54
import org.gvsig.fmap.dal.StoresRepository;
55
import org.gvsig.fmap.dal.exception.DataException;
56
import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator;
57
import org.gvsig.fmap.dal.feature.Feature;
58
import org.gvsig.fmap.dal.feature.FacadeOfAFeature;
59
import org.gvsig.fmap.dal.feature.FeatureQuery;
60
import org.gvsig.fmap.dal.feature.FeatureStore;
61
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
62
import org.gvsig.fmap.dal.feature.FeatureType;
63
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
64
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext;
65
import org.gvsig.fmap.dal.swing.DALActionFactory;
66
import org.gvsig.fmap.dal.swing.DALSwingLocator;
67
import org.gvsig.fmap.dal.swing.DataSwingManager;
68
import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager;
69
import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory;
70
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel;
71
import org.gvsig.tools.ToolsLocator;
72
import org.gvsig.tools.dispose.DisposeUtils;
73
import org.gvsig.tools.dynform.AbortActionException;
74
import org.gvsig.tools.dynform.DynFormDefinition;
75
import org.gvsig.tools.dynform.DynFormLocator;
76
import org.gvsig.tools.dynform.DynFormManager;
77
import org.gvsig.tools.dynform.JDynForm;
78
import org.gvsig.tools.dynform.JDynFormSet;
79
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener;
80
import org.gvsig.tools.dynobject.DynClass;
81
import org.gvsig.tools.dynobject.DynObject;
82
import org.gvsig.tools.evaluator.Evaluator;
83
import org.gvsig.tools.exception.BaseException;
84
import org.gvsig.tools.i18n.I18nManager;
85
import org.gvsig.tools.observer.Observable;
86
import org.gvsig.tools.observer.Observer;
87
import org.gvsig.tools.service.ServiceException;
88
import org.gvsig.tools.swing.api.ToolsSwingLocator;
89
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
90
import org.gvsig.tools.swing.api.windowmanager.Dialog;
91
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
92
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
93
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
94
import org.gvsig.tools.swing.icontheme.IconTheme;
95
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
96

    
97
/**
98
 * @author fdiaz
99
 *
100
 */
101
@SuppressWarnings("UseSpecificCatch")
102
public class DefaultJFeaturesForm implements JFeaturesForm {
103

    
104
    public class DefaultFeaturesFormContext implements FeaturesFormContext {
105

    
106
        private DefaultFeaturesFormContext() {
107
            
108
        }
109

    
110
        public FeatureStore getFeatureStore() {
111
            return store;
112
        }
113
        
114
        @Override
115
        public FeatureType getFeatureType() {
116
            try {
117
                return store.getDefaultFeatureType();
118
            } catch (DataException ex) {
119
                return null;
120
            }
121
        }
122
        
123
        @Override
124
        public ResourcesStorage getResourcesStorage() {
125
            return store.getResourcesStorage();
126
        }
127

    
128
        @Override
129
        public StoresRepository getStoresRepository() {
130
            return store.getStoresRepository();
131
        }
132
        
133
    }
134
    
135
    private class FormActionContext extends AbstractDALActionContext {
136

    
137
        public FormActionContext() {
138
            super(ACTION_CONTEXT_NAME);
139
        }
140
        
141
        @Override
142
        public DataStore getStore() {
143
            return store;
144
        }
145

    
146
        @Override
147
        public Expression getFilter() {
148
            FeatureQuery theCurrentQuery = currentQuery;
149
            if( theCurrentQuery!=null ) {
150
                Evaluator filter = theCurrentQuery.getFilter();
151
                if( filter instanceof ExpressionEvaluator ) {
152
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
153
                    return expression;
154
                }
155
            }
156
            return null;
157
        }
158

    
159
        @Override
160
        public int getSelectedsCount() {
161
            return 0;
162
        }
163

    
164
        @Override
165
        public Expression getFilterForSelecteds() {
166
            return null;
167
        }
168

    
169
        @Override
170
        public JComponent getActionButton(String actionName) {
171
            if( formset == null ) {
172
                return null;
173
            }
174
            return formset.getActionButton(actionName);
175
        }
176
        
177
    }
178
    
179
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class);
180

    
181
    private static final int PAGE_SIZE = 500;
182
    private JPanel panel;
183
    private JDynFormSet formset;
184
    private FeatureStore store;
185
    private FeaturePagingHelper ph;
186
    private DynFormDefinition definition = null;
187
    private FeatureQuery currentQuery;
188
    private List<Action> otherActions;
189

    
190
    public DefaultJFeaturesForm() {
191
        this.otherActions = new ArrayList<>();
192
        this.panel = new JPanel(new BorderLayout());
193
        this.panel.addComponentListener(new ComponentAdapter() {
194
            @Override
195
            public void componentHidden(ComponentEvent e) {
196
                doDispose();
197
            }
198
        });
199
    }
200

    
201
    @Override
202
    public void setPreferredSize(Dimension dimension) {
203
//        panel.setPreferredSize(dimension);
204
    }
205

    
206
    private void updateForm() {
207
        if (this.formset == null) {
208
            this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
209
        }
210
        try {
211
            if( this.currentQuery!=null ) {
212
                this.currentQuery.retrievesAllAttributes();
213
            }
214
            this.ph = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE);
215
            this.formset.setValues(ph.asListOfDynObjects());
216
        } catch (Exception ex) {
217
            throw new RuntimeException("Can't update form", ex);
218
        }
219
    }
220

    
221
    @Override
222
    public JComponent asJComponent() {
223
        if (this.ph == null) {
224
            try {
225
                updateForm();
226
            } catch (Exception ex) {
227
                throw new RuntimeException(ex);
228
            }
229
        }
230
        return this.panel;
231
    }
232

    
233
    @Override
234
    public void bind(FeatureStore store) {
235
        if (store == null) {
236
            throw new IllegalArgumentException("bind need a store as parameter, not a null.");
237
        }
238
        try {
239
            DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager();
240
            DynClass theDefinition = manager.featureType2DynClass(store, store.getDefaultFeatureType());
241
            this.bind(store, theDefinition);
242
        } catch (Exception ex) {
243
            throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex);
244
        }
245
    }
246

    
247
    public void bind(FeatureStore store, DynClass definition) throws ServiceException, DataException {
248
        if (this.store == store) {
249
            return;
250
        }
251
        DynFormManager formManager = DynFormLocator.getDynFormManager();
252
        this.definition = formManager.getDefinition(definition);
253
        if (formset != null) {
254
            this.panel.remove(formset.asJComponent());
255
            this.formset = null;
256
        }
257
        this.store = store;
258
        DisposeUtils.bind(store);
259
        this.ph = null;
260
    }
261

    
262
    private void doDispose() {
263
        DisposeUtils.disposeQuietly(store);
264
        this.store = null;
265
        this.panel = null;
266
        this.formset = null;
267
        this.ph = null;
268
        this.definition = null;
269
        this.currentQuery = null;
270
        this.otherActions = null;
271
    }
272
    
273
    @Override
274
    public JDynFormSet getFormset() {
275
        if (this.formset == null) {
276
            DynFormManager formManager = DynFormLocator.getDynFormManager();
277
            this.formset = formManager.createJDynFormSet(
278
                    new DefaultFeaturesFormContext(),
279
                    this.definition,
280
                    null
281
            );
282
            List<String> groups = this.definition.getGroups();
283
            if( groups.size()==1 && groups.get(0)==null ) {
284
                this.formset.setLayoutMode(JDynForm.USE_PLAIN);
285
            } else {
286
                this.formset.setLayoutMode(JDynForm.USE_TABS);
287
            }
288
            this.formset.setAllowNew(true);
289
            this.formset.setAllowDelete(true);
290
            this.formset.setAllowUpdate(true);
291
            this.formset.setAllowClose(true);
292
            this.formset.setAllowSearch(true);
293
            this.formset.setAutosave(true);
294

    
295
            this.formset.addAction(new StartEditingAction());
296
            this.formset.addAction(new FinishEditingAction());
297
            
298
            FormActionContext actionContext = new FormActionContext();
299
            Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
300
            for (DALActionFactory factory : factories) {
301
                if( StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME) ) {
302
                    continue;
303
                }
304
                Action action = factory.createAction(actionContext);
305
                this.formset.addAction(action);
306
            }            
307
            for( Action action : this.otherActions ) {
308
                this.formset.addAction(action);
309
            }
310

    
311
            this.formset.addListener(new FormSetListener());
312
        }
313
        if( this.store.isEditing() ) {
314
//            this.formset.setReadOnly(false);
315
            formset.setActionVisible("startEditing", false);
316
            formset.setActionEnabled("startEditing", false);
317
            formset.setActionVisible("finishEditing", true);
318
            formset.setActionEnabled("finishEditing", true);
319
        } else {
320
//            this.formset.setReadOnly(true);
321
            formset.setActionVisible("startEditing", true);
322
            formset.setActionEnabled("startEditing", true);
323
            formset.setActionVisible("finishEditing", false);
324
            formset.setActionEnabled("finishEditing", false);
325
        }
326
        return this.formset;
327
    }
328

    
329
    @Override
330
    public void addAction(Action action) {
331
        this.otherActions.add(action);
332
        if( this.formset!=null ) {
333
            this.formset.addAction(action);
334
        }
335
    }
336

    
337
    @Override
338
    public long getCurrentIndex() {
339
        if( this.formset==null ) {
340
            return -1;
341
        }
342
        return this.formset.getCurrentIndex();
343
    }
344

    
345
    @Override
346
    public Feature get(long index) {
347
        if( this.formset==null || this.ph==null ) {
348
            return null;
349
        }
350
        try {
351
            return this.ph.getFeatureAt(index);
352
        } catch (BaseException ex) {
353
            return null;
354
        }
355
    }
356

    
357
    private class FinishEditingAction extends AbstractAction implements Observer {
358

    
359
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
360
        public FinishEditingAction() {
361
            I18nManager i18nManager = ToolsLocator.getI18nManager();
362
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
363

    
364
            this.putValue(NAME,null);
365
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing"));
366
            this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing"));
367
            this.putValue(ACTION_COMMAND_KEY, "finishEditing");
368

    
369
            this.setEnabled(store.isEditing());
370
            store.addObserver(this);
371
        }
372

    
373
        @Override
374
        public void actionPerformed(ActionEvent ae) {
375
            if( store.isEditing() ) {
376
                try {
377
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
378
                    ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
379
                    int x = dialogManager.confirmDialog(
380
                            "? Desea terminar edicion y guardar los cambios ?\n\nPulse cancelar para cancelar la edicion y los cambios.",
381
                            i18nManager.getTranslation("_Stop_editing"),
382
                            JOptionPane.YES_NO_CANCEL_OPTION,
383
                            JOptionPane.QUESTION_MESSAGE
384
                    );
385
                    switch(x) {
386
                        case JOptionPane.YES_OPTION:
387
                            store.finishEditing();
388
                            break;
389
                        case JOptionPane.NO_OPTION:
390
                            break;
391
                        case JOptionPane.CANCEL_OPTION:
392
                            store.cancelEditing();
393
                            break;
394
                    }
395
                } catch (DataException ex) {
396
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
397
                }
398
            }
399
        }
400

    
401
        @Override
402
        public void update(Observable observable, Object notification) {
403
            if( notification instanceof FeatureStoreNotification ) {
404
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
405
                switch( n.getType() )  {
406
                    case FeatureStoreNotification.AFTER_STARTEDITING:
407
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
408
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
409
                        if( store.isEditing() ) {
410
                            formset.setReadOnly(false);
411
                            formset.setActionVisible("finishEditing", true);
412
                            formset.setActionEnabled("finishEditing", true);
413
                        } else {
414
                            formset.setReadOnly(true);
415
                            formset.setActionVisible("finishEditing", false);
416
                            formset.setActionEnabled("finishEditing", false);
417
                        }
418
                        break;
419
                }
420
            }
421
        }
422

    
423
    }
424

    
425
    private class StartEditingAction extends AbstractAction implements Observer {
426

    
427
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
428
        public StartEditingAction() {
429
            I18nManager i18nManager = ToolsLocator.getI18nManager();
430
            IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault();
431

    
432
            this.putValue(NAME,null);
433
            this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Start_editing"));
434
            this.putValue(SMALL_ICON, iconTheme.get("table-start-editing"));
435
            this.putValue(ACTION_COMMAND_KEY, "startEditing");
436

    
437
            this.setEnabled(!store.isEditing());
438
            store.addObserver(this);
439
        }
440

    
441
        @Override
442
        public void actionPerformed(ActionEvent ae) {
443
            if( !store.isEditing() ) {
444
                try {
445
                    store.edit();
446
                } catch (DataException ex) {
447
                    LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex);
448
                }
449
            }
450
        }
451

    
452
        @Override
453
        public void update(Observable observable, Object notification) {
454
            if( notification instanceof FeatureStoreNotification ) {
455
                FeatureStoreNotification n =  (FeatureStoreNotification) notification;
456
                switch( n.getType() )  {
457
                    case FeatureStoreNotification.AFTER_STARTEDITING:
458
                    case FeatureStoreNotification.AFTER_FINISHEDITING:
459
                    case FeatureStoreNotification.AFTER_CANCELEDITING:
460
                        if( store.isEditing() ) {
461
                            formset.setReadOnly(false);
462
                            formset.setActionVisible("startEditing", false);
463
                            formset.setActionEnabled("startEditing", false);
464
                        } else {
465
                            formset.setReadOnly(true);
466
                            formset.setActionVisible("startEditing", true);
467
                            formset.setActionEnabled("startEditing", true);
468
                        }
469
                        break;
470
                }
471
            }
472
        }
473

    
474
    }
475

    
476
    @Override
477
    public void setQuery(FeatureQuery query) {
478
        if (this.ph != null) {
479
            if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) {
480
                if (!store.isEditing()) {
481
                    try {
482
                        store.edit();
483
                    } catch (DataException e1) {
484
                        throw new RuntimeException("Can't set query", e1);
485
                    }
486
                }
487
                saveChanges(this.formset);
488
            }
489
        }
490
        this.currentQuery = query;
491
        updateForm();
492
    }
493

    
494
    private FeatureQuery getCurrentQuery() {
495
        return this.currentQuery;
496
    }
497
    
498
    @Override
499
    public void showForm(MODE mode) {
500
        this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER);
501
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
502
        String title = this.definition.getLabel();
503
        winmgr.showWindow(this.asJComponent(), title, mode);
504
    }
505

    
506
    private void saveChanges(JDynFormSet dynformSet) {
507
        if( dynformSet.isInNewState() ) {
508
            Feature feat = null;
509
            try {
510
                feat = store.createNewFeature(false);
511
            } catch (DataException ex) {
512
                LOGGER.warn("Can't create new feature.",ex);
513
                I18nManager i18nManager = ToolsLocator.getI18nManager();
514
                dynformSet.message(i18nManager.getTranslation("error_saving_data_will_not_save"));
515
                throw new RuntimeException("Can't new save values");
516
            }
517
            DynObject dynObject = feat.getAsDynObject();
518
            dynformSet.getFormValues(dynObject);
519
            try {
520
                ph.insert(((FacadeOfAFeature)dynObject).getEditableFeature());
521
            } catch (BaseException e) {
522
                throw new RuntimeException("Can't save values", e);
523
            }
524
            try {
525
                this.formset.setValues(ph.asListOfDynObjects());
526
                this.formset.setCurrentIndex((int)(ph.getTotalSize())-1);
527
            } catch(Exception ex) {
528
                LOGGER.warn("Can't reload form data after insert.",ex);
529
            }
530
        } else {
531
            int index = dynformSet.getCurrentIndex();
532
            DynObject dynObject = dynformSet.get(index);
533

    
534
            if ( !(dynObject instanceof FacadeOfAFeature) ) {
535
                LOGGER.warn("Can't get the associated feature index " + index);
536
                I18nManager i18nManager = ToolsLocator.getI18nManager();
537
                dynformSet.message(i18nManager.getTranslation("error_saving_data_will_not_save"));
538
                throw new RuntimeException("Can't save values");
539
            }
540
            dynformSet.getFormValues(dynObject);
541
            try {
542
                ph.update(((FacadeOfAFeature)dynObject).getEditableFeature());
543
            } catch (BaseException e) {
544
                throw new RuntimeException("Can't save values", e);
545
            }
546
        }
547

    
548
    }
549

    
550
    @Override
551
    public void saveChanges() {
552
        if (this.formset != null && this.formset.countValues() > 0) {
553
            if (store != null && !store.isEditing()) {
554
                try {
555
                    store.edit();
556
                } catch (DataException e1) {
557
                    LOGGER.warn("Can't edit the store " + store.getName());
558
                    throw new RuntimeException("Can't save changes.", e1);
559
                }
560
            }
561
            this.saveChanges(this.formset);
562
        }
563
    }
564

    
565
    @Override
566
    public long getDataSetSize() {
567
        if (this.ph != null) {
568
            return ph.getTotalSize();
569
        }
570
        return 0;
571
    }
572

    
573
    @Override
574
    public FeatureStore getFeatureStore() {
575
        return this.store;
576
    }
577

    
578
    private class FormSetListener implements JDynFormSetListener {
579

    
580
        @Override
581
        public void formMessage(String message) {
582
        }
583

    
584
        @Override
585
        public void formClose() {
586
            panel.setVisible(false);
587
        }
588

    
589
        @Override
590
        public void formMovedTo(int currentPosition) throws AbortActionException {
591
            LOGGER.trace("formMovedTo " + currentPosition);
592
        }
593

    
594
        @Override
595
        public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException {
596
            LOGGER.trace("formBeforeSave");
597
            if (!store.isEditing()) {
598
                try {
599
                    store.edit();
600
                } catch (DataException e1) {
601
                    throw new StoreEditException(e1, store.getName());
602
                }
603
            }
604
        }
605

    
606
        @Override
607
        public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException {
608
            LOGGER.trace("formBeforeNew");
609
        }
610

    
611
        @Override
612
        public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException {
613
            LOGGER.trace("formBeforeDelete");
614
        }
615

    
616
        @Override
617
        public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException {
618
            LOGGER.trace("formAfterSave");
619
            saveChanges(dynformSet);
620
        }
621

    
622
        @Override
623
        public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException {
624
            LOGGER.trace("formAfterNew");
625
        }
626

    
627
        @Override
628
        public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException {
629
            LOGGER.trace("formAfterDelete");
630
        }
631

    
632
//        @Override
633
//        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
634
//            LOGGER.trace("formBeforeSearch");
635
//            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
636
//            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
637
//
638
//            JExpressionBuilder builder = dataSwingmanager.createQueryFilterExpresion(store);
639
//            FeatureQuery currentQuery = getCurrentQuery();
640
//            if( currentQuery!=null ) {
641
//                Evaluator filter = currentQuery.getFilter();
642
//                if( filter instanceof ExpressionEvaluator ) {
643
//                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
644
//                    builder.setExpression(expression);
645
//                }
646
//            }
647
//            Dialog dialog = winmgr.createDialog(
648
//                    builder.asJComponent(),
649
//                    "Filtro",
650
//                    "Creacion de filtro sobre '"+store.getName()+"'",
651
//                    WindowManager_v2.BUTTONS_OK_CANCEL
652
//            );
653
//            dialog.show(WindowManager.MODE.DIALOG);
654
//            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
655
//                Expression expresion = builder.getExpression();
656
//                try {
657
//                    FeatureQuery query = store.createFeatureQuery();
658
//                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
659
//                        query.clearFilter();
660
//                    } else {
661
//                        query.setFilter(expresion);
662
//                    }
663
//                    setQuery(query);
664
//                } catch (Exception ex) {
665
//                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
666
//                }
667
//            }
668
//        }
669

    
670
        @Override
671
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
672
            LOGGER.trace("formBeforeSearch");
673
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
674
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
675

    
676
            final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
677
            searchPanel.setShowActions(false);
678
            FeatureQuery currentQuery = getCurrentQuery();
679
            if( currentQuery!=null ) {
680
                Evaluator filter = currentQuery.getFilter();
681
                if( filter instanceof ExpressionEvaluator ) {
682
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
683
                    searchPanel.setFilter(expression);
684
                }
685
            }
686
            searchPanel.asJComponent().setPreferredSize(
687
                    new Dimension(FeatureStoreSearchPanel.DEFAULT_WIDTH, 320)
688
            );
689
            Dialog dialog = winmgr.createDialog(
690
                    searchPanel.asJComponent(),
691
                    "Filtro",
692
                    "Creacion de filtro sobre '"+store.getName()+"'",
693
                    WindowManager_v2.BUTTONS_OK_CANCEL
694
            );
695
            dialog.show(WindowManager.MODE.DIALOG);
696
            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
697
                Expression expresion = searchPanel.getFilter();
698
                try {
699
                    FeatureQuery query = store.createFeatureQuery();
700
                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
701
                        query.clearFilter();
702
                    } else {
703
                        query.setFilter(expresion);
704
                    }
705
                    setQuery(query);
706
                } catch (Exception ex) {
707
                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
708
                }
709
            }
710
        }
711

    
712
        @Override
713
        public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException {
714
            LOGGER.trace("formAfterSearch");
715
        }
716

    
717
        @Override
718
        public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException {
719
            LOGGER.trace("formBeforeCancelNew");
720
        }
721

    
722
        @Override
723
        public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException {
724
            LOGGER.trace("formAfterCancelNew");
725
        }
726
    }
727

    
728
    private static class StoreEditException extends AbortActionException {
729

    
730
        /**
731
         *
732
         */
733
        private static final long serialVersionUID = -7682017811778577130L;
734

    
735
        public StoreEditException(Throwable cause, String storename) {
736
            super("Can't edit the store '%(storename)'", cause, "cant_edit_the store_XstorenameX", serialVersionUID);
737
            setValue("storename", storename);
738
        }
739
    }
740
}