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 |
package org.gvsig.fmap.dal.swing.impl.actions; |
---|---|
2 |
|
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 |
import org.gvsig.fmap.dal.DataStore; |
9 |
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 |
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory; |
15 |
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 |
import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext; |
21 |
|
22 |
/**
|
23 |
*
|
24 |
* @author jjdelcerro
|
25 |
*/
|
26 |
@SuppressWarnings("UseSpecificCatch") |
27 |
public class SelectionFilterAction |
28 |
extends AbstractAction |
29 |
{ |
30 |
|
31 |
public static class SelectionFilterActionFactory extends AbstractDALActionFactory { |
32 |
|
33 |
public static final String ACTION_NAME = "SelectionFilter"; |
34 |
|
35 |
public SelectionFilterActionFactory() {
|
36 |
super(ACTION_NAME);
|
37 |
} |
38 |
|
39 |
@Override
|
40 |
public Action createAction(DALActionContext context) { |
41 |
return new SelectionFilterAction(context); |
42 |
} |
43 |
|
44 |
} |
45 |
|
46 |
private static final Logger LOGGER = LoggerFactory.getLogger(SelectionFilterAction.class); |
47 |
|
48 |
private final DALActionContext context; |
49 |
|
50 |
public SelectionFilterAction(DALActionContext context) {
|
51 |
this.context = context;
|
52 |
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 |
Action.ACTION_COMMAND_KEY,
|
59 |
SelectionFilterActionFactory.ACTION_NAME |
60 |
); |
61 |
this.putValue(
|
62 |
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 |
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 |
FeatureQuery query = this.context.getQuery();
|
84 |
if( query != null ) { |
85 |
filter = query.getExpressionFilter(); |
86 |
} |
87 |
} |
88 |
FeatureQuery query = featureStore.createFeatureQuery(); |
89 |
query.setFilter(filter); |
90 |
FeatureSet set = featureStore.getFeatureSet(query); |
91 |
FeatureSelection newSelection = featureStore.createFeatureSelection(); |
92 |
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 |
} |