Statistics
| Revision:

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

History | View | Annotate | Download (7.69 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.io.IOException;
44

    
45
import com.hardcode.driverManager.DriverLoadException;
46
import com.hardcode.gdbms.engine.data.DataSource;
47
import com.hardcode.gdbms.engine.data.DataSourceFactory;
48
import com.hardcode.gdbms.engine.instruction.EvaluationException;
49
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
import com.iver.andami.ui.mdiManager.IWindow;
55
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.Table;
62
import com.iver.cit.gvsig.gui.filter.DefaultExpressionDataSource;
63
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
64
import com.iver.cit.gvsig.gui.filter.FilterDialog;
65
import com.iver.cit.gvsig.project.ProjectView;
66
import com.iver.utiles.exceptionHandling.ExceptionListener;
67

    
68

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

    
79
        /**
80
         * DOCUMENT ME!
81
         */
82
        public void initialize() {
83
        }
84

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

    
95
                                if (v instanceof Table) {
96
                                        vista = (Table) v;
97
                                        
98
                                        dataSource = (SelectableDataSource)vista.getModel().getModelo().getRecordset();
99
                                        filterTitle = vista.getModel().getName();
100
                                        
101
                                } else if (v instanceof com.iver.cit.gvsig.gui.View) {
102
                                        ProjectView pv = ((com.iver.cit.gvsig.gui.View) v).getModel();
103
                                        filterTitle = ((com.iver.cit.gvsig.gui.View) v).getModel().getName();
104
                                        FLayer layer = pv.getMapContext()
105
                                        .getLayers().getActives()[0];
106
                                        dataSource = pv.getProject().getDataSourceByLayer(layer);
107
                                }
108
                        }  catch (DriverException e) {
109
                                NotificationManager.addError("Error filtrando", e);
110
                        }
111

    
112
                        doExecute();
113
                }
114
        }
115

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

    
125
                        FilterDialog dlg = new FilterDialog(filterTitle);
126
                        dlg.addExpressionListener(this);
127
                        dlg.addExceptionListener(new ExceptionListener() {
128
                                public void exceptionThrown(Throwable t) {
129
                                        NotificationManager.addError(t.getMessage(), t);
130
                                }
131
                        });
132

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

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

    
149
                if (v == null) {
150
                        return false;
151
                }
152

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

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

    
169
                        return false;
170
                }
171

    
172
        }
173

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

    
182
                if (v == null) {
183
                        return false;
184
                }
185

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

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

    
202
                        return false;
203
                }
204
        }
205

    
206
        /**
207
         * DOCUMENT ME!
208
         *
209
         * @param expression DOCUMENT ME!
210
         */
211
        public void newSet(String expression) {
212
                long[] sel = doSet(expression);
213

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

    
219
                FBitSet selection = new FBitSet();
220

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

    
225
                dataSource.clearSelection();
226
                dataSource.setSelection(selection);
227
        }
228

    
229
        /**
230
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
231
         */
232
        private long[] doSet(String expression) {
233
                try {
234
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
235
                                        DataSourceFactory.MANUAL_OPENING);
236

    
237
                        return ds.getWhereFilter();
238
                } catch (DriverLoadException e) {
239
                        NotificationManager.addError("Error cargando el driver", e);
240
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
241
                        NotificationManager.addError("Error accediendo al driver", e);
242
                } catch (ParseException e) {
243
                        NotificationManager.addError("Parse error", e);
244
                } catch (SemanticException e) {
245
                        NotificationManager.addError(e.getMessage(), e);
246
                } catch (IOException e) {
247
                        NotificationManager.addError("GDBMS internal error", e);
248
                } catch (EvaluationException e) {
249
                        NotificationManager.addError("Sintaxis incorrecta", e);
250
                }
251

    
252
                return null;
253
        }
254

    
255
        /**
256
         * DOCUMENT ME!
257
         *
258
         * @param expression DOCUMENT ME!
259
         */
260
        public void addToSet(String expression) {
261
                long[] sel = doSet(expression);
262

    
263
                if (sel == null) {
264
                        //throw new RuntimeException("Not a 'where' clause?");
265
                        return;
266
                }
267

    
268
                FBitSet selection = new FBitSet();
269

    
270
                for (int i = 0; i < sel.length; i++) {
271
                        selection.set((int) sel[i]);
272
                }
273

    
274
                FBitSet fbs = dataSource.getSelection();
275
                fbs.or(selection);
276
                dataSource.setSelection(fbs);
277
        }
278

    
279
        /**
280
         * DOCUMENT ME!
281
         *
282
         * @param expression DOCUMENT ME!
283
         */
284
        public void fromSet(String expression) {
285
                long[] sel = doSet(expression);
286

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

    
291
                FBitSet selection = new FBitSet();
292

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

    
297
                FBitSet fbs = dataSource.getSelection();
298
                fbs.and(selection);
299
                dataSource.setSelection(fbs);
300
        }
301
}