Statistics
| Revision:

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

History | View | Annotate | Download (6.93 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.getClass() == Table.class) {
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
                }
189

    
190
                FBitSet selection = new FBitSet();
191

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

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

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

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

    
223
                return null;
224
        }
225

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

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

    
238
                FBitSet selection = new FBitSet();
239

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

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

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

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

    
261
                FBitSet selection = new FBitSet();
262

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

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