svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / FiltroExtension.java @ 40955
History | View | Annotate | Download (13.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.extension; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.util.Iterator; |
28 |
|
29 |
import javax.swing.JOptionPane; |
30 |
|
31 |
import org.gvsig.andami.IconThemeHelper; |
32 |
import org.gvsig.andami.PluginServices; |
33 |
import org.gvsig.andami.messages.NotificationManager; |
34 |
import org.gvsig.andami.plugins.Extension; |
35 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
36 |
import org.gvsig.app.ApplicationLocator; |
37 |
import org.gvsig.app.gui.filter.ExpressionListener; |
38 |
import org.gvsig.app.gui.filter.FilterDialog; |
39 |
import org.gvsig.app.project.documents.AbstractDocument; |
40 |
import org.gvsig.app.project.documents.view.ViewDocument; |
41 |
import org.gvsig.fmap.dal.DALLocator; |
42 |
import org.gvsig.fmap.dal.DataManager; |
43 |
import org.gvsig.fmap.dal.exception.DataException; |
44 |
import org.gvsig.fmap.dal.feature.Feature; |
45 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
46 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
47 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
48 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
49 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
50 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
51 |
import org.gvsig.i18n.Messages; |
52 |
import org.gvsig.utils.exceptionHandling.ExceptionListener; |
53 |
|
54 |
/**
|
55 |
* Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
|
56 |
*
|
57 |
* @author Vicente Caballero Navarro
|
58 |
*/
|
59 |
public class FiltroExtension extends Extension implements ExpressionListener { |
60 |
|
61 |
protected FeatureStore featureStore = null; |
62 |
private String filterTitle; |
63 |
|
64 |
/**
|
65 |
* DOCUMENT ME!
|
66 |
*/
|
67 |
public void initialize() { |
68 |
IconThemeHelper.registerIcon("action", "layer-filter", this); |
69 |
} |
70 |
|
71 |
/**
|
72 |
* DOCUMENT ME!
|
73 |
*
|
74 |
* @param actionCommand
|
75 |
* DOCUMENT ME!
|
76 |
*/
|
77 |
public void execute(String actionCommand) { |
78 |
if ("layer-filter".equalsIgnoreCase(actionCommand)) { |
79 |
// try {
|
80 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
81 |
|
82 |
if (v instanceof org.gvsig.app.project.documents.view.gui.DefaultViewPanel) { |
83 |
ViewDocument pv = |
84 |
((org.gvsig.app.project.documents.view.gui.DefaultViewPanel) v) |
85 |
.getModel(); |
86 |
filterTitle = |
87 |
((org.gvsig.app.project.documents.view.gui.DefaultViewPanel) v) |
88 |
.getModel().getName(); |
89 |
FLayer layer = pv.getMapContext().getLayers().getActives()[0];
|
90 |
featureStore = ((FLyrVect) layer).getFeatureStore();// pv.getProject().getDataSourceByLayer(layer);
|
91 |
((AbstractDocument) pv).setModified(true); |
92 |
} |
93 |
// } catch (ReadException e) {
|
94 |
// NotificationManager.addError("Error filtrando", e);
|
95 |
// }
|
96 |
|
97 |
doExecute(); |
98 |
} |
99 |
} |
100 |
|
101 |
/**
|
102 |
* "execute" method action.
|
103 |
*
|
104 |
*/
|
105 |
protected void doExecute() { |
106 |
// DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
|
107 |
// ds.setTable(featureStore);
|
108 |
FilterDialog dlg = new FilterDialog(filterTitle);
|
109 |
dlg.addExpressionListener(this);
|
110 |
dlg.addExceptionListener(new ExceptionListener() { |
111 |
|
112 |
public void exceptionThrown(Throwable t) { |
113 |
NotificationManager.addError(t.getMessage(), t); |
114 |
} |
115 |
}); |
116 |
dlg.setModel(featureStore); |
117 |
PluginServices.getMDIManager().addWindow(dlg); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* DOCUMENT ME!
|
122 |
*
|
123 |
* @return DOCUMENT ME!
|
124 |
*/
|
125 |
public boolean isEnabled() { |
126 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
127 |
|
128 |
if (v == null) { |
129 |
return false; |
130 |
} |
131 |
|
132 |
if (v instanceof org.gvsig.app.project.documents.view.gui.DefaultViewPanel) { |
133 |
org.gvsig.app.project.documents.view.gui.DefaultViewPanel view = |
134 |
(org.gvsig.app.project.documents.view.gui.DefaultViewPanel) v; |
135 |
ViewDocument pv = view.getModel(); |
136 |
FLayer[] seleccionadas =
|
137 |
pv.getMapContext().getLayers().getActives(); |
138 |
|
139 |
if (seleccionadas.length == 1) { |
140 |
if (seleccionadas[0].isAvailable() |
141 |
&& (seleccionadas[0] instanceof FLyrVect)) { |
142 |
return true; |
143 |
} |
144 |
} |
145 |
} |
146 |
return false; |
147 |
} |
148 |
|
149 |
/**
|
150 |
* DOCUMENT ME!
|
151 |
*
|
152 |
* @return DOCUMENT ME!
|
153 |
*/
|
154 |
public boolean isVisible() { |
155 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
156 |
|
157 |
if (v == null) { |
158 |
return false; |
159 |
} |
160 |
|
161 |
if (v instanceof org.gvsig.app.project.documents.view.gui.DefaultViewPanel) { |
162 |
org.gvsig.app.project.documents.view.gui.DefaultViewPanel view = |
163 |
(org.gvsig.app.project.documents.view.gui.DefaultViewPanel) v; |
164 |
ViewDocument pv = view.getModel(); |
165 |
FLayer[] seleccionadas =
|
166 |
pv.getMapContext().getLayers().getActives(); |
167 |
|
168 |
if (seleccionadas.length == 1) { |
169 |
if (seleccionadas[0] instanceof FLyrVect) { |
170 |
return true; |
171 |
} |
172 |
} |
173 |
} |
174 |
|
175 |
return false; |
176 |
} |
177 |
|
178 |
/**
|
179 |
* DOCUMENT ME!
|
180 |
*
|
181 |
* @param expression
|
182 |
* DOCUMENT ME!
|
183 |
*/
|
184 |
// By Pablo: if no filter expression -> no element selected
|
185 |
public void newSet(String expression) throws DataException { |
186 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
187 |
FeatureSet set = null;
|
188 |
try {
|
189 |
set = doSet(expression); |
190 |
|
191 |
if (set == null) { |
192 |
// throw new RuntimeException("Not a 'where' clause?");
|
193 |
return;
|
194 |
} |
195 |
featureStore.setSelection(set); |
196 |
|
197 |
} catch (Exception e) { |
198 |
|
199 |
JOptionPane.showMessageDialog(
|
200 |
ApplicationLocator.getManager().getRootComponent(), |
201 |
Messages.getText("expresion_error") + ":\n" |
202 |
+ getLastMessage(e), |
203 |
Messages.getText("expresion_error"),
|
204 |
JOptionPane.ERROR_MESSAGE);
|
205 |
} finally {
|
206 |
if (set != null) { |
207 |
set.dispose(); |
208 |
} |
209 |
} |
210 |
} else {
|
211 |
// By Pablo: if no expression -> no element selected
|
212 |
featureStore.getFeatureSelection().deselectAll(); |
213 |
} |
214 |
} |
215 |
|
216 |
/**
|
217 |
* @throws DataException
|
218 |
* @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String)
|
219 |
*/
|
220 |
private FeatureSet doSet(String expression) throws DataException { |
221 |
FeatureQuery query = featureStore.createFeatureQuery(); |
222 |
DataManager manager = DALLocator.getDataManager(); |
223 |
query.setFilter(manager.createExpresion(expression)); |
224 |
return featureStore.getFeatureSet(query);
|
225 |
// try {
|
226 |
// DataSource ds =
|
227 |
// LayerFactory.getDataSourceFactory().executeSQL(expression,
|
228 |
// DataSourceFactory.MANUAL_OPENING);
|
229 |
//
|
230 |
// return ds.getWhereFilter();
|
231 |
// } catch (DriverLoadException e) {
|
232 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
|
233 |
// } catch (ReadDriverException e) {
|
234 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
|
235 |
// } catch (ParseException e) {
|
236 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
|
237 |
// } catch (SemanticException e) {
|
238 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage());
|
239 |
// } catch (IOException e) {
|
240 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage());
|
241 |
// } catch (EvaluationException e) {
|
242 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
|
243 |
// } catch (com.hardcode.gdbms.parser.TokenMgrError e) {
|
244 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage());
|
245 |
// }
|
246 |
// return null;
|
247 |
} |
248 |
|
249 |
/**
|
250 |
* DOCUMENT ME!
|
251 |
*
|
252 |
* @param expression
|
253 |
* DOCUMENT ME!
|
254 |
* @throws DataException
|
255 |
*/
|
256 |
public void addToSet(String expression) throws DataException { |
257 |
// By Pablo: if no filter expression -> don't add more elements to set
|
258 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
259 |
FeatureSet set = null;
|
260 |
try {
|
261 |
set = doSet(expression); |
262 |
|
263 |
if (set == null) { |
264 |
// throw new RuntimeException("Not a 'where' clause?");
|
265 |
return;
|
266 |
} |
267 |
featureStore.getFeatureSelection().select(set); |
268 |
} finally {
|
269 |
if (set != null) { |
270 |
set.dispose(); |
271 |
} |
272 |
} |
273 |
|
274 |
// FBitSet selection = new FBitSet();
|
275 |
//
|
276 |
// for (int i = 0; i < sel.length; i++) {
|
277 |
// selection.set((int) sel[i]);
|
278 |
// }
|
279 |
//
|
280 |
// FBitSet fbs = featureStore.getSelection();
|
281 |
// fbs.or(selection);
|
282 |
// featureStore.setSelection(fbs);
|
283 |
} |
284 |
} |
285 |
|
286 |
/**
|
287 |
* DOCUMENT ME!
|
288 |
*
|
289 |
* @param expression
|
290 |
* DOCUMENT ME!
|
291 |
*/
|
292 |
public void fromSet(String expression) throws DataException { |
293 |
// By Pablo: if no filter expression -> no element selected
|
294 |
try {
|
295 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
296 |
// NotificationManager.showMessageInfo("Falta por implementar",
|
297 |
// null);
|
298 |
|
299 |
FeatureSet set = null;
|
300 |
set = doSet(expression); |
301 |
|
302 |
if (set == null) { |
303 |
throw new RuntimeException("Not a 'where' clause?"); |
304 |
} |
305 |
|
306 |
FeatureSelection oldSelection = |
307 |
featureStore.getFeatureSelection(); |
308 |
|
309 |
FeatureSelection newSelection = |
310 |
featureStore.createFeatureSelection(); |
311 |
Iterator iterator = set.iterator();
|
312 |
while (iterator.hasNext()) {
|
313 |
Feature feature = (Feature) iterator.next(); |
314 |
if (oldSelection.isSelected(feature)) {
|
315 |
newSelection.select(feature); |
316 |
} |
317 |
} |
318 |
featureStore.setSelection(newSelection); |
319 |
set.dispose(); |
320 |
|
321 |
} else {
|
322 |
// By Pablo: if no expression -> no element selected
|
323 |
featureStore.getFeatureSelection().deselectAll(); |
324 |
; |
325 |
} |
326 |
} catch (DataException e) {
|
327 |
NotificationManager.addError(e); |
328 |
} |
329 |
|
330 |
} |
331 |
|
332 |
/**
|
333 |
* Returns true if the WHERE subconsultation of the filterExpression is
|
334 |
* empty ("")
|
335 |
*
|
336 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
337 |
* @param expression
|
338 |
* An string
|
339 |
* @return A boolean value
|
340 |
*/
|
341 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
342 |
|
343 |
if (expression == null) { |
344 |
return true; |
345 |
} |
346 |
|
347 |
String subExpression = expression.trim();
|
348 |
|
349 |
if (subExpression.length() == 0) { |
350 |
return true; |
351 |
} |
352 |
|
353 |
int pos;
|
354 |
|
355 |
// Remove last ';' if exists
|
356 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
357 |
subExpression = |
358 |
subExpression.substring(0, subExpression.length() - 1).trim(); |
359 |
} |
360 |
|
361 |
// If there is no 'where' clause
|
362 |
if ((pos = subExpression.indexOf("where")) == -1) { |
363 |
return false; |
364 |
} |
365 |
|
366 |
// If there is no subexpression in the WHERE clause -> true
|
367 |
subExpression = |
368 |
subExpression.substring(pos + 5, subExpression.length()).trim(); // + |
369 |
// 5
|
370 |
// is
|
371 |
// the
|
372 |
// length
|
373 |
// of
|
374 |
// 'where'
|
375 |
if (subExpression.length() == 0) { |
376 |
return true; |
377 |
} else {
|
378 |
return false; |
379 |
} |
380 |
} |
381 |
|
382 |
private String getLastMessage(Throwable ex) { |
383 |
|
384 |
Throwable p = ex;
|
385 |
while (p.getCause() != null && p.getCause() != p) { |
386 |
p = p.getCause(); |
387 |
} |
388 |
return p.getMessage();
|
389 |
}} |