Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / FiltroExtension.java @ 21665

History | View | Annotate | Download (10.3 KB)

1 2183 fernando
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig;
42
43 8559 ldiaz
import java.awt.Component;
44 4682 jorpiell
import java.io.IOException;
45
46 8559 ldiaz
import javax.swing.JOptionPane;
47
48 21354 vcaballero
import org.gvsig.data.ReadException;
49 21665 vcaballero
import org.gvsig.data.vectorial.FeatureStore;
50 20994 jmvivo
import org.gvsig.fmap.mapcontext.layers.FBitSet;
51
import org.gvsig.fmap.mapcontext.layers.FLayer;
52
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
53
import org.gvsig.fmap.mapcontext.layers.operations.AlphanumericData;
54 21665 vcaballero
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
55 20994 jmvivo
56 2183 fernando
import com.iver.andami.PluginServices;
57
import com.iver.andami.messages.NotificationManager;
58
import com.iver.andami.plugins.Extension;
59 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
60 2183 fernando
import com.iver.cit.gvsig.gui.filter.DefaultExpressionDataSource;
61
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
62
import com.iver.cit.gvsig.gui.filter.FilterDialog;
63 9532 caballero
import com.iver.cit.gvsig.project.documents.ProjectDocument;
64 7304 caballero
import com.iver.cit.gvsig.project.documents.table.gui.Table;
65 7392 sbayarri
import com.iver.cit.gvsig.project.documents.view.IProjectView;
66 2183 fernando
import com.iver.utiles.exceptionHandling.ExceptionListener;
67
68
69 6217 caballero
/**
70 2183 fernando
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
71
 *
72
 * @author Vicente Caballero Navarro
73
 */
74 5005 jorpiell
public class FiltroExtension extends Extension implements ExpressionListener {
75 21665 vcaballero
        protected FeatureStore featureStore = null;
76
        protected Table table;
77 7028 ldiaz
        private String filterTitle;
78 2183 fernando
79
        /**
80
         * DOCUMENT ME!
81
         */
82 5005 jorpiell
        public void initialize() {
83 14821 jmvivo
                registerIcons();
84 2183 fernando
        }
85
86 14821 jmvivo
        private void registerIcons(){
87 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
88 14821 jmvivo
                                "table-filter",
89
                                this.getClass().getClassLoader().getResource("images/Filtro.png")
90
                        );
91
        }
92
93 2183 fernando
        /**
94
         * DOCUMENT ME!
95
         *
96
         * @param actionCommand DOCUMENT ME!
97
         */
98
        public void execute(String actionCommand) {
99
                if ("FILTRO".equals(actionCommand)) {
100
                        try {
101 6880 cesar
                                IWindow v = PluginServices.getMDIManager().getActiveWindow();
102 2183 fernando
103
                                if (v instanceof Table) {
104 21665 vcaballero
                                        table = (Table) v;
105 7392 sbayarri
106 21665 vcaballero
                                        featureStore = table.getModel().getModelo();
107
                                        filterTitle = table.getModel().getName();
108
                                        table.getModel().setModified(true);
109 7304 caballero
                                } else if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
110 7392 sbayarri
                                        IProjectView pv = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel();
111 7414 sbayarri
                                        filterTitle = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel().getName();
112 2610 fernando
                                        FLayer layer = pv.getMapContext()
113 4682 jorpiell
                                        .getLayers().getActives()[0];
114 21665 vcaballero
                                        featureStore = ((FLyrVect)layer).getFeatureStore();//pv.getProject().getDataSourceByLayer(layer);
115 9532 caballero
                                        ((ProjectDocument)pv).setModified(true);
116 2183 fernando
                                }
117 21354 vcaballero
                        }  catch (ReadException e) {
118 4682 jorpiell
                                NotificationManager.addError("Error filtrando", e);
119 6217 caballero
                        }
120 2183 fernando
121 4682 jorpiell
                        doExecute();
122
                }
123
        }
124 6217 caballero
125 4682 jorpiell
        /**
126 6217 caballero
         * "execute" method action.
127 4682 jorpiell
         *
128
         */
129
        protected void doExecute(){
130 10661 caballero
                DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
131 21665 vcaballero
                ds.setTable(featureStore);
132 10661 caballero
                FilterDialog dlg = new FilterDialog(filterTitle);
133
                dlg.addExpressionListener(this);
134
                dlg.addExceptionListener(new ExceptionListener() {
135
                        public void exceptionThrown(Throwable t) {
136
                                NotificationManager.addError(t.getMessage(), t);
137
                        }
138
                });
139
                dlg.setModel(ds);
140
                PluginServices.getMDIManager().addWindow(dlg);
141 2183 fernando
        }
142
143
        /**
144
         * DOCUMENT ME!
145
         *
146
         * @return DOCUMENT ME!
147
         */
148
        public boolean isEnabled() {
149 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
150 6778 jmvivo
151
                if (v == null) {
152
                        return false;
153
                }
154
155
                if (v instanceof Table) {
156
                        return true;
157
                } else {
158 7304 caballero
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
159
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
160 7392 sbayarri
                                IProjectView pv = view.getModel();
161 6778 jmvivo
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
162
                                .getActives();
163
164
                                if (seleccionadas.length == 1) {
165
                                        if (seleccionadas[0].isAvailable() && seleccionadas[0] instanceof AlphanumericData) {
166
                                                return true;
167
                                        }
168
                                }
169
                        }
170
171
                        return false;
172
                }
173
174 2183 fernando
        }
175
176
        /**
177
         * DOCUMENT ME!
178
         *
179
         * @return DOCUMENT ME!
180
         */
181
        public boolean isVisible() {
182 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
183 2183 fernando
184
                if (v == null) {
185
                        return false;
186
                }
187
188 5900 jorpiell
                if (v instanceof Table) {
189 2183 fernando
                        return true;
190
                } else {
191 7304 caballero
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
192
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
193 7392 sbayarri
                                IProjectView pv = view.getModel();
194 2183 fernando
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
195 4682 jorpiell
                                .getActives();
196 2183 fernando
197
                                if (seleccionadas.length == 1) {
198
                                        if (seleccionadas[0] instanceof AlphanumericData) {
199
                                                return true;
200
                                        }
201
                                }
202
                        }
203
204
                        return false;
205
                }
206
        }
207
208
        /**
209
         * DOCUMENT ME!
210
         *
211
         * @param expression DOCUMENT ME!
212
         */
213
        public void newSet(String expression) {
214 9494 ppiqueras
                // By Pablo: if no filter expression -> no element selected
215
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
216
                        try {
217
                                long[] sel = doSet(expression);
218 9532 caballero
219 9494 ppiqueras
                                if (sel == null) {
220
                                        //throw new RuntimeException("Not a 'where' clause?");
221
                                        return;
222
                                }
223 9532 caballero
224 9494 ppiqueras
                                FBitSet selection = new FBitSet();
225 9532 caballero
226 9494 ppiqueras
                                for (int i = 0; i < sel.length; i++) {
227
                                        selection.set((int) sel[i]);
228
                                }
229 9532 caballero
230 21665 vcaballero
                                featureStore.getSelection().clear();
231
                                featureStore.setSelection(selection);
232 9494 ppiqueras
                        }catch(Exception e){
233
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta.");
234 8559 ldiaz
                        }
235 9494 ppiqueras
                }
236
                else {
237
                        // By Pablo: if no expression -> no element selected
238 21665 vcaballero
                        featureStore.getSelection().clear();
239 9532 caballero
                }
240 2183 fernando
        }
241
242
        /**
243
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
244
         */
245
        private long[] doSet(String expression) {
246
                try {
247
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
248 2667 fernando
                                        DataSourceFactory.MANUAL_OPENING);
249 2183 fernando
250
                        return ds.getWhereFilter();
251
                } catch (DriverLoadException e) {
252 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
253 10626 caballero
                } catch (ReadDriverException e) {
254 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
255 2183 fernando
                } catch (ParseException e) {
256 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
257 2183 fernando
                } catch (SemanticException e) {
258 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage());
259 2183 fernando
                } catch (IOException e) {
260 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage());
261 2217 fernando
                } catch (EvaluationException e) {
262 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
263
                } catch (com.hardcode.gdbms.parser.TokenMgrError e) {
264
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage());
265 4682 jorpiell
                }
266 2183 fernando
                return null;
267
        }
268
269
        /**
270
         * DOCUMENT ME!
271
         *
272
         * @param expression DOCUMENT ME!
273
         */
274
        public void addToSet(String expression) {
275 9494 ppiqueras
                // By Pablo: if no filter expression -> don't add more elements to set
276
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
277
                        long[] sel = doSet(expression);
278 9532 caballero
279 9494 ppiqueras
                        if (sel == null) {
280
                                //throw new RuntimeException("Not a 'where' clause?");
281
                                return;
282
                        }
283 9532 caballero
284 9494 ppiqueras
                        FBitSet selection = new FBitSet();
285 9532 caballero
286 9494 ppiqueras
                        for (int i = 0; i < sel.length; i++) {
287
                                selection.set((int) sel[i]);
288
                        }
289 9532 caballero
290 21665 vcaballero
                        FBitSet fbs = featureStore.getSelection();
291 9494 ppiqueras
                        fbs.or(selection);
292 21665 vcaballero
                        featureStore.setSelection(fbs);
293 2183 fernando
                }
294
        }
295
296
        /**
297
         * DOCUMENT ME!
298
         *
299
         * @param expression DOCUMENT ME!
300
         */
301
        public void fromSet(String expression) {
302 9494 ppiqueras
                // By Pablo: if no filter expression -> no element selected
303
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
304
                        long[] sel = doSet(expression);
305 9532 caballero
306 9494 ppiqueras
                        if (sel == null) {
307
                                throw new RuntimeException("Not a 'where' clause?");
308
                        }
309 9532 caballero
310 9494 ppiqueras
                        FBitSet selection = new FBitSet();
311 9532 caballero
312 9494 ppiqueras
                        for (int i = 0; i < sel.length; i++) {
313
                                selection.set((int) sel[i]);
314
                        }
315 9532 caballero
316 21665 vcaballero
                        FBitSet fbs = featureStore.getSelection();
317 9494 ppiqueras
                        fbs.and(selection);
318 21665 vcaballero
                        featureStore.setSelection(fbs);
319 2183 fernando
                }
320 9494 ppiqueras
                else {
321
                        // By Pablo: if no expression -> no element selected
322 21665 vcaballero
                        featureStore.clearSelection();
323 2183 fernando
                }
324
        }
325 9532 caballero
326 9494 ppiqueras
        /**
327
         * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
328 9532 caballero
         *
329 9494 ppiqueras
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
330
         * @param expression An string
331 9532 caballero
         * @return A boolean value
332 9494 ppiqueras
         */
333
        private boolean filterExpressionFromWhereIsEmpty(String expression) {
334
                String subExpression = expression.trim();
335 9532 caballero
                int pos;
336
337 9494 ppiqueras
                // Remove last ';' if exists
338
                if (subExpression.charAt(subExpression.length() -1) == ';')
339
                        subExpression = subExpression.substring(0, subExpression.length() -1).trim();
340 9532 caballero
341 9494 ppiqueras
                // If there is no 'where' clause
342
                if ((pos = subExpression.indexOf("where")) == -1)
343
                        return false;
344 9532 caballero
345 9494 ppiqueras
                // If there is no subexpression in the WHERE clause -> true
346
                subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where'
347
                if ( subExpression.length() == 0 )
348
                        return true;
349
                else
350
                        return false;
351
        }
352 2183 fernando
}