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 / FiltroExtension.java @ 41258

History | View | Annotate | Download (8.8 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.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataManager;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.feature.Feature;
42
import org.gvsig.fmap.dal.feature.FeatureQuery;
43
import org.gvsig.fmap.dal.feature.FeatureSelection;
44
import org.gvsig.fmap.dal.feature.FeatureSet;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46
import org.gvsig.fmap.mapcontext.layers.FLayer;
47
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
48
import org.gvsig.i18n.Messages;
49
import org.gvsig.utils.exceptionHandling.ExceptionListener;
50

    
51
/**
52
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
53
 *
54
 * @author Vicente Caballero Navarro
55
 */
56
public class FiltroExtension extends Extension implements ExpressionListener {
57

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

    
61
    public void initialize() {
62
        IconThemeHelper.registerIcon("action", "layer-filter", this);
63
    }
64

    
65
    public void execute(String actionCommand) {
66
        if ("layer-filter".equalsIgnoreCase(actionCommand)) {
67
            ApplicationManager application = ApplicationLocator.getManager();
68

    
69
            ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class);
70
            if (document == null) {
71
                return;
72
            }
73
            filterTitle = document.getName();
74
            FLayer layer = document.getMapContext().getLayers().getActives()[0];
75
            featureStore = ((FLyrVect) layer).getFeatureStore();
76
            document.setModified(true);
77
            doExecute();
78
        }
79
    }
80

    
81
    /**
82
     * "execute" method action.
83
     *
84
     */
85
    protected void doExecute() {
86
        FilterDialog dlg = new FilterDialog(filterTitle);
87
        dlg.addExpressionListener(this);
88
        dlg.addExceptionListener(new ExceptionListener() {
89

    
90
            public void exceptionThrown(Throwable t) {
91
                NotificationManager.addError(t.getMessage(), t);
92
            }
93
        });
94
        dlg.setModel(featureStore);
95
        PluginServices.getMDIManager().addWindow(dlg);
96
    }
97

    
98
    public boolean isEnabled() {
99
        return true;
100
    }
101

    
102
    public boolean isVisible() {
103
        ApplicationManager application = ApplicationLocator.getManager();
104

    
105
        ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class);
106
        if (document == null) {
107
            return false;
108
        }
109
        return document.getMapContext().hasActiveVectorLayers();
110
    }
111

    
112
    // By Pablo: if no filter expression -> no element selected
113
    public void newSet(String expression) throws DataException {
114
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
115
            FeatureSet set = null;
116
            try {
117
                set = doSet(expression);
118

    
119
                if (set == null) {
120
                    // throw new RuntimeException("Not a 'where' clause?");
121
                    return;
122
                }
123
                featureStore.setSelection(set);
124

    
125
            } catch (Exception e) {
126

    
127
                JOptionPane.showMessageDialog(
128
                        ApplicationLocator.getManager().getRootComponent(),
129
                        Messages.getText("expresion_error") + ":\n"
130
                        + getLastMessage(e),
131
                        Messages.getText("expresion_error"),
132
                        JOptionPane.ERROR_MESSAGE);
133
            } finally {
134
                if (set != null) {
135
                    set.dispose();
136
                }
137
            }
138
        } else {
139
            // By Pablo: if no expression -> no element selected
140
            featureStore.getFeatureSelection().deselectAll();
141
        }
142
    }
143

    
144
    private FeatureSet doSet(String expression) throws DataException {
145
        FeatureQuery query = featureStore.createFeatureQuery();
146
        DataManager manager = DALLocator.getDataManager();
147
        query.setFilter(manager.createExpresion(expression));
148
        return featureStore.getFeatureSet(query);
149
    }
150

    
151
    public void addToSet(String expression) throws DataException {
152
        // By Pablo: if no filter expression -> don't add more elements to set
153
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
154
            FeatureSet set = null;
155
            try {
156
                set = doSet(expression);
157

    
158
                if (set == null) {
159
                    // throw new RuntimeException("Not a 'where' clause?");
160
                    return;
161
                }
162
                featureStore.getFeatureSelection().select(set);
163
            } finally {
164
                if (set != null) {
165
                    set.dispose();
166
                }
167
            }
168
        }
169
    }
170

    
171
    public void fromSet(String expression) throws DataException {
172
        // By Pablo: if no filter expression -> no element selected
173
        try {
174
            if (!this.filterExpressionFromWhereIsEmpty(expression)) {
175
                // NotificationManager.showMessageInfo("Falta por implementar",
176
                // null);
177

    
178
                FeatureSet set = null;
179
                set = doSet(expression);
180

    
181
                if (set == null) {
182
                    throw new RuntimeException("Not a 'where' clause?");
183
                }
184

    
185
                FeatureSelection oldSelection
186
                        = featureStore.getFeatureSelection();
187

    
188
                FeatureSelection newSelection
189
                        = featureStore.createFeatureSelection();
190
                Iterator iterator = set.iterator();
191
                while (iterator.hasNext()) {
192
                    Feature feature = (Feature) iterator.next();
193
                    if (oldSelection.isSelected(feature)) {
194
                        newSelection.select(feature);
195
                    }
196
                }
197
                featureStore.setSelection(newSelection);
198
                set.dispose();
199

    
200
            } else {
201
                // By Pablo: if no expression -> no element selected
202
                featureStore.getFeatureSelection().deselectAll();
203
                ;
204
            }
205
        } catch (DataException e) {
206
            NotificationManager.addError(e);
207
        }
208

    
209
    }
210

    
211
    /**
212
     * Returns true if the WHERE subconsultation of the filterExpression is
213
     * empty ("")
214
     *
215
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
216
     * @param expression An string
217
     * @return A boolean value
218
     */
219
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
220

    
221
        if (expression == null) {
222
            return true;
223
        }
224

    
225
        String subExpression = expression.trim();
226

    
227
        if (subExpression.length() == 0) {
228
            return true;
229
        }
230

    
231
        int pos;
232

    
233
        // Remove last ';' if exists
234
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
235
            subExpression
236
                    = subExpression.substring(0, subExpression.length() - 1).trim();
237
        }
238

    
239
        // If there is no 'where' clause
240
        if ((pos = subExpression.indexOf("where")) == -1) {
241
            return false;
242
        }
243

    
244
        // If there is no subexpression in the WHERE clause -> true
245
        // + 5 is the length of 'where'
246
        subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
247
        if (subExpression.length() == 0) {
248
            return true;
249
        } else {
250
            return false;
251
        }
252
    }
253

    
254
    private String getLastMessage(Throwable ex) {
255

    
256
        Throwable p = ex;
257
        while (p.getCause() != null && p.getCause() != p) {
258
            p = p.getCause();
259
        }
260
        return p.getMessage();
261
    }
262
}