root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / FiltroExtension.java @ 9212
History | View | Annotate | Download (8.16 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 | 8559 | ldiaz | import java.awt.Component; |
44 | 4682 | jorpiell | import java.io.IOException; |
45 | |||
46 | 8559 | ldiaz | import javax.swing.JOptionPane; |
47 | |||
48 | 2183 | fernando | import com.hardcode.driverManager.DriverLoadException; |
49 | import com.hardcode.gdbms.engine.data.DataSource; |
||
50 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
51 | 2217 | fernando | import com.hardcode.gdbms.engine.instruction.EvaluationException; |
52 | 2183 | fernando | import com.hardcode.gdbms.engine.instruction.SemanticException; |
53 | import com.hardcode.gdbms.parser.ParseException; |
||
54 | import com.iver.andami.PluginServices; |
||
55 | import com.iver.andami.messages.NotificationManager; |
||
56 | import com.iver.andami.plugins.Extension; |
||
57 | 6877 | cesar | import com.iver.andami.ui.mdiManager.IWindow; |
58 | 2183 | fernando | import com.iver.cit.gvsig.fmap.DriverException; |
59 | import com.iver.cit.gvsig.fmap.layers.FBitSet; |
||
60 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
61 | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
||
62 | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
||
63 | import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
||
64 | import com.iver.cit.gvsig.gui.filter.DefaultExpressionDataSource; |
||
65 | import com.iver.cit.gvsig.gui.filter.ExpressionListener; |
||
66 | import com.iver.cit.gvsig.gui.filter.FilterDialog; |
||
67 | 7304 | caballero | import com.iver.cit.gvsig.project.documents.table.gui.Table; |
68 | 7392 | sbayarri | import com.iver.cit.gvsig.project.documents.view.IProjectView; |
69 | 2183 | fernando | import com.iver.utiles.exceptionHandling.ExceptionListener; |
70 | |||
71 | |||
72 | 6217 | caballero | /**
|
73 | 2183 | fernando | * 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 | 5005 | jorpiell | public class FiltroExtension extends Extension implements ExpressionListener { |
78 | 4682 | jorpiell | protected SelectableDataSource dataSource = null; |
79 | protected Table vista;
|
||
80 | 7028 | ldiaz | private String filterTitle; |
81 | 2183 | fernando | |
82 | /**
|
||
83 | * DOCUMENT ME!
|
||
84 | */
|
||
85 | 5005 | jorpiell | public void initialize() { |
86 | 2183 | fernando | } |
87 | |||
88 | /**
|
||
89 | * DOCUMENT ME!
|
||
90 | *
|
||
91 | * @param actionCommand DOCUMENT ME!
|
||
92 | */
|
||
93 | public void execute(String actionCommand) { |
||
94 | if ("FILTRO".equals(actionCommand)) { |
||
95 | try {
|
||
96 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
97 | 2183 | fernando | |
98 | if (v instanceof Table) { |
||
99 | vista = (Table) v; |
||
100 | 7392 | sbayarri | |
101 | 3940 | caballero | dataSource = (SelectableDataSource)vista.getModel().getModelo().getRecordset(); |
102 | 7028 | ldiaz | filterTitle = vista.getModel().getName(); |
103 | 7392 | sbayarri | |
104 | 7304 | caballero | } else if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) { |
105 | 7392 | sbayarri | IProjectView pv = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel(); |
106 | 7414 | sbayarri | filterTitle = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel().getName(); |
107 | 2610 | fernando | FLayer layer = pv.getMapContext() |
108 | 4682 | jorpiell | .getLayers().getActives()[0];
|
109 | 7414 | sbayarri | dataSource = pv.getProject().getDataSourceByLayer(layer); |
110 | 2183 | fernando | } |
111 | 4682 | jorpiell | } catch (DriverException e) {
|
112 | NotificationManager.addError("Error filtrando", e);
|
||
113 | 6217 | caballero | } |
114 | 2183 | fernando | |
115 | 4682 | jorpiell | doExecute(); |
116 | } |
||
117 | } |
||
118 | 6217 | caballero | |
119 | 4682 | jorpiell | /**
|
120 | 6217 | caballero | * "execute" method action.
|
121 | 4682 | jorpiell | *
|
122 | */
|
||
123 | protected void doExecute(){ |
||
124 | try{
|
||
125 | DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
|
||
126 | ds.setTable(dataSource); |
||
127 | 2183 | fernando | |
128 | 7028 | ldiaz | FilterDialog dlg = new FilterDialog(filterTitle);
|
129 | 4682 | jorpiell | dlg.addExpressionListener(this);
|
130 | dlg.addExceptionListener(new ExceptionListener() { |
||
131 | public void exceptionThrown(Throwable t) { |
||
132 | NotificationManager.addError(t.getMessage(), t); |
||
133 | } |
||
134 | }); |
||
135 | 2183 | fernando | |
136 | 4682 | jorpiell | dlg.setModel(ds); |
137 | 6880 | cesar | PluginServices.getMDIManager().addWindow(dlg); |
138 | 4682 | jorpiell | } catch (DriverLoadException e) {
|
139 | // TODO Auto-generated catch block
|
||
140 | e.printStackTrace(); |
||
141 | 2183 | fernando | } |
142 | } |
||
143 | |||
144 | /**
|
||
145 | * DOCUMENT ME!
|
||
146 | *
|
||
147 | * @return DOCUMENT ME!
|
||
148 | */
|
||
149 | public boolean isEnabled() { |
||
150 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
151 | 6778 | jmvivo | |
152 | if (v == null) { |
||
153 | return false; |
||
154 | } |
||
155 | |||
156 | if (v instanceof Table) { |
||
157 | return true; |
||
158 | } else {
|
||
159 | 7304 | caballero | if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) { |
160 | com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v; |
||
161 | 7392 | sbayarri | IProjectView pv = view.getModel(); |
162 | 6778 | jmvivo | FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
163 | .getActives(); |
||
164 | |||
165 | if (seleccionadas.length == 1) { |
||
166 | if (seleccionadas[0].isAvailable() && seleccionadas[0] instanceof AlphanumericData) { |
||
167 | return true; |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | return false; |
||
173 | } |
||
174 | |||
175 | 2183 | fernando | } |
176 | |||
177 | /**
|
||
178 | * DOCUMENT ME!
|
||
179 | *
|
||
180 | * @return DOCUMENT ME!
|
||
181 | */
|
||
182 | public boolean isVisible() { |
||
183 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
184 | 2183 | fernando | |
185 | if (v == null) { |
||
186 | return false; |
||
187 | } |
||
188 | |||
189 | 5900 | jorpiell | if (v instanceof Table) { |
190 | 2183 | fernando | return true; |
191 | } else {
|
||
192 | 7304 | caballero | if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) { |
193 | com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v; |
||
194 | 7392 | sbayarri | IProjectView pv = view.getModel(); |
195 | 2183 | fernando | FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
196 | 4682 | jorpiell | .getActives(); |
197 | 2183 | fernando | |
198 | if (seleccionadas.length == 1) { |
||
199 | if (seleccionadas[0] instanceof AlphanumericData) { |
||
200 | return true; |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return false; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | /**
|
||
210 | * DOCUMENT ME!
|
||
211 | *
|
||
212 | * @param expression DOCUMENT ME!
|
||
213 | */
|
||
214 | public void newSet(String expression) { |
||
215 | 8559 | ldiaz | |
216 | try {
|
||
217 | long[] sel = doSet(expression); |
||
218 | |||
219 | if (sel == null) { |
||
220 | //throw new RuntimeException("Not a 'where' clause?");
|
||
221 | return;
|
||
222 | } |
||
223 | |||
224 | FBitSet selection = new FBitSet();
|
||
225 | |||
226 | for (int i = 0; i < sel.length; i++) { |
||
227 | selection.set((int) sel[i]);
|
||
228 | } |
||
229 | |||
230 | dataSource.clearSelection(); |
||
231 | dataSource.setSelection(selection); |
||
232 | }catch(Exception e){ |
||
233 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta."); |
||
234 | 2183 | fernando | } |
235 | 8559 | ldiaz | |
236 | 2183 | fernando | } |
237 | |||
238 | /**
|
||
239 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
||
240 | */
|
||
241 | private long[] doSet(String expression) { |
||
242 | try {
|
||
243 | DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
|
||
244 | 2667 | fernando | DataSourceFactory.MANUAL_OPENING); |
245 | 2183 | fernando | |
246 | return ds.getWhereFilter();
|
||
247 | } catch (DriverLoadException e) {
|
||
248 | NotificationManager.addError("Error cargando el driver", e);
|
||
249 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
250 | NotificationManager.addError("Error accediendo al driver", e);
|
||
251 | } catch (ParseException e) { |
||
252 | NotificationManager.addError("Parse error", e);
|
||
253 | } catch (SemanticException e) {
|
||
254 | NotificationManager.addError(e.getMessage(), e); |
||
255 | } catch (IOException e) { |
||
256 | NotificationManager.addError("GDBMS internal error", e);
|
||
257 | 2217 | fernando | } catch (EvaluationException e) {
|
258 | NotificationManager.addError("Sintaxis incorrecta", e);
|
||
259 | 4682 | jorpiell | } |
260 | 2183 | fernando | |
261 | return null; |
||
262 | } |
||
263 | |||
264 | /**
|
||
265 | * DOCUMENT ME!
|
||
266 | *
|
||
267 | * @param expression DOCUMENT ME!
|
||
268 | */
|
||
269 | public void addToSet(String expression) { |
||
270 | long[] sel = doSet(expression); |
||
271 | |||
272 | if (sel == null) { |
||
273 | 6217 | caballero | //throw new RuntimeException("Not a 'where' clause?");
|
274 | return;
|
||
275 | 2183 | fernando | } |
276 | |||
277 | FBitSet selection = new FBitSet();
|
||
278 | |||
279 | for (int i = 0; i < sel.length; i++) { |
||
280 | selection.set((int) sel[i]);
|
||
281 | } |
||
282 | |||
283 | FBitSet fbs = dataSource.getSelection(); |
||
284 | fbs.or(selection); |
||
285 | dataSource.setSelection(fbs); |
||
286 | } |
||
287 | |||
288 | /**
|
||
289 | * DOCUMENT ME!
|
||
290 | *
|
||
291 | * @param expression DOCUMENT ME!
|
||
292 | */
|
||
293 | public void fromSet(String expression) { |
||
294 | long[] sel = doSet(expression); |
||
295 | |||
296 | if (sel == null) { |
||
297 | throw new RuntimeException("Not a 'where' clause?"); |
||
298 | } |
||
299 | |||
300 | FBitSet selection = new FBitSet();
|
||
301 | |||
302 | for (int i = 0; i < sel.length; i++) { |
||
303 | selection.set((int) sel[i]);
|
||
304 | } |
||
305 | |||
306 | FBitSet fbs = dataSource.getSelection(); |
||
307 | fbs.and(selection); |
||
308 | dataSource.setSelection(fbs); |
||
309 | } |
||
310 | } |