Statistics
| Revision:

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

History | View | Annotate | Download (7.99 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 4682 jorpiell
import java.io.IOException;
44
45 2183 fernando
import com.hardcode.driverManager.DriverLoadException;
46
import com.hardcode.gdbms.engine.data.DataSource;
47
import com.hardcode.gdbms.engine.data.DataSourceFactory;
48 2217 fernando
import com.hardcode.gdbms.engine.instruction.EvaluationException;
49 2183 fernando
import com.hardcode.gdbms.engine.instruction.SemanticException;
50
import com.hardcode.gdbms.parser.ParseException;
51
import com.iver.andami.PluginServices;
52
import com.iver.andami.messages.NotificationManager;
53
import com.iver.andami.plugins.Extension;
54 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
55 2183 fernando
import com.iver.cit.gvsig.fmap.DriverException;
56
import com.iver.cit.gvsig.fmap.layers.FBitSet;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
59
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
60
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
61
import com.iver.cit.gvsig.gui.filter.DefaultExpressionDataSource;
62
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
63
import com.iver.cit.gvsig.gui.filter.FilterDialog;
64 7304 caballero
import com.iver.cit.gvsig.project.documents.table.gui.Table;
65 7392 sbayarri
import com.iver.cit.gvsig.project.documents.view.IProjectView;
66 7304 caballero
import com.iver.cit.gvsig.project.documents.view.ProjectView;
67 2183 fernando
import com.iver.utiles.exceptionHandling.ExceptionListener;
68
69
70 6217 caballero
/**
71 2183 fernando
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75 5005 jorpiell
public class FiltroExtension extends Extension implements ExpressionListener {
76 4682 jorpiell
        protected SelectableDataSource dataSource = null;
77
        protected Table vista;
78 7028 ldiaz
        private String filterTitle;
79 2183 fernando
80
        /**
81
         * DOCUMENT ME!
82
         */
83 5005 jorpiell
        public void initialize() {
84 2183 fernando
        }
85
86
        /**
87
         * DOCUMENT ME!
88
         *
89
         * @param actionCommand DOCUMENT ME!
90
         */
91
        public void execute(String actionCommand) {
92
                if ("FILTRO".equals(actionCommand)) {
93
                        try {
94 6880 cesar
                                IWindow v = PluginServices.getMDIManager().getActiveWindow();
95 2183 fernando
96
                                if (v instanceof Table) {
97
                                        vista = (Table) v;
98 7392 sbayarri
99 3940 caballero
                                        dataSource = (SelectableDataSource)vista.getModel().getModelo().getRecordset();
100 7028 ldiaz
                                        filterTitle = vista.getModel().getName();
101 7392 sbayarri
102 7304 caballero
                                } else if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
103 7392 sbayarri
                                        IProjectView pv = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel();
104 7414 sbayarri
                                        filterTitle = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel().getName();
105 2610 fernando
                                        FLayer layer = pv.getMapContext()
106 4682 jorpiell
                                        .getLayers().getActives()[0];
107 7414 sbayarri
                                        dataSource = pv.getProject().getDataSourceByLayer(layer);
108 2183 fernando
                                }
109 4682 jorpiell
                        }  catch (DriverException e) {
110
                                NotificationManager.addError("Error filtrando", e);
111 6217 caballero
                        }
112 2183 fernando
113 4682 jorpiell
                        doExecute();
114
                }
115
        }
116 6217 caballero
117 4682 jorpiell
        /**
118 6217 caballero
         * "execute" method action.
119 4682 jorpiell
         *
120
         */
121
        protected void doExecute(){
122
                try{
123
                        DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
124
                        ds.setTable(dataSource);
125 2183 fernando
126 7028 ldiaz
                        FilterDialog dlg = new FilterDialog(filterTitle);
127 4682 jorpiell
                        dlg.addExpressionListener(this);
128
                        dlg.addExceptionListener(new ExceptionListener() {
129
                                public void exceptionThrown(Throwable t) {
130
                                        NotificationManager.addError(t.getMessage(), t);
131
                                }
132
                        });
133 2183 fernando
134 4682 jorpiell
                        dlg.setModel(ds);
135 6880 cesar
                        PluginServices.getMDIManager().addWindow(dlg);
136 4682 jorpiell
                } catch (DriverLoadException e) {
137
                        // TODO Auto-generated catch block
138
                        e.printStackTrace();
139 2183 fernando
                }
140
        }
141
142
        /**
143
         * DOCUMENT ME!
144
         *
145
         * @return DOCUMENT ME!
146
         */
147
        public boolean isEnabled() {
148 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
149 6778 jmvivo
150
                if (v == null) {
151
                        return false;
152
                }
153
154
                if (v instanceof Table) {
155
                        return true;
156
                } else {
157 7304 caballero
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
158
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
159 7392 sbayarri
                                IProjectView pv = view.getModel();
160 6778 jmvivo
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
161
                                .getActives();
162
163
                                if (seleccionadas.length == 1) {
164
                                        if (seleccionadas[0].isAvailable() && seleccionadas[0] instanceof AlphanumericData) {
165
                                                return true;
166
                                        }
167
                                }
168
                        }
169
170
                        return false;
171
                }
172
173 2183 fernando
        }
174
175
        /**
176
         * DOCUMENT ME!
177
         *
178
         * @return DOCUMENT ME!
179
         */
180
        public boolean isVisible() {
181 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
182 2183 fernando
183
                if (v == null) {
184
                        return false;
185
                }
186
187 5900 jorpiell
                if (v instanceof Table) {
188 2183 fernando
                        return true;
189
                } else {
190 7304 caballero
                        if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) {
191
                                com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v;
192 7392 sbayarri
                                IProjectView pv = view.getModel();
193 2183 fernando
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
194 4682 jorpiell
                                .getActives();
195 2183 fernando
196
                                if (seleccionadas.length == 1) {
197
                                        if (seleccionadas[0] instanceof AlphanumericData) {
198
                                                return true;
199
                                        }
200
                                }
201
                        }
202
203
                        return false;
204
                }
205
        }
206
207
        /**
208
         * DOCUMENT ME!
209
         *
210
         * @param expression DOCUMENT ME!
211
         */
212
        public void newSet(String expression) {
213
                long[] sel = doSet(expression);
214
215
                if (sel == null) {
216 6217 caballero
                        //throw new RuntimeException("Not a 'where' clause?");
217
                        return;
218 2183 fernando
                }
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
        }
229
230
        /**
231
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
232
         */
233
        private long[] doSet(String expression) {
234
                try {
235
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
236 2667 fernando
                                        DataSourceFactory.MANUAL_OPENING);
237 2183 fernando
238
                        return ds.getWhereFilter();
239
                } catch (DriverLoadException e) {
240
                        NotificationManager.addError("Error cargando el driver", e);
241
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
242
                        NotificationManager.addError("Error accediendo al driver", e);
243
                } catch (ParseException e) {
244
                        NotificationManager.addError("Parse error", e);
245
                } catch (SemanticException e) {
246
                        NotificationManager.addError(e.getMessage(), e);
247
                } catch (IOException e) {
248
                        NotificationManager.addError("GDBMS internal error", e);
249 2217 fernando
                } catch (EvaluationException e) {
250
                        NotificationManager.addError("Sintaxis incorrecta", e);
251 4682 jorpiell
                }
252 2183 fernando
253
                return null;
254
        }
255
256
        /**
257
         * DOCUMENT ME!
258
         *
259
         * @param expression DOCUMENT ME!
260
         */
261
        public void addToSet(String expression) {
262
                long[] sel = doSet(expression);
263
264
                if (sel == null) {
265 6217 caballero
                        //throw new RuntimeException("Not a 'where' clause?");
266
                        return;
267 2183 fernando
                }
268
269
                FBitSet selection = new FBitSet();
270
271
                for (int i = 0; i < sel.length; i++) {
272
                        selection.set((int) sel[i]);
273
                }
274
275
                FBitSet fbs = dataSource.getSelection();
276
                fbs.or(selection);
277
                dataSource.setSelection(fbs);
278
        }
279
280
        /**
281
         * DOCUMENT ME!
282
         *
283
         * @param expression DOCUMENT ME!
284
         */
285
        public void fromSet(String expression) {
286
                long[] sel = doSet(expression);
287
288
                if (sel == null) {
289
                        throw new RuntimeException("Not a 'where' clause?");
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.and(selection);
300
                dataSource.setSelection(fbs);
301
        }
302
}