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