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

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