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 @ 44603
History | View | Annotate | Download (25.2 KB)
1 | 44259 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|---|---|
2 | |||
3 | 44351 | jjdelcerro | import java.awt.BorderLayout; |
4 | 44263 | jjdelcerro | import java.awt.Cursor; |
5 | 44259 | jjdelcerro | import java.awt.Dimension; |
6 | 44263 | jjdelcerro | import java.awt.FlowLayout; |
7 | 44259 | jjdelcerro | import java.awt.event.ActionEvent; |
8 | import java.awt.event.ActionListener; |
||
9 | import java.net.URL; |
||
10 | import java.util.ArrayList; |
||
11 | 44263 | jjdelcerro | import java.util.Collection; |
12 | 44275 | jjdelcerro | import java.util.HashMap; |
13 | 44259 | jjdelcerro | import java.util.List; |
14 | 44275 | jjdelcerro | import java.util.Map; |
15 | 44351 | jjdelcerro | import javax.swing.AbstractAction; |
16 | 44263 | jjdelcerro | import javax.swing.Action; |
17 | 44351 | jjdelcerro | import static javax.swing.Action.ACTION_COMMAND_KEY; |
18 | import static javax.swing.Action.NAME; |
||
19 | 44275 | jjdelcerro | import javax.swing.BorderFactory; |
20 | 44259 | jjdelcerro | import javax.swing.ImageIcon; |
21 | 44263 | jjdelcerro | import javax.swing.JButton; |
22 | 44259 | jjdelcerro | import javax.swing.JComponent; |
23 | 44262 | jjdelcerro | import javax.swing.SwingUtilities; |
24 | 44259 | jjdelcerro | import javax.swing.event.ListSelectionEvent; |
25 | import javax.swing.event.ListSelectionListener; |
||
26 | 44262 | jjdelcerro | import javax.swing.table.TableModel; |
27 | 44259 | jjdelcerro | import org.apache.commons.io.FilenameUtils; |
28 | 44262 | jjdelcerro | import org.apache.commons.lang.mutable.MutableObject; |
29 | 44351 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
30 | import org.gvsig.configurableactions.ConfigurableActionsMamager; |
||
31 | 44292 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
32 | 44259 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
33 | import org.gvsig.expressionevaluator.ExpressionBuilder; |
||
34 | import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_AND; |
||
35 | import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_OR; |
||
36 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
37 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
||
38 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
||
39 | import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
||
40 | 44351 | jjdelcerro | import static org.gvsig.fmap.dal.DataManager.DAL_USE_LABELS; |
41 | import static org.gvsig.fmap.dal.DataManager.USE_LABELS_BOTH; |
||
42 | import static org.gvsig.fmap.dal.DataManager.USE_LABELS_NO; |
||
43 | import static org.gvsig.fmap.dal.DataManager.USE_LABELS_YES; |
||
44 | 44281 | jjdelcerro | import org.gvsig.fmap.dal.DataStore; |
45 | 44262 | jjdelcerro | import org.gvsig.fmap.dal.complements.Search; |
46 | 44259 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
47 | import org.gvsig.fmap.dal.feature.Feature; |
||
48 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
49 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
50 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
51 | 44281 | jjdelcerro | import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext; |
52 | import org.gvsig.fmap.dal.swing.DALActionFactory; |
||
53 | 44259 | jjdelcerro | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
54 | 44340 | jjdelcerro | import org.gvsig.fmap.dal.swing.impl.featuretable.SimpleFeaturesTableModel; |
55 | 44259 | jjdelcerro | import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel; |
56 | 44262 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
57 | 44351 | jjdelcerro | import org.gvsig.tools.dynobject.Tags; |
58 | import org.gvsig.tools.i18n.I18nManager; |
||
59 | 44259 | jjdelcerro | import org.gvsig.tools.swing.api.ActionListenerSupport; |
60 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
61 | 44603 | omartinez | import org.gvsig.tools.swing.api.ToolsSwingManager; |
62 | 44259 | jjdelcerro | import org.gvsig.tools.swing.icontheme.IconTheme; |
63 | 44351 | jjdelcerro | import org.gvsig.tools.util.ToolsUtilLocator; |
64 | 44259 | jjdelcerro | import org.slf4j.Logger; |
65 | import org.slf4j.LoggerFactory; |
||
66 | |||
67 | /**
|
||
68 | *
|
||
69 | * @author jjdelcerro
|
||
70 | */
|
||
71 | 44351 | jjdelcerro | @SuppressWarnings({"UseSpecificCatch","Convert2Lambda"}) |
72 | 44259 | jjdelcerro | public class DefaultSearchPanel |
73 | extends DefaultSearchPanelView
|
||
74 | implements FeatureStoreSearchPanel {
|
||
75 | |||
76 | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSearchPanel.class); |
||
77 | private Expression currentSearch; |
||
78 | 44275 | jjdelcerro | |
79 | 44351 | jjdelcerro | static /* friend */ Integer useLabels = null; |
80 | |||
81 | public static class UseLabelsYesAction extends AbstractAction { |
||
82 | |||
83 | public UseLabelsYesAction() {
|
||
84 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
85 | |||
86 | this.putValue(NAME, i18n.getTranslation("_Use_labels")); |
||
87 | this.putValue(ACTION_COMMAND_KEY, "UseLabelsYes"); |
||
88 | } |
||
89 | |||
90 | @Override
|
||
91 | 44481 | jjdelcerro | public Object getValue(String key) { |
92 | if( NAME.equals(key) ) {
|
||
93 | // Cuando se registra la accion aun no se han cargado las traducciones
|
||
94 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
95 | return i18n.getTranslation("_Use_labels"); |
||
96 | } |
||
97 | return super.getValue(key); |
||
98 | } |
||
99 | |||
100 | @Override
|
||
101 | 44351 | jjdelcerro | public void actionPerformed(ActionEvent ae) { |
102 | DefaultSearchPanel.useLabels = USE_LABELS_YES; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | public static class UseLabelsNoAction extends AbstractAction { |
||
107 | |||
108 | public UseLabelsNoAction() {
|
||
109 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
110 | this.putValue(NAME, i18n.getTranslation("_Use_names")); |
||
111 | this.putValue(ACTION_COMMAND_KEY, "UseLabelsNo"); |
||
112 | } |
||
113 | |||
114 | @Override
|
||
115 | 44481 | jjdelcerro | public Object getValue(String key) { |
116 | if( NAME.equals(key) ) {
|
||
117 | // Cuando se registra la accion aun no se han cargado las traducciones
|
||
118 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
119 | return i18n.getTranslation("_Use_names"); |
||
120 | } |
||
121 | return super.getValue(key); |
||
122 | } |
||
123 | |||
124 | @Override
|
||
125 | 44351 | jjdelcerro | public void actionPerformed(ActionEvent ae) { |
126 | DefaultSearchPanel.useLabels = USE_LABELS_NO; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public static class UseLabelsBothAction extends AbstractAction { |
||
131 | |||
132 | public UseLabelsBothAction() {
|
||
133 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
134 | |||
135 | this.putValue(NAME, i18n.getTranslation("_Use_labels_and_names")); |
||
136 | this.putValue(ACTION_COMMAND_KEY, "UseLabelsBoth"); |
||
137 | } |
||
138 | |||
139 | @Override
|
||
140 | 44481 | jjdelcerro | public Object getValue(String key) { |
141 | if( NAME.equals(key) ) {
|
||
142 | // Cuando se registra la accion aun no se han cargado las traducciones
|
||
143 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
144 | return i18n.getTranslation("_Use_labels_and_names"); |
||
145 | } |
||
146 | return super.getValue(key); |
||
147 | } |
||
148 | |||
149 | @Override
|
||
150 | 44351 | jjdelcerro | public void actionPerformed(ActionEvent ae) { |
151 | DefaultSearchPanel.useLabels = USE_LABELS_BOTH; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | |||
156 | 44275 | jjdelcerro | private class ActionButtons { |
157 | |||
158 | 44281 | jjdelcerro | private final DALActionFactory factory; |
159 | private final Action action; |
||
160 | private final JButton button; |
||
161 | 44275 | jjdelcerro | |
162 | 44281 | jjdelcerro | public ActionButtons(DALActionFactory factory, Action action, JButton button) { |
163 | 44275 | jjdelcerro | this.factory = factory;
|
164 | this.action = action;
|
||
165 | this.button = button;
|
||
166 | } |
||
167 | } |
||
168 | 44259 | jjdelcerro | |
169 | 44281 | jjdelcerro | public static class SearchActionContext extends AbstractDALActionContext { |
170 | |||
171 | private final DefaultSearchPanel panel; |
||
172 | |||
173 | public SearchActionContext(DefaultSearchPanel panel) {
|
||
174 | super(FeatureStoreSearchPanel.ACTION_CONTEXT_NAME);
|
||
175 | this.panel = panel;
|
||
176 | } |
||
177 | |||
178 | @Override
|
||
179 | public DataStore getStore() {
|
||
180 | return this.panel.getStore(); |
||
181 | } |
||
182 | |||
183 | @Override
|
||
184 | public Expression getFilter() { |
||
185 | return this.panel.getCurrentSearch(); |
||
186 | } |
||
187 | |||
188 | @Override
|
||
189 | public JComponent getActionButton(String actionName) { |
||
190 | return this.panel.getActionButton(actionName); |
||
191 | } |
||
192 | |||
193 | @Override
|
||
194 | public int getSelectedsCount() { |
||
195 | return this.panel.getSelectedFeatureCount(); |
||
196 | } |
||
197 | |||
198 | @Override
|
||
199 | public Expression getFilterForSelecteds() { |
||
200 | return this.panel.getSearchForSelectedFeature(); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | 44259 | jjdelcerro | private final FeatureStore store; |
205 | private final ActionListenerSupport acctionListeners; |
||
206 | 44288 | jjdelcerro | private List<SearchFieldController> searchFields; |
207 | 44259 | jjdelcerro | private ExpressionPickerController advancedExpression;
|
208 | 44275 | jjdelcerro | private final Map<String, ActionButtons> actions; |
209 | 44288 | jjdelcerro | private boolean showActions = true; |
210 | 44297 | jjdelcerro | private int maxSearhFields = 4; |
211 | 44603 | omartinez | private final I18nManager i18n = ToolsLocator.getI18nManager(); |
212 | 44259 | jjdelcerro | |
213 | public DefaultSearchPanel(FeatureStore store) {
|
||
214 | this.store = store;
|
||
215 | this.acctionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
||
216 | 44288 | jjdelcerro | this.searchFields = null; |
217 | 44275 | jjdelcerro | this.actions = new HashMap<>(); |
218 | 44259 | jjdelcerro | } |
219 | |||
220 | @Override
|
||
221 | public JComponent asJComponent() { |
||
222 | 44288 | jjdelcerro | if( this.searchFields==null ) { |
223 | this.initComponents();
|
||
224 | } |
||
225 | 44259 | jjdelcerro | return this; |
226 | } |
||
227 | |||
228 | 44263 | jjdelcerro | private void addActions() { |
229 | 44288 | jjdelcerro | if( !this.showActions ) { |
230 | return;
|
||
231 | } |
||
232 | 44263 | jjdelcerro | this.pnlActions.removeAll();
|
233 | 44275 | jjdelcerro | this.pnlActions.setLayout(new FlowLayout(FlowLayout.TRAILING, 8, 4)); |
234 | 44281 | jjdelcerro | SearchActionContext actionContext = new SearchActionContext(this); |
235 | Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
|
||
236 | for (DALActionFactory factory : factories) {
|
||
237 | Action action = factory.createAction(actionContext);
|
||
238 | 44263 | jjdelcerro | JButton button = new JButton(action); |
239 | 44275 | jjdelcerro | this.actions.put(factory.getName(), new ActionButtons(factory, action, button)); |
240 | button.setBorder(BorderFactory.createEmptyBorder());
|
||
241 | 44263 | jjdelcerro | button.setBorderPainted(false);
|
242 | button.setFocusPainted(false);
|
||
243 | button.setContentAreaFilled(false);
|
||
244 | button.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
||
245 | this.pnlActions.add(button);
|
||
246 | } |
||
247 | this.pnlActions.revalidate();
|
||
248 | this.pnlActions.repaint();
|
||
249 | } |
||
250 | |||
251 | 44259 | jjdelcerro | @Override
|
252 | public void addActionListener(ActionListener listener) { |
||
253 | this.acctionListeners.addActionListener(listener);
|
||
254 | } |
||
255 | |||
256 | @Override
|
||
257 | public ActionListener[] getActionListeners() { |
||
258 | return this.acctionListeners.getActionListeners(); |
||
259 | } |
||
260 | |||
261 | @Override
|
||
262 | public void removeActionListener(ActionListener listener) { |
||
263 | this.acctionListeners.removeActionListener(listener);
|
||
264 | } |
||
265 | |||
266 | @Override
|
||
267 | public void removeAllActionListener() { |
||
268 | this.acctionListeners.removeAllActionListener();
|
||
269 | } |
||
270 | |||
271 | @Override
|
||
272 | public void fireActionEvent(ActionEvent event) { |
||
273 | this.acctionListeners.fireActionEvent(event);
|
||
274 | } |
||
275 | |||
276 | @Override
|
||
277 | public boolean hasActionListeners() { |
||
278 | return this.acctionListeners.hasActionListeners(); |
||
279 | } |
||
280 | |||
281 | private void initComponents() { |
||
282 | 44603 | omartinez | ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
283 | swingManager.translate(this.tabSearchMode);
|
||
284 | swingManager.translate(this.btnSearch);
|
||
285 | swingManager.translate(this.btnClear);
|
||
286 | swingManager.translate(this.lblExpressionDeBusqueda);
|
||
287 | |||
288 | 44351 | jjdelcerro | ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager(); |
289 | JComponent c = cfgActionsManager.getConfigurableActionsComponent(CONFIGURABLE_PANEL_ID, this); |
||
290 | this.pnlCfgActions.setLayout(new BorderLayout(0,0)); |
||
291 | this.pnlCfgActions.add(c, BorderLayout.CENTER); |
||
292 | |||
293 | 44288 | jjdelcerro | this.searchFields = new ArrayList<>(); |
294 | 44259 | jjdelcerro | SearchFieldController controller = new SearchFieldController(
|
295 | store, |
||
296 | lblField1, |
||
297 | lblExtraFields1, |
||
298 | lblRelationalOperator1, |
||
299 | cboValue1, |
||
300 | lblLogicalOperators1 |
||
301 | ); |
||
302 | this.searchFields.add(controller);
|
||
303 | controller = new SearchFieldController(
|
||
304 | store, |
||
305 | lblField2, |
||
306 | lblExtraFields2, |
||
307 | lblRelationalOperator2, |
||
308 | cboValue2, |
||
309 | lblLogicalOperators2 |
||
310 | ); |
||
311 | this.searchFields.add(controller);
|
||
312 | controller = new SearchFieldController(
|
||
313 | store, |
||
314 | lblField3, |
||
315 | lblExtraFields3, |
||
316 | lblRelationalOperator3, |
||
317 | cboValue3, |
||
318 | lblLogicalOperators3 |
||
319 | ); |
||
320 | this.searchFields.add(controller);
|
||
321 | controller = new SearchFieldController(
|
||
322 | store, |
||
323 | lblField4, |
||
324 | lblExtraFields4, |
||
325 | lblRelationalOperator4, |
||
326 | cboValue4, |
||
327 | null
|
||
328 | ); |
||
329 | this.searchFields.add(controller);
|
||
330 | 44262 | jjdelcerro | try {
|
331 | Search search = (Search) ToolsLocator.getComplementsManager().get( |
||
332 | Search.COMPLEMENT_MANE, this.store.getDefaultFeatureType()
|
||
333 | ); |
||
334 | 44337 | jjdelcerro | List<Search.OrderedAttribute> orderedAttributes = search.getOrderedAttributes(
|
335 | 44262 | jjdelcerro | Search.BASIC_TYPES_FILTER, |
336 | Search.STR_INT_LONG_LABEL_ORDER, |
||
337 | 5
|
||
338 | ); |
||
339 | 44297 | jjdelcerro | this.maxSearhFields = Integer.min(orderedAttributes.size(), 4); |
340 | 44262 | jjdelcerro | int n = 0; |
341 | for (SearchFieldController searchField : searchFields) {
|
||
342 | 44297 | jjdelcerro | if( n<this.maxSearhFields ) { |
343 | 44337 | jjdelcerro | searchField.setAttribute(orderedAttributes.get(n++).getDescriptor().getName()); |
344 | 44297 | jjdelcerro | } else {
|
345 | searchField.setEnabled(false);
|
||
346 | } |
||
347 | 44262 | jjdelcerro | } |
348 | } catch (DataException ex) {
|
||
349 | LOGGER.warn("Can't determine order of attributes", ex);
|
||
350 | 44259 | jjdelcerro | } |
351 | |||
352 | ExpressionEvaluatorSwingManager expressionSwingManager = ExpressionEvaluatorSwingLocator.getManager(); |
||
353 | 44267 | jjdelcerro | this.advancedExpression = expressionSwingManager.createExpressionPickerController(
|
354 | txtAdvancedExpression, |
||
355 | btnAdvancedExpression, |
||
356 | btnAdvancedExpressionHistory, |
||
357 | btnAdvancedExpressionBookmarks |
||
358 | ); |
||
359 | 44285 | jjdelcerro | this.advancedExpression.addElement(
|
360 | DALSwingLocator.getSwingManager().createFeatureStoreElement(store) |
||
361 | ); |
||
362 | 44259 | jjdelcerro | |
363 | this.btnSearch.addActionListener(new ActionListener() { |
||
364 | @Override
|
||
365 | public void actionPerformed(ActionEvent e) { |
||
366 | doSearch(); |
||
367 | } |
||
368 | }); |
||
369 | |||
370 | this.tblResults.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
||
371 | @Override
|
||
372 | public void valueChanged(ListSelectionEvent e) { |
||
373 | 44275 | jjdelcerro | for (ActionButtons actionButton : actions.values()) {
|
374 | if( actionButton.action instanceof ListSelectionListener) { |
||
375 | ((ListSelectionListener) actionButton.action).valueChanged(e);
|
||
376 | 44263 | jjdelcerro | } |
377 | 44259 | jjdelcerro | } |
378 | } |
||
379 | }); |
||
380 | this.btnClear.addActionListener(new ActionListener() { |
||
381 | @Override
|
||
382 | public void actionPerformed(ActionEvent e) { |
||
383 | clear(); |
||
384 | } |
||
385 | }); |
||
386 | 44263 | jjdelcerro | addActions(); |
387 | 44292 | jjdelcerro | this.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); |
388 | 44259 | jjdelcerro | |
389 | doSearch(null);
|
||
390 | } |
||
391 | |||
392 | 44262 | jjdelcerro | @Override
|
393 | public void setEnabled(boolean enabled) { |
||
394 | 44288 | jjdelcerro | if( this.searchFields==null ) { |
395 | initComponents(); |
||
396 | } |
||
397 | 44297 | jjdelcerro | int n=0; |
398 | 44262 | jjdelcerro | for (SearchFieldController searchField : searchFields) {
|
399 | 44297 | jjdelcerro | if( n<this.maxSearhFields ) { |
400 | searchField.setEnabled(enabled); |
||
401 | } else {
|
||
402 | searchField.setEnabled(false);
|
||
403 | } |
||
404 | n++; |
||
405 | 44262 | jjdelcerro | } |
406 | this.btnClear.setEnabled(enabled);
|
||
407 | this.btnSearch.setEnabled(enabled);
|
||
408 | this.advancedExpression.setEnabled(enabled);
|
||
409 | 44275 | jjdelcerro | for (ActionButtons actionButton : actions.values()) {
|
410 | actionButton.action.setEnabled(enabled); |
||
411 | 44263 | jjdelcerro | } |
412 | 44262 | jjdelcerro | } |
413 | |||
414 | 44259 | jjdelcerro | public void clear() { |
415 | 44288 | jjdelcerro | if( this.searchFields==null ) { |
416 | return;
|
||
417 | } |
||
418 | 44259 | jjdelcerro | for (SearchFieldController searchField : searchFields) {
|
419 | searchField.clear(); |
||
420 | 44262 | jjdelcerro | } |
421 | 44259 | jjdelcerro | this.advancedExpression.set(null); |
422 | } |
||
423 | 44262 | jjdelcerro | |
424 | 44292 | jjdelcerro | @Override
|
425 | public Expression getFilter() { |
||
426 | Expression filter;
|
||
427 | 44259 | jjdelcerro | int searchMode = this.tabSearchMode.getSelectedIndex(); |
428 | if (searchMode == 1) { // Avanzada |
||
429 | 44292 | jjdelcerro | filter = this.advancedExpression.get();
|
430 | } else {
|
||
431 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
432 | String relational = OPERATOR_OR;
|
||
433 | for (SearchFieldController searchField : searchFields) {
|
||
434 | if (searchField.getAttribute() != null && searchField.getValue() != null) { |
||
435 | ExpressionBuilder.BinaryOperator cond = builder.binaryOperator( |
||
436 | searchField.getRelationalOperator(), |
||
437 | searchField.isAttributeAnExpression() |
||
438 | ? builder.custom(searchField.getAttribute()) |
||
439 | : builder.column(searchField.getAttribute()), |
||
440 | builder.constant(searchField.getValue()) |
||
441 | ); |
||
442 | if (relational.equals(OPERATOR_AND)) {
|
||
443 | builder.and(cond); |
||
444 | } else {
|
||
445 | builder.or(cond); |
||
446 | } |
||
447 | relational = searchField.getLogicalOperator(); |
||
448 | 44259 | jjdelcerro | } |
449 | } |
||
450 | 44292 | jjdelcerro | if (builder.isEmpty()) {
|
451 | filter = null;
|
||
452 | } else {
|
||
453 | filter = ExpressionUtils.createExpression(builder.toString()); |
||
454 | } |
||
455 | 44259 | jjdelcerro | } |
456 | 44292 | jjdelcerro | if (ExpressionUtils.isPhraseEmpty(filter)) {
|
457 | return null; |
||
458 | 44259 | jjdelcerro | } |
459 | 44292 | jjdelcerro | return filter;
|
460 | 44259 | jjdelcerro | } |
461 | 44292 | jjdelcerro | |
462 | private void doSearch() { |
||
463 | Expression filter = this.getFilter(); |
||
464 | doSearch(filter); |
||
465 | } |
||
466 | 44259 | jjdelcerro | |
467 | 44262 | jjdelcerro | private void doSearch(final Expression exp) { |
468 | final MutableObject model = new MutableObject(null); |
||
469 | |||
470 | lblMsg.setText("Searching...");
|
||
471 | setEnabled(false);
|
||
472 | Thread th = new Thread(new Runnable() { |
||
473 | @Override
|
||
474 | public void run() { |
||
475 | try {
|
||
476 | final List<Feature> features; |
||
477 | if (exp == null) { |
||
478 | features = store.getFeatures(); |
||
479 | } else {
|
||
480 | features = store.getFeatures(exp); |
||
481 | } |
||
482 | currentSearch = exp; |
||
483 | 44340 | jjdelcerro | model.setValue( new SimpleFeaturesTableModel(
|
484 | 44262 | jjdelcerro | store.getDefaultFeatureType(), |
485 | null,
|
||
486 | features |
||
487 | ) |
||
488 | ); |
||
489 | } catch (DataException ex) {
|
||
490 | LOGGER.warn("Can't get features or create table model",ex);
|
||
491 | } finally {
|
||
492 | SwingUtilities.invokeLater(new Runnable() { |
||
493 | @Override
|
||
494 | public void run() { |
||
495 | TableModel m = (TableModel) model.getValue(); |
||
496 | tblResults.setModel(m); |
||
497 | 44603 | omartinez | lblMsg.setText(String.format("%d "+i18n.getTranslation("_elements"), m.getRowCount())); |
498 | 44262 | jjdelcerro | setEnabled(true);
|
499 | } |
||
500 | }); |
||
501 | } |
||
502 | 44259 | jjdelcerro | } |
503 | 44262 | jjdelcerro | }); |
504 | th.start(); |
||
505 | 44259 | jjdelcerro | } |
506 | |||
507 | 44263 | jjdelcerro | @Override
|
508 | public Expression getCurrentSearch() { |
||
509 | return this.currentSearch; |
||
510 | } |
||
511 | |||
512 | @Override
|
||
513 | 44292 | jjdelcerro | public boolean setFilter(Expression filter) { |
514 | try {
|
||
515 | if( this.advancedExpression==null ) { |
||
516 | this.initComponents();
|
||
517 | } |
||
518 | if( ExpressionUtils.isPhraseEmpty(filter) ) {
|
||
519 | this.clear();
|
||
520 | return true; |
||
521 | } |
||
522 | this.advancedExpression.set(filter);
|
||
523 | this.tabSearchMode.setSelectedIndex(1); |
||
524 | |||
525 | Code code = filter.getCode(); |
||
526 | if( code.code()==Code.CALLER) {
|
||
527 | SearchFieldController searchField = this.searchFields.get(0); |
||
528 | Code.Caller caller = (Code.Caller)code; |
||
529 | if( searchField.isAValidRelationOperator(caller.name())) {
|
||
530 | Code op1 = caller.parameters().get(0);
|
||
531 | Code op2 = caller.parameters().get(1);
|
||
532 | if( op1.code()==Code.IDENTIFIER && op2.code()==Code.CONSTANT ) {
|
||
533 | if( searchField.setAttribute(((Code.Identifier)op1).name())>=0 ) { |
||
534 | searchField.setRelationalOperator(caller.name()); |
||
535 | searchField.setValue(((Code.Constant)op2).value()); |
||
536 | this.tabSearchMode.setSelectedIndex(0); |
||
537 | } |
||
538 | } |
||
539 | } |
||
540 | } |
||
541 | 44340 | jjdelcerro | SimpleFeaturesTableModel model = new SimpleFeaturesTableModel(this.getStore()); |
542 | 44292 | jjdelcerro | tblResults.setModel(model); |
543 | lblMsg.setText("");
|
||
544 | return true; |
||
545 | } catch(Exception ex) { |
||
546 | LOGGER.warn("Can't set current search", ex);
|
||
547 | return false; |
||
548 | } |
||
549 | } |
||
550 | |||
551 | @Override
|
||
552 | public void setCurrentSearch(Expression filter) { |
||
553 | if( this.setFilter(filter) ) { |
||
554 | doSearch(); |
||
555 | } |
||
556 | } |
||
557 | |||
558 | @Override
|
||
559 | 44263 | jjdelcerro | public Expression getSearchForSelectedFeature() { |
560 | 44288 | jjdelcerro | if( this.searchFields==null ) { |
561 | return null; |
||
562 | } |
||
563 | 44263 | jjdelcerro | int selectedRow = this.tblResults.getSelectedRow(); |
564 | if (selectedRow < 0) { |
||
565 | return null; |
||
566 | } |
||
567 | 44259 | jjdelcerro | try {
|
568 | 44340 | jjdelcerro | List<Feature> features = ((SimpleFeaturesTableModel) this.tblResults.getModel()).getFeatures(); |
569 | 44263 | jjdelcerro | Feature feature = features.get(selectedRow); |
570 | |||
571 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
572 | FeatureType ftype = this.store.getDefaultFeatureType();
|
||
573 | for (FeatureAttributeDescriptor attrdesc : ftype.getPrimaryKey()) {
|
||
574 | builder.and( |
||
575 | builder.eq( |
||
576 | builder.column(attrdesc.getName()), |
||
577 | builder.constant(feature.get(attrdesc.getName())) |
||
578 | ) |
||
579 | ); |
||
580 | 44259 | jjdelcerro | } |
581 | 44263 | jjdelcerro | Expression filter = ExpressionUtils.createExpression(builder.toString());
|
582 | return filter;
|
||
583 | 44259 | jjdelcerro | } catch (Exception ex) { |
584 | 44263 | jjdelcerro | LOGGER.warn("Can't build search for the selected feature.", ex);
|
585 | return null; |
||
586 | 44259 | jjdelcerro | } |
587 | } |
||
588 | |||
589 | 44267 | jjdelcerro | @Override
|
590 | 44263 | jjdelcerro | public FeatureStore getStore() {
|
591 | return store;
|
||
592 | } |
||
593 | |||
594 | 44259 | jjdelcerro | @Override
|
595 | public ImageIcon loadImage(String imageName) { |
||
596 | String name = FilenameUtils.getBaseName(imageName);
|
||
597 | IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
||
598 | if (theme.exists(name)) {
|
||
599 | return theme.get(name);
|
||
600 | } |
||
601 | URL url = this.getClass().getResource(name + ".png"); |
||
602 | if (url == null) { |
||
603 | return null; |
||
604 | } |
||
605 | return new ImageIcon(url); |
||
606 | } |
||
607 | |||
608 | public static void selfRegister() { |
||
609 | 44262 | jjdelcerro | String[][] iconNames = new String[][]{ |
610 | new String[]{"dalswing", "featurestore-column"}, |
||
611 | new String[]{"dalswing", "featurestore-foreing-key"}, |
||
612 | 44263 | jjdelcerro | new String[]{"dalswing", "featurestore-table"}, |
613 | new String[]{"dalswing", "search-action-showform"}, |
||
614 | new String[]{"dalswing", "search-action-select"}, |
||
615 | new String[]{"dalswing", "search-action-select-add"}, |
||
616 | new String[]{"dalswing", "search-action-select-filter"} |
||
617 | 44259 | jjdelcerro | }; |
618 | IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
||
619 | for (String[] icon : iconNames) { |
||
620 | 44262 | jjdelcerro | URL url = DefaultSearchPanel.class.getResource(icon[1] + ".png"); |
621 | 44259 | jjdelcerro | theme.registerDefault("DALSwing", icon[0], icon[1], null, url); |
622 | } |
||
623 | 44262 | jjdelcerro | |
624 | 44351 | jjdelcerro | ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager(); |
625 | cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new UseLabelsYesAction());
|
||
626 | cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new UseLabelsNoAction());
|
||
627 | cfgActionsManager.addConfigurableAction(CONFIGURABLE_PANEL_ID, new UseLabelsBothAction());
|
||
628 | |||
629 | 44259 | jjdelcerro | } |
630 | 44263 | jjdelcerro | |
631 | @Override
|
||
632 | public int getSelectedFeatureCount() { |
||
633 | 44288 | jjdelcerro | if( this.searchFields==null ) { |
634 | return 0; |
||
635 | } |
||
636 | 44263 | jjdelcerro | return this.tblResults.getSelectedRowCount(); |
637 | } |
||
638 | |||
639 | 44275 | jjdelcerro | @Override
|
640 | public JComponent getActionButton(String name) { |
||
641 | ActionButtons actionButton = this.actions.get(name);
|
||
642 | if( actionButton==null ) { |
||
643 | return null; |
||
644 | } |
||
645 | return actionButton.button;
|
||
646 | } |
||
647 | |||
648 | 44288 | jjdelcerro | @Override
|
649 | public void setShowActions(boolean showActions) { |
||
650 | this.showActions = showActions;
|
||
651 | } |
||
652 | |||
653 | @Override
|
||
654 | public boolean isShowActions() { |
||
655 | return this.showActions; |
||
656 | } |
||
657 | 44351 | jjdelcerro | |
658 | |||
659 | public static String getAttributeDescriptorLabel(FeatureAttributeDescriptor attrdesc, String tableName) { |
||
660 | String theLabel;
|
||
661 | int theUseLabels;
|
||
662 | if( useLabels == null ) { |
||
663 | Tags tags = attrdesc.getTags(); |
||
664 | if( tags.has(DAL_USE_LABELS) ) {
|
||
665 | theUseLabels = tags.getInt(DAL_USE_LABELS, USE_LABELS_NO); |
||
666 | } else {
|
||
667 | tags = attrdesc.getFeatureType().getTags(); |
||
668 | theUseLabels = tags.getInt(DAL_USE_LABELS, USE_LABELS_NO); |
||
669 | } |
||
670 | } else {
|
||
671 | theUseLabels = useLabels; |
||
672 | } |
||
673 | switch(theUseLabels) {
|
||
674 | case USE_LABELS_YES:
|
||
675 | if( StringUtils.isBlank(tableName) ) {
|
||
676 | theLabel = attrdesc.getLocalizedLabel(); |
||
677 | } else {
|
||
678 | theLabel = String.format("%s [%s]", attrdesc.getLocalizedLabel(), tableName); |
||
679 | } |
||
680 | break;
|
||
681 | default:
|
||
682 | case USE_LABELS_NO:
|
||
683 | if( StringUtils.isBlank(tableName) ) {
|
||
684 | theLabel = attrdesc.getName(); |
||
685 | } else {
|
||
686 | theLabel = String.format("%s [%s]", attrdesc.getName(), tableName); |
||
687 | } |
||
688 | break;
|
||
689 | case USE_LABELS_BOTH:
|
||
690 | if( StringUtils.isBlank(tableName) ) {
|
||
691 | theLabel = String.format("%s [%s]", attrdesc.getLocalizedLabel(), attrdesc.getName()); |
||
692 | } else {
|
||
693 | theLabel = String.format("%s [%s/%s]", attrdesc.getLocalizedLabel(), attrdesc.getName(), tableName); |
||
694 | } |
||
695 | break;
|
||
696 | } |
||
697 | return theLabel;
|
||
698 | } |
||
699 | |||
700 | 44263 | jjdelcerro | } |