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

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