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