Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / FiltroExtension.java @ 2174

History | View | Annotate | Download (6.54 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 com.hardcode.driverManager.DriverLoadException;
44

    
45
import com.hardcode.gdbms.engine.data.DataSource;
46
import com.hardcode.gdbms.engine.data.DataSourceFactory;
47
import com.hardcode.gdbms.engine.instruction.SemanticException;
48
import com.hardcode.gdbms.parser.ParseException;
49

    
50
import com.iver.andami.PluginServices;
51
import com.iver.andami.messages.NotificationManager;
52
import com.iver.andami.plugins.Extension;
53
import com.iver.andami.ui.mdiManager.View;
54

    
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

    
67
import com.iver.utiles.exceptionHandling.ExceptionListener;
68

    
69
import java.io.IOException;
70

    
71

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

    
81
        /**
82
         * DOCUMENT ME!
83
         */
84
        public void inicializar() {
85
        }
86

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

    
97
                                if (v instanceof Table) {
98
                                        vista = (Table) v;
99
                                        dataSource = vista.getModel().getModelo();
100
                                } else if (v instanceof com.iver.cit.gvsig.gui.View) {
101
                                        ProjectView pv = ((com.iver.cit.gvsig.gui.View) v).getModel();
102

    
103
                                        dataSource = ((AlphanumericData) pv.getMapContext()
104
                                                                                                           .getLayers().getActives()[0]).getRecordset();
105
                                }
106

    
107
                                DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
108
                                ds.setTable(dataSource);
109

    
110
                                FilterDialog dlg = new FilterDialog();
111
                                dlg.addExpressionListener(this);
112
                                dlg.addExceptionListener(new ExceptionListener() {
113
                                                public void exceptionThrown(Throwable t) {
114
                                                        NotificationManager.addError(t.getMessage(), t);
115
                                                }
116
                                        });
117

    
118
                                dlg.setModel(ds);
119
                                PluginServices.getMDIManager().addView(dlg);
120
                        } catch (DriverException e) {
121
                                NotificationManager.addError("Error filtrando", e);
122
                        }
123
                }
124
        }
125

    
126
        /**
127
         * DOCUMENT ME!
128
         *
129
         * @return DOCUMENT ME!
130
         */
131
        public boolean isEnabled() {
132
                return true;
133
        }
134

    
135
        /**
136
         * DOCUMENT ME!
137
         *
138
         * @return DOCUMENT ME!
139
         */
140
        public boolean isVisible() {
141
                View v = PluginServices.getMDIManager().getActiveView();
142

    
143
                if (v == null) {
144
                        return false;
145
                }
146

    
147
                if (v.getClass() == Table.class) {
148
                        return true;
149
                } else {
150
                        if (v instanceof com.iver.cit.gvsig.gui.View) {
151
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
152
                                ProjectView pv = view.getModel();
153
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
154
                                                                                   .getActives();
155

    
156
                                if (seleccionadas.length == 1) {
157
                                        if (seleccionadas[0] instanceof AlphanumericData) {
158
                                                return true;
159
                                        }
160
                                }
161
                        }
162

    
163
                        return false;
164
                }
165
        }
166

    
167
        /**
168
         * DOCUMENT ME!
169
         *
170
         * @param expression DOCUMENT ME!
171
         */
172
        public void newSet(String expression) {
173
                long[] sel = doSet(expression);
174

    
175
                if (sel == null) {
176
                        throw new RuntimeException("Not a 'where' clause?");
177
                }
178

    
179
                FBitSet selection = new FBitSet();
180

    
181
                for (int i = 0; i < sel.length; i++) {
182
                        selection.set((int) sel[i]);
183
                }
184

    
185
                dataSource.clearSelection();
186
                dataSource.setSelection(selection);
187
        }
188

    
189
        /**
190
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
191
         */
192
        private long[] doSet(String expression) {
193
                try {
194
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
195
                                        DataSourceFactory.CLOSING_MANUAL_MODE);
196

    
197
                        return ds.getWhereFilter();
198
                } catch (DriverLoadException e) {
199
                        NotificationManager.addError("Error cargando el driver", e);
200
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
201
                        NotificationManager.addError("Error accediendo al driver", e);
202
                } catch (ParseException e) {
203
                        NotificationManager.addError("Parse error", e);
204
                } catch (SemanticException e) {
205
                        NotificationManager.addError(e.getMessage(), e);
206
                } catch (IOException e) {
207
                        NotificationManager.addError("GDBMS internal error", e);
208
                }
209

    
210
                return null;
211
        }
212

    
213
        /**
214
         * DOCUMENT ME!
215
         *
216
         * @param expression DOCUMENT ME!
217
         */
218
        public void addToSet(String expression) {
219
                long[] sel = doSet(expression);
220

    
221
                if (sel == null) {
222
                        throw new RuntimeException("Not a 'where' clause?");
223
                }
224

    
225
                FBitSet selection = new FBitSet();
226

    
227
                for (int i = 0; i < sel.length; i++) {
228
                        selection.set((int) sel[i]);
229
                }
230

    
231
                FBitSet fbs = dataSource.getSelection();
232
                fbs.or(selection);
233
                dataSource.setSelection(fbs);
234
        }
235

    
236
        /**
237
         * DOCUMENT ME!
238
         *
239
         * @param expression DOCUMENT ME!
240
         */
241
        public void fromSet(String expression) {
242
                long[] sel = doSet(expression);
243

    
244
                if (sel == null) {
245
                        throw new RuntimeException("Not a 'where' clause?");
246
                }
247

    
248
                FBitSet selection = new FBitSet();
249

    
250
                for (int i = 0; i < sel.length; i++) {
251
                        selection.set((int) sel[i]);
252
                }
253

    
254
                FBitSet fbs = dataSource.getSelection();
255
                fbs.and(selection);
256
                dataSource.setSelection(fbs);
257
        }
258
}