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