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