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 / fmap / dal / swing / impl / searchpanel / DefaultSearchPanel.java @ 44764

History | View | Annotate | Download (33.1 KB)

1 44259 jjdelcerro
package org.gvsig.fmap.dal.swing.impl.searchpanel;
2
3 44351 jjdelcerro
import java.awt.BorderLayout;
4 44263 jjdelcerro
import java.awt.Cursor;
5 44259 jjdelcerro
import java.awt.Dimension;
6 44263 jjdelcerro
import java.awt.FlowLayout;
7 44259 jjdelcerro
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.net.URL;
10
import java.util.ArrayList;
11 44263 jjdelcerro
import java.util.Collection;
12 44707 jjdelcerro
import java.util.Collections;
13 44275 jjdelcerro
import java.util.HashMap;
14 44259 jjdelcerro
import java.util.List;
15 44275 jjdelcerro
import java.util.Map;
16 44351 jjdelcerro
import javax.swing.AbstractAction;
17 44263 jjdelcerro
import javax.swing.Action;
18 44351 jjdelcerro
import static javax.swing.Action.ACTION_COMMAND_KEY;
19
import static javax.swing.Action.NAME;
20 44275 jjdelcerro
import javax.swing.BorderFactory;
21 44259 jjdelcerro
import javax.swing.ImageIcon;
22 44263 jjdelcerro
import javax.swing.JButton;
23 44259 jjdelcerro
import javax.swing.JComponent;
24 44764 jjdelcerro
import javax.swing.JOptionPane;
25 44262 jjdelcerro
import javax.swing.SwingUtilities;
26 44259 jjdelcerro
import javax.swing.event.ListSelectionEvent;
27
import javax.swing.event.ListSelectionListener;
28 44262 jjdelcerro
import javax.swing.table.TableModel;
29 44259 jjdelcerro
import org.apache.commons.io.FilenameUtils;
30 44262 jjdelcerro
import org.apache.commons.lang.mutable.MutableObject;
31 44351 jjdelcerro
import org.apache.commons.lang3.StringUtils;
32
import org.gvsig.configurableactions.ConfigurableActionsMamager;
33 44259 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
34
import org.gvsig.expressionevaluator.ExpressionBuilder;
35
import org.gvsig.expressionevaluator.ExpressionUtils;
36 44351 jjdelcerro
import static org.gvsig.fmap.dal.DataManager.DAL_USE_LABELS;
37
import static org.gvsig.fmap.dal.DataManager.USE_LABELS_BOTH;
38
import static org.gvsig.fmap.dal.DataManager.USE_LABELS_NO;
39
import static org.gvsig.fmap.dal.DataManager.USE_LABELS_YES;
40 44281 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
41 44262 jjdelcerro
import org.gvsig.fmap.dal.complements.Search;
42 44259 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
44 44712 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
45 44259 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
46 44712 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory;
47 44259 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
48 44281 jjdelcerro
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext;
49
import org.gvsig.fmap.dal.swing.DALActionFactory;
50 44259 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
51 44712 jjdelcerro
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryCalculatedColumnsPanel;
52 44340 jjdelcerro
import org.gvsig.fmap.dal.swing.impl.featuretable.SimpleFeaturesTableModel;
53 44712 jjdelcerro
import org.gvsig.fmap.dal.swing.impl.featurequery.DefaultFeatureQueryGroupByPanel;
54 44259 jjdelcerro
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel;
55 44707 jjdelcerro
import org.gvsig.fmap.dal.swing.searchpanel.SearchConditionPanel;
56
import org.gvsig.fmap.dal.swing.searchpanel.SearchConditionPanel.SearchConditionPanelFactory;
57 44262 jjdelcerro
import org.gvsig.tools.ToolsLocator;
58 44712 jjdelcerro
import org.gvsig.tools.dataTypes.DataType;
59 44351 jjdelcerro
import org.gvsig.tools.dynobject.Tags;
60
import org.gvsig.tools.i18n.I18nManager;
61 44259 jjdelcerro
import org.gvsig.tools.swing.api.ActionListenerSupport;
62
import org.gvsig.tools.swing.api.ToolsSwingLocator;
63 44603 omartinez
import org.gvsig.tools.swing.api.ToolsSwingManager;
64 44644 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.Dialog;
65
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
66
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
67 44259 jjdelcerro
import org.gvsig.tools.swing.icontheme.IconTheme;
68 44351 jjdelcerro
import org.gvsig.tools.util.ToolsUtilLocator;
69 44259 jjdelcerro
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71 44712 jjdelcerro
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryGroupByPanel;
72
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryOrderPanel;
73
import org.gvsig.fmap.dal.swing.impl.featurequery.DefaultFeatureQueryCalculatedColumnsPanel;
74 44713 jjdelcerro
import org.gvsig.fmap.dal.swing.featuretype.FeatureAttributesSelectionPanel;
75 44764 jjdelcerro
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
76 44259 jjdelcerro
77
/**
78
 *
79
 * @author jjdelcerro
80
 */
81 44707 jjdelcerro
@SuppressWarnings({"UseSpecificCatch"})
82 44259 jjdelcerro
public class DefaultSearchPanel
83
        extends DefaultSearchPanelView
84
        implements FeatureStoreSearchPanel {
85
86
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSearchPanel.class);
87 44275 jjdelcerro
88 44351 jjdelcerro
    static /* friend */ Integer useLabels = null;
89 44707 jjdelcerro
90 44351 jjdelcerro
    public static class UseLabelsYesAction extends AbstractAction {
91
92 44644 jjdelcerro
        @SuppressWarnings("OverridableMethodCallInConstructor")
93 44351 jjdelcerro
        public UseLabelsYesAction() {
94
            I18nManager i18n = ToolsLocator.getI18nManager();
95
96
            this.putValue(NAME, i18n.getTranslation("_Use_labels"));
97
            this.putValue(ACTION_COMMAND_KEY, "UseLabelsYes");
98
        }
99
100
        @Override
101 44481 jjdelcerro
        public Object getValue(String key) {
102
            if( NAME.equals(key) ) {
103
                // Cuando se registra la accion aun no se han cargado las traducciones
104
                I18nManager i18n = ToolsLocator.getI18nManager();
105
                return i18n.getTranslation("_Use_labels");
106
            }
107
            return super.getValue(key);
108
        }
109
110
        @Override
111 44351 jjdelcerro
        public void actionPerformed(ActionEvent ae) {
112
            DefaultSearchPanel.useLabels = USE_LABELS_YES;
113
        }
114
    }
115
116
    public static class UseLabelsNoAction extends AbstractAction {
117
118 44644 jjdelcerro
        @SuppressWarnings("OverridableMethodCallInConstructor")
119 44351 jjdelcerro
        public UseLabelsNoAction() {
120
            I18nManager i18n = ToolsLocator.getI18nManager();
121
            this.putValue(NAME, i18n.getTranslation("_Use_names"));
122
            this.putValue(ACTION_COMMAND_KEY, "UseLabelsNo");
123
        }
124
125
        @Override
126 44481 jjdelcerro
        public Object getValue(String key) {
127
            if( NAME.equals(key) ) {
128
                // Cuando se registra la accion aun no se han cargado las traducciones
129
                I18nManager i18n = ToolsLocator.getI18nManager();
130
                return i18n.getTranslation("_Use_names");
131
            }
132
            return super.getValue(key);
133
        }
134
135
        @Override
136 44351 jjdelcerro
        public void actionPerformed(ActionEvent ae) {
137
            DefaultSearchPanel.useLabels = USE_LABELS_NO;
138
        }
139
    }
140
141
    public static class UseLabelsBothAction extends AbstractAction {
142
143 44644 jjdelcerro
        @SuppressWarnings("OverridableMethodCallInConstructor")
144 44351 jjdelcerro
        public UseLabelsBothAction() {
145
            I18nManager i18n = ToolsLocator.getI18nManager();
146
147
            this.putValue(NAME, i18n.getTranslation("_Use_labels_and_names"));
148
            this.putValue(ACTION_COMMAND_KEY, "UseLabelsBoth");
149
        }
150
151
        @Override
152 44481 jjdelcerro
        public Object getValue(String key) {
153
            if( NAME.equals(key) ) {
154
                // Cuando se registra la accion aun no se han cargado las traducciones
155
                I18nManager i18n = ToolsLocator.getI18nManager();
156
                return i18n.getTranslation("_Use_labels_and_names");
157
            }
158
            return super.getValue(key);
159
        }
160
161
        @Override
162 44351 jjdelcerro
        public void actionPerformed(ActionEvent ae) {
163
            DefaultSearchPanel.useLabels = USE_LABELS_BOTH;
164
        }
165
    }
166
167
168 44712 jjdelcerro
    public static class SelectColumnsAction extends AbstractAction {
169 44644 jjdelcerro
170
        @SuppressWarnings("OverridableMethodCallInConstructor")
171 44712 jjdelcerro
        public SelectColumnsAction() {
172 44644 jjdelcerro
            I18nManager i18n = ToolsLocator.getI18nManager();
173
174 44712 jjdelcerro
            this.putValue(NAME, i18n.getTranslation("_Select_columns_to_display"));
175
            this.putValue(ACTION_COMMAND_KEY, "SelectColumns");
176 44644 jjdelcerro
        }
177
178
        @Override
179
        public Object getValue(String key) {
180
            if( NAME.equals(key) ) {
181
                // Cuando se registra la accion aun no se han cargado las traducciones
182
                I18nManager i18n = ToolsLocator.getI18nManager();
183 44712 jjdelcerro
                return i18n.getTranslation("_Select_columns_to_display");
184 44644 jjdelcerro
            }
185
            return super.getValue(key);
186
        }
187
188
        @Override
189
        public void actionPerformed(ActionEvent ae) {
190
            DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource();
191 44712 jjdelcerro
            panel.doSelectResultColumnNames();
192 44644 jjdelcerro
        }
193
    }
194
195 44712 jjdelcerro
    public static class CalculatedColumnsAction extends AbstractAction {
196 44707 jjdelcerro
197
        @SuppressWarnings("OverridableMethodCallInConstructor")
198 44712 jjdelcerro
        public CalculatedColumnsAction() {
199 44707 jjdelcerro
            I18nManager i18n = ToolsLocator.getI18nManager();
200
201 44712 jjdelcerro
            this.putValue(NAME, i18n.getTranslation("_Calculated_columns"));
202
            this.putValue(ACTION_COMMAND_KEY, "CalculatedColumns");
203 44707 jjdelcerro
        }
204
205
        @Override
206
        public Object getValue(String key) {
207
            if( NAME.equals(key) ) {
208
                // Cuando se registra la accion aun no se han cargado las traducciones
209
                I18nManager i18n = ToolsLocator.getI18nManager();
210 44712 jjdelcerro
                return i18n.getTranslation("_Calculated_columns");
211 44707 jjdelcerro
            }
212
            return super.getValue(key);
213
        }
214
215
        @Override
216
        public void actionPerformed(ActionEvent ae) {
217
            DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource();
218 44712 jjdelcerro
            panel.doCalculatedColumns();
219 44707 jjdelcerro
        }
220
    }
221 44644 jjdelcerro
222 44712 jjdelcerro
    public static class GroupByAction extends AbstractAction {
223 44707 jjdelcerro
224
        @SuppressWarnings("OverridableMethodCallInConstructor")
225 44712 jjdelcerro
        public GroupByAction() {
226 44707 jjdelcerro
            I18nManager i18n = ToolsLocator.getI18nManager();
227
228 44712 jjdelcerro
            this.putValue(NAME, i18n.getTranslation("_Group_by"));
229
            this.putValue(ACTION_COMMAND_KEY, "GroupBy");
230 44707 jjdelcerro
        }
231
232
        @Override
233
        public Object getValue(String key) {
234
            if( NAME.equals(key) ) {
235
                // Cuando se registra la accion aun no se han cargado las traducciones
236
                I18nManager i18n = ToolsLocator.getI18nManager();
237 44712 jjdelcerro
                return i18n.getTranslation("_Group_by");
238 44707 jjdelcerro
            }
239
            return super.getValue(key);
240
        }
241 44712 jjdelcerro
242
        @Override
243
        public void actionPerformed(ActionEvent ae) {
244
            DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource();
245
            panel.doGroupBy();
246
        }
247
    }
248
249
    public static class OrderByAction extends AbstractAction {
250 44707 jjdelcerro
251 44712 jjdelcerro
        @SuppressWarnings("OverridableMethodCallInConstructor")
252
        public OrderByAction() {
253
            I18nManager i18n = ToolsLocator.getI18nManager();
254
255
            this.putValue(NAME, i18n.getTranslation("_Order_by"));
256
            this.putValue(ACTION_COMMAND_KEY, "SelectOrderBy");
257
        }
258
259 44707 jjdelcerro
        @Override
260 44712 jjdelcerro
        public Object getValue(String key) {
261
            if( NAME.equals(key) ) {
262
                // Cuando se registra la accion aun no se han cargado las traducciones
263
                I18nManager i18n = ToolsLocator.getI18nManager();
264
                return i18n.getTranslation("_Order_by");
265
            }
266
            return super.getValue(key);
267
        }
268
269
        @Override
270 44707 jjdelcerro
        public void actionPerformed(ActionEvent ae) {
271
            DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource();
272 44712 jjdelcerro
            panel.doOrderBy();
273 44707 jjdelcerro
        }
274
    }
275
276
277 44275 jjdelcerro
    private class ActionButtons {
278
279 44281 jjdelcerro
        private final DALActionFactory factory;
280
        private final Action action;
281
        private final JButton button;
282 44275 jjdelcerro
283 44281 jjdelcerro
        public ActionButtons(DALActionFactory factory, Action action, JButton button) {
284 44275 jjdelcerro
            this.factory = factory;
285
            this.action = action;
286
            this.button = button;
287
        }
288
    }
289 44259 jjdelcerro
290 44281 jjdelcerro
    public static class SearchActionContext extends AbstractDALActionContext {
291
292
        private final DefaultSearchPanel panel;
293
294
        public SearchActionContext(DefaultSearchPanel panel) {
295
            super(FeatureStoreSearchPanel.ACTION_CONTEXT_NAME);
296
            this.panel = panel;
297
        }
298
299
        @Override
300
        public DataStore getStore() {
301
            return this.panel.getStore();
302
        }
303
304
        @Override
305
        public JComponent getActionButton(String actionName) {
306
            return this.panel.getActionButton(actionName);
307
        }
308
309
        @Override
310
        public int getSelectedsCount() {
311
            return this.panel.getSelectedFeatureCount();
312
        }
313
314
        @Override
315
        public Expression getFilterForSelecteds() {
316 44712 jjdelcerro
            return this.panel.getFilterForSelectedFeature();
317 44281 jjdelcerro
        }
318 44729 omartinez
319
        @Override
320
        public FeatureQuery getQuery() {
321
            return this.panel.getQuery();
322
        }
323 44281 jjdelcerro
    }
324
325 44259 jjdelcerro
    private final FeatureStore store;
326
    private final ActionListenerSupport acctionListeners;
327 44275 jjdelcerro
    private final Map<String, ActionButtons> actions;
328 44288 jjdelcerro
    private boolean showActions = true;
329 44712 jjdelcerro
    private SearchParameters parameters;
330 44259 jjdelcerro
331 44707 jjdelcerro
    private List<SearchConditionPanel> conditionPanels;
332
333
334
    private static final int PANEL_SIMPLIFIED = 0;
335
    private static final int PANEL_ADVANCED = 1;
336
337
338 44259 jjdelcerro
    public DefaultSearchPanel(FeatureStore store) {
339
        this.store = store;
340
        this.acctionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
341 44707 jjdelcerro
        this.actions = new HashMap<>();
342 44712 jjdelcerro
        this.parameters = new SearchParameters();
343 44729 omartinez
        FeatureQuery featureQuery = this.store.createFeatureQuery();
344
        featureQuery.retrievesAllAttributes();
345
        this.parameters.setQuery(featureQuery);
346 44712 jjdelcerro
347 44707 jjdelcerro
        Search search = (Search) ToolsLocator.getComplementsManager().get(
348
                Search.COMPLEMENT_MANE, getFeatureType()
349
        );
350
        List<Search.OrderedAttribute> attributos = search.getOrderedAttributes(
351
                Search.BASIC_TYPES_FILTER,
352
                Search.STR_INT_LONG_LABEL_ORDER,
353
                12
354
        );
355
        for (Search.OrderedAttribute attrdesc : attributos) {
356 44712 jjdelcerro
            this.parameters.getResultColumnNames().add(attrdesc.getDescriptor().getName());
357 44707 jjdelcerro
        }
358 44259 jjdelcerro
    }
359
360
    @Override
361
    public JComponent asJComponent() {
362 44707 jjdelcerro
        if( this.conditionPanels==null ) {
363 44288 jjdelcerro
            this.initComponents();
364
        }
365 44259 jjdelcerro
        return this;
366
    }
367
368 44263 jjdelcerro
    private void addActions() {
369 44288 jjdelcerro
        if( !this.showActions ) {
370
            return;
371
        }
372 44263 jjdelcerro
        this.pnlActions.removeAll();
373 44275 jjdelcerro
        this.pnlActions.setLayout(new FlowLayout(FlowLayout.TRAILING, 8, 4));
374 44281 jjdelcerro
        SearchActionContext actionContext = new SearchActionContext(this);
375
        Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
376
        for (DALActionFactory factory : factories) {
377
            Action action = factory.createAction(actionContext);
378 44263 jjdelcerro
            JButton button = new JButton(action);
379 44275 jjdelcerro
            this.actions.put(factory.getName(), new ActionButtons(factory, action, button));
380
            button.setBorder(BorderFactory.createEmptyBorder());
381 44263 jjdelcerro
            button.setBorderPainted(false);
382
            button.setFocusPainted(false);
383
            button.setContentAreaFilled(false);
384
            button.setCursor(new Cursor(Cursor.HAND_CURSOR));
385
            this.pnlActions.add(button);
386
        }
387
        this.pnlActions.revalidate();
388
        this.pnlActions.repaint();
389
    }
390
391 44259 jjdelcerro
    @Override
392
    public void addActionListener(ActionListener listener) {
393
        this.acctionListeners.addActionListener(listener);
394
    }
395
396
    @Override
397
    public ActionListener[] getActionListeners() {
398
        return this.acctionListeners.getActionListeners();
399
    }
400
401
    @Override
402
    public void removeActionListener(ActionListener listener) {
403
        this.acctionListeners.removeActionListener(listener);
404
    }
405
406
    @Override
407
    public void removeAllActionListener() {
408
        this.acctionListeners.removeAllActionListener();
409
    }
410
411
    @Override
412
    public void fireActionEvent(ActionEvent event) {
413
        this.acctionListeners.fireActionEvent(event);
414
    }
415
416
    @Override
417
    public boolean hasActionListeners() {
418
        return this.acctionListeners.hasActionListeners();
419
    }
420
421 44707 jjdelcerro
     private void initComponents() {
422
        this.conditionPanels = new ArrayList<>();
423
424 44603 omartinez
        ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
425
        swingManager.translate(this.tabSearchMode);
426
        swingManager.translate(this.btnSearch);
427
        swingManager.translate(this.btnClear);
428
        swingManager.translate(this.lblExpressionDeBusqueda);
429 44707 jjdelcerro
        swingManager.translate(this.btnAddAccumulatedFilter);
430
        swingManager.translate(this.btnRemoveAccumulatedFilter);
431
        swingManager.translate(this.btnViewAccumulatedFilter);
432 44603 omartinez
433 44351 jjdelcerro
        ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager();
434
        JComponent c = cfgActionsManager.getConfigurableActionsComponent(CONFIGURABLE_PANEL_ID, this);
435
        this.pnlCfgActions.setLayout(new BorderLayout(0,0));
436
        this.pnlCfgActions.add(c, BorderLayout.CENTER);
437 44707 jjdelcerro
438
        this.conditionPanels.add(
439
            new SearchConditionPanelSimplified(
440 44259 jjdelcerro
                store,
441 44707 jjdelcerro
                btnAddAccumulatedFilter,
442
                btnRemoveAccumulatedFilter,
443
                btnViewAccumulatedFilter,
444 44259 jjdelcerro
                lblField1,
445
                lblExtraFields1,
446
                lblRelationalOperator1,
447
                cboValue1,
448 44707 jjdelcerro
                lblLogicalOperators1,
449 44259 jjdelcerro
                lblField2,
450
                lblExtraFields2,
451
                lblRelationalOperator2,
452
                cboValue2,
453 44707 jjdelcerro
                lblLogicalOperators2,
454 44259 jjdelcerro
                lblField3,
455
                lblExtraFields3,
456
                lblRelationalOperator3,
457
                cboValue3,
458 44707 jjdelcerro
                lblLogicalOperators3,
459 44259 jjdelcerro
                lblField4,
460
                lblExtraFields4,
461
                lblRelationalOperator4,
462
                cboValue4,
463
                null
464 44707 jjdelcerro
            )
465 44259 jjdelcerro
        );
466 44707 jjdelcerro
        this.conditionPanels.add(
467
            new SearchConditionPanelAdvanced(
468
                this.store,
469 44267 jjdelcerro
                txtAdvancedExpression,
470
                btnAdvancedExpression,
471
                btnAdvancedExpressionHistory,
472
                btnAdvancedExpressionBookmarks
473 44707 jjdelcerro
            )
474 44267 jjdelcerro
        );
475 44707 jjdelcerro
        for (SearchConditionPanelFactory factory : DALSwingLocator.getManager().getSearchConditionPanels()) {
476 44760 jjdelcerro
          String factoryName = "unknown";
477
          try {
478
            factoryName = factory.getName();
479
            if( factory.isApplicable(store) ) {
480
             SearchConditionPanel panel = factory.create(this);
481
              this.conditionPanels.add(panel);
482
              this.tabSearchMode.add(factory.getName(), panel.asJComponent());
483
            }
484
          } catch(Throwable th) {
485
              LOGGER.warn("Can't create search panel '"+factoryName+"'.");
486 44707 jjdelcerro
          }
487
        }
488
489 44712 jjdelcerro
        this.btnSearch.addActionListener((ActionEvent e) -> {
490
          search();
491 44259 jjdelcerro
        });
492
493 44712 jjdelcerro
        this.tblResults.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
494
          for (ActionButtons actionButton : actions.values()) {
495
            if( actionButton.action instanceof ListSelectionListener) {
496
              ((ListSelectionListener) actionButton.action).valueChanged(e);
497 44259 jjdelcerro
            }
498 44712 jjdelcerro
          }
499 44259 jjdelcerro
        });
500 44712 jjdelcerro
        this.btnClear.addActionListener((ActionEvent e) -> {
501
          clear();
502 44259 jjdelcerro
        });
503 44263 jjdelcerro
        addActions();
504 44292 jjdelcerro
        this.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
505 44259 jjdelcerro
506 44712 jjdelcerro
        search();
507 44259 jjdelcerro
    }
508
509 44707 jjdelcerro
    private FeatureType getFeatureType() {
510
      try {
511
        return store.getDefaultFeatureType();
512
      } catch (Exception ex) {
513
        throw new RuntimeException("Can't retrieve the feature type.", ex);
514
      }
515
    }
516
517 44262 jjdelcerro
    @Override
518
    public void setEnabled(boolean enabled) {
519 44707 jjdelcerro
        if( this.conditionPanels==null ) {
520
            this.initComponents();
521 44288 jjdelcerro
        }
522 44707 jjdelcerro
        for (SearchConditionPanel conditionPanel : conditionPanels) {
523
          conditionPanel.setEnabled(enabled);
524 44262 jjdelcerro
        }
525 44707 jjdelcerro
526 44262 jjdelcerro
        this.btnClear.setEnabled(enabled);
527
        this.btnSearch.setEnabled(enabled);
528 44275 jjdelcerro
        for (ActionButtons actionButton : actions.values()) {
529
            actionButton.action.setEnabled(enabled);
530 44263 jjdelcerro
        }
531 44262 jjdelcerro
    }
532
533 44712 jjdelcerro
    @Override
534 44259 jjdelcerro
    public void clear() {
535 44707 jjdelcerro
        if( this.conditionPanels==null ) {
536 44288 jjdelcerro
            return;
537
        }
538 44707 jjdelcerro
        for (SearchConditionPanel conditionPanel : conditionPanels) {
539
          conditionPanel.clear();
540 44262 jjdelcerro
        }
541 44259 jjdelcerro
    }
542 44262 jjdelcerro
543 44292 jjdelcerro
    @Override
544 44712 jjdelcerro
    public FeatureQuery getQuery() {
545
        FeatureQuery query;
546
        try {
547
          int searchMode = this.tabSearchMode.getSelectedIndex();
548
          SearchConditionPanel panel = this.conditionPanels.get(searchMode);
549
          Expression filter = panel.get();
550
          if( searchMode!=PANEL_ADVANCED ) {
551
            this.conditionPanels.get(PANEL_ADVANCED).set(filter);
552
          }
553 44717 omartinez
          query = (FeatureQuery) this.parameters.getQuery().clone();
554 44727 jjdelcerro
          query.retrievesAllAttributes();
555 44712 jjdelcerro
          if (ExpressionUtils.isPhraseEmpty(filter)) {
556 44717 omartinez
              return query;
557 44712 jjdelcerro
          }
558
          query.setFilter(filter);
559 44729 omartinez
          query.retrievesAllAttributes();
560 44712 jjdelcerro
          return query;
561
        } catch (Exception ex) {
562
          LOGGER.warn("Can't build query.",ex);
563
          return null;
564
        }
565 44644 jjdelcerro
    }
566 44717 omartinez
567 44644 jjdelcerro
    @Override
568 44712 jjdelcerro
    public FeatureQuery getLastQuery() {
569
        return this.parameters.getQuery();
570 44259 jjdelcerro
    }
571
572 44764 jjdelcerro
    public boolean isValid(StringBuilder message) {
573
        int searchMode = this.tabSearchMode.getSelectedIndex();
574
        SearchConditionPanel panel = this.conditionPanels.get(searchMode);
575
        boolean valid = panel.isValid(message);
576
        return valid;
577
    }
578
579 44712 jjdelcerro
    @Override
580
    public void search() {
581 44262 jjdelcerro
        final MutableObject model = new MutableObject(null);
582 44764 jjdelcerro
583
        StringBuilder message = new StringBuilder();
584
        if( !this.isValid(message) ) {
585
          ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();
586
          dialogManager.messageDialog(
587
                  "_The_specified_search_condition_is_not_valid",
588
                  "_Search",
589
                  JOptionPane.WARNING_MESSAGE
590
          );
591
          return;
592
        }
593 44262 jjdelcerro
        lblMsg.setText("Searching...");
594
        setEnabled(false);
595 44712 jjdelcerro
        Thread th = new Thread(() -> {
596
          try {
597
            final List<Feature> features;
598
            features = store.getFeatures(this.getQuery());
599
            model.setValue( new SimpleFeaturesTableModel(
600
                    getFeatureType(),
601
                    parameters.getResultColumnNames(),
602
                    features
603
            )
604
            );
605
          } catch (Exception ex) {
606
            LOGGER.warn("Can't get features or create table model",ex);
607
          } finally {
608
            SwingUtilities.invokeLater(() -> {
609
              I18nManager i18n = ToolsLocator.getI18nManager();
610
              TableModel m = (TableModel) model.getValue();
611
              tblResults.setModel(m);
612
              lblMsg.setText(String.format("%d "+i18n.getTranslation("_elements"), m.getRowCount()));
613
              setEnabled(true);
614
            });
615
          }
616 44262 jjdelcerro
        });
617
        th.start();
618 44259 jjdelcerro
    }
619
620 44707 jjdelcerro
    public void setResultColumnNames(List<String> names) {
621 44712 jjdelcerro
      this.parameters.getResultColumnNames().clear();
622
      this.parameters.getResultColumnNames().addAll(names);
623 44707 jjdelcerro
      if( this.conditionPanels==null ) {
624
        return;
625
      }
626
      SimpleFeaturesTableModel model = (SimpleFeaturesTableModel) this.tblResults.getModel();
627
      List<Feature> features = model.getFeatures();
628
      model = new SimpleFeaturesTableModel(
629
          getFeatureType(),
630 44712 jjdelcerro
          this.parameters.getResultColumnNames(),
631 44707 jjdelcerro
          features
632
      );
633
      tblResults.setModel(model);
634
    }
635
636 44263 jjdelcerro
    @Override
637 44292 jjdelcerro
    public boolean setFilter(Expression filter) {
638
        try {
639 44707 jjdelcerro
            if( this.conditionPanels==null ) {
640 44292 jjdelcerro
                this.initComponents();
641
            }
642
            if( ExpressionUtils.isPhraseEmpty(filter) ) {
643 44707 jjdelcerro
              this.clear();
644
              return true;
645 44292 jjdelcerro
            }
646 44707 jjdelcerro
            int panel = 0;
647
            int selected = PANEL_ADVANCED;
648
            for (SearchConditionPanel conditionPanel : conditionPanels) {
649
              if( panel!=PANEL_ADVANCED && conditionPanel.set(filter) ) {
650
                selected = panel;
651
              }
652
              panel++;
653
            }
654
            this.tabSearchMode.setSelectedIndex(selected);
655 44292 jjdelcerro
656 44340 jjdelcerro
            SimpleFeaturesTableModel model = new SimpleFeaturesTableModel(this.getStore());
657 44292 jjdelcerro
            tblResults.setModel(model);
658
            lblMsg.setText("");
659
            return true;
660
        } catch(Exception ex) {
661
            LOGGER.warn("Can't set current search", ex);
662
            return false;
663
        }
664
    }
665 44707 jjdelcerro
666 44292 jjdelcerro
    @Override
667 44707 jjdelcerro
    public List<SearchConditionPanel> getConditionPanels() {
668
      return Collections.unmodifiableList(this.conditionPanels);
669
    }
670 44712 jjdelcerro
671 44707 jjdelcerro
    @Override
672 44712 jjdelcerro
    public SearchConditionPanel getConditionPanel(String name) {
673
      for (SearchConditionPanel panel : conditionPanels) {
674
        if( StringUtils.equalsIgnoreCase(name, panel.getFactory().getName()) ) {
675
          return panel;
676 44292 jjdelcerro
        }
677 44712 jjdelcerro
      }
678
      return null;
679 44292 jjdelcerro
    }
680
681
    @Override
682 44712 jjdelcerro
    public Expression getFilterForSelectedFeature() {
683 44707 jjdelcerro
        if( this.conditionPanels==null ) {
684 44288 jjdelcerro
            return null;
685
        }
686 44263 jjdelcerro
        int selectedRow = this.tblResults.getSelectedRow();
687
        if (selectedRow < 0) {
688
            return null;
689
        }
690 44259 jjdelcerro
        try {
691 44340 jjdelcerro
            List<Feature> features = ((SimpleFeaturesTableModel) this.tblResults.getModel()).getFeatures();
692 44263 jjdelcerro
            Feature feature = features.get(selectedRow);
693
694
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
695
            FeatureType ftype = this.store.getDefaultFeatureType();
696
            for (FeatureAttributeDescriptor attrdesc : ftype.getPrimaryKey()) {
697
                builder.and(
698
                        builder.eq(
699
                                builder.column(attrdesc.getName()),
700
                                builder.constant(feature.get(attrdesc.getName()))
701
                        )
702
                );
703 44259 jjdelcerro
            }
704 44263 jjdelcerro
            Expression filter = ExpressionUtils.createExpression(builder.toString());
705
            return filter;
706 44259 jjdelcerro
        } catch (Exception ex) {
707 44263 jjdelcerro
            LOGGER.warn("Can't build search for the selected feature.", ex);
708
            return null;
709 44259 jjdelcerro
        }
710
    }
711
712 44267 jjdelcerro
    @Override
713 44263 jjdelcerro
    public FeatureStore getStore() {
714
        return store;
715
    }
716 44644 jjdelcerro
717 44712 jjdelcerro
    private void doOrderBy() {
718 44707 jjdelcerro
        I18nManager i18n = ToolsLocator.getI18nManager();
719 44644 jjdelcerro
        WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
720 44712 jjdelcerro
        FeatureQueryOrderPanel orderPanel = DALSwingLocator.getDataSwingManager().createFeatureStoreOrderPanel();
721 44644 jjdelcerro
        orderPanel.setStore(store);
722 44712 jjdelcerro
        orderPanel.put(parameters.getQuery());
723 44644 jjdelcerro
        Dialog dialog = windowManager.createDialog(
724
                orderPanel.asJComponent(),
725
                i18n.getTranslation("_Select_order"),
726
                null,
727
                WindowManager_v2.BUTTONS_OK_CANCEL
728
        );
729 44712 jjdelcerro
        dialog.addActionListener((ActionEvent e) -> {
730
          if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
731
            orderPanel.fetch(this.parameters.getQuery());
732
            search();
733
          }
734 44644 jjdelcerro
        });
735
        dialog.show(WindowManager.MODE.DIALOG);
736
737
    }
738 44263 jjdelcerro
739 44259 jjdelcerro
    @Override
740
    public ImageIcon loadImage(String imageName) {
741
        String name = FilenameUtils.getBaseName(imageName);
742
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault();
743
        if (theme.exists(name)) {
744
            return theme.get(name);
745
        }
746
        URL url = this.getClass().getResource(name + ".png");
747
        if (url == null) {
748
            return null;
749
        }
750
        return new ImageIcon(url);
751
    }
752
753 44263 jjdelcerro
    @Override
754
    public int getSelectedFeatureCount() {
755 44707 jjdelcerro
        if( this.conditionPanels==null ) {
756 44288 jjdelcerro
            return 0;
757
        }
758 44263 jjdelcerro
        return this.tblResults.getSelectedRowCount();
759
    }
760
761 44275 jjdelcerro
    @Override
762
    public JComponent getActionButton(String name) {
763
        ActionButtons actionButton = this.actions.get(name);
764
        if( actionButton==null ) {
765
            return null;
766
        }
767
        return actionButton.button;
768
    }
769
770 44288 jjdelcerro
    @Override
771
    public void setShowActions(boolean showActions) {
772
        this.showActions = showActions;
773
    }
774
775
    @Override
776
    public boolean isShowActions() {
777
        return this.showActions;
778
    }
779 44351 jjdelcerro
780
    public static String getAttributeDescriptorLabel(FeatureAttributeDescriptor attrdesc, String tableName) {
781
        String theLabel;
782
        int theUseLabels;
783
        if( useLabels == null ) {
784
            Tags tags = attrdesc.getTags();
785
            if( tags.has(DAL_USE_LABELS) ) {
786
                theUseLabels = tags.getInt(DAL_USE_LABELS, USE_LABELS_NO);
787
            } else {
788
                tags = attrdesc.getFeatureType().getTags();
789
                theUseLabels = tags.getInt(DAL_USE_LABELS, USE_LABELS_NO);
790
            }
791
        } else {
792
            theUseLabels = useLabels;
793
        }
794
        switch(theUseLabels) {
795
            case USE_LABELS_YES:
796
                if( StringUtils.isBlank(tableName) ) {
797
                    theLabel = attrdesc.getLocalizedLabel();
798
                } else {
799
                    theLabel = String.format("%s [%s]", attrdesc.getLocalizedLabel(), tableName);
800
                }
801
                break;
802
            default:
803
            case USE_LABELS_NO:
804
                if( StringUtils.isBlank(tableName) ) {
805
                    theLabel = attrdesc.getName();
806
                } else {
807
                    theLabel = String.format("%s [%s]", attrdesc.getName(), tableName);
808
                }
809
                break;
810
            case USE_LABELS_BOTH:
811
                if( StringUtils.isBlank(tableName) ) {
812
                    theLabel = String.format("%s [%s]", attrdesc.getLocalizedLabel(), attrdesc.getName());
813
                } else {
814
                    theLabel = String.format("%s [%s/%s]", attrdesc.getLocalizedLabel(), attrdesc.getName(), tableName);
815
                }
816
                break;
817
        }
818
        return theLabel;
819
    }
820 44712 jjdelcerro
821
    private void doCalculatedColumns() {
822
      WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
823
      I18nManager i18n = ToolsLocator.getI18nManager();
824
      final FeatureQueryCalculatedColumnsPanel panel = new DefaultFeatureQueryCalculatedColumnsPanel();
825
      panel.setStore(this.store);
826
      panel.put(this.parameters.getQuery());
827
      final Dialog dialog = winmanager.createDialog(
828
              panel.asJComponent(),
829
              i18n.getTranslation("_Calculated_columns"),
830
              null,
831
              WindowManager_v2.BUTTONS_OK_CANCEL
832
      );
833
      dialog.addActionListener((ActionEvent e) -> {
834
        if( dialog.getAction()==WindowManager_v2.BUTTONS_OK ) {
835
            panel.fetch(this.parameters.getQuery());
836
            search();
837
        }
838
      });
839
      dialog.show(WindowManager.MODE.DIALOG);
840 44707 jjdelcerro
    }
841 44712 jjdelcerro
842
    private void doGroupBy() {
843
      int allowGroupBy = ((FeatureStoreProviderFactory)(this.store.getProviderFactory())).allowGroupBy();
844
      if( allowGroupBy!=DataType.YES ) {
845
        // FIXME: mensaje al usaurio.
846
        return;
847
      }
848 44707 jjdelcerro
849 44712 jjdelcerro
      WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
850
      I18nManager i18n = ToolsLocator.getI18nManager();
851
      final FeatureQueryGroupByPanel panel = new DefaultFeatureQueryGroupByPanel();
852
      panel.setStore(this.store);
853
      panel.put(this.parameters.getQuery());
854
      final Dialog dialog = winmanager.createDialog(
855
              panel.asJComponent(),
856
              i18n.getTranslation("_Select_group_columns_and_aggregate_functions"),
857
              null,
858
              WindowManager_v2.BUTTONS_OK_CANCEL
859
      );
860
      dialog.addActionListener((ActionEvent e) -> {
861
        if( dialog.getAction()==WindowManager_v2.BUTTONS_OK ) {
862
            panel.fetch(this.parameters.getQuery());
863
            search();
864
        }
865
      });
866
      dialog.show(WindowManager.MODE.DIALOG);
867
    }
868
869 44707 jjdelcerro
    private void doSelectResultColumnNames() {
870
      WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
871
      I18nManager i18n = ToolsLocator.getI18nManager();
872 44713 jjdelcerro
      final FeatureAttributesSelectionPanel panel = DALSwingLocator.getManager().createFeatureAttributeSelectionPanel();
873 44707 jjdelcerro
      panel.setFeatureType(this.getFeatureType());
874 44712 jjdelcerro
      panel.setSelectedNames(this.parameters.getResultColumnNames());
875 44707 jjdelcerro
      final Dialog dialog = winmanager.createDialog(
876
              panel.asJComponent(),
877
              i18n.getTranslation("_Select_the_columns_to_display"),
878
              null,
879
              WindowManager_v2.BUTTONS_OK_CANCEL
880
      );
881
      dialog.addActionListener((ActionEvent e) -> {
882
        if( dialog.getAction()==WindowManager_v2.BUTTONS_OK ) {
883
           this.setResultColumnNames(panel.getSelectedNames());
884
        }
885
      });
886
      dialog.show(WindowManager.MODE.DIALOG);
887
    }
888
889 44712 jjdelcerro
890
    public static void selfRegister() {
891
        String[][] iconNames = new String[][]{
892
            new String[]{"dalswing", "featurestore-column"},
893
            new String[]{"dalswing", "featurestore-foreing-key"},
894
            new String[]{"dalswing", "featurestore-table"},
895
            new String[]{"dalswing", "search-action-showform"},
896
            new String[]{"dalswing", "search-action-select"},
897
            new String[]{"dalswing", "search-action-select-add"},
898
            new String[]{"dalswing", "search-action-select-filter"}
899
        };
900
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent();
901
        for (String[] icon : iconNames) {
902
            URL url = DefaultSearchPanel.class.getResource(icon[1] + ".png");
903
            theme.registerDefault("DALSwing", icon[0], icon[1], null, url);
904
        }
905
906
        ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager();
907
        cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new UseLabelsYesAction());
908
        cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new UseLabelsNoAction());
909
        cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new UseLabelsBothAction());
910
        cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new SelectColumnsAction());
911
        cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new CalculatedColumnsAction());
912
        cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new GroupByAction());
913
        cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new OrderByAction());
914
    }
915 44263 jjdelcerro
}