svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / dalactions / AddFilterToLayerAction.java @ 46089
History | View | Annotate | Download (4.79 KB)
1 |
package org.gvsig.app.project.documents.view.dalactions; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import javax.swing.AbstractAction; |
5 |
import javax.swing.Action; |
6 |
import org.gvsig.app.ApplicationLocator; |
7 |
import org.gvsig.app.ApplicationManager; |
8 |
import org.gvsig.app.project.documents.view.ViewDocument; |
9 |
import org.gvsig.expressionevaluator.Expression; |
10 |
import org.gvsig.fmap.dal.DataStore; |
11 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
12 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
13 |
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory; |
14 |
import org.gvsig.tools.ToolsLocator; |
15 |
import org.gvsig.tools.i18n.I18nManager; |
16 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
17 |
import org.slf4j.Logger; |
18 |
import org.slf4j.LoggerFactory; |
19 |
import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext; |
20 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
21 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
22 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
23 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
24 |
|
25 |
/**
|
26 |
*
|
27 |
* @author jjdelcerro
|
28 |
*/
|
29 |
@SuppressWarnings("UseSpecificCatch") |
30 |
public class AddFilterToLayerAction |
31 |
extends AbstractAction |
32 |
{ |
33 |
|
34 |
public static class AddFilterToLayerActionFactory extends AbstractDALActionFactory { |
35 |
|
36 |
public static final String ACTION_NAME = "AddFilterToLayerAction"; |
37 |
|
38 |
public AddFilterToLayerActionFactory() {
|
39 |
super(ACTION_NAME);
|
40 |
} |
41 |
|
42 |
@Override
|
43 |
public Action createAction(DALActionContext context) { |
44 |
return new AddFilterToLayerAction(context); |
45 |
} |
46 |
|
47 |
public static void selfRegister() { |
48 |
DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager(); |
49 |
dalSwingManager.registerStoreAction(new AddFilterToLayerAction.AddFilterToLayerActionFactory());
|
50 |
} |
51 |
} |
52 |
|
53 |
private static final Logger LOGGER = LoggerFactory.getLogger(AddFilterToLayerAction.class); |
54 |
|
55 |
private final DALActionContext context; |
56 |
|
57 |
public AddFilterToLayerAction(DALActionContext context) {
|
58 |
this.context = context;
|
59 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
60 |
this.putValue(
|
61 |
Action.ACTION_COMMAND_KEY,
|
62 |
AddFilterToLayerActionFactory.ACTION_NAME |
63 |
); |
64 |
this.putValue(
|
65 |
Action.SHORT_DESCRIPTION,
|
66 |
i18n.getTranslation("_Add_filter_to_layer")
|
67 |
); |
68 |
this.putValue(
|
69 |
Action.SMALL_ICON,
|
70 |
ToolsSwingLocator.getIconThemeManager().getCurrent().get("filter-by-attributes-layer")
|
71 |
); |
72 |
} |
73 |
|
74 |
private FLyrVect getFLyrVect(FeatureStore store) {
|
75 |
ApplicationManager aplication = ApplicationLocator.getApplicationManager(); |
76 |
FLayer layer = aplication.getCurrentLayer(); |
77 |
if(layer instanceof FLyrVect && ((FLyrVect)layer).getFeatureStore()==store) { |
78 |
return (FLyrVect) layer;
|
79 |
} |
80 |
ViewDocument viewDoc = (ViewDocument) aplication.getActiveDocument(ViewDocument.class); |
81 |
if (viewDoc==null) { |
82 |
return null; |
83 |
} |
84 |
for (FLayer layer1 : viewDoc.getMapContext().getLayers().getLayers(null)) { |
85 |
if (layer1 instanceof FLyrVect && ((FLyrVect)layer1).getFeatureStore()==store) { |
86 |
return (FLyrVect) layer1;
|
87 |
} |
88 |
} |
89 |
return null; |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public void actionPerformed(ActionEvent e) { |
94 |
try {
|
95 |
DataStore store = this.context.getStore();
|
96 |
if( !(store instanceof FeatureStore) ) { |
97 |
return ;
|
98 |
} |
99 |
FeatureStore featureStore = (FeatureStore) store; |
100 |
FLyrVect layer = getFLyrVect(featureStore); |
101 |
|
102 |
FeatureQuery query = this.context.getQuery();
|
103 |
FeatureQuery layerQuery = null;
|
104 |
if (query != null){ |
105 |
Expression expFilter = query.getExpressionFilter();
|
106 |
if (expFilter != null && !expFilter.isEmpty()){ |
107 |
if( layerQuery==null ) { |
108 |
layerQuery = featureStore.createFeatureQuery(); |
109 |
} |
110 |
layerQuery.addFilter(expFilter); |
111 |
layerQuery.retrievesAllAttributes(); |
112 |
} |
113 |
if( query.hasOrder() ) {
|
114 |
if( layerQuery==null ) { |
115 |
layerQuery = featureStore.createFeatureQuery(); |
116 |
} |
117 |
layerQuery.getOrder().copyFrom(query.getOrder()); |
118 |
} |
119 |
if( layerQuery==null ) { |
120 |
// # Si el usuario pulso en OK sin haber introducido un filtro quitamos
|
121 |
// # el filtro que pudiese tener la capa.
|
122 |
layer.setBaseQuery(null);
|
123 |
} else {
|
124 |
layer.setBaseQuery(layerQuery); |
125 |
} |
126 |
// # Por ultimo forzamos un repintado de la vista.
|
127 |
layer.getMapContext().invalidate(); |
128 |
} |
129 |
|
130 |
} catch (Exception ex) { |
131 |
LOGGER.warn("Can't build selecction.", ex);
|
132 |
} |
133 |
} |
134 |
|
135 |
} |