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 @ 44259
History | View | Annotate | Download (14.8 KB)
1 | 44259 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|---|---|
2 | |||
3 | import java.awt.Dimension; |
||
4 | import java.awt.event.ActionEvent; |
||
5 | import java.awt.event.ActionListener; |
||
6 | import java.net.URL; |
||
7 | import java.util.ArrayList; |
||
8 | import java.util.List; |
||
9 | import javax.swing.ImageIcon; |
||
10 | import javax.swing.JComponent; |
||
11 | import javax.swing.event.ListSelectionEvent; |
||
12 | import javax.swing.event.ListSelectionListener; |
||
13 | import javax.swing.table.AbstractTableModel; |
||
14 | import org.apache.commons.io.FilenameUtils; |
||
15 | import org.gvsig.expressionevaluator.Expression; |
||
16 | import org.gvsig.expressionevaluator.ExpressionBuilder; |
||
17 | import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_AND; |
||
18 | import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_OR; |
||
19 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
20 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
||
21 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
||
22 | import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
||
23 | import org.gvsig.featureform.swing.JFeaturesForm; |
||
24 | import org.gvsig.fmap.dal.exception.DataException; |
||
25 | import org.gvsig.fmap.dal.feature.Feature; |
||
26 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
27 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
28 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
29 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
30 | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
||
31 | import org.gvsig.fmap.dal.swing.DataSwingManager; |
||
32 | import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel; |
||
33 | import org.gvsig.tools.dataTypes.DataType; |
||
34 | import org.gvsig.tools.swing.api.ActionListenerSupport; |
||
35 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
36 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
37 | import org.gvsig.tools.swing.icontheme.IconTheme; |
||
38 | import org.slf4j.Logger; |
||
39 | import org.slf4j.LoggerFactory; |
||
40 | |||
41 | /**
|
||
42 | *
|
||
43 | * @author jjdelcerro
|
||
44 | */
|
||
45 | @SuppressWarnings("UseSpecificCatch") |
||
46 | public class DefaultSearchPanel |
||
47 | extends DefaultSearchPanelView
|
||
48 | implements FeatureStoreSearchPanel {
|
||
49 | |||
50 | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSearchPanel.class); |
||
51 | private boolean showFormSingleElement; |
||
52 | private Expression currentSearch; |
||
53 | |||
54 | private class FeaturesTableModel extends AbstractTableModel { |
||
55 | |||
56 | private final List<Feature> features; |
||
57 | private final List<String> columnNames; |
||
58 | private final FeatureType featureType; |
||
59 | |||
60 | public FeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) { |
||
61 | this.features = features;
|
||
62 | this.featureType = featureType;
|
||
63 | if (columnNames == null || columnNames.isEmpty()) { |
||
64 | this.columnNames = new ArrayList<>(); |
||
65 | List<FeatureAttributeDescriptor> attributos = SearchUtils.getOrderedAttributes(
|
||
66 | featureType, |
||
67 | SearchUtils.BASIC_TYPES_FILTER, |
||
68 | SearchUtils.STR_INT_LONG_LABEL_ORDER, |
||
69 | 12
|
||
70 | ); |
||
71 | for (FeatureAttributeDescriptor attrdesc : attributos) {
|
||
72 | this.columnNames.add(attrdesc.getName());
|
||
73 | } |
||
74 | } else {
|
||
75 | this.columnNames = columnNames;
|
||
76 | } |
||
77 | } |
||
78 | |||
79 | public List<Feature> getFeatures() { |
||
80 | return this.features; |
||
81 | } |
||
82 | |||
83 | @Override
|
||
84 | public int getRowCount() { |
||
85 | if (this.features == null) { |
||
86 | return 0; |
||
87 | } |
||
88 | return this.features.size(); |
||
89 | } |
||
90 | |||
91 | @Override
|
||
92 | public int getColumnCount() { |
||
93 | return this.columnNames.size(); |
||
94 | } |
||
95 | |||
96 | @Override
|
||
97 | public String getColumnName(int columnIndex) { |
||
98 | String attrName = this.columnNames.get(columnIndex); |
||
99 | if (this.featureType == null) { |
||
100 | return attrName;
|
||
101 | } |
||
102 | FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
||
103 | if (attrdesc == null) { |
||
104 | return "C" + columnIndex; |
||
105 | } |
||
106 | return attrdesc.getLabel();
|
||
107 | } |
||
108 | |||
109 | @Override
|
||
110 | public Class<?> getColumnClass(int columnIndex) { |
||
111 | if (this.featureType == null) { |
||
112 | return String.class; |
||
113 | } |
||
114 | String attrName = this.columnNames.get(columnIndex); |
||
115 | FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
||
116 | if (attrdesc == null) { |
||
117 | return String.class; |
||
118 | } |
||
119 | return attrdesc.getDataType().getDefaultClass();
|
||
120 | } |
||
121 | |||
122 | @Override
|
||
123 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
||
124 | return false; |
||
125 | } |
||
126 | |||
127 | @Override
|
||
128 | public Object getValueAt(int rowIndex, int columnIndex) { |
||
129 | if (this.features == null) { |
||
130 | return null; |
||
131 | } |
||
132 | Feature feature = this.features.get(rowIndex);
|
||
133 | String attrName = this.columnNames.get(columnIndex); |
||
134 | try {
|
||
135 | return feature.get(attrName);
|
||
136 | } catch (Throwable th) { |
||
137 | return null; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | @Override
|
||
142 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
||
143 | |||
144 | } |
||
145 | } |
||
146 | |||
147 | private final FeatureStore store; |
||
148 | private final ActionListenerSupport acctionListeners; |
||
149 | private final List<SearchFieldController> searchFields; |
||
150 | private ExpressionPickerController advancedExpression;
|
||
151 | |||
152 | public DefaultSearchPanel(FeatureStore store) {
|
||
153 | this.store = store;
|
||
154 | this.acctionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
||
155 | this.searchFields = new ArrayList<>(); |
||
156 | this.initComponents();
|
||
157 | } |
||
158 | |||
159 | @Override
|
||
160 | public JComponent asJComponent() { |
||
161 | return this; |
||
162 | } |
||
163 | |||
164 | @Override
|
||
165 | public void addActionListener(ActionListener listener) { |
||
166 | this.acctionListeners.addActionListener(listener);
|
||
167 | } |
||
168 | |||
169 | @Override
|
||
170 | public ActionListener[] getActionListeners() { |
||
171 | return this.acctionListeners.getActionListeners(); |
||
172 | } |
||
173 | |||
174 | @Override
|
||
175 | public void removeActionListener(ActionListener listener) { |
||
176 | this.acctionListeners.removeActionListener(listener);
|
||
177 | } |
||
178 | |||
179 | @Override
|
||
180 | public void removeAllActionListener() { |
||
181 | this.acctionListeners.removeAllActionListener();
|
||
182 | } |
||
183 | |||
184 | @Override
|
||
185 | public void fireActionEvent(ActionEvent event) { |
||
186 | this.acctionListeners.fireActionEvent(event);
|
||
187 | } |
||
188 | |||
189 | @Override
|
||
190 | public boolean hasActionListeners() { |
||
191 | return this.acctionListeners.hasActionListeners(); |
||
192 | } |
||
193 | |||
194 | private void initComponents() { |
||
195 | SearchFieldController controller = new SearchFieldController(
|
||
196 | store, |
||
197 | lblField1, |
||
198 | lblExtraFields1, |
||
199 | lblRelationalOperator1, |
||
200 | cboValue1, |
||
201 | lblLogicalOperators1 |
||
202 | ); |
||
203 | this.searchFields.add(controller);
|
||
204 | controller = new SearchFieldController(
|
||
205 | store, |
||
206 | lblField2, |
||
207 | lblExtraFields2, |
||
208 | lblRelationalOperator2, |
||
209 | cboValue2, |
||
210 | lblLogicalOperators2 |
||
211 | ); |
||
212 | this.searchFields.add(controller);
|
||
213 | controller = new SearchFieldController(
|
||
214 | store, |
||
215 | lblField3, |
||
216 | lblExtraFields3, |
||
217 | lblRelationalOperator3, |
||
218 | cboValue3, |
||
219 | lblLogicalOperators3 |
||
220 | ); |
||
221 | this.searchFields.add(controller);
|
||
222 | controller = new SearchFieldController(
|
||
223 | store, |
||
224 | lblField4, |
||
225 | lblExtraFields4, |
||
226 | lblRelationalOperator4, |
||
227 | cboValue4, |
||
228 | null
|
||
229 | ); |
||
230 | this.searchFields.add(controller);
|
||
231 | |||
232 | List<FeatureAttributeDescriptor> orderedAttributes = SearchUtils.getOrderedAttributes(
|
||
233 | this.store,
|
||
234 | SearchUtils.BASIC_TYPES_FILTER, |
||
235 | SearchUtils.STR_INT_LONG_LABEL_ORDER, |
||
236 | 5
|
||
237 | ); |
||
238 | int n = 0; |
||
239 | for (SearchFieldController searchField : searchFields) {
|
||
240 | searchField.setAttribute(orderedAttributes.get(n++).getName()); |
||
241 | } |
||
242 | |||
243 | ExpressionEvaluatorSwingManager expressionSwingManager = ExpressionEvaluatorSwingLocator.getManager(); |
||
244 | this.advancedExpression = expressionSwingManager.createExpressionPickerController(txtAdvancedExpression, btnAdvancedExpression);
|
||
245 | |||
246 | this.btnSearch.addActionListener(new ActionListener() { |
||
247 | @Override
|
||
248 | public void actionPerformed(ActionEvent e) { |
||
249 | doSearch(); |
||
250 | } |
||
251 | }); |
||
252 | |||
253 | this.btnShowForm.addActionListener(new ActionListener() { |
||
254 | @Override
|
||
255 | public void actionPerformed(ActionEvent e) { |
||
256 | doShowForm(); |
||
257 | } |
||
258 | }); |
||
259 | try {
|
||
260 | this.showFormSingleElement = true; |
||
261 | FeatureAttributeDescriptor[] pks = this.store.getDefaultFeatureType().getPrimaryKey(); |
||
262 | if (pks == null || pks.length < 1) { |
||
263 | this.showFormSingleElement = false; |
||
264 | } |
||
265 | } catch (Exception ex) { |
||
266 | } |
||
267 | this.btnShowForm.setEnabled(true); |
||
268 | |||
269 | this.tblResults.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
||
270 | @Override
|
||
271 | public void valueChanged(ListSelectionEvent e) { |
||
272 | if( e.getValueIsAdjusting() ) {
|
||
273 | return;
|
||
274 | } |
||
275 | if( tblResults.getSelectedRowCount()==0 ) { |
||
276 | btnShowForm.setEnabled(true);
|
||
277 | return;
|
||
278 | } |
||
279 | btnShowForm.setEnabled(showFormSingleElement); |
||
280 | } |
||
281 | }); |
||
282 | this.btnClear.addActionListener(new ActionListener() { |
||
283 | @Override
|
||
284 | public void actionPerformed(ActionEvent e) { |
||
285 | clear(); |
||
286 | } |
||
287 | }); |
||
288 | this.setPreferredSize(new Dimension(700, 450)); |
||
289 | |||
290 | doSearch(null);
|
||
291 | } |
||
292 | |||
293 | public void clear() { |
||
294 | for (SearchFieldController searchField : searchFields) {
|
||
295 | searchField.clear(); |
||
296 | } |
||
297 | this.advancedExpression.set(null); |
||
298 | } |
||
299 | |||
300 | private void doSearch() { |
||
301 | int searchMode = this.tabSearchMode.getSelectedIndex(); |
||
302 | if (searchMode == 1) { // Avanzada |
||
303 | Expression filter = this.advancedExpression.get(); |
||
304 | if (ExpressionUtils.isPhraseEmpty(filter)) {
|
||
305 | return;
|
||
306 | } |
||
307 | doSearch(filter); |
||
308 | return;
|
||
309 | } |
||
310 | |||
311 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
312 | String relational = OPERATOR_OR;
|
||
313 | for (SearchFieldController searchField : searchFields) {
|
||
314 | if( searchField.getAttribute()!=null && searchField.getValue()!=null ) { |
||
315 | ExpressionBuilder.BinaryOperator cond = builder.binaryOperator( |
||
316 | searchField.getRelationalOperator(), |
||
317 | searchField.isAttributeAnExpression()? |
||
318 | builder.custom(searchField.getAttribute()): |
||
319 | builder.column(searchField.getAttribute()), |
||
320 | builder.constant(searchField.getValue()) |
||
321 | ); |
||
322 | if( relational.equals(OPERATOR_AND) ) {
|
||
323 | builder.and(cond); |
||
324 | } else {
|
||
325 | builder.or(cond); |
||
326 | } |
||
327 | relational = searchField.getLogicalOperator(); |
||
328 | } |
||
329 | } |
||
330 | if( builder.isEmpty() ) {
|
||
331 | doSearch(null);
|
||
332 | return;
|
||
333 | } |
||
334 | this.txtAdvancedExpression.setText(builder.toString());
|
||
335 | doSearch(ExpressionUtils.createExpression(builder.toString())); |
||
336 | } |
||
337 | |||
338 | private void doSearch(Expression exp) { |
||
339 | FeaturesTableModel model; |
||
340 | try {
|
||
341 | List<Feature> features;
|
||
342 | if (exp == null) { |
||
343 | features = this.store.getFeatures();
|
||
344 | } else {
|
||
345 | features = this.store.getFeatures(exp);
|
||
346 | } |
||
347 | this.currentSearch = exp;
|
||
348 | model = new FeaturesTableModel(
|
||
349 | this.store.getDefaultFeatureType(),
|
||
350 | null,
|
||
351 | features |
||
352 | ); |
||
353 | this.tblResults.setModel(model);
|
||
354 | this.lblMsg.setText(String.format("%d elementos", features.size())); |
||
355 | } catch (DataException ex) {
|
||
356 | |||
357 | } |
||
358 | } |
||
359 | |||
360 | private void doShowForm() { |
||
361 | try {
|
||
362 | this.btnShowForm.setEnabled(false); |
||
363 | DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
||
364 | |||
365 | Expression filter;
|
||
366 | int selectedRow = this.tblResults.getSelectedRow(); |
||
367 | if( selectedRow<0 ) { |
||
368 | filter = this.currentSearch;
|
||
369 | } else {
|
||
370 | List<Feature> features = ((FeaturesTableModel) this.tblResults.getModel()).getFeatures(); |
||
371 | Feature feature = features.get(selectedRow); |
||
372 | |||
373 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
374 | FeatureType ftype = this.store.getDefaultFeatureType();
|
||
375 | for (FeatureAttributeDescriptor attrdesc : ftype.getPrimaryKey()) {
|
||
376 | builder.and( |
||
377 | builder.eq( |
||
378 | builder.column(attrdesc.getName()), |
||
379 | builder.constant(feature.get(attrdesc.getName())) |
||
380 | ) |
||
381 | ); |
||
382 | } |
||
383 | filter = ExpressionUtils.createExpression(builder.toString()); |
||
384 | } |
||
385 | FeatureQuery query = store.createFeatureQuery(); |
||
386 | query.setFilter(filter); |
||
387 | query.retrievesAllAttributes(); |
||
388 | JFeaturesForm form = dataSwingManager.createJFeaturesForm(store); |
||
389 | form.setQuery(query); |
||
390 | form.showForm(WindowManager.MODE.WINDOW); |
||
391 | |||
392 | } catch (Exception ex) { |
||
393 | LOGGER.warn("Can't show form", ex);
|
||
394 | } finally {
|
||
395 | this.btnShowForm.setEnabled(true); |
||
396 | } |
||
397 | } |
||
398 | |||
399 | @Override
|
||
400 | public ImageIcon loadImage(String imageName) { |
||
401 | String name = FilenameUtils.getBaseName(imageName);
|
||
402 | IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
||
403 | if (theme.exists(name)) {
|
||
404 | return theme.get(name);
|
||
405 | } |
||
406 | URL url = this.getClass().getResource(name + ".png"); |
||
407 | if (url == null) { |
||
408 | return null; |
||
409 | } |
||
410 | return new ImageIcon(url); |
||
411 | } |
||
412 | |||
413 | public static void selfRegister() { |
||
414 | String[][] iconNames = new String[][] { |
||
415 | new String[] { "dalswing", "featurestore-search-attribute-selector" } |
||
416 | }; |
||
417 | IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
||
418 | for (String[] icon : iconNames) { |
||
419 | URL url = DefaultSearchPanel.class.getResource(icon[1]+".png"); |
||
420 | theme.registerDefault("DALSwing", icon[0], icon[1], null, url); |
||
421 | } |
||
422 | |||
423 | |||
424 | } |
||
425 | } |