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 @ 46277
History | View | Annotate | Download (10.7 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40558 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 41248 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 41248 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 41248 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
20 | 41248 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.app.extension; |
||
24 | |||
25 | import java.util.Iterator; |
||
26 | 44601 | omartinez | import java.util.List; |
27 | 40435 | jjdelcerro | |
28 | import javax.swing.JOptionPane; |
||
29 | |||
30 | import org.gvsig.andami.IconThemeHelper; |
||
31 | import org.gvsig.andami.PluginServices; |
||
32 | import org.gvsig.andami.messages.NotificationManager; |
||
33 | import org.gvsig.andami.plugins.Extension; |
||
34 | 40955 | jldominguez | import org.gvsig.app.ApplicationLocator; |
35 | 41248 | jjdelcerro | import org.gvsig.app.ApplicationManager; |
36 | 40435 | jjdelcerro | import org.gvsig.app.gui.filter.ExpressionListener; |
37 | import org.gvsig.app.gui.filter.FilterDialog; |
||
38 | import org.gvsig.app.project.documents.view.ViewDocument; |
||
39 | 41264 | jjdelcerro | import org.gvsig.app.project.documents.view.gui.IView; |
40 | 40435 | jjdelcerro | 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 | 44601 | omartinez | import org.gvsig.fmap.mapcontext.layers.FLayers; |
50 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
51 | 40955 | jldominguez | import org.gvsig.i18n.Messages; |
52 | 43987 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
53 | 40435 | jjdelcerro | import org.gvsig.utils.exceptionHandling.ExceptionListener; |
54 | |||
55 | /**
|
||
56 | * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
|
||
57 | 41248 | jjdelcerro | *
|
58 | 40435 | jjdelcerro | * @author Vicente Caballero Navarro
|
59 | */
|
||
60 | 43987 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
61 | 41641 | jjdelcerro | public class SelectByAttributesExtension extends Extension implements ExpressionListener { |
62 | 40435 | jjdelcerro | |
63 | protected FeatureStore featureStore = null; |
||
64 | private String filterTitle; |
||
65 | |||
66 | 43987 | jjdelcerro | @Override
|
67 | 40435 | jjdelcerro | public void initialize() { |
68 | 41641 | jjdelcerro | IconThemeHelper.registerIcon("action", "selection-by-attributes", this); |
69 | 40435 | jjdelcerro | } |
70 | |||
71 | 43987 | jjdelcerro | @Override
|
72 | 40435 | jjdelcerro | public void execute(String actionCommand) { |
73 | 41264 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
74 | |||
75 | IView view = (IView) application.getActiveComponent(ViewDocument.class); |
||
76 | if (view == null) { |
||
77 | return;
|
||
78 | } |
||
79 | 44011 | jjdelcerro | if ("selection-by-attributes-layer".equals(actionCommand)) { |
80 | 43987 | jjdelcerro | ViewDocument document = view.getViewDocument(); |
81 | |||
82 | 44601 | omartinez | FLayer layer = document.getMapContext().getLayers().getFirstActiveVectorLayer(); |
83 | 41744 | jjdelcerro | filterTitle = layer.getName(); |
84 | 41248 | jjdelcerro | featureStore = ((FLyrVect) layer).getFeatureStore(); |
85 | document.setModified(true);
|
||
86 | 44011 | jjdelcerro | SelectByAttributes selector = new SelectByAttributes();
|
87 | selector.showWindow(filterTitle, featureStore, WindowManager.MODE.TOOL); |
||
88 | 43987 | jjdelcerro | |
89 | 44011 | jjdelcerro | |
90 | } else if ("selection-by-attributes-layer-old".equals(actionCommand)) { |
||
91 | 43987 | jjdelcerro | ViewDocument document = view.getViewDocument(); |
92 | |||
93 | FLayer layer = document.getMapContext().getLayers().getActives()[0];
|
||
94 | filterTitle = layer.getName(); |
||
95 | featureStore = ((FLyrVect) layer).getFeatureStore(); |
||
96 | document.setModified(true);
|
||
97 | 40435 | jjdelcerro | doExecute(); |
98 | } |
||
99 | } |
||
100 | |||
101 | /**
|
||
102 | * "execute" method action.
|
||
103 | 41248 | jjdelcerro | *
|
104 | 40435 | jjdelcerro | */
|
105 | protected void doExecute() { |
||
106 | FilterDialog dlg = new FilterDialog(filterTitle);
|
||
107 | dlg.addExpressionListener(this);
|
||
108 | dlg.addExceptionListener(new ExceptionListener() { |
||
109 | |||
110 | 43987 | jjdelcerro | @Override
|
111 | 40435 | jjdelcerro | public void exceptionThrown(Throwable t) { |
112 | NotificationManager.addError(t.getMessage(), t); |
||
113 | } |
||
114 | }); |
||
115 | dlg.setModel(featureStore); |
||
116 | PluginServices.getMDIManager().addWindow(dlg); |
||
117 | } |
||
118 | |||
119 | 43987 | jjdelcerro | @Override
|
120 | 40435 | jjdelcerro | public boolean isEnabled() { |
121 | 44437 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
122 | |||
123 | IView view = (IView) application.getActiveComponent(ViewDocument.class); |
||
124 | if (view == null) { |
||
125 | return false; |
||
126 | } |
||
127 | ViewDocument document = view.getViewDocument(); |
||
128 | 44601 | omartinez | if (document == null) { |
129 | 44437 | jjdelcerro | return false; |
130 | } |
||
131 | 44601 | omartinez | |
132 | FLayers layers = document.getMapContext().getLayers(); |
||
133 | List<FLayer> activeLayers = layers.getLayers(
|
||
134 | FLayers.LAYER_ACTIVE |
||
135 | .and(FLayers.LAYER_AVALAIBLE) |
||
136 | .and(FLayers.LAYER_FLYRVECT) |
||
137 | ); |
||
138 | if (activeLayers.size() != 1) { |
||
139 | return false; |
||
140 | 44437 | jjdelcerro | } |
141 | 44601 | omartinez | List<FLayer> selectivedLayers = document.getMapContext().getLayers().getLayers(
|
142 | FLayers.LAYER_ACTIVE |
||
143 | .and(FLayers.LAYER_AVALAIBLE) |
||
144 | .and(FLayers.LAYER_SELECTION_AVAILABLE) |
||
145 | ); |
||
146 | if (selectivedLayers.size()==1) { |
||
147 | return true; |
||
148 | } |
||
149 | |||
150 | return false; |
||
151 | 40435 | jjdelcerro | } |
152 | |||
153 | 43987 | jjdelcerro | @Override
|
154 | 40435 | jjdelcerro | public boolean isVisible() { |
155 | 41248 | jjdelcerro | ApplicationManager application = ApplicationLocator.getManager(); |
156 | 40435 | jjdelcerro | |
157 | 41264 | jjdelcerro | IView view = (IView) application.getActiveComponent(ViewDocument.class); |
158 | if (view == null) { |
||
159 | 40435 | jjdelcerro | return false; |
160 | } |
||
161 | 41264 | jjdelcerro | ViewDocument document = view.getViewDocument(); |
162 | 41248 | jjdelcerro | return document.getMapContext().hasActiveVectorLayers();
|
163 | 40435 | jjdelcerro | } |
164 | |||
165 | // By Pablo: if no filter expression -> no element selected
|
||
166 | 43987 | jjdelcerro | @Override
|
167 | 40435 | jjdelcerro | public void newSet(String expression) throws DataException { |
168 | if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
||
169 | FeatureSet set = null;
|
||
170 | try {
|
||
171 | set = doSet(expression); |
||
172 | |||
173 | if (set == null) { |
||
174 | // throw new RuntimeException("Not a 'where' clause?");
|
||
175 | return;
|
||
176 | } |
||
177 | featureStore.setSelection(set); |
||
178 | |||
179 | } catch (Exception e) { |
||
180 | 41248 | jjdelcerro | |
181 | 40955 | jldominguez | JOptionPane.showMessageDialog(
|
182 | 41248 | jjdelcerro | ApplicationLocator.getManager().getRootComponent(), |
183 | Messages.getText("expresion_error") + ":\n" |
||
184 | 40955 | jldominguez | + getLastMessage(e), |
185 | 41248 | jjdelcerro | Messages.getText("expresion_error"),
|
186 | JOptionPane.ERROR_MESSAGE);
|
||
187 | 40435 | jjdelcerro | } finally {
|
188 | if (set != null) { |
||
189 | set.dispose(); |
||
190 | } |
||
191 | } |
||
192 | } else {
|
||
193 | // By Pablo: if no expression -> no element selected
|
||
194 | featureStore.getFeatureSelection().deselectAll(); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | private FeatureSet doSet(String expression) throws DataException { |
||
199 | FeatureQuery query = featureStore.createFeatureQuery(); |
||
200 | DataManager manager = DALLocator.getDataManager(); |
||
201 | query.setFilter(manager.createExpresion(expression)); |
||
202 | return featureStore.getFeatureSet(query);
|
||
203 | } |
||
204 | |||
205 | 43987 | jjdelcerro | @Override
|
206 | 40435 | jjdelcerro | public void addToSet(String expression) throws DataException { |
207 | // By Pablo: if no filter expression -> don't add more elements to set
|
||
208 | if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
||
209 | FeatureSet set = null;
|
||
210 | try {
|
||
211 | set = doSet(expression); |
||
212 | |||
213 | if (set == null) { |
||
214 | // throw new RuntimeException("Not a 'where' clause?");
|
||
215 | return;
|
||
216 | } |
||
217 | featureStore.getFeatureSelection().select(set); |
||
218 | } finally {
|
||
219 | if (set != null) { |
||
220 | set.dispose(); |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | 43987 | jjdelcerro | @Override
|
227 | 40435 | jjdelcerro | public void fromSet(String expression) throws DataException { |
228 | // By Pablo: if no filter expression -> no element selected
|
||
229 | try {
|
||
230 | if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
||
231 | // NotificationManager.showMessageInfo("Falta por implementar",
|
||
232 | // null);
|
||
233 | |||
234 | 43987 | jjdelcerro | FeatureSet set; |
235 | 40435 | jjdelcerro | set = doSet(expression); |
236 | |||
237 | if (set == null) { |
||
238 | throw new RuntimeException("Not a 'where' clause?"); |
||
239 | } |
||
240 | |||
241 | 41248 | jjdelcerro | FeatureSelection oldSelection |
242 | = featureStore.getFeatureSelection(); |
||
243 | 40435 | jjdelcerro | |
244 | 41248 | jjdelcerro | FeatureSelection newSelection |
245 | = featureStore.createFeatureSelection(); |
||
246 | 40435 | jjdelcerro | Iterator iterator = set.iterator();
|
247 | while (iterator.hasNext()) {
|
||
248 | Feature feature = (Feature) iterator.next(); |
||
249 | if (oldSelection.isSelected(feature)) {
|
||
250 | newSelection.select(feature); |
||
251 | } |
||
252 | } |
||
253 | featureStore.setSelection(newSelection); |
||
254 | set.dispose(); |
||
255 | |||
256 | } else {
|
||
257 | // By Pablo: if no expression -> no element selected
|
||
258 | featureStore.getFeatureSelection().deselectAll(); |
||
259 | } |
||
260 | } catch (DataException e) {
|
||
261 | NotificationManager.addError(e); |
||
262 | } |
||
263 | |||
264 | } |
||
265 | |||
266 | /**
|
||
267 | * Returns true if the WHERE subconsultation of the filterExpression is
|
||
268 | * empty ("")
|
||
269 | 41248 | jjdelcerro | *
|
270 | 40435 | jjdelcerro | * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
271 | 41248 | jjdelcerro | * @param expression An string
|
272 | 40435 | jjdelcerro | * @return A boolean value
|
273 | */
|
||
274 | private boolean filterExpressionFromWhereIsEmpty(String expression) { |
||
275 | 41248 | jjdelcerro | |
276 | 40435 | jjdelcerro | if (expression == null) { |
277 | return true; |
||
278 | } |
||
279 | 41248 | jjdelcerro | |
280 | 40435 | jjdelcerro | String subExpression = expression.trim();
|
281 | 41248 | jjdelcerro | |
282 | 40435 | jjdelcerro | if (subExpression.length() == 0) { |
283 | return true; |
||
284 | } |
||
285 | 41248 | jjdelcerro | |
286 | 40435 | jjdelcerro | int pos;
|
287 | |||
288 | // Remove last ';' if exists
|
||
289 | if (subExpression.charAt(subExpression.length() - 1) == ';') { |
||
290 | 41248 | jjdelcerro | subExpression |
291 | = subExpression.substring(0, subExpression.length() - 1).trim(); |
||
292 | 40435 | jjdelcerro | } |
293 | |||
294 | // If there is no 'where' clause
|
||
295 | if ((pos = subExpression.indexOf("where")) == -1) { |
||
296 | return false; |
||
297 | } |
||
298 | |||
299 | // If there is no subexpression in the WHERE clause -> true
|
||
300 | 41248 | jjdelcerro | // + 5 is the length of 'where'
|
301 | subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
|
||
302 | 40435 | jjdelcerro | if (subExpression.length() == 0) { |
303 | return true; |
||
304 | } else {
|
||
305 | return false; |
||
306 | } |
||
307 | } |
||
308 | 41248 | jjdelcerro | |
309 | 40955 | jldominguez | private String getLastMessage(Throwable ex) { |
310 | 41248 | jjdelcerro | |
311 | 40955 | jldominguez | Throwable p = ex;
|
312 | while (p.getCause() != null && p.getCause() != p) { |
||
313 | p = p.getCause(); |
||
314 | } |
||
315 | return p.getMessage();
|
||
316 | 41248 | jjdelcerro | } |
317 | } |