Statistics
| Revision:

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 46089 omartinez
package org.gvsig.app.project.documents.view.dalactions;
2 44263 jjdelcerro
3
import java.awt.event.ActionEvent;
4
import javax.swing.AbstractAction;
5
import javax.swing.Action;
6 46089 omartinez
import org.gvsig.app.ApplicationLocator;
7
import org.gvsig.app.ApplicationManager;
8
import org.gvsig.app.project.documents.view.ViewDocument;
9 44263 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
10 44281 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
11 44263 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
12
import org.gvsig.fmap.dal.feature.FeatureStore;
13 44281 jjdelcerro
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory;
14 44263 jjdelcerro
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 44281 jjdelcerro
import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext;
20 46089 omartinez
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 44263 jjdelcerro
25
/**
26
 *
27
 * @author jjdelcerro
28
 */
29
@SuppressWarnings("UseSpecificCatch")
30 46089 omartinez
public class AddFilterToLayerAction
31 44263 jjdelcerro
        extends AbstractAction
32
    {
33
34 46089 omartinez
    public static class AddFilterToLayerActionFactory extends AbstractDALActionFactory {
35 44263 jjdelcerro
36 46089 omartinez
        public static final String ACTION_NAME = "AddFilterToLayerAction";
37 44281 jjdelcerro
38 46089 omartinez
        public AddFilterToLayerActionFactory() {
39 44281 jjdelcerro
            super(ACTION_NAME);
40 44263 jjdelcerro
        }
41
42
        @Override
43 44281 jjdelcerro
        public Action createAction(DALActionContext context) {
44 46089 omartinez
            return new AddFilterToLayerAction(context);
45 44263 jjdelcerro
        }
46 46089 omartinez
47
                public static void selfRegister() {
48
            DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager();
49
            dalSwingManager.registerStoreAction(new AddFilterToLayerAction.AddFilterToLayerActionFactory());
50
        }
51 44263 jjdelcerro
    }
52
53 46089 omartinez
    private static final Logger LOGGER = LoggerFactory.getLogger(AddFilterToLayerAction.class);
54 44263 jjdelcerro
55 44281 jjdelcerro
    private final DALActionContext context;
56 44263 jjdelcerro
57 46089 omartinez
    public AddFilterToLayerAction(DALActionContext context) {
58 44281 jjdelcerro
        this.context = context;
59 44263 jjdelcerro
        I18nManager i18n = ToolsLocator.getI18nManager();
60
        this.putValue(
61 44281 jjdelcerro
                Action.ACTION_COMMAND_KEY,
62 46089 omartinez
                AddFilterToLayerActionFactory.ACTION_NAME
63 44281 jjdelcerro
        );
64
        this.putValue(
65 44263 jjdelcerro
                Action.SHORT_DESCRIPTION,
66 46089 omartinez
                i18n.getTranslation("_Add_filter_to_layer")
67 44263 jjdelcerro
        );
68
        this.putValue(
69
                Action.SMALL_ICON,
70 46089 omartinez
                ToolsSwingLocator.getIconThemeManager().getCurrent().get("filter-by-attributes-layer")
71 44263 jjdelcerro
        );
72
    }
73 46089 omartinez
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 44263 jjdelcerro
92
    @Override
93
    public void actionPerformed(ActionEvent e) {
94
        try {
95 44281 jjdelcerro
            DataStore store = this.context.getStore();
96
            if( !(store instanceof FeatureStore) ) {
97
                return ;
98
            }
99
            FeatureStore featureStore = (FeatureStore) store;
100 46089 omartinez
                        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 44281 jjdelcerro
            }
129 46089 omartinez
130 44263 jjdelcerro
        } catch (Exception ex) {
131
            LOGGER.warn("Can't build selecction.", ex);
132
        }
133
    }
134
135
}