Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / FiltroExtension.java @ 17170

History | View | Annotate | Download (10.5 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 2183 fernando
import com.hardcode.driverManager.DriverLoadException;
49 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
50 2183 fernando
import com.hardcode.gdbms.engine.data.DataSource;
51
import com.hardcode.gdbms.engine.data.DataSourceFactory;
52 2217 fernando
import com.hardcode.gdbms.engine.instruction.EvaluationException;
53 2183 fernando
import com.hardcode.gdbms.engine.instruction.SemanticException;
54
import com.hardcode.gdbms.parser.ParseException;
55
import com.iver.andami.PluginServices;
56
import com.iver.andami.messages.NotificationManager;
57
import com.iver.andami.plugins.Extension;
58 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
59 2183 fernando
import com.iver.cit.gvsig.fmap.layers.FBitSet;
60
import com.iver.cit.gvsig.fmap.layers.FLayer;
61
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
62
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
63
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
64
import com.iver.cit.gvsig.gui.filter.DefaultExpressionDataSource;
65
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
66
import com.iver.cit.gvsig.gui.filter.FilterDialog;
67 9532 caballero
import com.iver.cit.gvsig.project.documents.ProjectDocument;
68 7304 caballero
import com.iver.cit.gvsig.project.documents.table.gui.Table;
69 7392 sbayarri
import com.iver.cit.gvsig.project.documents.view.IProjectView;
70 2183 fernando
import com.iver.utiles.exceptionHandling.ExceptionListener;
71
72
73 6217 caballero
/**
74 2183 fernando
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78 5005 jorpiell
public class FiltroExtension extends Extension implements ExpressionListener {
79 4682 jorpiell
        protected SelectableDataSource dataSource = null;
80
        protected Table vista;
81 7028 ldiaz
        private String filterTitle;
82 2183 fernando
83
        /**
84
         * DOCUMENT ME!
85
         */
86 5005 jorpiell
        public void initialize() {
87 14821 jmvivo
                registerIcons();
88 2183 fernando
        }
89
90 14821 jmvivo
        private void registerIcons(){
91 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
92 14821 jmvivo
                                "table-filter",
93
                                this.getClass().getClassLoader().getResource("images/Filtro.png")
94
                        );
95
        }
96
97 2183 fernando
        /**
98
         * DOCUMENT ME!
99
         *
100
         * @param actionCommand DOCUMENT ME!
101
         */
102
        public void execute(String actionCommand) {
103
                if ("FILTRO".equals(actionCommand)) {
104
                        try {
105 6880 cesar
                                IWindow v = PluginServices.getMDIManager().getActiveWindow();
106 2183 fernando
107
                                if (v instanceof Table) {
108
                                        vista = (Table) v;
109 7392 sbayarri
110 9532 caballero
                                        dataSource = vista.getModel().getModelo().getRecordset();
111 7028 ldiaz
                                        filterTitle = vista.getModel().getName();
112 9532 caballero
                                        vista.getModel().setModified(true);
113 7304 caballero
                                } else if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
114 7392 sbayarri
                                        IProjectView pv = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel();
115 7414 sbayarri
                                        filterTitle = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel().getName();
116 2610 fernando
                                        FLayer layer = pv.getMapContext()
117 4682 jorpiell
                                        .getLayers().getActives()[0];
118 7414 sbayarri
                                        dataSource = pv.getProject().getDataSourceByLayer(layer);
119 9532 caballero
                                        ((ProjectDocument)pv).setModified(true);
120 2183 fernando
                                }
121 10626 caballero
                        }  catch (ReadDriverException e) {
122 4682 jorpiell
                                NotificationManager.addError("Error filtrando", e);
123 6217 caballero
                        }
124 2183 fernando
125 4682 jorpiell
                        doExecute();
126
                }
127
        }
128 6217 caballero
129 4682 jorpiell
        /**
130 6217 caballero
         * "execute" method action.
131 4682 jorpiell
         *
132
         */
133
        protected void doExecute(){
134 10661 caballero
                DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
135
                ds.setTable(dataSource);
136
                FilterDialog dlg = new FilterDialog(filterTitle);
137
                dlg.addExpressionListener(this);
138
                dlg.addExceptionListener(new ExceptionListener() {
139
                        public void exceptionThrown(Throwable t) {
140
                                NotificationManager.addError(t.getMessage(), t);
141
                        }
142
                });
143
                dlg.setModel(ds);
144
                PluginServices.getMDIManager().addWindow(dlg);
145 2183 fernando
        }
146
147
        /**
148
         * DOCUMENT ME!
149
         *
150
         * @return DOCUMENT ME!
151
         */
152
        public boolean isEnabled() {
153 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
154 6778 jmvivo
155
                if (v == null) {
156
                        return false;
157
                }
158
159
                if (v instanceof Table) {
160
                        return true;
161
                } else {
162 7304 caballero
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
163
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
164 7392 sbayarri
                                IProjectView pv = view.getModel();
165 6778 jmvivo
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
166
                                .getActives();
167
168
                                if (seleccionadas.length == 1) {
169
                                        if (seleccionadas[0].isAvailable() && seleccionadas[0] instanceof AlphanumericData) {
170
                                                return true;
171
                                        }
172
                                }
173
                        }
174
175
                        return false;
176
                }
177
178 2183 fernando
        }
179
180
        /**
181
         * DOCUMENT ME!
182
         *
183
         * @return DOCUMENT ME!
184
         */
185
        public boolean isVisible() {
186 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
187 2183 fernando
188
                if (v == null) {
189
                        return false;
190
                }
191
192 5900 jorpiell
                if (v instanceof Table) {
193 2183 fernando
                        return true;
194
                } else {
195 7304 caballero
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
196
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
197 7392 sbayarri
                                IProjectView pv = view.getModel();
198 2183 fernando
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
199 4682 jorpiell
                                .getActives();
200 2183 fernando
201
                                if (seleccionadas.length == 1) {
202
                                        if (seleccionadas[0] instanceof AlphanumericData) {
203
                                                return true;
204
                                        }
205
                                }
206
                        }
207
208
                        return false;
209
                }
210
        }
211
212
        /**
213
         * DOCUMENT ME!
214
         *
215
         * @param expression DOCUMENT ME!
216
         */
217
        public void newSet(String expression) {
218 9494 ppiqueras
                // By Pablo: if no filter expression -> no element selected
219
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
220
                        try {
221
                                long[] sel = doSet(expression);
222 9532 caballero
223 9494 ppiqueras
                                if (sel == null) {
224
                                        //throw new RuntimeException("Not a 'where' clause?");
225
                                        return;
226
                                }
227 9532 caballero
228 9494 ppiqueras
                                FBitSet selection = new FBitSet();
229 9532 caballero
230 9494 ppiqueras
                                for (int i = 0; i < sel.length; i++) {
231
                                        selection.set((int) sel[i]);
232
                                }
233 9532 caballero
234 9494 ppiqueras
                                dataSource.clearSelection();
235
                                dataSource.setSelection(selection);
236
                        }catch(Exception e){
237
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta.");
238 8559 ldiaz
                        }
239 9494 ppiqueras
                }
240
                else {
241
                        // By Pablo: if no expression -> no element selected
242 8559 ldiaz
                        dataSource.clearSelection();
243 9532 caballero
                }
244 2183 fernando
        }
245
246
        /**
247
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
248
         */
249
        private long[] doSet(String expression) {
250
                try {
251
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
252 2667 fernando
                                        DataSourceFactory.MANUAL_OPENING);
253 2183 fernando
254
                        return ds.getWhereFilter();
255
                } catch (DriverLoadException e) {
256 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
257 10626 caballero
                } catch (ReadDriverException e) {
258 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
259 2183 fernando
                } catch (ParseException e) {
260 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
261 2183 fernando
                } catch (SemanticException e) {
262 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage());
263 2183 fernando
                } catch (IOException e) {
264 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage());
265 2217 fernando
                } catch (EvaluationException e) {
266 10708 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
267
                } catch (com.hardcode.gdbms.parser.TokenMgrError e) {
268
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage());
269 4682 jorpiell
                }
270 2183 fernando
                return null;
271
        }
272
273
        /**
274
         * DOCUMENT ME!
275
         *
276
         * @param expression DOCUMENT ME!
277
         */
278
        public void addToSet(String expression) {
279 9494 ppiqueras
                // By Pablo: if no filter expression -> don't add more elements to set
280
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
281
                        long[] sel = doSet(expression);
282 9532 caballero
283 9494 ppiqueras
                        if (sel == null) {
284
                                //throw new RuntimeException("Not a 'where' clause?");
285
                                return;
286
                        }
287 9532 caballero
288 9494 ppiqueras
                        FBitSet selection = new FBitSet();
289 9532 caballero
290 9494 ppiqueras
                        for (int i = 0; i < sel.length; i++) {
291
                                selection.set((int) sel[i]);
292
                        }
293 9532 caballero
294 9494 ppiqueras
                        FBitSet fbs = dataSource.getSelection();
295
                        fbs.or(selection);
296
                        dataSource.setSelection(fbs);
297 2183 fernando
                }
298
        }
299
300
        /**
301
         * DOCUMENT ME!
302
         *
303
         * @param expression DOCUMENT ME!
304
         */
305
        public void fromSet(String expression) {
306 9494 ppiqueras
                // By Pablo: if no filter expression -> no element selected
307
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
308
                        long[] sel = doSet(expression);
309 9532 caballero
310 9494 ppiqueras
                        if (sel == null) {
311
                                throw new RuntimeException("Not a 'where' clause?");
312
                        }
313 9532 caballero
314 9494 ppiqueras
                        FBitSet selection = new FBitSet();
315 9532 caballero
316 9494 ppiqueras
                        for (int i = 0; i < sel.length; i++) {
317
                                selection.set((int) sel[i]);
318
                        }
319 9532 caballero
320 9494 ppiqueras
                        FBitSet fbs = dataSource.getSelection();
321
                        fbs.and(selection);
322
                        dataSource.setSelection(fbs);
323 2183 fernando
                }
324 9494 ppiqueras
                else {
325
                        // By Pablo: if no expression -> no element selected
326
                        dataSource.clearSelection();
327 2183 fernando
                }
328
        }
329 9532 caballero
330 9494 ppiqueras
        /**
331
         * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
332 9532 caballero
         *
333 9494 ppiqueras
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
334
         * @param expression An string
335 9532 caballero
         * @return A boolean value
336 9494 ppiqueras
         */
337
        private boolean filterExpressionFromWhereIsEmpty(String expression) {
338
                String subExpression = expression.trim();
339 9532 caballero
                int pos;
340
341 9494 ppiqueras
                // Remove last ';' if exists
342
                if (subExpression.charAt(subExpression.length() -1) == ';')
343
                        subExpression = subExpression.substring(0, subExpression.length() -1).trim();
344 9532 caballero
345 9494 ppiqueras
                // If there is no 'where' clause
346
                if ((pos = subExpression.indexOf("where")) == -1)
347
                        return false;
348 9532 caballero
349 9494 ppiqueras
                // If there is no subexpression in the WHERE clause -> true
350
                subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where'
351
                if ( subExpression.length() == 0 )
352
                        return true;
353
                else
354
                        return false;
355
        }
356 2183 fernando
}