Statistics
| Revision:

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

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
                registerIcons();
88
        }
89

    
90
        private void registerIcons(){
91
                PluginServices.getIconTheme().registerDefault(
92
                                "table-filter",
93
                                this.getClass().getClassLoader().getResource("images/Filtro.png")
94
                        );
95
        }
96

    
97
        /**
98
         * DOCUMENT ME!
99
         *
100
         * @param actionCommand DOCUMENT ME!
101
         */
102
        public void execute(String actionCommand) {
103
                if ("FILTRO".equals(actionCommand)) {
104
                        try {
105
                                IWindow v = PluginServices.getMDIManager().getActiveWindow();
106

    
107
                                if (v instanceof Table) {
108
                                        vista = (Table) v;
109

    
110
                                        dataSource = vista.getModel().getModelo().getRecordset();
111
                                        filterTitle = vista.getModel().getName();
112
                                        vista.getModel().setModified(true);
113
                                } else if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
114
                                        IProjectView pv = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel();
115
                                        filterTitle = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel().getName();
116
                                        FLayer layer = pv.getMapContext()
117
                                        .getLayers().getActives()[0];
118
                                        dataSource = pv.getProject().getDataSourceByLayer(layer);
119
                                        ((ProjectDocument)pv).setModified(true);
120
                                }
121
                        }  catch (ReadDriverException e) {
122
                                NotificationManager.addError("Error filtrando", e);
123
                        }
124

    
125
                        doExecute();
126
                }
127
        }
128

    
129
        /**
130
         * "execute" method action.
131
         *
132
         */
133
        protected void doExecute(){
134
                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
        }
146

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

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

    
159
                if (v instanceof Table) {
160
                        return true;
161
                } else {
162
                        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
                                IProjectView pv = view.getModel();
165
                                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
        }
179

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

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

    
192
                if (v instanceof Table) {
193
                        return true;
194
                } else {
195
                        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
                                IProjectView pv = view.getModel();
198
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
199
                                .getActives();
200

    
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
                // By Pablo: if no filter expression -> no element selected
219
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
220
                        try {
221
                                long[] sel = doSet(expression);
222

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

    
228
                                FBitSet selection = new FBitSet();
229

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

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

    
254
                        return ds.getWhereFilter();
255
                } catch (DriverLoadException e) {
256
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
257
                } catch (ReadDriverException e) {
258
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
259
                } catch (ParseException e) {
260
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
261
                } catch (SemanticException e) {
262
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage());
263
                } catch (IOException e) {
264
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage());
265
                } catch (EvaluationException e) {
266
                        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
                }
270
                return null;
271
        }
272

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

    
283
                        if (sel == null) {
284
                                //throw new RuntimeException("Not a 'where' clause?");
285
                                return;
286
                        }
287

    
288
                        FBitSet selection = new FBitSet();
289

    
290
                        for (int i = 0; i < sel.length; i++) {
291
                                selection.set((int) sel[i]);
292
                        }
293

    
294
                        FBitSet fbs = dataSource.getSelection();
295
                        fbs.or(selection);
296
                        dataSource.setSelection(fbs);
297
                }
298
        }
299

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

    
310
                        if (sel == null) {
311
                                throw new RuntimeException("Not a 'where' clause?");
312
                        }
313

    
314
                        FBitSet selection = new FBitSet();
315

    
316
                        for (int i = 0; i < sel.length; i++) {
317
                                selection.set((int) sel[i]);
318
                        }
319

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

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

    
341
                // Remove last ';' if exists
342
                if (subExpression.charAt(subExpression.length() -1) == ';')
343
                        subExpression = subExpression.substring(0, subExpression.length() -1).trim();
344

    
345
                // If there is no 'where' clause
346
                if ((pos = subExpression.indexOf("where")) == -1)
347
                        return false;
348

    
349
                // 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
}