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 @ 44531

History | View | Annotate | Download (10.6 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 44437 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
48 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
49
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
50 40955 jldominguez
import org.gvsig.i18n.Messages;
51 43987 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
52 40435 jjdelcerro
import org.gvsig.utils.exceptionHandling.ExceptionListener;
53
54
/**
55
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
56 41248 jjdelcerro
 *
57 40435 jjdelcerro
 * @author Vicente Caballero Navarro
58
 */
59 43987 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
60 41641 jjdelcerro
public class SelectByAttributesExtension extends Extension implements ExpressionListener {
61 40435 jjdelcerro
62
    protected FeatureStore featureStore = null;
63
    private String filterTitle;
64
65 43987 jjdelcerro
    @Override
66 40435 jjdelcerro
    public void initialize() {
67 41641 jjdelcerro
        IconThemeHelper.registerIcon("action", "selection-by-attributes", this);
68 40435 jjdelcerro
    }
69
70 43987 jjdelcerro
    @Override
71 40435 jjdelcerro
    public void execute(String actionCommand) {
72 41264 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
73
74
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
75
        if (view == null) {
76
            return;
77
        }
78 44011 jjdelcerro
        if ("selection-by-attributes-layer".equals(actionCommand)) {
79 43987 jjdelcerro
            ViewDocument document = view.getViewDocument();
80
81 41248 jjdelcerro
            FLayer layer = document.getMapContext().getLayers().getActives()[0];
82 41744 jjdelcerro
            filterTitle = layer.getName();
83 41248 jjdelcerro
            featureStore = ((FLyrVect) layer).getFeatureStore();
84
            document.setModified(true);
85 44011 jjdelcerro
            SelectByAttributes selector = new SelectByAttributes();
86
            selector.showWindow(filterTitle, featureStore, WindowManager.MODE.TOOL);
87 43987 jjdelcerro
88 44011 jjdelcerro
89
        } else if ("selection-by-attributes-layer-old".equals(actionCommand)) {
90 43987 jjdelcerro
            ViewDocument document = view.getViewDocument();
91
92
            FLayer layer = document.getMapContext().getLayers().getActives()[0];
93
            filterTitle = layer.getName();
94
            featureStore = ((FLyrVect) layer).getFeatureStore();
95
            document.setModified(true);
96 40435 jjdelcerro
            doExecute();
97
        }
98
    }
99
100
    /**
101
     * "execute" method action.
102 41248 jjdelcerro
     *
103 40435 jjdelcerro
     */
104
    protected void doExecute() {
105
        FilterDialog dlg = new FilterDialog(filterTitle);
106
        dlg.addExpressionListener(this);
107
        dlg.addExceptionListener(new ExceptionListener() {
108
109 43987 jjdelcerro
            @Override
110 40435 jjdelcerro
            public void exceptionThrown(Throwable t) {
111
                NotificationManager.addError(t.getMessage(), t);
112
            }
113
        });
114
        dlg.setModel(featureStore);
115
        PluginServices.getMDIManager().addWindow(dlg);
116
    }
117
118 43987 jjdelcerro
    @Override
119 40435 jjdelcerro
    public boolean isEnabled() {
120 44437 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
121
122
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
123
        if (view == null) {
124
            return false;
125
        }
126
        ViewDocument document = view.getViewDocument();
127
        if( document == null ) {
128
            return false;
129
        }
130
        boolean hasActiveVectorLayers = false;
131
        for (FLayer layer : document.getMapContext().getLayers()) {
132
            if( layer.isActive() && layer.isAvailable() && layer instanceof FLyrVect ) {
133
                try {
134
                    hasActiveVectorLayers = true;
135
                    FeatureStore store = ((FLyrVect)layer).getFeatureStore();
136
                    if( !store.getFeatureSelection().isAvailable() ) {
137
                        return false;
138
                    }
139
                } catch (Exception ex) {
140
                }
141
            }
142
        }
143
        return hasActiveVectorLayers;
144 40435 jjdelcerro
    }
145
146 43987 jjdelcerro
    @Override
147 40435 jjdelcerro
    public boolean isVisible() {
148 41248 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
149 40435 jjdelcerro
150 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
151
        if (view == null) {
152 40435 jjdelcerro
            return false;
153
        }
154 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
155 41248 jjdelcerro
        return document.getMapContext().hasActiveVectorLayers();
156 40435 jjdelcerro
    }
157
158
    // By Pablo: if no filter expression -> no element selected
159 43987 jjdelcerro
    @Override
160 40435 jjdelcerro
    public void newSet(String expression) throws DataException {
161
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
162
            FeatureSet set = null;
163
            try {
164
                set = doSet(expression);
165
166
                if (set == null) {
167
                    // throw new RuntimeException("Not a 'where' clause?");
168
                    return;
169
                }
170
                featureStore.setSelection(set);
171
172
            } catch (Exception e) {
173 41248 jjdelcerro
174 40955 jldominguez
                JOptionPane.showMessageDialog(
175 41248 jjdelcerro
                        ApplicationLocator.getManager().getRootComponent(),
176
                        Messages.getText("expresion_error") + ":\n"
177 40955 jldominguez
                        + getLastMessage(e),
178 41248 jjdelcerro
                        Messages.getText("expresion_error"),
179
                        JOptionPane.ERROR_MESSAGE);
180 40435 jjdelcerro
            } finally {
181
                if (set != null) {
182
                    set.dispose();
183
                }
184
            }
185
        } else {
186
            // By Pablo: if no expression -> no element selected
187
            featureStore.getFeatureSelection().deselectAll();
188
        }
189
    }
190
191
    private FeatureSet doSet(String expression) throws DataException {
192
        FeatureQuery query = featureStore.createFeatureQuery();
193
        DataManager manager = DALLocator.getDataManager();
194
        query.setFilter(manager.createExpresion(expression));
195
        return featureStore.getFeatureSet(query);
196
    }
197
198 43987 jjdelcerro
    @Override
199 40435 jjdelcerro
    public void addToSet(String expression) throws DataException {
200
        // By Pablo: if no filter expression -> don't add more elements to set
201
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
202
            FeatureSet set = null;
203
            try {
204
                set = doSet(expression);
205
206
                if (set == null) {
207
                    // throw new RuntimeException("Not a 'where' clause?");
208
                    return;
209
                }
210
                featureStore.getFeatureSelection().select(set);
211
            } finally {
212
                if (set != null) {
213
                    set.dispose();
214
                }
215
            }
216
        }
217
    }
218
219 43987 jjdelcerro
    @Override
220 40435 jjdelcerro
    public void fromSet(String expression) throws DataException {
221
        // By Pablo: if no filter expression -> no element selected
222
        try {
223
            if (!this.filterExpressionFromWhereIsEmpty(expression)) {
224
                // NotificationManager.showMessageInfo("Falta por implementar",
225
                // null);
226
227 43987 jjdelcerro
                FeatureSet set;
228 40435 jjdelcerro
                set = doSet(expression);
229
230
                if (set == null) {
231
                    throw new RuntimeException("Not a 'where' clause?");
232
                }
233
234 41248 jjdelcerro
                FeatureSelection oldSelection
235
                        = featureStore.getFeatureSelection();
236 40435 jjdelcerro
237 41248 jjdelcerro
                FeatureSelection newSelection
238
                        = featureStore.createFeatureSelection();
239 40435 jjdelcerro
                Iterator iterator = set.iterator();
240
                while (iterator.hasNext()) {
241
                    Feature feature = (Feature) iterator.next();
242
                    if (oldSelection.isSelected(feature)) {
243
                        newSelection.select(feature);
244
                    }
245
                }
246
                featureStore.setSelection(newSelection);
247
                set.dispose();
248
249
            } else {
250
                // By Pablo: if no expression -> no element selected
251
                featureStore.getFeatureSelection().deselectAll();
252
            }
253
        } catch (DataException e) {
254
            NotificationManager.addError(e);
255
        }
256
257
    }
258
259
    /**
260
     * Returns true if the WHERE subconsultation of the filterExpression is
261
     * empty ("")
262 41248 jjdelcerro
     *
263 40435 jjdelcerro
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
264 41248 jjdelcerro
     * @param expression An string
265 40435 jjdelcerro
     * @return A boolean value
266
     */
267
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
268 41248 jjdelcerro
269 40435 jjdelcerro
        if (expression == null) {
270
            return true;
271
        }
272 41248 jjdelcerro
273 40435 jjdelcerro
        String subExpression = expression.trim();
274 41248 jjdelcerro
275 40435 jjdelcerro
        if (subExpression.length() == 0) {
276
            return true;
277
        }
278 41248 jjdelcerro
279 40435 jjdelcerro
        int pos;
280
281
        // Remove last ';' if exists
282
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
283 41248 jjdelcerro
            subExpression
284
                    = subExpression.substring(0, subExpression.length() - 1).trim();
285 40435 jjdelcerro
        }
286
287
        // If there is no 'where' clause
288
        if ((pos = subExpression.indexOf("where")) == -1) {
289
            return false;
290
        }
291
292
        // If there is no subexpression in the WHERE clause -> true
293 41248 jjdelcerro
        // + 5 is the length of 'where'
294
        subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
295 40435 jjdelcerro
        if (subExpression.length() == 0) {
296
            return true;
297
        } else {
298
            return false;
299
        }
300
    }
301 41248 jjdelcerro
302 40955 jldominguez
    private String getLastMessage(Throwable ex) {
303 41248 jjdelcerro
304 40955 jldominguez
        Throwable p = ex;
305
        while (p.getCause() != null && p.getCause() != p) {
306
            p = p.getCause();
307
        }
308
        return p.getMessage();
309 41248 jjdelcerro
    }
310
}