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 / extension / SelectByAttributesExtension.java @ 44011

History | View | Annotate | Download (9.64 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
6 41248 jjdelcerro
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 40435 jjdelcerro
 *
11 41248 jjdelcerro
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 40435 jjdelcerro
 *
16 41248 jjdelcerro
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 40435 jjdelcerro
 *
20 41248 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40435 jjdelcerro
 */
23
package org.gvsig.app.extension;
24
25
import java.util.Iterator;
26
27
import javax.swing.JOptionPane;
28
29
import org.gvsig.andami.IconThemeHelper;
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.messages.NotificationManager;
32
import org.gvsig.andami.plugins.Extension;
33 40955 jldominguez
import org.gvsig.app.ApplicationLocator;
34 41248 jjdelcerro
import org.gvsig.app.ApplicationManager;
35 40435 jjdelcerro
import org.gvsig.app.gui.filter.ExpressionListener;
36
import org.gvsig.app.gui.filter.FilterDialog;
37
import org.gvsig.app.project.documents.view.ViewDocument;
38 41264 jjdelcerro
import org.gvsig.app.project.documents.view.gui.IView;
39 40435 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
40
import org.gvsig.fmap.dal.DataManager;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FeatureQuery;
44
import org.gvsig.fmap.dal.feature.FeatureSelection;
45
import org.gvsig.fmap.dal.feature.FeatureSet;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.mapcontext.layers.FLayer;
48
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
49 40955 jldominguez
import org.gvsig.i18n.Messages;
50 43987 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
51 40435 jjdelcerro
import org.gvsig.utils.exceptionHandling.ExceptionListener;
52
53
/**
54
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
55 41248 jjdelcerro
 *
56 40435 jjdelcerro
 * @author Vicente Caballero Navarro
57
 */
58 43987 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
59 41641 jjdelcerro
public class SelectByAttributesExtension extends Extension implements ExpressionListener {
60 40435 jjdelcerro
61
    protected FeatureStore featureStore = null;
62
    private String filterTitle;
63
64 43987 jjdelcerro
    @Override
65 40435 jjdelcerro
    public void initialize() {
66 41641 jjdelcerro
        IconThemeHelper.registerIcon("action", "selection-by-attributes", this);
67 40435 jjdelcerro
    }
68
69 43987 jjdelcerro
    @Override
70 40435 jjdelcerro
    public void execute(String actionCommand) {
71 41264 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
72
73
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
74
        if (view == null) {
75
            return;
76
        }
77 44011 jjdelcerro
        if ("selection-by-attributes-layer".equals(actionCommand)) {
78 43987 jjdelcerro
            ViewDocument document = view.getViewDocument();
79
80 41248 jjdelcerro
            FLayer layer = document.getMapContext().getLayers().getActives()[0];
81 41744 jjdelcerro
            filterTitle = layer.getName();
82 41248 jjdelcerro
            featureStore = ((FLyrVect) layer).getFeatureStore();
83
            document.setModified(true);
84 44011 jjdelcerro
            SelectByAttributes selector = new SelectByAttributes();
85
            selector.showWindow(filterTitle, featureStore, WindowManager.MODE.TOOL);
86 43987 jjdelcerro
87 44011 jjdelcerro
88
        } else if ("selection-by-attributes-layer-old".equals(actionCommand)) {
89 43987 jjdelcerro
            ViewDocument document = view.getViewDocument();
90
91
            FLayer layer = document.getMapContext().getLayers().getActives()[0];
92
            filterTitle = layer.getName();
93
            featureStore = ((FLyrVect) layer).getFeatureStore();
94
            document.setModified(true);
95 40435 jjdelcerro
            doExecute();
96
        }
97
    }
98
99
    /**
100
     * "execute" method action.
101 41248 jjdelcerro
     *
102 40435 jjdelcerro
     */
103
    protected void doExecute() {
104
        FilterDialog dlg = new FilterDialog(filterTitle);
105
        dlg.addExpressionListener(this);
106
        dlg.addExceptionListener(new ExceptionListener() {
107
108 43987 jjdelcerro
            @Override
109 40435 jjdelcerro
            public void exceptionThrown(Throwable t) {
110
                NotificationManager.addError(t.getMessage(), t);
111
            }
112
        });
113
        dlg.setModel(featureStore);
114
        PluginServices.getMDIManager().addWindow(dlg);
115
    }
116
117 43987 jjdelcerro
    @Override
118 40435 jjdelcerro
    public boolean isEnabled() {
119 41248 jjdelcerro
        return true;
120 40435 jjdelcerro
    }
121
122 43987 jjdelcerro
    @Override
123 40435 jjdelcerro
    public boolean isVisible() {
124 41248 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
125 40435 jjdelcerro
126 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
127
        if (view == null) {
128 40435 jjdelcerro
            return false;
129
        }
130 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
131 41248 jjdelcerro
        return document.getMapContext().hasActiveVectorLayers();
132 40435 jjdelcerro
    }
133
134
    // By Pablo: if no filter expression -> no element selected
135 43987 jjdelcerro
    @Override
136 40435 jjdelcerro
    public void newSet(String expression) throws DataException {
137
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
138
            FeatureSet set = null;
139
            try {
140
                set = doSet(expression);
141
142
                if (set == null) {
143
                    // throw new RuntimeException("Not a 'where' clause?");
144
                    return;
145
                }
146
                featureStore.setSelection(set);
147
148
            } catch (Exception e) {
149 41248 jjdelcerro
150 40955 jldominguez
                JOptionPane.showMessageDialog(
151 41248 jjdelcerro
                        ApplicationLocator.getManager().getRootComponent(),
152
                        Messages.getText("expresion_error") + ":\n"
153 40955 jldominguez
                        + getLastMessage(e),
154 41248 jjdelcerro
                        Messages.getText("expresion_error"),
155
                        JOptionPane.ERROR_MESSAGE);
156 40435 jjdelcerro
            } finally {
157
                if (set != null) {
158
                    set.dispose();
159
                }
160
            }
161
        } else {
162
            // By Pablo: if no expression -> no element selected
163
            featureStore.getFeatureSelection().deselectAll();
164
        }
165
    }
166
167
    private FeatureSet doSet(String expression) throws DataException {
168
        FeatureQuery query = featureStore.createFeatureQuery();
169
        DataManager manager = DALLocator.getDataManager();
170
        query.setFilter(manager.createExpresion(expression));
171
        return featureStore.getFeatureSet(query);
172
    }
173
174 43987 jjdelcerro
    @Override
175 40435 jjdelcerro
    public void addToSet(String expression) throws DataException {
176
        // By Pablo: if no filter expression -> don't add more elements to set
177
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
178
            FeatureSet set = null;
179
            try {
180
                set = doSet(expression);
181
182
                if (set == null) {
183
                    // throw new RuntimeException("Not a 'where' clause?");
184
                    return;
185
                }
186
                featureStore.getFeatureSelection().select(set);
187
            } finally {
188
                if (set != null) {
189
                    set.dispose();
190
                }
191
            }
192
        }
193
    }
194
195 43987 jjdelcerro
    @Override
196 40435 jjdelcerro
    public void fromSet(String expression) throws DataException {
197
        // By Pablo: if no filter expression -> no element selected
198
        try {
199
            if (!this.filterExpressionFromWhereIsEmpty(expression)) {
200
                // NotificationManager.showMessageInfo("Falta por implementar",
201
                // null);
202
203 43987 jjdelcerro
                FeatureSet set;
204 40435 jjdelcerro
                set = doSet(expression);
205
206
                if (set == null) {
207
                    throw new RuntimeException("Not a 'where' clause?");
208
                }
209
210 41248 jjdelcerro
                FeatureSelection oldSelection
211
                        = featureStore.getFeatureSelection();
212 40435 jjdelcerro
213 41248 jjdelcerro
                FeatureSelection newSelection
214
                        = featureStore.createFeatureSelection();
215 40435 jjdelcerro
                Iterator iterator = set.iterator();
216
                while (iterator.hasNext()) {
217
                    Feature feature = (Feature) iterator.next();
218
                    if (oldSelection.isSelected(feature)) {
219
                        newSelection.select(feature);
220
                    }
221
                }
222
                featureStore.setSelection(newSelection);
223
                set.dispose();
224
225
            } else {
226
                // By Pablo: if no expression -> no element selected
227
                featureStore.getFeatureSelection().deselectAll();
228
            }
229
        } catch (DataException e) {
230
            NotificationManager.addError(e);
231
        }
232
233
    }
234
235
    /**
236
     * Returns true if the WHERE subconsultation of the filterExpression is
237
     * empty ("")
238 41248 jjdelcerro
     *
239 40435 jjdelcerro
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
240 41248 jjdelcerro
     * @param expression An string
241 40435 jjdelcerro
     * @return A boolean value
242
     */
243
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
244 41248 jjdelcerro
245 40435 jjdelcerro
        if (expression == null) {
246
            return true;
247
        }
248 41248 jjdelcerro
249 40435 jjdelcerro
        String subExpression = expression.trim();
250 41248 jjdelcerro
251 40435 jjdelcerro
        if (subExpression.length() == 0) {
252
            return true;
253
        }
254 41248 jjdelcerro
255 40435 jjdelcerro
        int pos;
256
257
        // Remove last ';' if exists
258
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
259 41248 jjdelcerro
            subExpression
260
                    = subExpression.substring(0, subExpression.length() - 1).trim();
261 40435 jjdelcerro
        }
262
263
        // If there is no 'where' clause
264
        if ((pos = subExpression.indexOf("where")) == -1) {
265
            return false;
266
        }
267
268
        // If there is no subexpression in the WHERE clause -> true
269 41248 jjdelcerro
        // + 5 is the length of 'where'
270
        subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
271 40435 jjdelcerro
        if (subExpression.length() == 0) {
272
            return true;
273
        } else {
274
            return false;
275
        }
276
    }
277 41248 jjdelcerro
278 40955 jldominguez
    private String getLastMessage(Throwable ex) {
279 41248 jjdelcerro
280 40955 jldominguez
        Throwable p = ex;
281
        while (p.getCause() != null && p.getCause() != p) {
282
            p = p.getCause();
283
        }
284
        return p.getMessage();
285 41248 jjdelcerro
    }
286
}