Revision 44263 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
DefaultSearchPanel.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.dal.swing.impl.searchpanel; |
2 | 2 |
|
3 |
import java.awt.Cursor; |
|
3 | 4 |
import java.awt.Dimension; |
5 |
import java.awt.FlowLayout; |
|
4 | 6 |
import java.awt.event.ActionEvent; |
5 | 7 |
import java.awt.event.ActionListener; |
6 | 8 |
import java.net.URL; |
7 | 9 |
import java.util.ArrayList; |
10 |
import java.util.Collection; |
|
11 |
import java.util.LinkedHashMap; |
|
8 | 12 |
import java.util.List; |
13 |
import java.util.Map; |
|
14 |
import javax.swing.Action; |
|
9 | 15 |
import javax.swing.ImageIcon; |
16 |
import javax.swing.JButton; |
|
10 | 17 |
import javax.swing.JComponent; |
11 | 18 |
import javax.swing.SwingUtilities; |
12 | 19 |
import javax.swing.event.ListSelectionEvent; |
... | ... | |
14 | 21 |
import javax.swing.table.AbstractTableModel; |
15 | 22 |
import javax.swing.table.TableModel; |
16 | 23 |
import org.apache.commons.io.FilenameUtils; |
17 |
import org.apache.commons.lang.mutable.MutableLong; |
|
18 | 24 |
import org.apache.commons.lang.mutable.MutableObject; |
19 | 25 |
import org.gvsig.expressionevaluator.Expression; |
20 | 26 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
... | ... | |
24 | 30 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
25 | 31 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
26 | 32 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
27 |
import org.gvsig.featureform.swing.JFeaturesForm; |
|
28 | 33 |
import org.gvsig.fmap.dal.complements.Search; |
29 | 34 |
import org.gvsig.fmap.dal.exception.DataException; |
30 | 35 |
import org.gvsig.fmap.dal.feature.Feature; |
31 | 36 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
32 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
33 | 37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 | 38 |
import org.gvsig.fmap.dal.feature.FeatureType; |
35 | 39 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
36 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
|
40 |
import org.gvsig.fmap.dal.swing.impl.searchpanel.actions.SelectionAddAction; |
|
41 |
import org.gvsig.fmap.dal.swing.impl.searchpanel.actions.SelectionFilterAction; |
|
42 |
import org.gvsig.fmap.dal.swing.impl.searchpanel.actions.SelectionSetAction; |
|
43 |
import org.gvsig.fmap.dal.swing.impl.searchpanel.actions.ShowFormAction; |
|
37 | 44 |
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel; |
38 | 45 |
import org.gvsig.tools.ToolsLocator; |
39 | 46 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
40 | 47 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
41 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
|
42 | 48 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
43 | 49 |
import org.slf4j.Logger; |
44 | 50 |
import org.slf4j.LoggerFactory; |
... | ... | |
53 | 59 |
implements FeatureStoreSearchPanel { |
54 | 60 |
|
55 | 61 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSearchPanel.class); |
56 |
private boolean showFormSingleElement; |
|
57 | 62 |
private Expression currentSearch; |
58 | 63 |
|
59 | 64 |
private class FeaturesTableModel extends AbstractTableModel { |
... | ... | |
155 | 160 |
private final ActionListenerSupport acctionListeners; |
156 | 161 |
private final List<SearchFieldController> searchFields; |
157 | 162 |
private ExpressionPickerController advancedExpression; |
163 |
private List<Action> actions; |
|
158 | 164 |
|
159 | 165 |
public DefaultSearchPanel(FeatureStore store) { |
160 | 166 |
this.store = store; |
161 | 167 |
this.acctionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport(); |
162 | 168 |
this.searchFields = new ArrayList<>(); |
169 |
this.actions = new ArrayList<>(); |
|
163 | 170 |
this.initComponents(); |
164 | 171 |
} |
165 | 172 |
|
... | ... | |
168 | 175 |
return this; |
169 | 176 |
} |
170 | 177 |
|
178 |
private void addActions() { |
|
179 |
this.pnlActions.removeAll(); |
|
180 |
this.pnlActions.setLayout(new FlowLayout(FlowLayout.TRAILING, 2, 2)); |
|
181 |
Collection<ActionFactory> factories = DALSwingLocator.getSwingManager().getFeatureStoreSearchActions(); |
|
182 |
for (ActionFactory factory : factories) { |
|
183 |
Action action = factory.createAction(this); |
|
184 |
this.actions.add(action); |
|
185 |
JButton button = new JButton(action); |
|
186 |
// button.setBorder(BorderFactory.createEmptyBorder()); |
|
187 |
button.setBorderPainted(false); |
|
188 |
button.setFocusPainted(false); |
|
189 |
button.setContentAreaFilled(false); |
|
190 |
button.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
|
191 |
this.pnlActions.add(button); |
|
192 |
} |
|
193 |
this.pnlActions.revalidate(); |
|
194 |
this.pnlActions.repaint(); |
|
195 |
} |
|
196 |
|
|
171 | 197 |
@Override |
172 | 198 |
public void addActionListener(ActionListener listener) { |
173 | 199 |
this.acctionListeners.addActionListener(listener); |
... | ... | |
262 | 288 |
} |
263 | 289 |
}); |
264 | 290 |
|
265 |
this.btnShowForm.addActionListener(new ActionListener() { |
|
266 |
@Override |
|
267 |
public void actionPerformed(ActionEvent e) { |
|
268 |
doShowForm(); |
|
269 |
} |
|
270 |
}); |
|
271 |
try { |
|
272 |
this.showFormSingleElement = true; |
|
273 |
FeatureAttributeDescriptor[] pks = this.store.getDefaultFeatureType().getPrimaryKey(); |
|
274 |
if (pks == null || pks.length < 1) { |
|
275 |
this.showFormSingleElement = false; |
|
276 |
} |
|
277 |
} catch (Exception ex) { |
|
278 |
} |
|
279 |
this.btnShowForm.setEnabled(true); |
|
280 |
|
|
281 | 291 |
this.tblResults.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
282 | 292 |
@Override |
283 | 293 |
public void valueChanged(ListSelectionEvent e) { |
284 |
if (e.getValueIsAdjusting()) { |
|
285 |
return; |
|
294 |
for (Action action : actions) { |
|
295 |
if( action instanceof ListSelectionListener) { |
|
296 |
((ListSelectionListener) action).valueChanged(e); |
|
297 |
} |
|
286 | 298 |
} |
287 |
if (tblResults.getSelectedRowCount() == 0) { |
|
288 |
btnShowForm.setEnabled(true); |
|
289 |
return; |
|
290 |
} |
|
291 |
btnShowForm.setEnabled(showFormSingleElement); |
|
292 | 299 |
} |
293 | 300 |
}); |
294 | 301 |
this.btnClear.addActionListener(new ActionListener() { |
... | ... | |
297 | 304 |
clear(); |
298 | 305 |
} |
299 | 306 |
}); |
307 |
addActions(); |
|
300 | 308 |
this.setPreferredSize(new Dimension(700, 450)); |
301 | 309 |
|
302 | 310 |
doSearch(null); |
... | ... | |
309 | 317 |
} |
310 | 318 |
this.btnClear.setEnabled(enabled); |
311 | 319 |
this.btnSearch.setEnabled(enabled); |
312 |
this.btnShowForm.setEnabled(enabled); |
|
313 | 320 |
this.advancedExpression.setEnabled(enabled); |
321 |
for (Action action : actions) { |
|
322 |
action.setEnabled(enabled); |
|
323 |
} |
|
314 | 324 |
} |
315 | 325 |
|
316 | 326 |
public void clear() { |
... | ... | |
398 | 408 |
th.start(); |
399 | 409 |
} |
400 | 410 |
|
401 |
private void doShowForm() { |
|
411 |
@Override |
|
412 |
public Expression getCurrentSearch() { |
|
413 |
return this.currentSearch; |
|
414 |
} |
|
415 |
|
|
416 |
@Override |
|
417 |
public Expression getSearchForSelectedFeature() { |
|
418 |
int selectedRow = this.tblResults.getSelectedRow(); |
|
419 |
if (selectedRow < 0) { |
|
420 |
return null; |
|
421 |
} |
|
402 | 422 |
try { |
403 |
this.btnShowForm.setEnabled(false); |
|
404 |
DataSwingManager dataSwingManager = DALSwingLocator.getSwingManager(); |
|
405 |
|
|
406 |
Expression filter; |
|
407 |
int selectedRow = this.tblResults.getSelectedRow(); |
|
408 |
if (selectedRow < 0) { |
|
409 |
filter = this.currentSearch; |
|
410 |
} else { |
|
411 |
List<Feature> features = ((FeaturesTableModel) this.tblResults.getModel()).getFeatures(); |
|
412 |
Feature feature = features.get(selectedRow); |
|
413 |
|
|
414 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
|
415 |
FeatureType ftype = this.store.getDefaultFeatureType(); |
|
416 |
for (FeatureAttributeDescriptor attrdesc : ftype.getPrimaryKey()) { |
|
417 |
builder.and( |
|
418 |
builder.eq( |
|
419 |
builder.column(attrdesc.getName()), |
|
420 |
builder.constant(feature.get(attrdesc.getName())) |
|
421 |
) |
|
422 |
); |
|
423 |
} |
|
424 |
filter = ExpressionUtils.createExpression(builder.toString()); |
|
423 |
List<Feature> features = ((FeaturesTableModel) this.tblResults.getModel()).getFeatures(); |
|
424 |
Feature feature = features.get(selectedRow); |
|
425 |
|
|
426 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
|
427 |
FeatureType ftype = this.store.getDefaultFeatureType(); |
|
428 |
for (FeatureAttributeDescriptor attrdesc : ftype.getPrimaryKey()) { |
|
429 |
builder.and( |
|
430 |
builder.eq( |
|
431 |
builder.column(attrdesc.getName()), |
|
432 |
builder.constant(feature.get(attrdesc.getName())) |
|
433 |
) |
|
434 |
); |
|
425 | 435 |
} |
426 |
FeatureQuery query = store.createFeatureQuery(); |
|
427 |
query.setFilter(filter); |
|
428 |
query.retrievesAllAttributes(); |
|
429 |
JFeaturesForm form = dataSwingManager.createJFeaturesForm(store); |
|
430 |
form.setQuery(query); |
|
431 |
form.showForm(WindowManager.MODE.WINDOW); |
|
432 |
|
|
436 |
Expression filter = ExpressionUtils.createExpression(builder.toString()); |
|
437 |
return filter; |
|
433 | 438 |
} catch (Exception ex) { |
434 |
LOGGER.warn("Can't show form", ex); |
|
435 |
} finally { |
|
436 |
this.btnShowForm.setEnabled(true); |
|
439 |
LOGGER.warn("Can't build search for the selected feature.", ex); |
|
440 |
return null; |
|
437 | 441 |
} |
438 | 442 |
} |
439 | 443 |
|
444 |
public FeatureStore getStore() { |
|
445 |
return store; |
|
446 |
} |
|
447 |
|
|
440 | 448 |
@Override |
441 | 449 |
public ImageIcon loadImage(String imageName) { |
442 | 450 |
String name = FilenameUtils.getBaseName(imageName); |
... | ... | |
455 | 463 |
String[][] iconNames = new String[][]{ |
456 | 464 |
new String[]{"dalswing", "featurestore-column"}, |
457 | 465 |
new String[]{"dalswing", "featurestore-foreing-key"}, |
458 |
new String[]{"dalswing", "featurestore-table"} |
|
466 |
new String[]{"dalswing", "featurestore-table"}, |
|
467 |
new String[]{"dalswing", "search-action-showform"}, |
|
468 |
new String[]{"dalswing", "search-action-select"}, |
|
469 |
new String[]{"dalswing", "search-action-select-add"}, |
|
470 |
new String[]{"dalswing", "search-action-select-filter"} |
|
459 | 471 |
}; |
460 | 472 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
461 | 473 |
for (String[] icon : iconNames) { |
... | ... | |
464 | 476 |
} |
465 | 477 |
|
466 | 478 |
} |
467 |
}; |
|
479 |
|
|
480 |
@Override |
|
481 |
public int getSelectedFeatureCount() { |
|
482 |
return this.tblResults.getSelectedRowCount(); |
|
483 |
} |
|
484 |
|
|
485 |
} |
Also available in: Unified diff