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

History | View | Annotate | Download (8.95 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * 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
 *
11
 * 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
 *
16
 * 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
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
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
import org.gvsig.app.ApplicationLocator;
34
import org.gvsig.app.ApplicationManager;
35
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
import org.gvsig.app.project.documents.view.gui.IView;
39
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
import org.gvsig.i18n.Messages;
50
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
 *
55
 * @author Vicente Caballero Navarro
56
 */
57
public class SelectByAttributesExtension extends Extension implements ExpressionListener {
58

    
59
    protected FeatureStore featureStore = null;
60
    private String filterTitle;
61

    
62
    public void initialize() {
63
        IconThemeHelper.registerIcon("action", "selection-by-attributes", this);
64
    }
65

    
66
    public void execute(String actionCommand) {
67
        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
        if ("selection-by-attributes-layer".equalsIgnoreCase(actionCommand)) {
76
            FLayer layer = document.getMapContext().getLayers().getActives()[0];
77
            filterTitle = layer.getName();
78
            featureStore = ((FLyrVect) layer).getFeatureStore();
79
            document.setModified(true);
80
            doExecute();
81
        }
82
    }
83

    
84
    /**
85
     * "execute" method action.
86
     *
87
     */
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
        return true;
103
    }
104

    
105
    public boolean isVisible() {
106
        ApplicationManager application = ApplicationLocator.getManager();
107

    
108
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
109
        if (view == null) {
110
            return false;
111
        }
112
        ViewDocument document = view.getViewDocument();
113
        return document.getMapContext().hasActiveVectorLayers();
114
    }
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

    
131
                JOptionPane.showMessageDialog(
132
                        ApplicationLocator.getManager().getRootComponent(),
133
                        Messages.getText("expresion_error") + ":\n"
134
                        + getLastMessage(e),
135
                        Messages.getText("expresion_error"),
136
                        JOptionPane.ERROR_MESSAGE);
137
            } 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
                FeatureSelection oldSelection
190
                        = featureStore.getFeatureSelection();
191

    
192
                FeatureSelection newSelection
193
                        = featureStore.createFeatureSelection();
194
                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
     *
219
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
220
     * @param expression An string
221
     * @return A boolean value
222
     */
223
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
224

    
225
        if (expression == null) {
226
            return true;
227
        }
228

    
229
        String subExpression = expression.trim();
230

    
231
        if (subExpression.length() == 0) {
232
            return true;
233
        }
234

    
235
        int pos;
236

    
237
        // Remove last ';' if exists
238
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
239
            subExpression
240
                    = subExpression.substring(0, subExpression.length() - 1).trim();
241
        }
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
        // + 5 is the length of 'where'
250
        subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
251
        if (subExpression.length() == 0) {
252
            return true;
253
        } else {
254
            return false;
255
        }
256
    }
257

    
258
    private String getLastMessage(Throwable ex) {
259

    
260
        Throwable p = ex;
261
        while (p.getCause() != null && p.getCause() != p) {
262
            p = p.getCause();
263
        }
264
        return p.getMessage();
265
    }
266
}