svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / SelectByAttributesInTableExtension.java @ 43987
History | View | Annotate | Download (11.8 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.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.util.Iterator; |
29 |
|
30 |
import javax.swing.JOptionPane; |
31 |
|
32 |
import org.gvsig.andami.IconThemeHelper; |
33 |
import org.gvsig.andami.PluginServices; |
34 |
import org.gvsig.andami.messages.NotificationManager; |
35 |
import org.gvsig.andami.plugins.Extension; |
36 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
37 |
import org.gvsig.app.ApplicationLocator; |
38 |
import org.gvsig.app.gui.filter.ExpressionListener; |
39 |
import org.gvsig.app.gui.filter.FilterDialog; |
40 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
41 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
42 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
43 |
import org.gvsig.expressionevaluator.swing.Element; |
44 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
45 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
46 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
47 |
import org.gvsig.fmap.dal.DALLocator; |
48 |
import org.gvsig.fmap.dal.DataManager; |
49 |
import org.gvsig.fmap.dal.exception.DataException; |
50 |
import org.gvsig.fmap.dal.feature.Feature; |
51 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
52 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
53 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
54 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
55 |
import org.gvsig.i18n.Messages; |
56 |
import org.gvsig.tools.dispose.DisposeUtils; |
57 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
58 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
59 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
60 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
61 |
import org.gvsig.utils.exceptionHandling.ExceptionListener; |
62 |
|
63 |
/**
|
64 |
* Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
|
65 |
*
|
66 |
* @author Vicente Caballero Navarro
|
67 |
*/
|
68 |
public class SelectByAttributesInTableExtension extends Extension implements ExpressionListener { |
69 |
|
70 |
protected FeatureStore featureStore = null; |
71 |
protected FeatureTableDocumentPanel table;
|
72 |
private String filterTitle; |
73 |
|
74 |
public void initialize() { |
75 |
registerIcons(); |
76 |
} |
77 |
|
78 |
private void registerIcons() { |
79 |
IconThemeHelper.registerIcon("action", "selection-by-attributes", this); |
80 |
} |
81 |
|
82 |
public void execute(String actionCommand) { |
83 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
84 |
|
85 |
if (!(v instanceof FeatureTableDocumentPanel)) { |
86 |
return;
|
87 |
} |
88 |
if ("selection-by-attributes-table2".equals(actionCommand)) { |
89 |
table = (FeatureTableDocumentPanel) v; |
90 |
|
91 |
featureStore = table.getModel().getStore(); |
92 |
filterTitle = table.getModel().getName(); |
93 |
table.getModel().setModified(true);
|
94 |
|
95 |
doSelectByAttributes2(filterTitle, featureStore); |
96 |
|
97 |
} else if ("selection-by-attributes-table".equals(actionCommand)) { |
98 |
table = (FeatureTableDocumentPanel) v; |
99 |
|
100 |
featureStore = table.getModel().getStore(); |
101 |
filterTitle = table.getModel().getName(); |
102 |
table.getModel().setModified(true);
|
103 |
|
104 |
doExecute(); |
105 |
} |
106 |
} |
107 |
|
108 |
protected void doExecute() { |
109 |
FilterDialog dlg = new FilterDialog(filterTitle);
|
110 |
dlg.addExpressionListener(this);
|
111 |
dlg.addExceptionListener(new ExceptionListener() { |
112 |
|
113 |
public void exceptionThrown(Throwable t) { |
114 |
NotificationManager.addError(t.getMessage(), t); |
115 |
} |
116 |
}); |
117 |
dlg.setModel(featureStore); |
118 |
PluginServices.getMDIManager().addWindow(dlg); |
119 |
} |
120 |
|
121 |
public boolean isEnabled() { |
122 |
return isVisible();
|
123 |
} |
124 |
|
125 |
public boolean isVisible() { |
126 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
127 |
return (v instanceof FeatureTableDocumentPanel); |
128 |
} |
129 |
|
130 |
// if no filter expression -> no element selected
|
131 |
public void newSet(String expression) throws DataException { |
132 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
133 |
FeatureSet set = null;
|
134 |
try {
|
135 |
set = doSet(expression); |
136 |
if (set == null) { |
137 |
return;
|
138 |
} |
139 |
featureStore.setSelection(set); |
140 |
|
141 |
} catch (Exception e) { |
142 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
143 |
JOptionPane.showMessageDialog(
|
144 |
ApplicationLocator.getManager().getRootComponent(), |
145 |
Messages.getText("_Invalid_expression") + ":\n" |
146 |
+ getLastMessage(e), |
147 |
Messages.getText("_Invalid_expression"),
|
148 |
JOptionPane.ERROR_MESSAGE);
|
149 |
} finally {
|
150 |
DisposeUtils.disposeQuietly(set); |
151 |
} |
152 |
} else {
|
153 |
// if no expression -> no element selected
|
154 |
featureStore.getFeatureSelection().deselectAll(); |
155 |
} |
156 |
} |
157 |
|
158 |
private FeatureSet doSet(String expression) throws DataException { |
159 |
FeatureQuery query = featureStore.createFeatureQuery(); |
160 |
DataManager manager = DALLocator.getDataManager(); |
161 |
query.setFilter(manager.createExpresion(expression)); |
162 |
return featureStore.getFeatureSet(query);
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
public void addToSet(String expression) throws DataException { |
167 |
// if no filter expression -> don't add more elements to set
|
168 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
169 |
FeatureSet set = null;
|
170 |
try {
|
171 |
set = doSet(expression); |
172 |
if (set == null) { |
173 |
return;
|
174 |
} |
175 |
featureStore.getFeatureSelection().select(set); |
176 |
|
177 |
} catch (Exception e) { |
178 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
179 |
JOptionPane.showMessageDialog(
|
180 |
ApplicationLocator.getManager().getRootComponent(), |
181 |
Messages.getText("_Invalid_expression") + ":\n" |
182 |
+ getLastMessage(e), |
183 |
Messages.getText("_Invalid_expression"),
|
184 |
JOptionPane.ERROR_MESSAGE);
|
185 |
} finally {
|
186 |
DisposeUtils.disposeQuietly(set); |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public void fromSet(String expression) throws DataException { |
193 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
194 |
FeatureSet set = null;
|
195 |
try {
|
196 |
set = doSet(expression); |
197 |
if (set == null) { |
198 |
return;
|
199 |
} |
200 |
FeatureSelection oldSelection = featureStore.getFeatureSelection(); |
201 |
FeatureSelection newSelection = featureStore.createFeatureSelection(); |
202 |
Iterator iterator = set.iterator();
|
203 |
while (iterator.hasNext()) {
|
204 |
Feature feature = (Feature) iterator.next(); |
205 |
if (oldSelection.isSelected(feature)) {
|
206 |
newSelection.select(feature); |
207 |
} |
208 |
} |
209 |
featureStore.setSelection(newSelection); |
210 |
} catch (Exception e) { |
211 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
212 |
JOptionPane.showMessageDialog(
|
213 |
ApplicationLocator.getManager().getRootComponent(), |
214 |
Messages.getText("_Invalid_expression") + ":\n" |
215 |
+ getLastMessage(e), |
216 |
Messages.getText("_Invalid_expression"),
|
217 |
JOptionPane.ERROR_MESSAGE);
|
218 |
} finally {
|
219 |
DisposeUtils.disposeQuietly(set); |
220 |
} |
221 |
} else {
|
222 |
featureStore.getFeatureSelection().deselectAll(); |
223 |
} |
224 |
} |
225 |
|
226 |
/**
|
227 |
* Returns true if the WHERE subconsultation of the filterExpression is
|
228 |
* empty ("")
|
229 |
*
|
230 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
231 |
* @param expression An string
|
232 |
* @return A boolean value
|
233 |
*/
|
234 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
235 |
|
236 |
if (expression == null) { |
237 |
return true; |
238 |
} |
239 |
|
240 |
String subExpression = expression.trim();
|
241 |
|
242 |
if (subExpression.length() == 0) { |
243 |
return true; |
244 |
} |
245 |
|
246 |
int pos;
|
247 |
|
248 |
// Remove last ';' if exists
|
249 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
250 |
subExpression |
251 |
= subExpression.substring(0, subExpression.length() - 1).trim(); |
252 |
} |
253 |
|
254 |
// If there is no 'where' clause
|
255 |
if ((pos = subExpression.indexOf("where")) == -1) { |
256 |
return false; |
257 |
} |
258 |
|
259 |
// If there is no subexpression in the WHERE clause -> true
|
260 |
// ( + 5 is the length of the 'where')
|
261 |
subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
|
262 |
|
263 |
if (subExpression.length() == 0) { |
264 |
return true; |
265 |
} else {
|
266 |
return false; |
267 |
} |
268 |
} |
269 |
|
270 |
/**
|
271 |
* @param ex
|
272 |
* @return
|
273 |
*/
|
274 |
public static String getLastMessage(Throwable ex) { |
275 |
|
276 |
Throwable p = ex;
|
277 |
while (p.getCause() != null && p.getCause() != p) { |
278 |
p = p.getCause(); |
279 |
} |
280 |
return p.getMessage();
|
281 |
} |
282 |
|
283 |
private void doSelectByAttributes2(String title, final FeatureStore store) { |
284 |
WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
285 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
286 |
ExpressionEvaluatorSwingManager swingManager = ExpressionEvaluatorSwingLocator.getManager(); |
287 |
|
288 |
final JExpressionBuilder panel = swingManager.createJExpressionBuilder();
|
289 |
Element element = swingManager.createElement("DAL.FeatureStoreElement", panel, store); |
290 |
if (element != null) { |
291 |
panel.getElements().add(element); |
292 |
} |
293 |
final Dialog dialog = windowManager.createDialog( |
294 |
panel.asJComponent(), |
295 |
title, |
296 |
null,
|
297 |
WindowManager_v2.BUTTONS_OK_CANCEL |
298 |
); |
299 |
dialog.addActionListener(new ActionListener() { |
300 |
@Override
|
301 |
public void actionPerformed(ActionEvent e) { |
302 |
if (dialog.getAction() == WindowManager_v2.BUTTON_OK) {
|
303 |
try {
|
304 |
DataManager manager = DALLocator.getDataManager(); |
305 |
|
306 |
FeatureQuery query = store.createFeatureQuery(); |
307 |
query.setFilter(manager.createExpresion(panel.getExpression())); |
308 |
FeatureSet selection = store.getFeatureSet(query); |
309 |
store.getFeatureSelection().select(selection); |
310 |
} catch (Exception ex) { |
311 |
logger.warn("Can't build selecction from filter expression.", ex);
|
312 |
} |
313 |
} |
314 |
} |
315 |
}); |
316 |
dialog.show(WindowManager.MODE.WINDOW); |
317 |
|
318 |
} |
319 |
} |