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 @ 44601
History | View | Annotate | Download (10.7 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.util.Iterator; |
26 |
import java.util.List; |
27 |
|
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 |
import org.gvsig.app.ApplicationLocator; |
35 |
import org.gvsig.app.ApplicationManager; |
36 |
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 |
import org.gvsig.app.project.documents.view.gui.IView; |
40 |
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 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
50 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
51 |
import org.gvsig.i18n.Messages; |
52 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
53 |
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 |
*
|
58 |
* @author Vicente Caballero Navarro
|
59 |
*/
|
60 |
@SuppressWarnings("UseSpecificCatch") |
61 |
public class SelectByAttributesExtension extends Extension implements ExpressionListener { |
62 |
|
63 |
protected FeatureStore featureStore = null; |
64 |
private String filterTitle; |
65 |
|
66 |
@Override
|
67 |
public void initialize() { |
68 |
IconThemeHelper.registerIcon("action", "selection-by-attributes", this); |
69 |
} |
70 |
|
71 |
@Override
|
72 |
public void execute(String actionCommand) { |
73 |
ApplicationManager application = ApplicationLocator.getManager(); |
74 |
|
75 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
76 |
if (view == null) { |
77 |
return;
|
78 |
} |
79 |
if ("selection-by-attributes-layer".equals(actionCommand)) { |
80 |
ViewDocument document = view.getViewDocument(); |
81 |
|
82 |
FLayer layer = document.getMapContext().getLayers().getFirstActiveVectorLayer(); |
83 |
filterTitle = layer.getName(); |
84 |
featureStore = ((FLyrVect) layer).getFeatureStore(); |
85 |
document.setModified(true);
|
86 |
SelectByAttributes selector = new SelectByAttributes();
|
87 |
selector.showWindow(filterTitle, featureStore, WindowManager.MODE.TOOL); |
88 |
|
89 |
|
90 |
} else if ("selection-by-attributes-layer-old".equals(actionCommand)) { |
91 |
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 |
doExecute(); |
98 |
} |
99 |
} |
100 |
|
101 |
/**
|
102 |
* "execute" method action.
|
103 |
*
|
104 |
*/
|
105 |
protected void doExecute() { |
106 |
FilterDialog dlg = new FilterDialog(filterTitle);
|
107 |
dlg.addExpressionListener(this);
|
108 |
dlg.addExceptionListener(new ExceptionListener() { |
109 |
|
110 |
@Override
|
111 |
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 |
@Override
|
120 |
public boolean isEnabled() { |
121 |
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 |
if (document == null) { |
129 |
return false; |
130 |
} |
131 |
|
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 |
} |
141 |
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 |
} |
152 |
|
153 |
@Override
|
154 |
public boolean isVisible() { |
155 |
ApplicationManager application = ApplicationLocator.getManager(); |
156 |
|
157 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
158 |
if (view == null) { |
159 |
return false; |
160 |
} |
161 |
ViewDocument document = view.getViewDocument(); |
162 |
return document.getMapContext().hasActiveVectorLayers();
|
163 |
} |
164 |
|
165 |
// By Pablo: if no filter expression -> no element selected
|
166 |
@Override
|
167 |
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 |
|
181 |
JOptionPane.showMessageDialog(
|
182 |
ApplicationLocator.getManager().getRootComponent(), |
183 |
Messages.getText("expresion_error") + ":\n" |
184 |
+ getLastMessage(e), |
185 |
Messages.getText("expresion_error"),
|
186 |
JOptionPane.ERROR_MESSAGE);
|
187 |
} 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 |
@Override
|
206 |
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 |
@Override
|
227 |
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 |
FeatureSet set; |
235 |
set = doSet(expression); |
236 |
|
237 |
if (set == null) { |
238 |
throw new RuntimeException("Not a 'where' clause?"); |
239 |
} |
240 |
|
241 |
FeatureSelection oldSelection |
242 |
= featureStore.getFeatureSelection(); |
243 |
|
244 |
FeatureSelection newSelection |
245 |
= featureStore.createFeatureSelection(); |
246 |
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 |
*
|
270 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
271 |
* @param expression An string
|
272 |
* @return A boolean value
|
273 |
*/
|
274 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
275 |
|
276 |
if (expression == null) { |
277 |
return true; |
278 |
} |
279 |
|
280 |
String subExpression = expression.trim();
|
281 |
|
282 |
if (subExpression.length() == 0) { |
283 |
return true; |
284 |
} |
285 |
|
286 |
int pos;
|
287 |
|
288 |
// Remove last ';' if exists
|
289 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
290 |
subExpression |
291 |
= subExpression.substring(0, subExpression.length() - 1).trim(); |
292 |
} |
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 |
// + 5 is the length of 'where'
|
301 |
subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
|
302 |
if (subExpression.length() == 0) { |
303 |
return true; |
304 |
} else {
|
305 |
return false; |
306 |
} |
307 |
} |
308 |
|
309 |
private String getLastMessage(Throwable ex) { |
310 |
|
311 |
Throwable p = ex;
|
312 |
while (p.getCause() != null && p.getCause() != p) { |
313 |
p = p.getCause(); |
314 |
} |
315 |
return p.getMessage();
|
316 |
} |
317 |
} |