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