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 / SelectByAttributesExtension.java @ 43987
History | View | Annotate | Download (14.4 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 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 |
*
|
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 |
*
|
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.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.app.extension; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.FlowLayout; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.util.Iterator; |
29 |
import javax.swing.AbstractAction; |
30 |
import javax.swing.JButton; |
31 |
|
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.JPanel; |
34 |
|
35 |
import org.gvsig.andami.IconThemeHelper; |
36 |
import org.gvsig.andami.PluginServices; |
37 |
import org.gvsig.andami.messages.NotificationManager; |
38 |
import org.gvsig.andami.plugins.Extension; |
39 |
import org.gvsig.app.ApplicationLocator; |
40 |
import org.gvsig.app.ApplicationManager; |
41 |
import org.gvsig.app.gui.filter.ExpressionListener; |
42 |
import org.gvsig.app.gui.filter.FilterDialog; |
43 |
import org.gvsig.app.project.documents.view.ViewDocument; |
44 |
import org.gvsig.app.project.documents.view.gui.IView; |
45 |
import org.gvsig.expressionevaluator.Expression; |
46 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
47 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
48 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
49 |
import org.gvsig.expressionevaluator.swing.Element; |
50 |
import org.gvsig.fmap.dal.DALLocator; |
51 |
import org.gvsig.fmap.dal.DataManager; |
52 |
import org.gvsig.fmap.dal.exception.DataException; |
53 |
import org.gvsig.fmap.dal.feature.Feature; |
54 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
55 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
56 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
57 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
58 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
59 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
60 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
61 |
import org.gvsig.i18n.Messages; |
62 |
import org.gvsig.tools.ToolsLocator; |
63 |
import org.gvsig.tools.i18n.I18nManager; |
64 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
65 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
66 |
import org.gvsig.utils.exceptionHandling.ExceptionListener; |
67 |
|
68 |
/**
|
69 |
* Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
|
70 |
*
|
71 |
* @author Vicente Caballero Navarro
|
72 |
*/
|
73 |
@SuppressWarnings("UseSpecificCatch") |
74 |
public class SelectByAttributesExtension extends Extension implements ExpressionListener { |
75 |
|
76 |
protected FeatureStore featureStore = null; |
77 |
private String filterTitle; |
78 |
|
79 |
@Override
|
80 |
public void initialize() { |
81 |
IconThemeHelper.registerIcon("action", "selection-by-attributes", this); |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void execute(String actionCommand) { |
86 |
ApplicationManager application = ApplicationLocator.getManager(); |
87 |
|
88 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
89 |
if (view == null) { |
90 |
return;
|
91 |
} |
92 |
if ("selection-by-attributes-layer2".equals(actionCommand)) { |
93 |
ViewDocument document = view.getViewDocument(); |
94 |
|
95 |
FLayer layer = document.getMapContext().getLayers().getActives()[0];
|
96 |
filterTitle = layer.getName(); |
97 |
featureStore = ((FLyrVect) layer).getFeatureStore(); |
98 |
document.setModified(true);
|
99 |
doSelectByAttributes2(filterTitle, featureStore); |
100 |
|
101 |
} else if ("selection-by-attributes-layer".equals(actionCommand)) { |
102 |
ViewDocument document = view.getViewDocument(); |
103 |
|
104 |
FLayer layer = document.getMapContext().getLayers().getActives()[0];
|
105 |
filterTitle = layer.getName(); |
106 |
featureStore = ((FLyrVect) layer).getFeatureStore(); |
107 |
document.setModified(true);
|
108 |
doExecute(); |
109 |
} |
110 |
} |
111 |
|
112 |
/**
|
113 |
* "execute" method action.
|
114 |
*
|
115 |
*/
|
116 |
protected void doExecute() { |
117 |
FilterDialog dlg = new FilterDialog(filterTitle);
|
118 |
dlg.addExpressionListener(this);
|
119 |
dlg.addExceptionListener(new ExceptionListener() { |
120 |
|
121 |
@Override
|
122 |
public void exceptionThrown(Throwable t) { |
123 |
NotificationManager.addError(t.getMessage(), t); |
124 |
} |
125 |
}); |
126 |
dlg.setModel(featureStore); |
127 |
PluginServices.getMDIManager().addWindow(dlg); |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public boolean isEnabled() { |
132 |
return true; |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public boolean isVisible() { |
137 |
ApplicationManager application = ApplicationLocator.getManager(); |
138 |
|
139 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
140 |
if (view == null) { |
141 |
return false; |
142 |
} |
143 |
ViewDocument document = view.getViewDocument(); |
144 |
return document.getMapContext().hasActiveVectorLayers();
|
145 |
} |
146 |
|
147 |
// By Pablo: if no filter expression -> no element selected
|
148 |
@Override
|
149 |
public void newSet(String expression) throws DataException { |
150 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
151 |
FeatureSet set = null;
|
152 |
try {
|
153 |
set = doSet(expression); |
154 |
|
155 |
if (set == null) { |
156 |
// throw new RuntimeException("Not a 'where' clause?");
|
157 |
return;
|
158 |
} |
159 |
featureStore.setSelection(set); |
160 |
|
161 |
} catch (Exception e) { |
162 |
|
163 |
JOptionPane.showMessageDialog(
|
164 |
ApplicationLocator.getManager().getRootComponent(), |
165 |
Messages.getText("expresion_error") + ":\n" |
166 |
+ getLastMessage(e), |
167 |
Messages.getText("expresion_error"),
|
168 |
JOptionPane.ERROR_MESSAGE);
|
169 |
} finally {
|
170 |
if (set != null) { |
171 |
set.dispose(); |
172 |
} |
173 |
} |
174 |
} else {
|
175 |
// By Pablo: if no expression -> no element selected
|
176 |
featureStore.getFeatureSelection().deselectAll(); |
177 |
} |
178 |
} |
179 |
|
180 |
private FeatureSet doSet(String expression) throws DataException { |
181 |
FeatureQuery query = featureStore.createFeatureQuery(); |
182 |
DataManager manager = DALLocator.getDataManager(); |
183 |
query.setFilter(manager.createExpresion(expression)); |
184 |
return featureStore.getFeatureSet(query);
|
185 |
} |
186 |
|
187 |
@Override
|
188 |
public void addToSet(String expression) throws DataException { |
189 |
// By Pablo: if no filter expression -> don't add more elements to set
|
190 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
191 |
FeatureSet set = null;
|
192 |
try {
|
193 |
set = doSet(expression); |
194 |
|
195 |
if (set == null) { |
196 |
// throw new RuntimeException("Not a 'where' clause?");
|
197 |
return;
|
198 |
} |
199 |
featureStore.getFeatureSelection().select(set); |
200 |
} finally {
|
201 |
if (set != null) { |
202 |
set.dispose(); |
203 |
} |
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public void fromSet(String expression) throws DataException { |
210 |
// By Pablo: if no filter expression -> no element selected
|
211 |
try {
|
212 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
213 |
// NotificationManager.showMessageInfo("Falta por implementar",
|
214 |
// null);
|
215 |
|
216 |
FeatureSet set; |
217 |
set = doSet(expression); |
218 |
|
219 |
if (set == null) { |
220 |
throw new RuntimeException("Not a 'where' clause?"); |
221 |
} |
222 |
|
223 |
FeatureSelection oldSelection |
224 |
= featureStore.getFeatureSelection(); |
225 |
|
226 |
FeatureSelection newSelection |
227 |
= featureStore.createFeatureSelection(); |
228 |
Iterator iterator = set.iterator();
|
229 |
while (iterator.hasNext()) {
|
230 |
Feature feature = (Feature) iterator.next(); |
231 |
if (oldSelection.isSelected(feature)) {
|
232 |
newSelection.select(feature); |
233 |
} |
234 |
} |
235 |
featureStore.setSelection(newSelection); |
236 |
set.dispose(); |
237 |
|
238 |
} else {
|
239 |
// By Pablo: if no expression -> no element selected
|
240 |
featureStore.getFeatureSelection().deselectAll(); |
241 |
} |
242 |
} catch (DataException e) {
|
243 |
NotificationManager.addError(e); |
244 |
} |
245 |
|
246 |
} |
247 |
|
248 |
/**
|
249 |
* Returns true if the WHERE subconsultation of the filterExpression is
|
250 |
* empty ("")
|
251 |
*
|
252 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
253 |
* @param expression An string
|
254 |
* @return A boolean value
|
255 |
*/
|
256 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
257 |
|
258 |
if (expression == null) { |
259 |
return true; |
260 |
} |
261 |
|
262 |
String subExpression = expression.trim();
|
263 |
|
264 |
if (subExpression.length() == 0) { |
265 |
return true; |
266 |
} |
267 |
|
268 |
int pos;
|
269 |
|
270 |
// Remove last ';' if exists
|
271 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
272 |
subExpression |
273 |
= subExpression.substring(0, subExpression.length() - 1).trim(); |
274 |
} |
275 |
|
276 |
// If there is no 'where' clause
|
277 |
if ((pos = subExpression.indexOf("where")) == -1) { |
278 |
return false; |
279 |
} |
280 |
|
281 |
// If there is no subexpression in the WHERE clause -> true
|
282 |
// + 5 is the length of 'where'
|
283 |
subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
|
284 |
if (subExpression.length() == 0) { |
285 |
return true; |
286 |
} else {
|
287 |
return false; |
288 |
} |
289 |
} |
290 |
|
291 |
private String getLastMessage(Throwable ex) { |
292 |
|
293 |
Throwable p = ex;
|
294 |
while (p.getCause() != null && p.getCause() != p) { |
295 |
p = p.getCause(); |
296 |
} |
297 |
return p.getMessage();
|
298 |
} |
299 |
|
300 |
private void doSelectByAttributes2(String title, final FeatureStore store) { |
301 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
302 |
WindowManager windowManager = ToolsSwingLocator.getWindowManager(); |
303 |
ExpressionEvaluatorSwingManager swingManager = ExpressionEvaluatorSwingLocator.getManager(); |
304 |
|
305 |
final JPanel panel = new JPanel(); |
306 |
panel.setLayout(new BorderLayout()); |
307 |
|
308 |
final JExpressionBuilder builder = swingManager.createJExpressionBuilder();
|
309 |
builder.addSymbolTable(DataManager.FEATURE_SYMBOL_TABLE); |
310 |
Element element = swingManager.createElement(
|
311 |
DataSwingManager.FEATURE_STORE_EXPRESSION_ELEMENT, |
312 |
builder, |
313 |
store |
314 |
); |
315 |
if (element != null) { |
316 |
builder.getElements().add(element); |
317 |
} |
318 |
panel.add(builder.asJComponent(), BorderLayout.CENTER);
|
319 |
JPanel buttons = new JPanel(); |
320 |
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 4)); |
321 |
buttons.add(new JButton(new AbstractAction(i18n.getTranslation("_Set_selection")) { |
322 |
@Override
|
323 |
public void actionPerformed(ActionEvent e) { |
324 |
doSetSelection(store, builder.getExpression()); |
325 |
} |
326 |
})); |
327 |
buttons.add(new JButton(new AbstractAction(i18n.getTranslation("_Add_to_selection")) { |
328 |
@Override
|
329 |
public void actionPerformed(ActionEvent e) { |
330 |
doAddToSelection(store, builder.getExpression()); |
331 |
} |
332 |
})); |
333 |
buttons.add(new JButton(new AbstractAction(i18n.getTranslation("_Filter_selection")) { |
334 |
@Override
|
335 |
public void actionPerformed(ActionEvent e) { |
336 |
doFilterSelection(store, builder.getExpression()); |
337 |
} |
338 |
})); |
339 |
buttons.add(new JButton(new AbstractAction(i18n.getTranslation("_Close")) { |
340 |
@Override
|
341 |
public void actionPerformed(ActionEvent e) { |
342 |
panel.setVisible(false);
|
343 |
} |
344 |
})); |
345 |
panel.add(buttons, BorderLayout.SOUTH);
|
346 |
|
347 |
windowManager.showWindow( |
348 |
panel, |
349 |
title, |
350 |
WindowManager.MODE.WINDOW |
351 |
); |
352 |
|
353 |
} |
354 |
|
355 |
private void doSetSelection(FeatureStore store, Expression expression) { |
356 |
try {
|
357 |
DataManager manager = DALLocator.getDataManager(); |
358 |
|
359 |
FeatureSelection currentSelection = store.getFeatureSelection(); |
360 |
FeatureQuery query = store.createFeatureQuery(); |
361 |
query.setFilter(manager.createExpresion(expression)); |
362 |
FeatureSet selection = store.getFeatureSet(query); |
363 |
currentSelection.deselectAll(); |
364 |
currentSelection.select(selection); |
365 |
} catch (Exception ex) { |
366 |
logger.warn("Can't build selecction from filter expression.", ex);
|
367 |
} |
368 |
} |
369 |
|
370 |
private void doAddToSelection(FeatureStore store, Expression expression) { |
371 |
try {
|
372 |
DataManager manager = DALLocator.getDataManager(); |
373 |
|
374 |
FeatureSelection currentSelection = store.getFeatureSelection(); |
375 |
FeatureQuery query = store.createFeatureQuery(); |
376 |
query.setFilter(manager.createExpresion(expression)); |
377 |
FeatureSet selection = store.getFeatureSet(query); |
378 |
currentSelection.select(selection); |
379 |
} catch (Exception ex) { |
380 |
logger.warn("Can't build selecction from filter expression.", ex);
|
381 |
} |
382 |
} |
383 |
|
384 |
private void doFilterSelection(FeatureStore store, Expression expression) { |
385 |
try {
|
386 |
DataManager manager = DALLocator.getDataManager(); |
387 |
|
388 |
FeatureSelection currentSelection = store.getFeatureSelection(); |
389 |
FeatureQuery query = store.createFeatureQuery(); |
390 |
query.setFilter(manager.createExpresion(expression)); |
391 |
FeatureSet set = store.getFeatureSet(query); |
392 |
FeatureSelection newSelection = store.createFeatureSelection(); |
393 |
for (Feature feature : set) {
|
394 |
if( currentSelection.isSelected(feature) ) {
|
395 |
newSelection.select(feature); |
396 |
} |
397 |
} |
398 |
store.setSelection(newSelection); |
399 |
} catch (Exception ex) { |
400 |
logger.warn("Can't build selecction from filter expression.", ex);
|
401 |
} |
402 |
} |
403 |
|
404 |
} |