Statistics
| Revision:

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

History | View | Annotate | Download (6.95 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.View;
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

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

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

    
94
                                if (v instanceof Table) {
95
                                        vista = (Table) v;
96
                                        dataSource = (SelectableDataSource)vista.getModel().getModelo().getRecordset();
97
                                } else if (v instanceof com.iver.cit.gvsig.gui.View) {
98
                                        ProjectView pv = ((com.iver.cit.gvsig.gui.View) v).getModel();
99

    
100
                                        FLayer layer = pv.getMapContext()
101
                                        .getLayers().getActives()[0];
102
                                        dataSource = pv.getProject().getDataSourceByLayer(layer);
103
                                }
104
                        }  catch (DriverException e) {
105
                                NotificationManager.addError("Error filtrando", e);
106
                        }
107

    
108
                        doExecute();
109
                }
110
        }
111

    
112
        /**
113
         * "execute" method action.
114
         *
115
         */
116
        protected void doExecute(){
117
                try{
118
                        DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
119
                        ds.setTable(dataSource);
120

    
121
                        FilterDialog dlg = new FilterDialog();
122
                        dlg.addExpressionListener(this);
123
                        dlg.addExceptionListener(new ExceptionListener() {
124
                                public void exceptionThrown(Throwable t) {
125
                                        NotificationManager.addError(t.getMessage(), t);
126
                                }
127
                        });
128

    
129
                        dlg.setModel(ds);
130
                        PluginServices.getMDIManager().addView(dlg);
131
                } catch (DriverLoadException e) {
132
                        // TODO Auto-generated catch block
133
                        e.printStackTrace();
134
                }
135
        }
136

    
137
        /**
138
         * DOCUMENT ME!
139
         *
140
         * @return DOCUMENT ME!
141
         */
142
        public boolean isEnabled() {
143
                return true;
144
        }
145

    
146
        /**
147
         * DOCUMENT ME!
148
         *
149
         * @return DOCUMENT ME!
150
         */
151
        public boolean isVisible() {
152
                View v = PluginServices.getMDIManager().getActiveView();
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.gui.View) {
162
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
163
                                ProjectView pv = view.getModel();
164
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
165
                                .getActives();
166

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

    
174
                        return false;
175
                }
176
        }
177

    
178
        /**
179
         * DOCUMENT ME!
180
         *
181
         * @param expression DOCUMENT ME!
182
         */
183
        public void newSet(String expression) {
184
                long[] sel = doSet(expression);
185

    
186
                if (sel == null) {
187
                        //throw new RuntimeException("Not a 'where' clause?");
188
                        return;
189
                }
190

    
191
                FBitSet selection = new FBitSet();
192

    
193
                for (int i = 0; i < sel.length; i++) {
194
                        selection.set((int) sel[i]);
195
                }
196

    
197
                dataSource.clearSelection();
198
                dataSource.setSelection(selection);
199
        }
200

    
201
        /**
202
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
203
         */
204
        private long[] doSet(String expression) {
205
                try {
206
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
207
                                        DataSourceFactory.MANUAL_OPENING);
208

    
209
                        return ds.getWhereFilter();
210
                } catch (DriverLoadException e) {
211
                        NotificationManager.addError("Error cargando el driver", e);
212
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
213
                        NotificationManager.addError("Error accediendo al driver", e);
214
                } catch (ParseException e) {
215
                        NotificationManager.addError("Parse error", e);
216
                } catch (SemanticException e) {
217
                        NotificationManager.addError(e.getMessage(), e);
218
                } catch (IOException e) {
219
                        NotificationManager.addError("GDBMS internal error", e);
220
                } catch (EvaluationException e) {
221
                        NotificationManager.addError("Sintaxis incorrecta", e);
222
                }
223

    
224
                return null;
225
        }
226

    
227
        /**
228
         * DOCUMENT ME!
229
         *
230
         * @param expression DOCUMENT ME!
231
         */
232
        public void addToSet(String expression) {
233
                long[] sel = doSet(expression);
234

    
235
                if (sel == null) {
236
                        //throw new RuntimeException("Not a 'where' clause?");
237
                        return;
238
                }
239

    
240
                FBitSet selection = new FBitSet();
241

    
242
                for (int i = 0; i < sel.length; i++) {
243
                        selection.set((int) sel[i]);
244
                }
245

    
246
                FBitSet fbs = dataSource.getSelection();
247
                fbs.or(selection);
248
                dataSource.setSelection(fbs);
249
        }
250

    
251
        /**
252
         * DOCUMENT ME!
253
         *
254
         * @param expression DOCUMENT ME!
255
         */
256
        public void fromSet(String expression) {
257
                long[] sel = doSet(expression);
258

    
259
                if (sel == null) {
260
                        throw new RuntimeException("Not a 'where' clause?");
261
                }
262

    
263
                FBitSet selection = new FBitSet();
264

    
265
                for (int i = 0; i < sel.length; i++) {
266
                        selection.set((int) sel[i]);
267
                }
268

    
269
                FBitSet fbs = dataSource.getSelection();
270
                fbs.and(selection);
271
                dataSource.setSelection(fbs);
272
        }
273
}