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 @ 47426
History | View | Annotate | Download (4.62 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.FeatureReference; |
12 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
13 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
14 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
15 |
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory; |
16 |
import org.gvsig.tools.ToolsLocator; |
17 |
import org.gvsig.tools.i18n.I18nManager; |
18 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
19 |
import org.slf4j.Logger; |
20 |
import org.slf4j.LoggerFactory; |
21 |
import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext; |
22 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
23 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
24 |
|
25 |
/**
|
26 |
*
|
27 |
* @author jjdelcerro
|
28 |
*/
|
29 |
@SuppressWarnings("UseSpecificCatch") |
30 |
public class SelectionFilterAction |
31 |
extends AbstractAction |
32 |
{ |
33 |
|
34 |
public static class SelectionFilterActionFactory extends AbstractDALActionFactory { |
35 |
|
36 |
public static final String ACTION_NAME = "SelectionFilter"; |
37 |
|
38 |
public SelectionFilterActionFactory() {
|
39 |
super(ACTION_NAME);
|
40 |
} |
41 |
|
42 |
@Override
|
43 |
public Action createAction(DALActionContext context) { |
44 |
return new SelectionFilterAction(context); |
45 |
} |
46 |
|
47 |
public static void selfRegister() { |
48 |
DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager(); |
49 |
dalSwingManager.registerStoreAction(new SelectionFilterActionFactory());
|
50 |
} |
51 |
} |
52 |
|
53 |
private static final Logger LOGGER = LoggerFactory.getLogger(SelectionFilterAction.class); |
54 |
|
55 |
private final DALActionContext context; |
56 |
|
57 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
58 |
public SelectionFilterAction(DALActionContext context) {
|
59 |
this.context = context;
|
60 |
ImageIcon icon = ToolsSwingLocator.getIconThemeManager().getCurrent().get("storeaction-select-filter"); |
61 |
if( icon == null ) { |
62 |
LOGGER.warn("Can't locate icon 'search-action-select-filter'.");
|
63 |
} |
64 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
65 |
this.putValue(
|
66 |
Action.ACTION_COMMAND_KEY,
|
67 |
SelectionFilterActionFactory.ACTION_NAME |
68 |
); |
69 |
this.putValue(
|
70 |
Action.SHORT_DESCRIPTION,
|
71 |
i18n.getTranslation("_Filter_selection")
|
72 |
); |
73 |
this.putValue(
|
74 |
Action.SMALL_ICON,
|
75 |
icon |
76 |
); |
77 |
} |
78 |
|
79 |
@Override
|
80 |
public void actionPerformed(ActionEvent e) { |
81 |
try {
|
82 |
DataStore store = this.context.getStore();
|
83 |
if( !(store instanceof FeatureStore) ) { |
84 |
return ;
|
85 |
} |
86 |
FeatureStore featureStore = (FeatureStore) store; |
87 |
FeatureSelection contextSelection = this.context.getSelecteds();
|
88 |
if (contextSelection == null) { |
89 |
Expression filter;
|
90 |
FeatureQuery query = null;
|
91 |
FeatureQuery contextQuery = this.context.getQuery();
|
92 |
if (contextQuery != null) { |
93 |
filter = contextQuery.getExpressionFilter(); |
94 |
if (filter != null) { |
95 |
query = featureStore.createFeatureQuery(); |
96 |
query.setFilter(filter); |
97 |
} |
98 |
} |
99 |
contextSelection = featureStore.getFeatureSelection(); |
100 |
if( query == null ) { |
101 |
return;
|
102 |
} |
103 |
FeatureSet set = featureStore.getFeatureSet(query); |
104 |
FeatureSelection newSelection = featureStore.createFeatureSelection(); |
105 |
for (Feature feature : set) {
|
106 |
if( contextSelection.isSelected(feature) ) {
|
107 |
newSelection.select(feature); |
108 |
} |
109 |
} |
110 |
store.setSelection(newSelection); |
111 |
} else {
|
112 |
FeatureSelection newSelection = featureStore.createFeatureSelection(); |
113 |
FeatureSelection currentSelection = featureStore.getFeatureSelection(); |
114 |
for (FeatureReference ref : contextSelection.referenceIterable()) {
|
115 |
if( currentSelection.isSelected(ref) ) {
|
116 |
newSelection.select(ref); |
117 |
} |
118 |
} |
119 |
store.setSelection(newSelection); |
120 |
} |
121 |
|
122 |
} catch (Exception ex) { |
123 |
LOGGER.warn("Can't build selecction.", ex);
|
124 |
} |
125 |
} |
126 |
|
127 |
} |