Statistics
| Revision:

root / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / FiltroExtension.java @ 9563

History | View | Annotate | Download (9.65 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.engine.data.DataSource;
50
import com.hardcode.gdbms.engine.data.DataSourceFactory;
51
import com.hardcode.gdbms.engine.instruction.EvaluationException;
52
import com.hardcode.gdbms.engine.instruction.SemanticException;
53
import com.hardcode.gdbms.parser.ParseException;
54
import com.iver.andami.PluginServices;
55
import com.iver.andami.messages.NotificationManager;
56
import com.iver.andami.plugins.Extension;
57
import com.iver.andami.ui.mdiManager.IWindow;
58
import com.iver.cit.gvsig.fmap.DriverException;
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.table.gui.Table;
68
import com.iver.cit.gvsig.project.documents.view.IProjectView;
69
import com.iver.utiles.exceptionHandling.ExceptionListener;
70

    
71

    
72
/**
73
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
74
 *
75
 * @author Vicente Caballero Navarro
76
 */
77
public class FiltroExtension extends Extension implements ExpressionListener {
78
        protected SelectableDataSource dataSource = null;
79
        protected Table vista;
80
        private String filterTitle;
81

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

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

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

    
101
                                        dataSource = (SelectableDataSource)vista.getModel().getModelo().getRecordset();
102
                                        filterTitle = vista.getModel().getName();
103

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

    
115
                        doExecute();
116
                }
117
        }
118

    
119
        /**
120
         * "execute" method action.
121
         *
122
         */
123
        protected void doExecute(){
124
                try{
125
                        DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
126
                        ds.setTable(dataSource);
127

    
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

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

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

    
152
                if (v == null) {
153
                        return false;
154
                }
155

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

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

    
172
                        return false;
173
                }
174

    
175
        }
176

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

    
185
                if (v == null) {
186
                        return false;
187
                }
188

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

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

    
205
                        return false;
206
                }
207
        }
208

    
209
        /**
210
         * DOCUMENT ME!
211
         *
212
         * @param expression DOCUMENT ME!
213
         */
214
        public void newSet(String expression) {
215
                // By Pablo: if no filter expression -> no element selected
216
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
217
                        try {
218
                                long[] sel = doSet(expression);
219
                
220
                                if (sel == null) {
221
                                        //throw new RuntimeException("Not a 'where' clause?");
222
                                        return;
223
                                }
224
                
225
                                FBitSet selection = new FBitSet();
226
                
227
                                for (int i = 0; i < sel.length; i++) {
228
                                        selection.set((int) sel[i]);
229
                                }
230
                
231
                                dataSource.clearSelection();
232
                                dataSource.setSelection(selection);
233
                        }catch(Exception e){
234
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta.");
235
                        }
236
                }
237
                else {
238
                        // By Pablo: if no expression -> no element selected
239
                        dataSource.clearSelection();
240
                }
241
        }
242

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

    
251
                        return ds.getWhereFilter();
252
                } catch (DriverLoadException e) {
253
                        NotificationManager.addError("Error cargando el driver", e);
254
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
255
                        NotificationManager.addError("Error accediendo al driver", e);
256
                } catch (ParseException e) {
257
                        NotificationManager.addError("Parse error", e);
258
                } catch (SemanticException e) {
259
                        NotificationManager.addError(e.getMessage(), e);
260
                } catch (IOException e) {
261
                        NotificationManager.addError("GDBMS internal error", e);
262
                } catch (EvaluationException e) {
263
                        NotificationManager.addError("Sintaxis incorrecta", e);
264
                }
265

    
266
                return null;
267
        }
268

    
269
        /**
270
         * DOCUMENT ME!
271
         *
272
         * @param expression DOCUMENT ME!
273
         */
274
        public void addToSet(String expression) {
275
                // By Pablo: if no filter expression -> don't add more elements to set
276
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
277
                        long[] sel = doSet(expression);
278
        
279
                        if (sel == null) {
280
                                //throw new RuntimeException("Not a 'where' clause?");
281
                                return;
282
                        }
283
        
284
                        FBitSet selection = new FBitSet();
285
        
286
                        for (int i = 0; i < sel.length; i++) {
287
                                selection.set((int) sel[i]);
288
                        }
289
        
290
                        FBitSet fbs = dataSource.getSelection();
291
                        fbs.or(selection);
292
                        dataSource.setSelection(fbs);
293
                }
294
        }
295

    
296
        /**
297
         * DOCUMENT ME!
298
         *
299
         * @param expression DOCUMENT ME!
300
         */
301
        public void fromSet(String expression) {
302
                // By Pablo: if no filter expression -> no element selected
303
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
304
                        long[] sel = doSet(expression);
305
        
306
                        if (sel == null) {
307
                                throw new RuntimeException("Not a 'where' clause?");
308
                        }
309
        
310
                        FBitSet selection = new FBitSet();
311
        
312
                        for (int i = 0; i < sel.length; i++) {
313
                                selection.set((int) sel[i]);
314
                        }
315
        
316
                        FBitSet fbs = dataSource.getSelection();
317
                        fbs.and(selection);
318
                        dataSource.setSelection(fbs);
319
                }
320
                else {
321
                        // By Pablo: if no expression -> no element selected
322
                        dataSource.clearSelection();
323
                }
324
        }        
325

    
326
        /**
327
         * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
328
         * 
329
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
330
         * @param expression An string
331
         * @return A boolean value 
332
         */
333
        private boolean filterExpressionFromWhereIsEmpty(String expression) {
334
                String subExpression = expression.trim();
335
                int pos;        
336
                
337
                // Remove last ';' if exists
338
                if (subExpression.charAt(subExpression.length() -1) == ';')
339
                        subExpression = subExpression.substring(0, subExpression.length() -1).trim();
340
                
341
                // If there is no 'where' clause
342
                if ((pos = subExpression.indexOf("where")) == -1)
343
                        return false;
344
                
345
                // 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
}