root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / FiltroExtension.java @ 29596
History | View | Annotate | Download (11.2 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 org.gvsig.app.extension; |
42 |
|
43 |
import java.awt.Component; |
44 |
|
45 |
import javax.swing.JOptionPane; |
46 |
|
47 |
import org.gvsig.andami.PluginServices; |
48 |
import org.gvsig.andami.messages.NotificationManager; |
49 |
import org.gvsig.andami.plugins.Extension; |
50 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
51 |
import org.gvsig.app.gui.filter.ExpressionListener; |
52 |
import org.gvsig.app.gui.filter.FilterDialog; |
53 |
import org.gvsig.app.project.documents.ProjectDocument; |
54 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
55 |
import org.gvsig.app.project.documents.view.IProjectView; |
56 |
import org.gvsig.fmap.dal.DALLocator; |
57 |
import org.gvsig.fmap.dal.DataManager; |
58 |
import org.gvsig.fmap.dal.exception.DataException; |
59 |
import org.gvsig.fmap.dal.exception.ReadException; |
60 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
61 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
62 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
63 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
64 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
65 |
import org.gvsig.utils.exceptionHandling.ExceptionListener; |
66 |
|
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 FeatureStore featureStore = null; |
76 |
protected FeatureTableDocumentPanel table;
|
77 |
private String filterTitle; |
78 |
|
79 |
/**
|
80 |
* DOCUMENT ME!
|
81 |
*/
|
82 |
public void initialize() { |
83 |
registerIcons(); |
84 |
} |
85 |
|
86 |
private void registerIcons(){ |
87 |
PluginServices.getIconTheme().registerDefault( |
88 |
"table-filter",
|
89 |
this.getClass().getClassLoader().getResource("images/Filtro.png") |
90 |
); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* DOCUMENT ME!
|
95 |
*
|
96 |
* @param actionCommand DOCUMENT ME!
|
97 |
*/
|
98 |
public void execute(String actionCommand) { |
99 |
if ("FILTRO".equals(actionCommand)) { |
100 |
try {
|
101 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
102 |
|
103 |
if (v instanceof FeatureTableDocumentPanel) { |
104 |
table = (FeatureTableDocumentPanel) v; |
105 |
|
106 |
featureStore = table.getModel().getStore(); |
107 |
filterTitle = table.getModel().getName(); |
108 |
table.getModel().setModified(true);
|
109 |
} else if (v instanceof org.gvsig.app.project.documents.view.gui.View) { |
110 |
IProjectView pv = ((org.gvsig.app.project.documents.view.gui.View) v).getModel(); |
111 |
filterTitle = ((org.gvsig.app.project.documents.view.gui.View) v).getModel().getName(); |
112 |
FLayer layer = pv.getMapContext() |
113 |
.getLayers().getActives()[0];
|
114 |
featureStore = ((FLyrVect)layer).getFeatureStore();//pv.getProject().getDataSourceByLayer(layer);
|
115 |
((ProjectDocument)pv).setModified(true);
|
116 |
} |
117 |
} catch (ReadException e) {
|
118 |
NotificationManager.addError("Error filtrando", e);
|
119 |
} |
120 |
|
121 |
doExecute(); |
122 |
} |
123 |
} |
124 |
|
125 |
/**
|
126 |
* "execute" method action.
|
127 |
*
|
128 |
*/
|
129 |
protected void doExecute(){ |
130 |
// DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
|
131 |
// ds.setTable(featureStore);
|
132 |
FilterDialog dlg = new FilterDialog(filterTitle);
|
133 |
dlg.addExpressionListener(this);
|
134 |
dlg.addExceptionListener(new ExceptionListener() { |
135 |
public void exceptionThrown(Throwable t) { |
136 |
NotificationManager.addError(t.getMessage(), t); |
137 |
} |
138 |
}); |
139 |
dlg.setModel(featureStore); |
140 |
PluginServices.getMDIManager().addWindow(dlg); |
141 |
} |
142 |
|
143 |
/**
|
144 |
* DOCUMENT ME!
|
145 |
*
|
146 |
* @return DOCUMENT ME!
|
147 |
*/
|
148 |
public boolean isEnabled() { |
149 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
150 |
|
151 |
if (v == null) { |
152 |
return false; |
153 |
} |
154 |
|
155 |
if (v instanceof FeatureTableDocumentPanel) { |
156 |
return true; |
157 |
} else {
|
158 |
if (v instanceof org.gvsig.app.project.documents.view.gui.View) { |
159 |
org.gvsig.app.project.documents.view.gui.View view = (org.gvsig.app.project.documents.view.gui.View) v; |
160 |
IProjectView pv = view.getModel(); |
161 |
FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
162 |
.getActives(); |
163 |
|
164 |
if (seleccionadas.length == 1) { |
165 |
if (seleccionadas[0].isAvailable() && seleccionadas[0] instanceof FLyrVect) { |
166 |
return true; |
167 |
} |
168 |
} |
169 |
} |
170 |
|
171 |
return false; |
172 |
} |
173 |
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* DOCUMENT ME!
|
178 |
*
|
179 |
* @return DOCUMENT ME!
|
180 |
*/
|
181 |
public boolean isVisible() { |
182 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
183 |
|
184 |
if (v == null) { |
185 |
return false; |
186 |
} |
187 |
|
188 |
if (v instanceof FeatureTableDocumentPanel) { |
189 |
return true; |
190 |
} else {
|
191 |
if (v instanceof org.gvsig.app.project.documents.view.gui.View) { |
192 |
org.gvsig.app.project.documents.view.gui.View view = (org.gvsig.app.project.documents.view.gui.View) v; |
193 |
IProjectView pv = view.getModel(); |
194 |
FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
195 |
.getActives(); |
196 |
|
197 |
if (seleccionadas.length == 1) { |
198 |
if (seleccionadas[0] instanceof FLyrVect) { |
199 |
return true; |
200 |
} |
201 |
} |
202 |
} |
203 |
|
204 |
return false; |
205 |
} |
206 |
} |
207 |
|
208 |
/**
|
209 |
* DOCUMENT ME!
|
210 |
*
|
211 |
* @param expression DOCUMENT ME!
|
212 |
*/
|
213 |
public void newSet(String expression) throws DataException { |
214 |
// By Pablo: if no filter expression -> no element selected
|
215 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
216 |
try {
|
217 |
FeatureSet set = doSet(expression); |
218 |
|
219 |
if (set == null) { |
220 |
//throw new RuntimeException("Not a 'where' clause?");
|
221 |
return;
|
222 |
} |
223 |
featureStore.setSelection(set); |
224 |
|
225 |
set.dispose(); |
226 |
}catch(Exception e){ |
227 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta."); |
228 |
} |
229 |
} |
230 |
else {
|
231 |
// By Pablo: if no expression -> no element selected
|
232 |
featureStore.getFeatureSelection().deselectAll(); |
233 |
} |
234 |
} |
235 |
|
236 |
/**
|
237 |
* @throws DataException
|
238 |
* @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String)
|
239 |
*/
|
240 |
private FeatureSet doSet(String expression) throws DataException { |
241 |
FeatureQuery query = featureStore.createFeatureQuery(); |
242 |
DataManager manager = DALLocator.getDataManager(); |
243 |
query.setFilter(manager.createExpresion(expression)); |
244 |
return featureStore.getFeatureSet(query);
|
245 |
// try {
|
246 |
// DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
|
247 |
// DataSourceFactory.MANUAL_OPENING);
|
248 |
//
|
249 |
// return ds.getWhereFilter();
|
250 |
// } catch (DriverLoadException e) {
|
251 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
|
252 |
// } catch (ReadDriverException e) {
|
253 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
|
254 |
// } catch (ParseException e) {
|
255 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
|
256 |
// } catch (SemanticException e) {
|
257 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage());
|
258 |
// } catch (IOException e) {
|
259 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage());
|
260 |
// } catch (EvaluationException e) {
|
261 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
|
262 |
// } catch (com.hardcode.gdbms.parser.TokenMgrError e) {
|
263 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage());
|
264 |
// }
|
265 |
// return null;
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* DOCUMENT ME!
|
270 |
*
|
271 |
* @param expression
|
272 |
* DOCUMENT ME!
|
273 |
* @throws DataException
|
274 |
*/
|
275 |
public void addToSet(String expression) throws DataException { |
276 |
// By Pablo: if no filter expression -> don't add more elements to set
|
277 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
278 |
FeatureSet set = null;
|
279 |
set = doSet(expression); |
280 |
|
281 |
if (set == null) { |
282 |
//throw new RuntimeException("Not a 'where' clause?");
|
283 |
return;
|
284 |
} |
285 |
featureStore.getFeatureSelection().select(set); |
286 |
|
287 |
set.dispose(); |
288 |
|
289 |
// FBitSet selection = new FBitSet();
|
290 |
//
|
291 |
// for (int i = 0; i < sel.length; i++) {
|
292 |
// selection.set((int) sel[i]);
|
293 |
// }
|
294 |
//
|
295 |
// FBitSet fbs = featureStore.getSelection();
|
296 |
// fbs.or(selection);
|
297 |
// featureStore.setSelection(fbs);
|
298 |
} |
299 |
} |
300 |
|
301 |
/**
|
302 |
* DOCUMENT ME!
|
303 |
*
|
304 |
* @param expression DOCUMENT ME!
|
305 |
*/
|
306 |
public void fromSet(String expression) throws DataException { |
307 |
// By Pablo: if no filter expression -> no element selected
|
308 |
try{
|
309 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
310 |
NotificationManager.showMessageInfo("Falta por implementar",
|
311 |
null);
|
312 |
|
313 |
// FeatureSet set = null;
|
314 |
// set = doSet(expression);
|
315 |
//
|
316 |
// if (set == null) {
|
317 |
// throw new RuntimeException("Not a 'where' clause?");
|
318 |
// }
|
319 |
//
|
320 |
// FeatureSelection oldSelection = featureStore
|
321 |
// .getFeatureSelection();
|
322 |
//
|
323 |
// FeatureSelection newSelection = featureStore
|
324 |
// .createFeatureSelection();
|
325 |
// Iterator iterator = set.iterator();
|
326 |
// while (iterator.hasNext()) {
|
327 |
// Feature feature = (Feature) iterator.next();
|
328 |
// if (oldSelection.isSelected(feature)) {
|
329 |
// newSelection.select(feature);
|
330 |
// }
|
331 |
// }
|
332 |
// featureStore.setSelection(newSelection);
|
333 |
// set.dispose();
|
334 |
|
335 |
} else {
|
336 |
// By Pablo: if no expression -> no element selected
|
337 |
featureStore.getFeatureSelection().deselectAll(); |
338 |
; |
339 |
} |
340 |
} catch (DataException e) {
|
341 |
NotificationManager.addError(e); |
342 |
} |
343 |
|
344 |
} |
345 |
|
346 |
/**
|
347 |
* Returns true if the WHERE subconsultation of the filterExpression is empty ("")
|
348 |
*
|
349 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
350 |
* @param expression An string
|
351 |
* @return A boolean value
|
352 |
*/
|
353 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
354 |
String subExpression = expression.trim();
|
355 |
int pos;
|
356 |
|
357 |
// Remove last ';' if exists
|
358 |
if (subExpression.charAt(subExpression.length() -1) == ';') { |
359 |
subExpression = subExpression.substring(0, subExpression.length() -1).trim(); |
360 |
} |
361 |
|
362 |
// If there is no 'where' clause
|
363 |
if ((pos = subExpression.indexOf("where")) == -1) { |
364 |
return false; |
365 |
} |
366 |
|
367 |
// If there is no subexpression in the WHERE clause -> true
|
368 |
subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where' |
369 |
if ( subExpression.length() == 0 ) { |
370 |
return true; |
371 |
} else {
|
372 |
return false; |
373 |
} |
374 |
} |
375 |
} |