svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / FiltroExtension.java @ 3718
History | View | Annotate | Download (6.82 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 | import com.hardcode.driverManager.DriverLoadException; |
||
44 | |||
45 | import com.hardcode.gdbms.engine.data.DataSource; |
||
46 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
47 | 2217 | fernando | import com.hardcode.gdbms.engine.instruction.EvaluationException; |
48 | 2183 | fernando | import com.hardcode.gdbms.engine.instruction.SemanticException; |
49 | import com.hardcode.gdbms.parser.ParseException; |
||
50 | |||
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 | |||
56 | import com.iver.cit.gvsig.fmap.DriverException; |
||
57 | import com.iver.cit.gvsig.fmap.layers.FBitSet; |
||
58 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
59 | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
||
60 | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
||
61 | import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
||
62 | import com.iver.cit.gvsig.gui.Table; |
||
63 | import com.iver.cit.gvsig.gui.filter.DefaultExpressionDataSource; |
||
64 | import com.iver.cit.gvsig.gui.filter.ExpressionListener; |
||
65 | import com.iver.cit.gvsig.gui.filter.FilterDialog; |
||
66 | 2610 | fernando | import com.iver.cit.gvsig.project.ProjectTable; |
67 | 2183 | fernando | import com.iver.cit.gvsig.project.ProjectView; |
68 | |||
69 | import com.iver.utiles.exceptionHandling.ExceptionListener; |
||
70 | |||
71 | import java.io.IOException; |
||
72 | 2610 | fernando | import java.util.ArrayList; |
73 | 2183 | fernando | |
74 | |||
75 | /**
|
||
76 | * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
|
||
77 | *
|
||
78 | * @author Vicente Caballero Navarro
|
||
79 | */
|
||
80 | public class FiltroExtension implements Extension, ExpressionListener { |
||
81 | private SelectableDataSource dataSource = null; |
||
82 | private Table vista;
|
||
83 | |||
84 | /**
|
||
85 | * DOCUMENT ME!
|
||
86 | */
|
||
87 | public void inicializar() { |
||
88 | } |
||
89 | |||
90 | /**
|
||
91 | * DOCUMENT ME!
|
||
92 | *
|
||
93 | * @param actionCommand DOCUMENT ME!
|
||
94 | */
|
||
95 | public void execute(String actionCommand) { |
||
96 | if ("FILTRO".equals(actionCommand)) { |
||
97 | try {
|
||
98 | View v = PluginServices.getMDIManager().getActiveView();
|
||
99 | |||
100 | if (v instanceof Table) { |
||
101 | vista = (Table) v; |
||
102 | dataSource = vista.getModel().getModelo(); |
||
103 | } else if (v instanceof com.iver.cit.gvsig.gui.View) { |
||
104 | ProjectView pv = ((com.iver.cit.gvsig.gui.View) v).getModel(); |
||
105 | |||
106 | 2610 | fernando | FLayer layer = pv.getMapContext() |
107 | .getLayers().getActives()[0];
|
||
108 | dataSource = pv.getProject().getDataSourceByLayer(layer); |
||
109 | 2183 | fernando | } |
110 | |||
111 | DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
|
||
112 | ds.setTable(dataSource); |
||
113 | |||
114 | FilterDialog dlg = new FilterDialog();
|
||
115 | dlg.addExpressionListener(this);
|
||
116 | dlg.addExceptionListener(new ExceptionListener() { |
||
117 | public void exceptionThrown(Throwable t) { |
||
118 | NotificationManager.addError(t.getMessage(), t); |
||
119 | } |
||
120 | }); |
||
121 | |||
122 | dlg.setModel(ds); |
||
123 | PluginServices.getMDIManager().addView(dlg); |
||
124 | } catch (DriverException e) {
|
||
125 | NotificationManager.addError("Error filtrando", e);
|
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /**
|
||
131 | * DOCUMENT ME!
|
||
132 | *
|
||
133 | * @return DOCUMENT ME!
|
||
134 | */
|
||
135 | public boolean isEnabled() { |
||
136 | return true; |
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * DOCUMENT ME!
|
||
141 | *
|
||
142 | * @return DOCUMENT ME!
|
||
143 | */
|
||
144 | public boolean isVisible() { |
||
145 | View v = PluginServices.getMDIManager().getActiveView();
|
||
146 | |||
147 | if (v == null) { |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | if (v.getClass() == Table.class) {
|
||
152 | return true; |
||
153 | } else {
|
||
154 | if (v instanceof com.iver.cit.gvsig.gui.View) { |
||
155 | com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v; |
||
156 | ProjectView pv = view.getModel(); |
||
157 | FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
||
158 | .getActives(); |
||
159 | |||
160 | if (seleccionadas.length == 1) { |
||
161 | if (seleccionadas[0] instanceof AlphanumericData) { |
||
162 | return true; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | return false; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /**
|
||
172 | * DOCUMENT ME!
|
||
173 | *
|
||
174 | * @param expression DOCUMENT ME!
|
||
175 | */
|
||
176 | public void newSet(String expression) { |
||
177 | long[] sel = doSet(expression); |
||
178 | |||
179 | if (sel == null) { |
||
180 | throw new RuntimeException("Not a 'where' clause?"); |
||
181 | } |
||
182 | |||
183 | FBitSet selection = new FBitSet();
|
||
184 | |||
185 | for (int i = 0; i < sel.length; i++) { |
||
186 | selection.set((int) sel[i]);
|
||
187 | } |
||
188 | |||
189 | dataSource.clearSelection(); |
||
190 | dataSource.setSelection(selection); |
||
191 | } |
||
192 | |||
193 | /**
|
||
194 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
||
195 | */
|
||
196 | private long[] doSet(String expression) { |
||
197 | try {
|
||
198 | DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
|
||
199 | 2667 | fernando | DataSourceFactory.MANUAL_OPENING); |
200 | 2183 | fernando | |
201 | return ds.getWhereFilter();
|
||
202 | } catch (DriverLoadException e) {
|
||
203 | NotificationManager.addError("Error cargando el driver", e);
|
||
204 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
205 | NotificationManager.addError("Error accediendo al driver", e);
|
||
206 | } catch (ParseException e) { |
||
207 | NotificationManager.addError("Parse error", e);
|
||
208 | } catch (SemanticException e) {
|
||
209 | NotificationManager.addError(e.getMessage(), e); |
||
210 | } catch (IOException e) { |
||
211 | NotificationManager.addError("GDBMS internal error", e);
|
||
212 | 2217 | fernando | } catch (EvaluationException e) {
|
213 | NotificationManager.addError("Sintaxis incorrecta", e);
|
||
214 | } |
||
215 | 2183 | fernando | |
216 | return null; |
||
217 | } |
||
218 | |||
219 | /**
|
||
220 | * DOCUMENT ME!
|
||
221 | *
|
||
222 | * @param expression DOCUMENT ME!
|
||
223 | */
|
||
224 | public void addToSet(String expression) { |
||
225 | long[] sel = doSet(expression); |
||
226 | |||
227 | if (sel == null) { |
||
228 | throw new RuntimeException("Not a 'where' clause?"); |
||
229 | } |
||
230 | |||
231 | FBitSet selection = new FBitSet();
|
||
232 | |||
233 | for (int i = 0; i < sel.length; i++) { |
||
234 | selection.set((int) sel[i]);
|
||
235 | } |
||
236 | |||
237 | FBitSet fbs = dataSource.getSelection(); |
||
238 | fbs.or(selection); |
||
239 | dataSource.setSelection(fbs); |
||
240 | } |
||
241 | |||
242 | /**
|
||
243 | * DOCUMENT ME!
|
||
244 | *
|
||
245 | * @param expression DOCUMENT ME!
|
||
246 | */
|
||
247 | public void fromSet(String expression) { |
||
248 | long[] sel = doSet(expression); |
||
249 | |||
250 | if (sel == null) { |
||
251 | throw new RuntimeException("Not a 'where' clause?"); |
||
252 | } |
||
253 | |||
254 | FBitSet selection = new FBitSet();
|
||
255 | |||
256 | for (int i = 0; i < sel.length; i++) { |
||
257 | selection.set((int) sel[i]);
|
||
258 | } |
||
259 | |||
260 | FBitSet fbs = dataSource.getSelection(); |
||
261 | fbs.and(selection); |
||
262 | dataSource.setSelection(fbs); |
||
263 | } |
||
264 | } |