Revision 41248 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/FiltroExtension.java
FiltroExtension.java | ||
---|---|---|
3 | 3 |
* |
4 | 4 |
* Copyright (C) 2007-2013 gvSIG Association. |
5 | 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.
|
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
|
9 |
* version. |
|
10 | 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.
|
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
14 |
* details. |
|
15 | 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. |
|
16 |
* You should have received a copy of the GNU General Public License along with |
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51 |
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
20 | 19 |
* |
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
23 | 22 |
*/ |
24 | 23 |
package org.gvsig.app.extension; |
25 | 24 |
|
26 |
import java.awt.Component; |
|
27 | 25 |
import java.util.Iterator; |
28 | 26 |
|
29 | 27 |
import javax.swing.JOptionPane; |
... | ... | |
32 | 30 |
import org.gvsig.andami.PluginServices; |
33 | 31 |
import org.gvsig.andami.messages.NotificationManager; |
34 | 32 |
import org.gvsig.andami.plugins.Extension; |
35 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
|
36 | 33 |
import org.gvsig.app.ApplicationLocator; |
34 |
import org.gvsig.app.ApplicationManager; |
|
37 | 35 |
import org.gvsig.app.gui.filter.ExpressionListener; |
38 | 36 |
import org.gvsig.app.gui.filter.FilterDialog; |
39 |
import org.gvsig.app.project.documents.AbstractDocument; |
|
40 | 37 |
import org.gvsig.app.project.documents.view.ViewDocument; |
41 | 38 |
import org.gvsig.fmap.dal.DALLocator; |
42 | 39 |
import org.gvsig.fmap.dal.DataManager; |
... | ... | |
53 | 50 |
|
54 | 51 |
/** |
55 | 52 |
* Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla. |
56 |
*
|
|
53 |
* |
|
57 | 54 |
* @author Vicente Caballero Navarro |
58 | 55 |
*/ |
59 | 56 |
public class FiltroExtension extends Extension implements ExpressionListener { |
... | ... | |
61 | 58 |
protected FeatureStore featureStore = null; |
62 | 59 |
private String filterTitle; |
63 | 60 |
|
64 |
/** |
|
65 |
* DOCUMENT ME! |
|
66 |
*/ |
|
67 | 61 |
public void initialize() { |
68 |
IconThemeHelper.registerIcon("action", "layer-filter", this);
|
|
62 |
IconThemeHelper.registerIcon("action", "layer-filter", this);
|
|
69 | 63 |
} |
70 | 64 |
|
71 |
/** |
|
72 |
* DOCUMENT ME! |
|
73 |
* |
|
74 |
* @param actionCommand |
|
75 |
* DOCUMENT ME! |
|
76 |
*/ |
|
77 | 65 |
public void execute(String actionCommand) { |
78 | 66 |
if ("layer-filter".equalsIgnoreCase(actionCommand)) { |
79 |
// try { |
|
80 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
|
67 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
81 | 68 |
|
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); |
|
69 |
ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class); |
|
70 |
if (document == null) { |
|
71 |
return; |
|
92 | 72 |
} |
93 |
// } catch (ReadException e) {
|
|
94 |
// NotificationManager.addError("Error filtrando", e);
|
|
95 |
// }
|
|
96 |
|
|
73 |
filterTitle = document.getName();
|
|
74 |
FLayer layer = document.getMapContext().getLayers().getActives()[0];
|
|
75 |
featureStore = ((FLyrVect) layer).getFeatureStore();
|
|
76 |
document.setModified(true); |
|
97 | 77 |
doExecute(); |
98 | 78 |
} |
99 | 79 |
} |
100 | 80 |
|
101 | 81 |
/** |
102 | 82 |
* "execute" method action. |
103 |
*
|
|
83 |
* |
|
104 | 84 |
*/ |
105 | 85 |
protected void doExecute() { |
106 |
// DefaultExpressionDataSource ds = new DefaultExpressionDataSource(); |
|
107 |
// ds.setTable(featureStore); |
|
108 | 86 |
FilterDialog dlg = new FilterDialog(filterTitle); |
109 | 87 |
dlg.addExpressionListener(this); |
110 | 88 |
dlg.addExceptionListener(new ExceptionListener() { |
... | ... | |
117 | 95 |
PluginServices.getMDIManager().addWindow(dlg); |
118 | 96 |
} |
119 | 97 |
|
120 |
/** |
|
121 |
* DOCUMENT ME! |
|
122 |
* |
|
123 |
* @return DOCUMENT ME! |
|
124 |
*/ |
|
125 | 98 |
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; |
|
99 |
return true; |
|
147 | 100 |
} |
148 | 101 |
|
149 |
/** |
|
150 |
* DOCUMENT ME! |
|
151 |
* |
|
152 |
* @return DOCUMENT ME! |
|
153 |
*/ |
|
154 | 102 |
public boolean isVisible() { |
155 |
IWindow v = PluginServices.getMDIManager().getActiveWindow();
|
|
103 |
ApplicationManager application = ApplicationLocator.getManager();
|
|
156 | 104 |
|
157 |
if (v == null) { |
|
105 |
ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class); |
|
106 |
if (document == null) { |
|
158 | 107 |
return false; |
159 | 108 |
} |
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; |
|
109 |
return document.getMapContext().hasActiveVectorLayers(); |
|
176 | 110 |
} |
177 | 111 |
|
178 |
/** |
|
179 |
* DOCUMENT ME! |
|
180 |
* |
|
181 |
* @param expression |
|
182 |
* DOCUMENT ME! |
|
183 |
*/ |
|
184 | 112 |
// By Pablo: if no filter expression -> no element selected |
185 | 113 |
public void newSet(String expression) throws DataException { |
186 | 114 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
... | ... | |
195 | 123 |
featureStore.setSelection(set); |
196 | 124 |
|
197 | 125 |
} catch (Exception e) { |
198 |
|
|
126 |
|
|
199 | 127 |
JOptionPane.showMessageDialog( |
200 |
ApplicationLocator.getManager().getRootComponent(), |
|
201 |
Messages.getText("expresion_error") + ":\n" |
|
128 |
ApplicationLocator.getManager().getRootComponent(),
|
|
129 |
Messages.getText("expresion_error") + ":\n"
|
|
202 | 130 |
+ getLastMessage(e), |
203 |
Messages.getText("expresion_error"), |
|
204 |
JOptionPane.ERROR_MESSAGE); |
|
131 |
Messages.getText("expresion_error"),
|
|
132 |
JOptionPane.ERROR_MESSAGE);
|
|
205 | 133 |
} finally { |
206 | 134 |
if (set != null) { |
207 | 135 |
set.dispose(); |
... | ... | |
213 | 141 |
} |
214 | 142 |
} |
215 | 143 |
|
216 |
/** |
|
217 |
* @throws DataException |
|
218 |
* @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String) |
|
219 |
*/ |
|
220 | 144 |
private FeatureSet doSet(String expression) throws DataException { |
221 | 145 |
FeatureQuery query = featureStore.createFeatureQuery(); |
222 | 146 |
DataManager manager = DALLocator.getDataManager(); |
223 | 147 |
query.setFilter(manager.createExpresion(expression)); |
224 | 148 |
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 | 149 |
} |
248 | 150 |
|
249 |
/** |
|
250 |
* DOCUMENT ME! |
|
251 |
* |
|
252 |
* @param expression |
|
253 |
* DOCUMENT ME! |
|
254 |
* @throws DataException |
|
255 |
*/ |
|
256 | 151 |
public void addToSet(String expression) throws DataException { |
257 | 152 |
// By Pablo: if no filter expression -> don't add more elements to set |
258 | 153 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
... | ... | |
270 | 165 |
set.dispose(); |
271 | 166 |
} |
272 | 167 |
} |
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 | 168 |
} |
284 | 169 |
} |
285 | 170 |
|
286 |
/** |
|
287 |
* DOCUMENT ME! |
|
288 |
* |
|
289 |
* @param expression |
|
290 |
* DOCUMENT ME! |
|
291 |
*/ |
|
292 | 171 |
public void fromSet(String expression) throws DataException { |
293 | 172 |
// By Pablo: if no filter expression -> no element selected |
294 | 173 |
try { |
... | ... | |
303 | 182 |
throw new RuntimeException("Not a 'where' clause?"); |
304 | 183 |
} |
305 | 184 |
|
306 |
FeatureSelection oldSelection =
|
|
307 |
featureStore.getFeatureSelection(); |
|
185 |
FeatureSelection oldSelection |
|
186 |
= featureStore.getFeatureSelection();
|
|
308 | 187 |
|
309 |
FeatureSelection newSelection =
|
|
310 |
featureStore.createFeatureSelection(); |
|
188 |
FeatureSelection newSelection |
|
189 |
= featureStore.createFeatureSelection();
|
|
311 | 190 |
Iterator iterator = set.iterator(); |
312 | 191 |
while (iterator.hasNext()) { |
313 | 192 |
Feature feature = (Feature) iterator.next(); |
... | ... | |
332 | 211 |
/** |
333 | 212 |
* Returns true if the WHERE subconsultation of the filterExpression is |
334 | 213 |
* empty ("") |
335 |
*
|
|
214 |
* |
|
336 | 215 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com) |
337 |
* @param expression |
|
338 |
* An string |
|
216 |
* @param expression An string |
|
339 | 217 |
* @return A boolean value |
340 | 218 |
*/ |
341 | 219 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
342 |
|
|
220 |
|
|
343 | 221 |
if (expression == null) { |
344 | 222 |
return true; |
345 | 223 |
} |
346 |
|
|
224 |
|
|
347 | 225 |
String subExpression = expression.trim(); |
348 |
|
|
226 |
|
|
349 | 227 |
if (subExpression.length() == 0) { |
350 | 228 |
return true; |
351 | 229 |
} |
352 |
|
|
230 |
|
|
353 | 231 |
int pos; |
354 | 232 |
|
355 | 233 |
// Remove last ';' if exists |
356 | 234 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
357 |
subExpression =
|
|
358 |
subExpression.substring(0, subExpression.length() - 1).trim(); |
|
235 |
subExpression |
|
236 |
= subExpression.substring(0, subExpression.length() - 1).trim();
|
|
359 | 237 |
} |
360 | 238 |
|
361 | 239 |
// If there is no 'where' clause |
... | ... | |
364 | 242 |
} |
365 | 243 |
|
366 | 244 |
// 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' |
|
245 |
// + 5 is the length of 'where' |
|
246 |
subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); |
|
375 | 247 |
if (subExpression.length() == 0) { |
376 | 248 |
return true; |
377 | 249 |
} else { |
378 | 250 |
return false; |
379 | 251 |
} |
380 | 252 |
} |
381 |
|
|
253 |
|
|
382 | 254 |
private String getLastMessage(Throwable ex) { |
383 |
|
|
255 |
|
|
384 | 256 |
Throwable p = ex; |
385 | 257 |
while (p.getCause() != null && p.getCause() != p) { |
386 | 258 |
p = p.getCause(); |
387 | 259 |
} |
388 | 260 |
return p.getMessage(); |
389 |
}} |
|
261 |
} |
|
262 |
} |
Also available in: Unified diff