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