Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / FiltroExtension.java @ 10661

History | View | Annotate | Download (10.4 KB)

1
/* 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
import java.awt.Component;
44
import java.io.IOException;
45

    
46
import javax.swing.JOptionPane;
47

    
48
import com.hardcode.driverManager.DriverLoadException;
49
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
50
import com.hardcode.gdbms.engine.data.DataSource;
51
import com.hardcode.gdbms.engine.data.DataSourceFactory;
52
import com.hardcode.gdbms.engine.instruction.EvaluationException;
53
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
import com.iver.andami.ui.mdiManager.IWindow;
59
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
import com.iver.cit.gvsig.project.documents.ProjectDocument;
68
import com.iver.cit.gvsig.project.documents.table.gui.Table;
69
import com.iver.cit.gvsig.project.documents.view.IProjectView;
70
import com.iver.utiles.exceptionHandling.ExceptionListener;
71

    
72

    
73
/**
74
 * 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
public class FiltroExtension extends Extension implements ExpressionListener {
79
        protected SelectableDataSource dataSource = null;
80
        protected Table vista;
81
        private String filterTitle;
82

    
83
        /**
84
         * DOCUMENT ME!
85
         */
86
        public void initialize() {
87
        }
88

    
89
        /**
90
         * DOCUMENT ME!
91
         *
92
         * @param actionCommand DOCUMENT ME!
93
         */
94
        public void execute(String actionCommand) {
95
                if ("FILTRO".equals(actionCommand)) {
96
                        try {
97
                                IWindow v = PluginServices.getMDIManager().getActiveWindow();
98

    
99
                                if (v instanceof Table) {
100
                                        vista = (Table) v;
101

    
102
                                        dataSource = vista.getModel().getModelo().getRecordset();
103
                                        filterTitle = vista.getModel().getName();
104
                                        vista.getModel().setModified(true);
105
                                } else if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
106
                                        IProjectView pv = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel();
107
                                        filterTitle = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel().getName();
108
                                        FLayer layer = pv.getMapContext()
109
                                        .getLayers().getActives()[0];
110
                                        dataSource = pv.getProject().getDataSourceByLayer(layer);
111
                                        ((ProjectDocument)pv).setModified(true);
112
                                }
113
                        }  catch (ReadDriverException e) {
114
                                NotificationManager.addError("Error filtrando", e);
115
                        }
116

    
117
                        doExecute();
118
                }
119
        }
120

    
121
        /**
122
         * "execute" method action.
123
         *
124
         */
125
        protected void doExecute(){
126
                DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
127
                ds.setTable(dataSource);
128
                FilterDialog dlg = new FilterDialog(filterTitle);
129
                dlg.addExpressionListener(this);
130
                dlg.addExceptionListener(new ExceptionListener() {
131
                        public void exceptionThrown(Throwable t) {
132
                                NotificationManager.addError(t.getMessage(), t);
133
                        }
134
                });
135
                dlg.setModel(ds);
136
                PluginServices.getMDIManager().addWindow(dlg);
137
        }
138

    
139
        /**
140
         * DOCUMENT ME!
141
         *
142
         * @return DOCUMENT ME!
143
         */
144
        public boolean isEnabled() {
145
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
146

    
147
                if (v == null) {
148
                        return false;
149
                }
150

    
151
                if (v instanceof Table) {
152
                        return true;
153
                } else {
154
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
155
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
156
                                IProjectView pv = view.getModel();
157
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
158
                                .getActives();
159

    
160
                                if (seleccionadas.length == 1) {
161
                                        if (seleccionadas[0].isAvailable() && seleccionadas[0] instanceof AlphanumericData) {
162
                                                return true;
163
                                        }
164
                                }
165
                        }
166

    
167
                        return false;
168
                }
169

    
170
        }
171

    
172
        /**
173
         * DOCUMENT ME!
174
         *
175
         * @return DOCUMENT ME!
176
         */
177
        public boolean isVisible() {
178
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
179

    
180
                if (v == null) {
181
                        return false;
182
                }
183

    
184
                if (v instanceof Table) {
185
                        return true;
186
                } else {
187
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
188
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
189
                                IProjectView pv = view.getModel();
190
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
191
                                .getActives();
192

    
193
                                if (seleccionadas.length == 1) {
194
                                        if (seleccionadas[0] instanceof AlphanumericData) {
195
                                                return true;
196
                                        }
197
                                }
198
                        }
199

    
200
                        return false;
201
                }
202
        }
203

    
204
        /**
205
         * DOCUMENT ME!
206
         *
207
         * @param expression DOCUMENT ME!
208
         */
209
        public void newSet(String expression) {
210
                // By Pablo: if no filter expression -> no element selected
211
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
212
                        try {
213
                                long[] sel = doSet(expression);
214

    
215
                                if (sel == null) {
216
                                        //throw new RuntimeException("Not a 'where' clause?");
217
                                        return;
218
                                }
219

    
220
                                FBitSet selection = new FBitSet();
221

    
222
                                for (int i = 0; i < sel.length; i++) {
223
                                        selection.set((int) sel[i]);
224
                                }
225

    
226
                                dataSource.clearSelection();
227
                                dataSource.setSelection(selection);
228
                        }catch(Exception e){
229
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta.");
230
                        }
231
                }
232
                else {
233
                        // By Pablo: if no expression -> no element selected
234
                        dataSource.clearSelection();
235
                }
236
        }
237

    
238
        /**
239
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
240
         */
241
        private long[] doSet(String expression) {
242
                try {
243
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
244
                                        DataSourceFactory.MANUAL_OPENING);
245

    
246
                        return ds.getWhereFilter();
247
                } catch (DriverLoadException e) {
248
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error"));
249
                        //NotificationManager.addError("Error cargando el driver", e);
250
                } catch (ReadDriverException e) {
251
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error"));
252
                        //NotificationManager.addError("Error accediendo al driver", e);
253
                } catch (ParseException e) {
254
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error"));
255
                        //NotificationManager.addError("Parse error", e);
256
                } catch (SemanticException e) {
257
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error"));
258
                        //NotificationManager.addError(e.getMessage(), e);
259
                } catch (IOException e) {
260
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error"));
261
                        //NotificationManager.addError("GDBMS internal error", e);
262
                } catch (EvaluationException e) {
263
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error"));
264
                        //NotificationManager.addError("Sintaxis incorrecta", e);
265
                }
266

    
267
                return null;
268
        }
269

    
270
        /**
271
         * DOCUMENT ME!
272
         *
273
         * @param expression DOCUMENT ME!
274
         */
275
        public void addToSet(String expression) {
276
                // By Pablo: if no filter expression -> don't add more elements to set
277
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
278
                        long[] sel = doSet(expression);
279

    
280
                        if (sel == null) {
281
                                //throw new RuntimeException("Not a 'where' clause?");
282
                                return;
283
                        }
284

    
285
                        FBitSet selection = new FBitSet();
286

    
287
                        for (int i = 0; i < sel.length; i++) {
288
                                selection.set((int) sel[i]);
289
                        }
290

    
291
                        FBitSet fbs = dataSource.getSelection();
292
                        fbs.or(selection);
293
                        dataSource.setSelection(fbs);
294
                }
295
        }
296

    
297
        /**
298
         * DOCUMENT ME!
299
         *
300
         * @param expression DOCUMENT ME!
301
         */
302
        public void fromSet(String expression) {
303
                // By Pablo: if no filter expression -> no element selected
304
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
305
                        long[] sel = doSet(expression);
306

    
307
                        if (sel == null) {
308
                                throw new RuntimeException("Not a 'where' clause?");
309
                        }
310

    
311
                        FBitSet selection = new FBitSet();
312

    
313
                        for (int i = 0; i < sel.length; i++) {
314
                                selection.set((int) sel[i]);
315
                        }
316

    
317
                        FBitSet fbs = dataSource.getSelection();
318
                        fbs.and(selection);
319
                        dataSource.setSelection(fbs);
320
                }
321
                else {
322
                        // By Pablo: if no expression -> no element selected
323
                        dataSource.clearSelection();
324
                }
325
        }
326

    
327
        /**
328
         * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
329
         *
330
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
331
         * @param expression An string
332
         * @return A boolean value
333
         */
334
        private boolean filterExpressionFromWhereIsEmpty(String expression) {
335
                String subExpression = expression.trim();
336
                int pos;
337

    
338
                // Remove last ';' if exists
339
                if (subExpression.charAt(subExpression.length() -1) == ';')
340
                        subExpression = subExpression.substring(0, subExpression.length() -1).trim();
341

    
342
                // If there is no 'where' clause
343
                if ((pos = subExpression.indexOf("where")) == -1)
344
                        return false;
345

    
346
                // If there is no subexpression in the WHERE clause -> true
347
                subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where'
348
                if ( subExpression.length() == 0 )
349
                        return true;
350
                else
351
                        return false;
352
        }
353
}