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 |
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.JOptionPane; |
25 |
import javax.swing.SwingUtilities; |
26 |
import javax.swing.event.ListSelectionEvent; |
27 |
import javax.swing.event.ListSelectionListener; |
28 |
import javax.swing.table.TableModel; |
29 |
import org.apache.commons.io.FilenameUtils; |
30 |
import org.apache.commons.lang.mutable.MutableObject; |
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.gvsig.configurableactions.ConfigurableActionsMamager; |
33 |
import org.gvsig.expressionevaluator.Expression; |
34 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
35 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
36 |
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 |
import org.gvsig.fmap.dal.DataStore; |
41 |
import org.gvsig.fmap.dal.complements.Search; |
42 |
import org.gvsig.fmap.dal.feature.Feature; |
43 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
44 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
47 |
import org.gvsig.fmap.dal.feature.FeatureType; |
48 |
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext; |
49 |
import org.gvsig.fmap.dal.swing.DALActionFactory; |
50 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
51 |
import org.gvsig.fmap.dal.swing.featurequery.FeatureQueryCalculatedColumnsPanel; |
52 |
import org.gvsig.fmap.dal.swing.impl.featuretable.SimpleFeaturesTableModel; |
53 |
import org.gvsig.fmap.dal.swing.impl.featurequery.DefaultFeatureQueryGroupByPanel; |
54 |
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel; |
55 |
import org.gvsig.fmap.dal.swing.searchpanel.SearchConditionPanel; |
56 |
import org.gvsig.fmap.dal.swing.searchpanel.SearchConditionPanel.SearchConditionPanelFactory; |
57 |
import org.gvsig.tools.ToolsLocator; |
58 |
import org.gvsig.tools.dataTypes.DataType; |
59 |
import org.gvsig.tools.dynobject.Tags; |
60 |
import org.gvsig.tools.i18n.I18nManager; |
61 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
62 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
63 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
64 |
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 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
68 |
import org.gvsig.tools.util.ToolsUtilLocator; |
69 |
import org.slf4j.Logger; |
70 |
import org.slf4j.LoggerFactory; |
71 |
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 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureAttributesSelectionPanel; |
75 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
76 |
|
77 |
/**
|
78 |
*
|
79 |
* @author jjdelcerro
|
80 |
*/
|
81 |
@SuppressWarnings({"UseSpecificCatch"}) |
82 |
public class DefaultSearchPanel |
83 |
extends DefaultSearchPanelView
|
84 |
implements FeatureStoreSearchPanel {
|
85 |
|
86 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSearchPanel.class); |
87 |
|
88 |
static /* friend */ Integer useLabels = null; |
89 |
|
90 |
public static class UseLabelsYesAction extends AbstractAction { |
91 |
|
92 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
93 |
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 |
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 |
public void actionPerformed(ActionEvent ae) { |
112 |
DefaultSearchPanel.useLabels = USE_LABELS_YES; |
113 |
} |
114 |
} |
115 |
|
116 |
public static class UseLabelsNoAction extends AbstractAction { |
117 |
|
118 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
119 |
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 |
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 |
public void actionPerformed(ActionEvent ae) { |
137 |
DefaultSearchPanel.useLabels = USE_LABELS_NO; |
138 |
} |
139 |
} |
140 |
|
141 |
public static class UseLabelsBothAction extends AbstractAction { |
142 |
|
143 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
144 |
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 |
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 |
public void actionPerformed(ActionEvent ae) { |
163 |
DefaultSearchPanel.useLabels = USE_LABELS_BOTH; |
164 |
} |
165 |
} |
166 |
|
167 |
|
168 |
public static class SelectColumnsAction extends AbstractAction { |
169 |
|
170 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
171 |
public SelectColumnsAction() {
|
172 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
173 |
|
174 |
this.putValue(NAME, i18n.getTranslation("_Select_columns_to_display")); |
175 |
this.putValue(ACTION_COMMAND_KEY, "SelectColumns"); |
176 |
} |
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 |
return i18n.getTranslation("_Select_columns_to_display"); |
184 |
} |
185 |
return super.getValue(key); |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public void actionPerformed(ActionEvent ae) { |
190 |
DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource(); |
191 |
panel.doSelectResultColumnNames(); |
192 |
} |
193 |
} |
194 |
|
195 |
public static class CalculatedColumnsAction extends AbstractAction { |
196 |
|
197 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
198 |
public CalculatedColumnsAction() {
|
199 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
200 |
|
201 |
this.putValue(NAME, i18n.getTranslation("_Calculated_columns")); |
202 |
this.putValue(ACTION_COMMAND_KEY, "CalculatedColumns"); |
203 |
} |
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 |
return i18n.getTranslation("_Calculated_columns"); |
211 |
} |
212 |
return super.getValue(key); |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public void actionPerformed(ActionEvent ae) { |
217 |
DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource(); |
218 |
panel.doCalculatedColumns(); |
219 |
} |
220 |
} |
221 |
|
222 |
public static class GroupByAction extends AbstractAction { |
223 |
|
224 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
225 |
public GroupByAction() {
|
226 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
227 |
|
228 |
this.putValue(NAME, i18n.getTranslation("_Group_by")); |
229 |
this.putValue(ACTION_COMMAND_KEY, "GroupBy"); |
230 |
} |
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 |
return i18n.getTranslation("_Group_by"); |
238 |
} |
239 |
return super.getValue(key); |
240 |
} |
241 |
|
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 |
|
251 |
@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 |
@Override
|
260 |
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 |
public void actionPerformed(ActionEvent ae) { |
271 |
DefaultSearchPanel panel = (DefaultSearchPanel) ae.getSource(); |
272 |
panel.doOrderBy(); |
273 |
} |
274 |
} |
275 |
|
276 |
|
277 |
private class ActionButtons { |
278 |
|
279 |
private final DALActionFactory factory; |
280 |
private final Action action; |
281 |
private final JButton button; |
282 |
|
283 |
public ActionButtons(DALActionFactory factory, Action action, JButton button) { |
284 |
this.factory = factory;
|
285 |
this.action = action;
|
286 |
this.button = button;
|
287 |
} |
288 |
} |
289 |
|
290 |
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 |
return this.panel.getFilterForSelectedFeature(); |
317 |
} |
318 |
|
319 |
@Override
|
320 |
public FeatureQuery getQuery() {
|
321 |
return this.panel.getQuery(); |
322 |
} |
323 |
} |
324 |
|
325 |
private final FeatureStore store; |
326 |
private final ActionListenerSupport acctionListeners; |
327 |
private final Map<String, ActionButtons> actions; |
328 |
private boolean showActions = true; |
329 |
private SearchParameters parameters;
|
330 |
|
331 |
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 |
public DefaultSearchPanel(FeatureStore store) {
|
339 |
this.store = store;
|
340 |
this.acctionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
341 |
this.actions = new HashMap<>(); |
342 |
this.parameters = new SearchParameters(); |
343 |
FeatureQuery featureQuery = this.store.createFeatureQuery();
|
344 |
featureQuery.retrievesAllAttributes(); |
345 |
this.parameters.setQuery(featureQuery);
|
346 |
|
347 |
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 |
this.parameters.getResultColumnNames().add(attrdesc.getDescriptor().getName());
|
357 |
} |
358 |
} |
359 |
|
360 |
@Override
|
361 |
public JComponent asJComponent() { |
362 |
if( this.conditionPanels==null ) { |
363 |
this.initComponents();
|
364 |
} |
365 |
return this; |
366 |
} |
367 |
|
368 |
private void addActions() { |
369 |
if( !this.showActions ) { |
370 |
return;
|
371 |
} |
372 |
this.pnlActions.removeAll();
|
373 |
this.pnlActions.setLayout(new FlowLayout(FlowLayout.TRAILING, 8, 4)); |
374 |
SearchActionContext actionContext = new SearchActionContext(this); |
375 |
Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
|
376 |
for (DALActionFactory factory : factories) {
|
377 |
Action action = factory.createAction(actionContext);
|
378 |
JButton button = new JButton(action); |
379 |
this.actions.put(factory.getName(), new ActionButtons(factory, action, button)); |
380 |
button.setBorder(BorderFactory.createEmptyBorder());
|
381 |
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 |
@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 |
private void initComponents() { |
422 |
this.conditionPanels = new ArrayList<>(); |
423 |
|
424 |
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 |
swingManager.translate(this.btnAddAccumulatedFilter);
|
430 |
swingManager.translate(this.btnRemoveAccumulatedFilter);
|
431 |
swingManager.translate(this.btnViewAccumulatedFilter);
|
432 |
|
433 |
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 |
|
438 |
this.conditionPanels.add(
|
439 |
new SearchConditionPanelSimplified(
|
440 |
store, |
441 |
btnAddAccumulatedFilter, |
442 |
btnRemoveAccumulatedFilter, |
443 |
btnViewAccumulatedFilter, |
444 |
lblField1, |
445 |
lblExtraFields1, |
446 |
lblRelationalOperator1, |
447 |
cboValue1, |
448 |
lblLogicalOperators1, |
449 |
lblField2, |
450 |
lblExtraFields2, |
451 |
lblRelationalOperator2, |
452 |
cboValue2, |
453 |
lblLogicalOperators2, |
454 |
lblField3, |
455 |
lblExtraFields3, |
456 |
lblRelationalOperator3, |
457 |
cboValue3, |
458 |
lblLogicalOperators3, |
459 |
lblField4, |
460 |
lblExtraFields4, |
461 |
lblRelationalOperator4, |
462 |
cboValue4, |
463 |
null
|
464 |
) |
465 |
); |
466 |
this.conditionPanels.add(
|
467 |
new SearchConditionPanelAdvanced(
|
468 |
this.store,
|
469 |
txtAdvancedExpression, |
470 |
btnAdvancedExpression, |
471 |
btnAdvancedExpressionHistory, |
472 |
btnAdvancedExpressionBookmarks |
473 |
) |
474 |
); |
475 |
for (SearchConditionPanelFactory factory : DALSwingLocator.getManager().getSearchConditionPanels()) {
|
476 |
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 |
} |
487 |
} |
488 |
|
489 |
this.btnSearch.addActionListener((ActionEvent e) -> { |
490 |
search(); |
491 |
}); |
492 |
|
493 |
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 |
} |
498 |
} |
499 |
}); |
500 |
this.btnClear.addActionListener((ActionEvent e) -> { |
501 |
clear(); |
502 |
}); |
503 |
addActions(); |
504 |
this.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); |
505 |
|
506 |
search(); |
507 |
} |
508 |
|
509 |
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 |
@Override
|
518 |
public void setEnabled(boolean enabled) { |
519 |
if( this.conditionPanels==null ) { |
520 |
this.initComponents();
|
521 |
} |
522 |
for (SearchConditionPanel conditionPanel : conditionPanels) {
|
523 |
conditionPanel.setEnabled(enabled); |
524 |
} |
525 |
|
526 |
this.btnClear.setEnabled(enabled);
|
527 |
this.btnSearch.setEnabled(enabled);
|
528 |
for (ActionButtons actionButton : actions.values()) {
|
529 |
actionButton.action.setEnabled(enabled); |
530 |
} |
531 |
} |
532 |
|
533 |
@Override
|
534 |
public void clear() { |
535 |
if( this.conditionPanels==null ) { |
536 |
return;
|
537 |
} |
538 |
for (SearchConditionPanel conditionPanel : conditionPanels) {
|
539 |
conditionPanel.clear(); |
540 |
} |
541 |
} |
542 |
|
543 |
@Override
|
544 |
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 |
query = (FeatureQuery) this.parameters.getQuery().clone();
|
554 |
query.retrievesAllAttributes(); |
555 |
if (ExpressionUtils.isPhraseEmpty(filter)) {
|
556 |
return query;
|
557 |
} |
558 |
query.setFilter(filter); |
559 |
query.retrievesAllAttributes(); |
560 |
return query;
|
561 |
} catch (Exception ex) { |
562 |
LOGGER.warn("Can't build query.",ex);
|
563 |
return null; |
564 |
} |
565 |
} |
566 |
|
567 |
@Override
|
568 |
public FeatureQuery getLastQuery() {
|
569 |
return this.parameters.getQuery(); |
570 |
} |
571 |
|
572 |
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 |
@Override
|
580 |
public void search() { |
581 |
final MutableObject model = new MutableObject(null); |
582 |
|
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 |
lblMsg.setText("Searching...");
|
594 |
setEnabled(false);
|
595 |
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 |
}); |
617 |
th.start(); |
618 |
} |
619 |
|
620 |
public void setResultColumnNames(List<String> names) { |
621 |
this.parameters.getResultColumnNames().clear();
|
622 |
this.parameters.getResultColumnNames().addAll(names);
|
623 |
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 |
this.parameters.getResultColumnNames(),
|
631 |
features |
632 |
); |
633 |
tblResults.setModel(model); |
634 |
} |
635 |
|
636 |
@Override
|
637 |
public boolean setFilter(Expression filter) { |
638 |
try {
|
639 |
if( this.conditionPanels==null ) { |
640 |
this.initComponents();
|
641 |
} |
642 |
if( ExpressionUtils.isPhraseEmpty(filter) ) {
|
643 |
this.clear();
|
644 |
return true; |
645 |
} |
646 |
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 |
|
656 |
SimpleFeaturesTableModel model = new SimpleFeaturesTableModel(this.getStore()); |
657 |
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 |
|
666 |
@Override
|
667 |
public List<SearchConditionPanel> getConditionPanels() { |
668 |
return Collections.unmodifiableList(this.conditionPanels); |
669 |
} |
670 |
|
671 |
@Override
|
672 |
public SearchConditionPanel getConditionPanel(String name) { |
673 |
for (SearchConditionPanel panel : conditionPanels) {
|
674 |
if( StringUtils.equalsIgnoreCase(name, panel.getFactory().getName()) ) {
|
675 |
return panel;
|
676 |
} |
677 |
} |
678 |
return null; |
679 |
} |
680 |
|
681 |
@Override
|
682 |
public Expression getFilterForSelectedFeature() { |
683 |
if( this.conditionPanels==null ) { |
684 |
return null; |
685 |
} |
686 |
int selectedRow = this.tblResults.getSelectedRow(); |
687 |
if (selectedRow < 0) { |
688 |
return null; |
689 |
} |
690 |
try {
|
691 |
List<Feature> features = ((SimpleFeaturesTableModel) this.tblResults.getModel()).getFeatures(); |
692 |
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 |
} |
704 |
Expression filter = ExpressionUtils.createExpression(builder.toString());
|
705 |
return filter;
|
706 |
} catch (Exception ex) { |
707 |
LOGGER.warn("Can't build search for the selected feature.", ex);
|
708 |
return null; |
709 |
} |
710 |
} |
711 |
|
712 |
@Override
|
713 |
public FeatureStore getStore() {
|
714 |
return store;
|
715 |
} |
716 |
|
717 |
private void doOrderBy() { |
718 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
719 |
WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
720 |
FeatureQueryOrderPanel orderPanel = DALSwingLocator.getDataSwingManager().createFeatureStoreOrderPanel(); |
721 |
orderPanel.setStore(store); |
722 |
orderPanel.put(parameters.getQuery()); |
723 |
Dialog dialog = windowManager.createDialog(
|
724 |
orderPanel.asJComponent(), |
725 |
i18n.getTranslation("_Select_order"),
|
726 |
null,
|
727 |
WindowManager_v2.BUTTONS_OK_CANCEL |
728 |
); |
729 |
dialog.addActionListener((ActionEvent e) -> {
|
730 |
if( dialog.getAction()==WindowManager_v2.BUTTON_OK ) {
|
731 |
orderPanel.fetch(this.parameters.getQuery());
|
732 |
search(); |
733 |
} |
734 |
}); |
735 |
dialog.show(WindowManager.MODE.DIALOG); |
736 |
|
737 |
} |
738 |
|
739 |
@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 |
@Override
|
754 |
public int getSelectedFeatureCount() { |
755 |
if( this.conditionPanels==null ) { |
756 |
return 0; |
757 |
} |
758 |
return this.tblResults.getSelectedRowCount(); |
759 |
} |
760 |
|
761 |
@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 |
@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 |
|
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 |
|
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 |
} |
841 |
|
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 |
|
849 |
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 |
private void doSelectResultColumnNames() { |
870 |
WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
871 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
872 |
final FeatureAttributesSelectionPanel panel = DALSwingLocator.getManager().createFeatureAttributeSelectionPanel();
|
873 |
panel.setFeatureType(this.getFeatureType());
|
874 |
panel.setSelectedNames(this.parameters.getResultColumnNames());
|
875 |
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 |
|
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 |
} |