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