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 / actions / SelectionFilterAction.java @ 46723
History | View | Annotate | Download (3.45 KB)
1 | 44281 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.actions; |
---|---|---|---|
2 | 44263 | jjdelcerro | |
3 | import java.awt.event.ActionEvent; |
||
4 | import javax.swing.AbstractAction; |
||
5 | import javax.swing.Action; |
||
6 | import javax.swing.ImageIcon; |
||
7 | import org.gvsig.expressionevaluator.Expression; |
||
8 | 44281 | jjdelcerro | import org.gvsig.fmap.dal.DataStore; |
9 | 44263 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
10 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
11 | import org.gvsig.fmap.dal.feature.FeatureSelection; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
13 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
14 | 44281 | jjdelcerro | import org.gvsig.fmap.dal.swing.AbstractDALActionFactory; |
15 | 44263 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
16 | import org.gvsig.tools.i18n.I18nManager; |
||
17 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
18 | import org.slf4j.Logger; |
||
19 | import org.slf4j.LoggerFactory; |
||
20 | 44281 | jjdelcerro | import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext; |
21 | 44263 | jjdelcerro | |
22 | /**
|
||
23 | *
|
||
24 | * @author jjdelcerro
|
||
25 | */
|
||
26 | @SuppressWarnings("UseSpecificCatch") |
||
27 | public class SelectionFilterAction |
||
28 | extends AbstractAction |
||
29 | { |
||
30 | |||
31 | 44281 | jjdelcerro | public static class SelectionFilterActionFactory extends AbstractDALActionFactory { |
32 | 44263 | jjdelcerro | |
33 | 44281 | jjdelcerro | public static final String ACTION_NAME = "SelectionFilter"; |
34 | |||
35 | public SelectionFilterActionFactory() {
|
||
36 | super(ACTION_NAME);
|
||
37 | 44263 | jjdelcerro | } |
38 | |||
39 | @Override
|
||
40 | 44281 | jjdelcerro | public Action createAction(DALActionContext context) { |
41 | return new SelectionFilterAction(context); |
||
42 | 44263 | jjdelcerro | } |
43 | |||
44 | } |
||
45 | |||
46 | private static final Logger LOGGER = LoggerFactory.getLogger(SelectionFilterAction.class); |
||
47 | |||
48 | 44281 | jjdelcerro | private final DALActionContext context; |
49 | 44263 | jjdelcerro | |
50 | 44281 | jjdelcerro | public SelectionFilterAction(DALActionContext context) {
|
51 | this.context = context;
|
||
52 | 44263 | jjdelcerro | ImageIcon icon = ToolsSwingLocator.getIconThemeManager().getCurrent().get("search-action-select-filter"); |
53 | if( icon == null ) { |
||
54 | LOGGER.warn("Can't locate icon 'search-action-select-filter'.");
|
||
55 | } |
||
56 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
57 | this.putValue(
|
||
58 | 44281 | jjdelcerro | Action.ACTION_COMMAND_KEY,
|
59 | SelectionFilterActionFactory.ACTION_NAME |
||
60 | ); |
||
61 | this.putValue(
|
||
62 | 44263 | jjdelcerro | Action.SHORT_DESCRIPTION,
|
63 | i18n.getTranslation("_Filter_selection")
|
||
64 | ); |
||
65 | this.putValue(
|
||
66 | Action.SMALL_ICON,
|
||
67 | icon |
||
68 | ); |
||
69 | } |
||
70 | |||
71 | @Override
|
||
72 | public void actionPerformed(ActionEvent e) { |
||
73 | try {
|
||
74 | 44281 | jjdelcerro | DataStore store = this.context.getStore();
|
75 | if( !(store instanceof FeatureStore) ) { |
||
76 | return ;
|
||
77 | } |
||
78 | FeatureStore featureStore = (FeatureStore) store; |
||
79 | FeatureSelection currentSelection = featureStore.getFeatureSelection(); |
||
80 | |||
81 | Expression filter = this.context.getFilterForSelecteds(); |
||
82 | if( filter == null ) { |
||
83 | 44712 | jjdelcerro | FeatureQuery query = this.context.getQuery();
|
84 | if( query != null ) { |
||
85 | filter = query.getExpressionFilter(); |
||
86 | } |
||
87 | 44281 | jjdelcerro | } |
88 | FeatureQuery query = featureStore.createFeatureQuery(); |
||
89 | 44263 | jjdelcerro | query.setFilter(filter); |
90 | 44281 | jjdelcerro | FeatureSet set = featureStore.getFeatureSet(query); |
91 | FeatureSelection newSelection = featureStore.createFeatureSelection(); |
||
92 | 44263 | jjdelcerro | for (Feature feature : set) {
|
93 | if( currentSelection.isSelected(feature) ) {
|
||
94 | newSelection.select(feature); |
||
95 | } |
||
96 | } |
||
97 | store.setSelection(newSelection); |
||
98 | } catch (Exception ex) { |
||
99 | LOGGER.warn("Can't build selecction.", ex);
|
||
100 | } |
||
101 | } |
||
102 | |||
103 | } |