svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / FiltroExtension.java @ 34395
History | View | Annotate | Download (10.8 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 | 10626 | caballero | import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
50 | 2183 | fernando | import com.hardcode.gdbms.engine.data.DataSource; |
51 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
52 | 2217 | fernando | import com.hardcode.gdbms.engine.instruction.EvaluationException; |
53 | 2183 | fernando | import com.hardcode.gdbms.engine.instruction.SemanticException; |
54 | import com.hardcode.gdbms.parser.ParseException; |
||
55 | import com.iver.andami.PluginServices; |
||
56 | import com.iver.andami.messages.NotificationManager; |
||
57 | import com.iver.andami.plugins.Extension; |
||
58 | 6877 | cesar | import com.iver.andami.ui.mdiManager.IWindow; |
59 | 2183 | fernando | 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 | 9532 | caballero | import com.iver.cit.gvsig.project.documents.ProjectDocument; |
68 | 7304 | caballero | import com.iver.cit.gvsig.project.documents.table.gui.Table; |
69 | 7392 | sbayarri | import com.iver.cit.gvsig.project.documents.view.IProjectView; |
70 | 2183 | fernando | import com.iver.utiles.exceptionHandling.ExceptionListener; |
71 | |||
72 | |||
73 | 6217 | caballero | /**
|
74 | 2183 | fernando | * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
|
75 | *
|
||
76 | * @author Vicente Caballero Navarro
|
||
77 | */
|
||
78 | 5005 | jorpiell | public class FiltroExtension extends Extension implements ExpressionListener { |
79 | 4682 | jorpiell | protected SelectableDataSource dataSource = null; |
80 | protected Table vista;
|
||
81 | 7028 | ldiaz | private String filterTitle; |
82 | 2183 | fernando | |
83 | /**
|
||
84 | * DOCUMENT ME!
|
||
85 | */
|
||
86 | 5005 | jorpiell | public void initialize() { |
87 | 14821 | jmvivo | registerIcons(); |
88 | 2183 | fernando | } |
89 | |||
90 | 14821 | jmvivo | private void registerIcons(){ |
91 | 15647 | jmvivo | PluginServices.getIconTheme().registerDefault( |
92 | 14821 | jmvivo | "table-filter",
|
93 | this.getClass().getClassLoader().getResource("images/Filtro.png") |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | 2183 | fernando | /**
|
98 | * DOCUMENT ME!
|
||
99 | *
|
||
100 | * @param actionCommand DOCUMENT ME!
|
||
101 | */
|
||
102 | public void execute(String actionCommand) { |
||
103 | if ("FILTRO".equals(actionCommand)) { |
||
104 | try {
|
||
105 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
106 | 2183 | fernando | |
107 | if (v instanceof Table) { |
||
108 | vista = (Table) v; |
||
109 | 7392 | sbayarri | |
110 | 9532 | caballero | dataSource = vista.getModel().getModelo().getRecordset(); |
111 | 7028 | ldiaz | filterTitle = vista.getModel().getName(); |
112 | 9532 | caballero | vista.getModel().setModified(true);
|
113 | 7304 | caballero | } else if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) { |
114 | 7392 | sbayarri | IProjectView pv = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel(); |
115 | 7414 | sbayarri | filterTitle = ((com.iver.cit.gvsig.project.documents.view.gui.View) v).getModel().getName(); |
116 | 2610 | fernando | FLayer layer = pv.getMapContext() |
117 | 4682 | jorpiell | .getLayers().getActives()[0];
|
118 | 7414 | sbayarri | dataSource = pv.getProject().getDataSourceByLayer(layer); |
119 | 9532 | caballero | ((ProjectDocument)pv).setModified(true);
|
120 | 2183 | fernando | } |
121 | 10626 | caballero | } catch (ReadDriverException e) {
|
122 | 4682 | jorpiell | NotificationManager.addError("Error filtrando", e);
|
123 | 6217 | caballero | } |
124 | 2183 | fernando | |
125 | 4682 | jorpiell | doExecute(); |
126 | } |
||
127 | } |
||
128 | 6217 | caballero | |
129 | 4682 | jorpiell | /**
|
130 | 6217 | caballero | * "execute" method action.
|
131 | 4682 | jorpiell | *
|
132 | */
|
||
133 | protected void doExecute(){ |
||
134 | 10661 | caballero | DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
|
135 | ds.setTable(dataSource); |
||
136 | FilterDialog dlg = new FilterDialog(filterTitle);
|
||
137 | dlg.addExpressionListener(this);
|
||
138 | dlg.addExceptionListener(new ExceptionListener() { |
||
139 | public void exceptionThrown(Throwable t) { |
||
140 | NotificationManager.addError(t.getMessage(), t); |
||
141 | } |
||
142 | }); |
||
143 | dlg.setModel(ds); |
||
144 | PluginServices.getMDIManager().addWindow(dlg); |
||
145 | 2183 | fernando | } |
146 | |||
147 | /**
|
||
148 | * DOCUMENT ME!
|
||
149 | *
|
||
150 | * @return DOCUMENT ME!
|
||
151 | */
|
||
152 | public boolean isEnabled() { |
||
153 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
154 | 6778 | jmvivo | |
155 | if (v == null) { |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | if (v instanceof Table) { |
||
160 | return true; |
||
161 | } else {
|
||
162 | 7304 | caballero | if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) { |
163 | com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v; |
||
164 | 7392 | sbayarri | IProjectView pv = view.getModel(); |
165 | 6778 | jmvivo | FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
166 | .getActives(); |
||
167 | |||
168 | if (seleccionadas.length == 1) { |
||
169 | if (seleccionadas[0].isAvailable() && seleccionadas[0] instanceof AlphanumericData) { |
||
170 | return true; |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | |||
175 | return false; |
||
176 | } |
||
177 | |||
178 | 2183 | fernando | } |
179 | |||
180 | /**
|
||
181 | * DOCUMENT ME!
|
||
182 | *
|
||
183 | * @return DOCUMENT ME!
|
||
184 | */
|
||
185 | public boolean isVisible() { |
||
186 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
187 | 2183 | fernando | |
188 | if (v == null) { |
||
189 | return false; |
||
190 | } |
||
191 | |||
192 | 5900 | jorpiell | if (v instanceof Table) { |
193 | 2183 | fernando | return true; |
194 | } else {
|
||
195 | 7304 | caballero | if (v instanceof com.iver.cit.gvsig.project.documents.view.gui.View) { |
196 | com.iver.cit.gvsig.project.documents.view.gui.View view = (com.iver.cit.gvsig.project.documents.view.gui.View) v; |
||
197 | 7392 | sbayarri | IProjectView pv = view.getModel(); |
198 | 2183 | fernando | FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
199 | 4682 | jorpiell | .getActives(); |
200 | 2183 | fernando | |
201 | if (seleccionadas.length == 1) { |
||
202 | if (seleccionadas[0] instanceof AlphanumericData) { |
||
203 | return true; |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return false; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /**
|
||
213 | * DOCUMENT ME!
|
||
214 | *
|
||
215 | * @param expression DOCUMENT ME!
|
||
216 | */
|
||
217 | public void newSet(String expression) { |
||
218 | 9494 | ppiqueras | // By Pablo: if no filter expression -> no element selected
|
219 | if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
||
220 | try {
|
||
221 | long[] sel = doSet(expression); |
||
222 | 9532 | caballero | |
223 | 9494 | ppiqueras | if (sel == null) { |
224 | //throw new RuntimeException("Not a 'where' clause?");
|
||
225 | return;
|
||
226 | } |
||
227 | 9532 | caballero | |
228 | 9494 | ppiqueras | FBitSet selection = new FBitSet();
|
229 | 9532 | caballero | |
230 | 9494 | ppiqueras | for (int i = 0; i < sel.length; i++) { |
231 | selection.set((int) sel[i]);
|
||
232 | } |
||
233 | 9532 | caballero | |
234 | 9494 | ppiqueras | dataSource.clearSelection(); |
235 | dataSource.setSelection(selection); |
||
236 | }catch(Exception e){ |
||
237 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta."); |
||
238 | 8559 | ldiaz | } |
239 | 9494 | ppiqueras | } |
240 | else {
|
||
241 | // By Pablo: if no expression -> no element selected
|
||
242 | 8559 | ldiaz | dataSource.clearSelection(); |
243 | 9532 | caballero | } |
244 | 2183 | fernando | } |
245 | |||
246 | /**
|
||
247 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
||
248 | */
|
||
249 | private long[] doSet(String expression) { |
||
250 | try {
|
||
251 | DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
|
||
252 | 2667 | fernando | DataSourceFactory.MANUAL_OPENING); |
253 | 2183 | fernando | |
254 | return ds.getWhereFilter();
|
||
255 | } catch (DriverLoadException e) {
|
||
256 | 10708 | caballero | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage()); |
257 | 10626 | caballero | } catch (ReadDriverException e) {
|
258 | 10708 | caballero | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage()); |
259 | 2183 | fernando | } catch (ParseException e) { |
260 | 10708 | caballero | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage()); |
261 | 2183 | fernando | } catch (SemanticException e) {
|
262 | 10708 | caballero | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage()); |
263 | 2183 | fernando | } catch (IOException e) { |
264 | 10708 | caballero | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage()); |
265 | 2217 | fernando | } catch (EvaluationException e) {
|
266 | 10708 | caballero | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage()); |
267 | } catch (com.hardcode.gdbms.parser.TokenMgrError e) {
|
||
268 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage()); |
||
269 | 29102 | vcaballero | } catch (Exception e) { |
270 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage()); |
||
271 | }catch (Error e) { |
||
272 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage()); |
||
273 | 4682 | jorpiell | } |
274 | 2183 | fernando | return null; |
275 | } |
||
276 | |||
277 | /**
|
||
278 | * DOCUMENT ME!
|
||
279 | *
|
||
280 | * @param expression DOCUMENT ME!
|
||
281 | */
|
||
282 | public void addToSet(String expression) { |
||
283 | 9494 | ppiqueras | // By Pablo: if no filter expression -> don't add more elements to set
|
284 | if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
||
285 | long[] sel = doSet(expression); |
||
286 | 9532 | caballero | |
287 | 9494 | ppiqueras | if (sel == null) { |
288 | //throw new RuntimeException("Not a 'where' clause?");
|
||
289 | return;
|
||
290 | } |
||
291 | 9532 | caballero | |
292 | 9494 | ppiqueras | FBitSet selection = new FBitSet();
|
293 | 9532 | caballero | |
294 | 9494 | ppiqueras | for (int i = 0; i < sel.length; i++) { |
295 | selection.set((int) sel[i]);
|
||
296 | } |
||
297 | 9532 | caballero | |
298 | 9494 | ppiqueras | FBitSet fbs = dataSource.getSelection(); |
299 | fbs.or(selection); |
||
300 | dataSource.setSelection(fbs); |
||
301 | 2183 | fernando | } |
302 | } |
||
303 | |||
304 | /**
|
||
305 | * DOCUMENT ME!
|
||
306 | *
|
||
307 | * @param expression DOCUMENT ME!
|
||
308 | */
|
||
309 | public void fromSet(String expression) { |
||
310 | 9494 | ppiqueras | // By Pablo: if no filter expression -> no element selected
|
311 | if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
||
312 | long[] sel = doSet(expression); |
||
313 | 9532 | caballero | |
314 | 9494 | ppiqueras | if (sel == null) { |
315 | throw new RuntimeException("Not a 'where' clause?"); |
||
316 | } |
||
317 | 9532 | caballero | |
318 | 9494 | ppiqueras | FBitSet selection = new FBitSet();
|
319 | 9532 | caballero | |
320 | 9494 | ppiqueras | for (int i = 0; i < sel.length; i++) { |
321 | selection.set((int) sel[i]);
|
||
322 | } |
||
323 | 9532 | caballero | |
324 | 9494 | ppiqueras | FBitSet fbs = dataSource.getSelection(); |
325 | fbs.and(selection); |
||
326 | dataSource.setSelection(fbs); |
||
327 | 2183 | fernando | } |
328 | 9494 | ppiqueras | else {
|
329 | // By Pablo: if no expression -> no element selected
|
||
330 | dataSource.clearSelection(); |
||
331 | 2183 | fernando | } |
332 | } |
||
333 | 9532 | caballero | |
334 | 9494 | ppiqueras | /**
|
335 | * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
|
||
336 | 9532 | caballero | *
|
337 | 9494 | ppiqueras | * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
338 | * @param expression An string
|
||
339 | 9532 | caballero | * @return A boolean value
|
340 | 9494 | ppiqueras | */
|
341 | private boolean filterExpressionFromWhereIsEmpty(String expression) { |
||
342 | String subExpression = expression.trim();
|
||
343 | 9532 | caballero | int pos;
|
344 | |||
345 | 9494 | ppiqueras | // Remove last ';' if exists
|
346 | if (subExpression.charAt(subExpression.length() -1) == ';') |
||
347 | subExpression = subExpression.substring(0, subExpression.length() -1).trim(); |
||
348 | 9532 | caballero | |
349 | 9494 | ppiqueras | // If there is no 'where' clause
|
350 | if ((pos = subExpression.indexOf("where")) == -1) |
||
351 | return false; |
||
352 | 9532 | caballero | |
353 | 9494 | ppiqueras | // If there is no subexpression in the WHERE clause -> true
|
354 | subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where' |
||
355 | if ( subExpression.length() == 0 ) |
||
356 | return true; |
||
357 | else
|
||
358 | return false; |
||
359 | } |
||
360 | 2183 | fernando | } |