Statistics
| Revision:

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

History | View | Annotate | Download (10.5 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
                try{
127
                        DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
128
                        ds.setTable(dataSource);
129

    
130
                        FilterDialog dlg = new FilterDialog(filterTitle);
131
                        dlg.addExpressionListener(this);
132
                        dlg.addExceptionListener(new ExceptionListener() {
133
                                public void exceptionThrown(Throwable t) {
134
                                        NotificationManager.addError(t.getMessage(), t);
135
                                }
136
                        });
137

    
138
                        dlg.setModel(ds);
139
                        PluginServices.getMDIManager().addWindow(dlg);
140
                } catch (DriverLoadException e) {
141
                        // TODO Auto-generated catch block
142
                        e.printStackTrace();
143
                }
144
        }
145

    
146
        /**
147
         * DOCUMENT ME!
148
         *
149
         * @return DOCUMENT ME!
150
         */
151
        public boolean isEnabled() {
152
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
153

    
154
                if (v == null) {
155
                        return false;
156
                }
157

    
158
                if (v instanceof Table) {
159
                        return true;
160
                } else {
161
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
162
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
163
                                IProjectView pv = view.getModel();
164
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
165
                                .getActives();
166

    
167
                                if (seleccionadas.length == 1) {
168
                                        if (seleccionadas[0].isAvailable() && seleccionadas[0] instanceof AlphanumericData) {
169
                                                return true;
170
                                        }
171
                                }
172
                        }
173

    
174
                        return false;
175
                }
176

    
177
        }
178

    
179
        /**
180
         * DOCUMENT ME!
181
         *
182
         * @return DOCUMENT ME!
183
         */
184
        public boolean isVisible() {
185
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
186

    
187
                if (v == null) {
188
                        return false;
189
                }
190

    
191
                if (v instanceof Table) {
192
                        return true;
193
                } else {
194
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
195
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
196
                                IProjectView pv = view.getModel();
197
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
198
                                .getActives();
199

    
200
                                if (seleccionadas.length == 1) {
201
                                        if (seleccionadas[0] instanceof AlphanumericData) {
202
                                                return true;
203
                                        }
204
                                }
205
                        }
206

    
207
                        return false;
208
                }
209
        }
210

    
211
        /**
212
         * DOCUMENT ME!
213
         *
214
         * @param expression DOCUMENT ME!
215
         */
216
        public void newSet(String expression) {
217
                // By Pablo: if no filter expression -> no element selected
218
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
219
                        try {
220
                                long[] sel = doSet(expression);
221

    
222
                                if (sel == null) {
223
                                        //throw new RuntimeException("Not a 'where' clause?");
224
                                        return;
225
                                }
226

    
227
                                FBitSet selection = new FBitSet();
228

    
229
                                for (int i = 0; i < sel.length; i++) {
230
                                        selection.set((int) sel[i]);
231
                                }
232

    
233
                                dataSource.clearSelection();
234
                                dataSource.setSelection(selection);
235
                        }catch(Exception e){
236
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta.");
237
                        }
238
                }
239
                else {
240
                        // By Pablo: if no expression -> no element selected
241
                        dataSource.clearSelection();
242
                }
243
        }
244

    
245
        /**
246
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
247
         */
248
        private long[] doSet(String expression) {
249
                try {
250
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
251
                                        DataSourceFactory.MANUAL_OPENING);
252

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

    
274
                return null;
275
        }
276

    
277
        /**
278
         * DOCUMENT ME!
279
         *
280
         * @param expression DOCUMENT ME!
281
         */
282
        public void addToSet(String expression) {
283
                // By Pablo: if no filter expression -> don't add more elements to set
284
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
285
                        long[] sel = doSet(expression);
286

    
287
                        if (sel == null) {
288
                                //throw new RuntimeException("Not a 'where' clause?");
289
                                return;
290
                        }
291

    
292
                        FBitSet selection = new FBitSet();
293

    
294
                        for (int i = 0; i < sel.length; i++) {
295
                                selection.set((int) sel[i]);
296
                        }
297

    
298
                        FBitSet fbs = dataSource.getSelection();
299
                        fbs.or(selection);
300
                        dataSource.setSelection(fbs);
301
                }
302
        }
303

    
304
        /**
305
         * DOCUMENT ME!
306
         *
307
         * @param expression DOCUMENT ME!
308
         */
309
        public void fromSet(String expression) {
310
                // By Pablo: if no filter expression -> no element selected
311
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
312
                        long[] sel = doSet(expression);
313

    
314
                        if (sel == null) {
315
                                throw new RuntimeException("Not a 'where' clause?");
316
                        }
317

    
318
                        FBitSet selection = new FBitSet();
319

    
320
                        for (int i = 0; i < sel.length; i++) {
321
                                selection.set((int) sel[i]);
322
                        }
323

    
324
                        FBitSet fbs = dataSource.getSelection();
325
                        fbs.and(selection);
326
                        dataSource.setSelection(fbs);
327
                }
328
                else {
329
                        // By Pablo: if no expression -> no element selected
330
                        dataSource.clearSelection();
331
                }
332
        }
333

    
334
        /**
335
         * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
336
         *
337
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
338
         * @param expression An string
339
         * @return A boolean value
340
         */
341
        private boolean filterExpressionFromWhereIsEmpty(String expression) {
342
                String subExpression = expression.trim();
343
                int pos;
344

    
345
                // Remove last ';' if exists
346
                if (subExpression.charAt(subExpression.length() -1) == ';')
347
                        subExpression = subExpression.substring(0, subExpression.length() -1).trim();
348

    
349
                // If there is no 'where' clause
350
                if ((pos = subExpression.indexOf("where")) == -1)
351
                        return false;
352

    
353
                // If there is no subexpression in the WHERE clause -> true
354
                subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where'
355
                if ( subExpression.length() == 0 )
356
                        return true;
357
                else
358
                        return false;
359
        }
360
}