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 | 40558 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40558 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40558 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40558 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40558 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40558 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40558 | jjdelcerro | *
|
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 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.app.extension; |
||
25 | |||
26 | 43987 | jjdelcerro | import java.awt.event.ActionEvent; |
27 | import java.awt.event.ActionListener; |
||
28 | 41747 | jjdelcerro | import java.util.Iterator; |
29 | 40435 | jjdelcerro | |
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 | 40955 | jldominguez | import org.gvsig.app.ApplicationLocator; |
38 | 40435 | jjdelcerro | 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 | 43987 | jjdelcerro | 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 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
48 | import org.gvsig.fmap.dal.DataManager; |
||
49 | import org.gvsig.fmap.dal.exception.DataException; |
||
50 | 41747 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
51 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
52 | 41747 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSelection; |
53 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSet; |
54 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
55 | 40955 | jldominguez | import org.gvsig.i18n.Messages; |
56 | 43521 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
57 | 43987 | jjdelcerro | 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 | 40435 | jjdelcerro | 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 | 43987 | jjdelcerro | *
|
66 | 40435 | jjdelcerro | * @author Vicente Caballero Navarro
|
67 | */
|
||
68 | 43521 | jjdelcerro | public class SelectByAttributesInTableExtension extends Extension implements ExpressionListener { |
69 | 40435 | jjdelcerro | |
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 | 43987 | jjdelcerro | IconThemeHelper.registerIcon("action", "selection-by-attributes", this); |
80 | 40435 | jjdelcerro | } |
81 | |||
82 | public void execute(String actionCommand) { |
||
83 | 43987 | jjdelcerro | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
84 | 40435 | jjdelcerro | |
85 | 43987 | jjdelcerro | if (!(v instanceof FeatureTableDocumentPanel)) { |
86 | return;
|
||
87 | } |
||
88 | if ("selection-by-attributes-table2".equals(actionCommand)) { |
||
89 | table = (FeatureTableDocumentPanel) v; |
||
90 | 40435 | jjdelcerro | |
91 | 43987 | jjdelcerro | featureStore = table.getModel().getStore(); |
92 | filterTitle = table.getModel().getName(); |
||
93 | table.getModel().setModified(true);
|
||
94 | 40435 | jjdelcerro | |
95 | 43987 | jjdelcerro | 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 | 40435 | jjdelcerro | 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 | 41747 | jjdelcerro | // if no filter expression -> no element selected
|
131 | 40435 | jjdelcerro | 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 | 43987 | jjdelcerro | |
141 | 40435 | jjdelcerro | } catch (Exception e) { |
142 | 43987 | jjdelcerro | logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
143 | 40435 | jjdelcerro | JOptionPane.showMessageDialog(
|
144 | 43987 | jjdelcerro | ApplicationLocator.getManager().getRootComponent(), |
145 | Messages.getText("_Invalid_expression") + ":\n" |
||
146 | 40955 | jldominguez | + getLastMessage(e), |
147 | 43987 | jjdelcerro | Messages.getText("_Invalid_expression"),
|
148 | JOptionPane.ERROR_MESSAGE);
|
||
149 | 40435 | jjdelcerro | } finally {
|
150 | 43521 | jjdelcerro | DisposeUtils.disposeQuietly(set); |
151 | 40435 | jjdelcerro | } |
152 | } else {
|
||
153 | 41747 | jjdelcerro | // if no expression -> no element selected
|
154 | 40435 | jjdelcerro | 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 | 43521 | jjdelcerro | @Override
|
166 | 40435 | jjdelcerro | public void addToSet(String expression) throws DataException { |
167 | 41747 | jjdelcerro | // if no filter expression -> don't add more elements to set
|
168 | 40435 | jjdelcerro | 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 | 43521 | jjdelcerro | |
177 | } catch (Exception e) { |
||
178 | 43987 | jjdelcerro | logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
179 | 43521 | jjdelcerro | JOptionPane.showMessageDialog(
|
180 | 43987 | jjdelcerro | ApplicationLocator.getManager().getRootComponent(), |
181 | Messages.getText("_Invalid_expression") + ":\n" |
||
182 | 43521 | jjdelcerro | + getLastMessage(e), |
183 | 43987 | jjdelcerro | Messages.getText("_Invalid_expression"),
|
184 | JOptionPane.ERROR_MESSAGE);
|
||
185 | 40435 | jjdelcerro | } finally {
|
186 | 43521 | jjdelcerro | DisposeUtils.disposeQuietly(set); |
187 | 40435 | jjdelcerro | } |
188 | } |
||
189 | } |
||
190 | |||
191 | 43521 | jjdelcerro | @Override
|
192 | 40435 | jjdelcerro | public void fromSet(String expression) throws DataException { |
193 | 43987 | jjdelcerro | if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
194 | 41747 | jjdelcerro | FeatureSet set = null;
|
195 | try {
|
||
196 | set = doSet(expression); |
||
197 | 43987 | jjdelcerro | if (set == null) { |
198 | 41747 | jjdelcerro | return;
|
199 | } |
||
200 | FeatureSelection oldSelection = featureStore.getFeatureSelection(); |
||
201 | FeatureSelection newSelection = featureStore.createFeatureSelection(); |
||
202 | Iterator iterator = set.iterator();
|
||
203 | 43987 | jjdelcerro | while (iterator.hasNext()) {
|
204 | 41747 | jjdelcerro | Feature feature = (Feature) iterator.next(); |
205 | 43987 | jjdelcerro | if (oldSelection.isSelected(feature)) {
|
206 | 41747 | jjdelcerro | newSelection.select(feature); |
207 | } |
||
208 | } |
||
209 | featureStore.setSelection(newSelection); |
||
210 | 43521 | jjdelcerro | } catch (Exception e) { |
211 | 43987 | jjdelcerro | logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
212 | 43521 | jjdelcerro | JOptionPane.showMessageDialog(
|
213 | 43987 | jjdelcerro | ApplicationLocator.getManager().getRootComponent(), |
214 | Messages.getText("_Invalid_expression") + ":\n" |
||
215 | 43521 | jjdelcerro | + getLastMessage(e), |
216 | 43987 | jjdelcerro | Messages.getText("_Invalid_expression"),
|
217 | JOptionPane.ERROR_MESSAGE);
|
||
218 | 41747 | jjdelcerro | } finally {
|
219 | 43521 | jjdelcerro | DisposeUtils.disposeQuietly(set); |
220 | 40435 | jjdelcerro | } |
221 | 41747 | jjdelcerro | } else {
|
222 | featureStore.getFeatureSelection().deselectAll(); |
||
223 | 40435 | jjdelcerro | } |
224 | } |
||
225 | 43987 | jjdelcerro | |
226 | 40435 | jjdelcerro | /**
|
227 | * Returns true if the WHERE subconsultation of the filterExpression is
|
||
228 | * empty ("")
|
||
229 | 43987 | jjdelcerro | *
|
230 | 40435 | jjdelcerro | * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
231 | 43987 | jjdelcerro | * @param expression An string
|
232 | 40435 | jjdelcerro | * @return A boolean value
|
233 | */
|
||
234 | private boolean filterExpressionFromWhereIsEmpty(String expression) { |
||
235 | 43987 | jjdelcerro | |
236 | 40435 | jjdelcerro | if (expression == null) { |
237 | return true; |
||
238 | } |
||
239 | 43987 | jjdelcerro | |
240 | 40435 | jjdelcerro | String subExpression = expression.trim();
|
241 | 43987 | jjdelcerro | |
242 | 40435 | jjdelcerro | if (subExpression.length() == 0) { |
243 | return true; |
||
244 | } |
||
245 | 43987 | jjdelcerro | |
246 | 40435 | jjdelcerro | int pos;
|
247 | |||
248 | // Remove last ';' if exists
|
||
249 | if (subExpression.charAt(subExpression.length() - 1) == ';') { |
||
250 | 43987 | jjdelcerro | subExpression |
251 | = subExpression.substring(0, subExpression.length() - 1).trim(); |
||
252 | 40435 | jjdelcerro | } |
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 | 41747 | jjdelcerro | // ( + 5 is the length of the 'where')
|
261 | 43987 | jjdelcerro | subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
|
262 | |||
263 | 40435 | jjdelcerro | if (subExpression.length() == 0) { |
264 | return true; |
||
265 | } else {
|
||
266 | return false; |
||
267 | } |
||
268 | } |
||
269 | 43987 | jjdelcerro | |
270 | 40955 | jldominguez | /**
|
271 | * @param ex
|
||
272 | * @return
|
||
273 | */
|
||
274 | public static String getLastMessage(Throwable ex) { |
||
275 | 43987 | jjdelcerro | |
276 | 40955 | jldominguez | Throwable p = ex;
|
277 | while (p.getCause() != null && p.getCause() != p) { |
||
278 | p = p.getCause(); |
||
279 | } |
||
280 | return p.getMessage();
|
||
281 | 43987 | jjdelcerro | } |
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 | 40435 | jjdelcerro | } |