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 |
package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Cursor; |
5 |
import java.awt.Dimension; |
6 |
import java.awt.FlowLayout; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.net.URL; |
10 |
import java.util.ArrayList; |
11 |
import java.util.Collection; |
12 |
import java.util.Collections; |
13 |
import java.util.HashMap; |
14 |
import java.util.List; |
15 |
import java.util.Map; |
16 |
import javax.swing.AbstractAction; |
17 |
import javax.swing.Action; |
18 |
import static javax.swing.Action.ACTION_COMMAND_KEY; |
19 |
import static javax.swing.Action.NAME; |
20 |
import javax.swing.BorderFactory; |
21 |
import javax.swing.ImageIcon; |
22 |
import javax.swing.JButton; |
23 |
import javax.swing.JComponent; |
24 |
import javax.swing.SwingUtilities; |
25 |
import javax.swing.event.ListSelectionEvent; |
26 |
import javax.swing.event.ListSelectionListener; |
27 |
import javax.swing.table.TableModel; |
28 |
import org.apache.commons.io.FilenameUtils; |
29 |
import org.apache.commons.lang.mutable.MutableObject; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.gvsig.configurableactions.ConfigurableActionsMamager; |
32 |
import org.gvsig.expressionevaluator.Expression; |
33 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
34 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
35 |
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 |
import org.gvsig.fmap.dal.DataStore; |
40 |
import org.gvsig.fmap.dal.complements.Search; |
41 |
import org.gvsig.fmap.dal.feature.Feature; |
42 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
43 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
47 |
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext; |
48 |
import org.gvsig.fmap.dal.swing.DALActionFactory; |
49 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
50 |
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryCalculatedColumnsPanel; |
51 |
import org.gvsig.fmap.dal.swing.impl.featuretable.SimpleFeaturesTableModel; |
52 |
import org.gvsig.fmap.dal.swing.impl.featurequery.DefaultFeatureQueryGroupByPanel; |
53 |
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel; |
54 |
import org.gvsig.fmap.dal.swing.searchpanel.SearchConditionPanel; |
55 |
import org.gvsig.fmap.dal.swing.searchpanel.SearchConditionPanel.SearchConditionPanelFactory; |
56 |
import org.gvsig.tools.ToolsLocator; |
57 |
import org.gvsig.tools.dataTypes.DataType; |
58 |
import org.gvsig.tools.dynobject.Tags; |
59 |
import org.gvsig.tools.i18n.I18nManager; |
60 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
61 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
62 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
63 |
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 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
67 |
import org.gvsig.tools.util.ToolsUtilLocator; |
68 |
import org.slf4j.Logger; |
69 |
import org.slf4j.LoggerFactory; |
70 |
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 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureAttributesSelectionPanel; |
74 |
|
75 |
/**
|
76 |
*
|
77 |
* @author jjdelcerro
|
78 |
*/
|
79 |
@SuppressWarnings({"UseSpecificCatch"}) |
80 |
public class DefaultSearchPanel |
81 |
extends DefaultSearchPanelView
|
82 |
implements FeatureStoreSearchPanel {
|
83 |
|
84 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSearchPanel.class); |
85 |
|
86 |
static /* friend */ Integer useLabels = null; |
87 |
|
88 |
public static class UseLabelsYesAction extends AbstractAction { |
89 |
|
90 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
91 |
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 |
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 |
public void actionPerformed(ActionEvent ae) { |
110 |
DefaultSearchPanel.useLabels = USE_LABELS_YES; |
111 |
} |
112 |
} |
113 |
|
114 |
public static class UseLabelsNoAction extends AbstractAction { |
115 |
|
116 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
117 |
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 |
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 |
public void actionPerformed(ActionEvent ae) { |
135 |
DefaultSearchPanel.useLabels = USE_LABELS_NO; |
136 |
} |
137 |
} |
138 |
|
139 |
public static class UseLabelsBothAction extends AbstractAction { |
140 |
|
141 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
142 |
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 |
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 |
public void actionPerformed(ActionEvent ae) { |
161 |
DefaultSearchPanel.useLabels = USE_LABELS_BOTH; |
162 |
} |
163 |
} |
164 |
|
165 |
|
166 |
public static class SelectColumnsAction extends AbstractAction { |
167 |
|
168 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
169 |
public SelectColumnsAction() {
|
170 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
171 |
|
172 |
this.putValue(NAME, i18n.getTranslation("_Select_columns_to_display")); |
173 |
this.putValue(ACTION_COMMAND_KEY, "SelectColumns"); |
174 |
} |
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 |
return i18n.getTranslation("_Select_columns_to_display"); |
182 |
} |
183 |
return super.getValue(key); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public void actionPerformed(ActionEvent ae) { |
188 |
DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource(); |
189 |
panel.doSelectResultColumnNames(); |
190 |
} |
191 |
} |
192 |
|
193 |
public static class CalculatedColumnsAction extends AbstractAction { |
194 |
|
195 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
196 |
public CalculatedColumnsAction() {
|
197 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
198 |
|
199 |
this.putValue(NAME, i18n.getTranslation("_Calculated_columns")); |
200 |
this.putValue(ACTION_COMMAND_KEY, "CalculatedColumns"); |
201 |
} |
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 |
return i18n.getTranslation("_Calculated_columns"); |
209 |
} |
210 |
return super.getValue(key); |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void actionPerformed(ActionEvent ae) { |
215 |
DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource(); |
216 |
panel.doCalculatedColumns(); |
217 |
} |
218 |
} |
219 |
|
220 |
public static class GroupByAction extends AbstractAction { |
221 |
|
222 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
223 |
public GroupByAction() {
|
224 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
225 |
|
226 |
this.putValue(NAME, i18n.getTranslation("_Group_by")); |
227 |
this.putValue(ACTION_COMMAND_KEY, "GroupBy"); |
228 |
} |
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 |
return i18n.getTranslation("_Group_by"); |
236 |
} |
237 |
return super.getValue(key); |
238 |
} |
239 |
|
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 |
|
249 |
@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 |
@Override
|
258 |
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 |
public void actionPerformed(ActionEvent ae) { |
269 |
DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource(); |
270 |
panel.doOrderBy(); |
271 |
} |
272 |
} |
273 |
|
274 |
|
275 |
private class ActionButtons { |
276 |
|
277 |
private final DALActionFactory factory; |
278 |
private final Action action; |
279 |
private final JButton button; |
280 |
|
281 |
public ActionButtons(DALActionFactory factory, Action action, JButton button) { |
282 |
this.factory = factory;
|
283 |
this.action = action;
|
284 |
this.button = button;
|
285 |
} |
286 |
} |
287 |
|
288 |
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 |
return this.panel.getFilterForSelectedFeature(); |
315 |
} |
316 |
|
317 |
@Override
|
318 |
public FeatureQuery getQuery() {
|
319 |
return this.panel.getQuery(); |
320 |
} |
321 |
} |
322 |
|
323 |
private final FeatureStore store; |
324 |
private final ActionListenerSupport acctionListeners; |
325 |
private final Map<String, ActionButtons> actions; |
326 |
private boolean showActions = true; |
327 |
private SearchParameters parameters;
|
328 |
|
329 |
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 |
public DefaultSearchPanel(FeatureStore store) {
|
337 |
this.store = store;
|
338 |
this.acctionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
339 |
this.actions = new HashMap<>(); |
340 |
this.parameters = new SearchParameters(); |
341 |
FeatureQuery featureQuery = this.store.createFeatureQuery();
|
342 |
featureQuery.retrievesAllAttributes(); |
343 |
this.parameters.setQuery(featureQuery);
|
344 |
|
345 |
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 |
this.parameters.getResultColumnNames().add(attrdesc.getDescriptor().getName());
|
355 |
} |
356 |
} |
357 |
|
358 |
@Override
|
359 |
public JComponent asJComponent() { |
360 |
if( this.conditionPanels==null ) { |
361 |
this.initComponents();
|
362 |
} |
363 |
return this; |
364 |
} |
365 |
|
366 |
private void addActions() { |
367 |
if( !this.showActions ) { |
368 |
return;
|
369 |
} |
370 |
this.pnlActions.removeAll();
|
371 |
this.pnlActions.setLayout(new FlowLayout(FlowLayout.TRAILING, 8, 4)); |
372 |
SearchActionContext actionContext = new SearchActionContext(this); |
373 |
Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
|
374 |
for (DALActionFactory factory : factories) {
|
375 |
Action action = factory.createAction(actionContext);
|
376 |
JButton button = new JButton(action); |
377 |
this.actions.put(factory.getName(), new ActionButtons(factory, action, button)); |
378 |
button.setBorder(BorderFactory.createEmptyBorder());
|
379 |
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 |
@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 |
private void initComponents() { |
420 |
this.conditionPanels = new ArrayList<>(); |
421 |
|
422 |
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 |
swingManager.translate(this.btnAddAccumulatedFilter);
|
428 |
swingManager.translate(this.btnRemoveAccumulatedFilter);
|
429 |
swingManager.translate(this.btnViewAccumulatedFilter);
|
430 |
|
431 |
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 |
|
436 |
this.conditionPanels.add(
|
437 |
new SearchConditionPanelSimplified(
|
438 |
store, |
439 |
btnAddAccumulatedFilter, |
440 |
btnRemoveAccumulatedFilter, |
441 |
btnViewAccumulatedFilter, |
442 |
lblField1, |
443 |
lblExtraFields1, |
444 |
lblRelationalOperator1, |
445 |
cboValue1, |
446 |
lblLogicalOperators1, |
447 |
lblField2, |
448 |
lblExtraFields2, |
449 |
lblRelationalOperator2, |
450 |
cboValue2, |
451 |
lblLogicalOperators2, |
452 |
lblField3, |
453 |
lblExtraFields3, |
454 |
lblRelationalOperator3, |
455 |
cboValue3, |
456 |
lblLogicalOperators3, |
457 |
lblField4, |
458 |
lblExtraFields4, |
459 |
lblRelationalOperator4, |
460 |
cboValue4, |
461 |
null
|
462 |
) |
463 |
); |
464 |
this.conditionPanels.add(
|
465 |
new SearchConditionPanelAdvanced(
|
466 |
this.store,
|
467 |
txtAdvancedExpression, |
468 |
btnAdvancedExpression, |
469 |
btnAdvancedExpressionHistory, |
470 |
btnAdvancedExpressionBookmarks |
471 |
) |
472 |
); |
473 |
for (SearchConditionPanelFactory factory : DALSwingLocator.getManager().getSearchConditionPanels()) {
|
474 |
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 |
} |
485 |
} |
486 |
|
487 |
this.btnSearch.addActionListener((ActionEvent e) -> { |
488 |
search(); |
489 |
}); |
490 |
|
491 |
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 |
} |
496 |
} |
497 |
}); |
498 |
this.btnClear.addActionListener((ActionEvent e) -> { |
499 |
clear(); |
500 |
}); |
501 |
addActions(); |
502 |
this.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); |
503 |
|
504 |
search(); |
505 |
} |
506 |
|
507 |
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 |
@Override
|
516 |
public void setEnabled(boolean enabled) { |
517 |
if( this.conditionPanels==null ) { |
518 |
this.initComponents();
|
519 |
} |
520 |
for (SearchConditionPanel conditionPanel : conditionPanels) {
|
521 |
conditionPanel.setEnabled(enabled); |
522 |
} |
523 |
|
524 |
this.btnClear.setEnabled(enabled);
|
525 |
this.btnSearch.setEnabled(enabled);
|
526 |
for (ActionButtons actionButton : actions.values()) {
|
527 |
actionButton.action.setEnabled(enabled); |
528 |
} |
529 |
} |
530 |
|
531 |
@Override
|
532 |
public void clear() { |
533 |
if( this.conditionPanels==null ) { |
534 |
return;
|
535 |
} |
536 |
for (SearchConditionPanel conditionPanel : conditionPanels) {
|
537 |
conditionPanel.clear(); |
538 |
} |
539 |
} |
540 |
|
541 |
@Override
|
542 |
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 |
query = (FeatureQuery) this.parameters.getQuery().clone();
|
552 |
query.retrievesAllAttributes(); |
553 |
if (ExpressionUtils.isPhraseEmpty(filter)) {
|
554 |
return query;
|
555 |
} |
556 |
query.setFilter(filter); |
557 |
query.retrievesAllAttributes(); |
558 |
return query;
|
559 |
} catch (Exception ex) { |
560 |
LOGGER.warn("Can't build query.",ex);
|
561 |
return null; |
562 |
} |
563 |
} |
564 |
|
565 |
@Override
|
566 |
public FeatureQuery getLastQuery() {
|
567 |
return this.parameters.getQuery(); |
568 |
} |
569 |
|
570 |
@Override
|
571 |
public void search() { |
572 |
final MutableObject model = new MutableObject(null); |
573 |
|
574 |
lblMsg.setText("Searching...");
|
575 |
setEnabled(false);
|
576 |
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 |
}); |
598 |
th.start(); |
599 |
} |
600 |
|
601 |
public void setResultColumnNames(List<String> names) { |
602 |
this.parameters.getResultColumnNames().clear();
|
603 |
this.parameters.getResultColumnNames().addAll(names);
|
604 |
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 |
this.parameters.getResultColumnNames(),
|
612 |
features |
613 |
); |
614 |
tblResults.setModel(model); |
615 |
} |
616 |
|
617 |
@Override
|
618 |
public boolean setFilter(Expression filter) { |
619 |
try {
|
620 |
if( this.conditionPanels==null ) { |
621 |
this.initComponents();
|
622 |
} |
623 |
if( ExpressionUtils.isPhraseEmpty(filter) ) {
|
624 |
this.clear();
|
625 |
return true; |
626 |
} |
627 |
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 |
|
637 |
SimpleFeaturesTableModel model = new SimpleFeaturesTableModel(this.getStore()); |
638 |
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 |
|
647 |
@Override
|
648 |
public List<SearchConditionPanel> getConditionPanels() { |
649 |
return Collections.unmodifiableList(this.conditionPanels); |
650 |
} |
651 |
|
652 |
@Override
|
653 |
public SearchConditionPanel getConditionPanel(String name) { |
654 |
for (SearchConditionPanel panel : conditionPanels) {
|
655 |
if( StringUtils.equalsIgnoreCase(name, panel.getFactory().getName()) ) {
|
656 |
return panel;
|
657 |
} |
658 |
} |
659 |
return null; |
660 |
} |
661 |
|
662 |
@Override
|
663 |
public Expression getFilterForSelectedFeature() { |
664 |
if( this.conditionPanels==null ) { |
665 |
return null; |
666 |
} |
667 |
int selectedRow = this.tblResults.getSelectedRow(); |
668 |
if (selectedRow < 0) { |
669 |
return null; |
670 |
} |
671 |
try {
|
672 |
List<Feature> features = ((SimpleFeaturesTableModel) this.tblResults.getModel()).getFeatures(); |
673 |
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 |
} |
685 |
Expression filter = ExpressionUtils.createExpression(builder.toString());
|
686 |
return filter;
|
687 |
} catch (Exception ex) { |
688 |
LOGGER.warn("Can't build search for the selected feature.", ex);
|
689 |
return null; |
690 |
} |
691 |
} |
692 |
|
693 |
@Override
|
694 |
public FeatureStore getStore() {
|
695 |
return store;
|
696 |
} |
697 |
|
698 |
private void doOrderBy() { |
699 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
700 |
WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
701 |
FeatureQueryOrderPanel orderPanel = DALSwingLocator.getDataSwingManager().createFeatureStoreOrderPanel(); |
702 |
orderPanel.setStore(store); |
703 |
orderPanel.put(parameters.getQuery()); |
704 |
Dialog dialog = windowManager.createDialog(
|
705 |
orderPanel.asJComponent(), |
706 |
i18n.getTranslation("_Select_order"),
|
707 |
null,
|
708 |
WindowManager_v2.BUTTONS_OK_CANCEL |
709 |
); |
710 |
dialog.addActionListener((ActionEvent e) -> {
|
711 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
712 |
orderPanel.fetch(this.parameters.getQuery());
|
713 |
search(); |
714 |
} |
715 |
}); |
716 |
dialog.show(WindowManager.MODE.DIALOG); |
717 |
|
718 |
} |
719 |
|
720 |
@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 |
@Override
|
735 |
public int getSelectedFeatureCount() { |
736 |
if( this.conditionPanels==null ) { |
737 |
return 0; |
738 |
} |
739 |
return this.tblResults.getSelectedRowCount(); |
740 |
} |
741 |
|
742 |
@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 |
@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 |
|
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 |
|
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 |
} |
822 |
|
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 |
|
830 |
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 |
private void doSelectResultColumnNames() { |
851 |
WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
852 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
853 |
final FeatureAttributesSelectionPanel panel = DALSwingLocator.getManager().createFeatureAttributeSelectionPanel();
|
854 |
panel.setFeatureType(this.getFeatureType());
|
855 |
panel.setSelectedNames(this.parameters.getResultColumnNames());
|
856 |
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 |
|
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 |
} |