svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / SelectByAttributesInTableExtension.java @ 45739
History | View | Annotate | Download (10.5 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
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.extension; |
25 |
|
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.andami.ui.mdiManager.IWindow; |
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.table.TableDocument; |
39 |
import org.gvsig.app.project.documents.table.TableManager; |
40 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
41 |
import org.gvsig.fmap.dal.DALLocator; |
42 |
import org.gvsig.fmap.dal.DataManager; |
43 |
import org.gvsig.fmap.dal.exception.DataException; |
44 |
import org.gvsig.fmap.dal.feature.Feature; |
45 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
46 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
47 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
48 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
49 |
import org.gvsig.fmap.dal.feature.FeatureType; |
50 |
import org.gvsig.i18n.Messages; |
51 |
import org.gvsig.tools.dispose.DisposeUtils; |
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 SelectByAttributesInTableExtension extends Extension implements ExpressionListener { |
62 |
|
63 |
protected FeatureStore featureStore = null; |
64 |
protected FeatureTableDocumentPanel table;
|
65 |
private String filterTitle; |
66 |
|
67 |
@Override
|
68 |
public void initialize() { |
69 |
registerIcons(); |
70 |
} |
71 |
|
72 |
private void registerIcons() { |
73 |
IconThemeHelper.registerIcon("action", "selection-by-attributes", this); |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public void execute(String actionCommand) { |
78 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
79 |
|
80 |
if (!(v instanceof FeatureTableDocumentPanel)) { |
81 |
return;
|
82 |
} |
83 |
if ("selection-by-attributes-table".equals(actionCommand)) { |
84 |
table = (FeatureTableDocumentPanel) v; |
85 |
|
86 |
|
87 |
featureStore = table.getFeatureStore(); |
88 |
filterTitle = featureStore.getName(); |
89 |
// table.getModel().setModified(true);
|
90 |
SelectByAttributes selector = new SelectByAttributes();
|
91 |
selector.showWindow(filterTitle, featureStore, WindowManager.MODE.TOOL); |
92 |
|
93 |
} else if ("selection-by-attributes-table-old".equals(actionCommand)) { |
94 |
table = (FeatureTableDocumentPanel) v; |
95 |
|
96 |
featureStore = table.getFeatureStore(); |
97 |
filterTitle = featureStore.getName(); |
98 |
// table.getModel().setModified(true);
|
99 |
|
100 |
doExecute(); |
101 |
} |
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 |
TableDocument doc = (TableDocument) application.getActiveDocument(TableManager.TYPENAME); |
122 |
if (doc == null) { |
123 |
return false; |
124 |
} |
125 |
FeatureTableDocumentPanel tablePanel = (FeatureTableDocumentPanel) doc.getMainComponent(); |
126 |
if( tablePanel == null ) { |
127 |
return false; |
128 |
} |
129 |
FeatureStore store = tablePanel.getFeatureStore(); |
130 |
try {
|
131 |
if( ! store.getFeatureSelection().isAvailable() ) {
|
132 |
return false; |
133 |
} |
134 |
} catch (Exception ex) { |
135 |
} |
136 |
|
137 |
return true; |
138 |
} |
139 |
|
140 |
@Override
|
141 |
public boolean isVisible() { |
142 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
143 |
return (v instanceof FeatureTableDocumentPanel); |
144 |
} |
145 |
|
146 |
// if no filter expression -> no element selected
|
147 |
@Override
|
148 |
public void newSet(String expression) throws DataException { |
149 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
150 |
FeatureSet set = null;
|
151 |
try {
|
152 |
set = doSet(expression); |
153 |
if (set == null) { |
154 |
return;
|
155 |
} |
156 |
featureStore.setSelection(set); |
157 |
|
158 |
} catch (Exception e) { |
159 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
160 |
JOptionPane.showMessageDialog(
|
161 |
ApplicationLocator.getManager().getRootComponent(), |
162 |
Messages.getText("_Invalid_expression") + ":\n" |
163 |
+ getLastMessage(e), |
164 |
Messages.getText("_Invalid_expression"),
|
165 |
JOptionPane.ERROR_MESSAGE);
|
166 |
} finally {
|
167 |
DisposeUtils.disposeQuietly(set); |
168 |
} |
169 |
} else {
|
170 |
// if no expression -> no element selected
|
171 |
featureStore.getFeatureSelection().deselectAll(); |
172 |
} |
173 |
} |
174 |
|
175 |
private FeatureSet doSet(String expression) throws DataException { |
176 |
FeatureQuery query = featureStore.createFeatureQuery(); |
177 |
DataManager manager = DALLocator.getDataManager(); |
178 |
query.setFilter(manager.createExpresion(expression)); |
179 |
return featureStore.getFeatureSet(query);
|
180 |
} |
181 |
|
182 |
@Override
|
183 |
public void addToSet(String expression) throws DataException { |
184 |
// if no filter expression -> don't add more elements to set
|
185 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
186 |
FeatureSet set = null;
|
187 |
try {
|
188 |
set = doSet(expression); |
189 |
if (set == null) { |
190 |
return;
|
191 |
} |
192 |
featureStore.getFeatureSelection().select(set); |
193 |
|
194 |
} catch (Exception e) { |
195 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
196 |
JOptionPane.showMessageDialog(
|
197 |
ApplicationLocator.getManager().getRootComponent(), |
198 |
Messages.getText("_Invalid_expression") + ":\n" |
199 |
+ getLastMessage(e), |
200 |
Messages.getText("_Invalid_expression"),
|
201 |
JOptionPane.ERROR_MESSAGE);
|
202 |
} finally {
|
203 |
DisposeUtils.disposeQuietly(set); |
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public void fromSet(String expression) throws DataException { |
210 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
211 |
FeatureSet set = null;
|
212 |
try {
|
213 |
set = doSet(expression); |
214 |
if (set == null) { |
215 |
return;
|
216 |
} |
217 |
FeatureSelection oldSelection = featureStore.getFeatureSelection(); |
218 |
FeatureSelection newSelection = featureStore.createFeatureSelection(); |
219 |
for (Feature feature : set) {
|
220 |
if (oldSelection.isSelected(feature)) {
|
221 |
newSelection.select(feature); |
222 |
} |
223 |
} |
224 |
featureStore.setSelection(newSelection); |
225 |
} catch (Exception e) { |
226 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e); |
227 |
JOptionPane.showMessageDialog(
|
228 |
ApplicationLocator.getManager().getRootComponent(), |
229 |
Messages.getText("_Invalid_expression") + ":\n" |
230 |
+ getLastMessage(e), |
231 |
Messages.getText("_Invalid_expression"),
|
232 |
JOptionPane.ERROR_MESSAGE);
|
233 |
} finally {
|
234 |
DisposeUtils.disposeQuietly(set); |
235 |
} |
236 |
} else {
|
237 |
featureStore.getFeatureSelection().deselectAll(); |
238 |
} |
239 |
} |
240 |
|
241 |
/**
|
242 |
* Returns true if the WHERE subconsultation of the filterExpression is
|
243 |
* empty ("")
|
244 |
*
|
245 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
246 |
* @param expression An string
|
247 |
* @return A boolean value
|
248 |
*/
|
249 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
250 |
|
251 |
if (expression == null) { |
252 |
return true; |
253 |
} |
254 |
|
255 |
String subExpression = expression.trim();
|
256 |
|
257 |
if (subExpression.length() == 0) { |
258 |
return true; |
259 |
} |
260 |
|
261 |
int pos;
|
262 |
|
263 |
// Remove last ';' if exists
|
264 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
265 |
subExpression |
266 |
= subExpression.substring(0, subExpression.length() - 1).trim(); |
267 |
} |
268 |
|
269 |
// If there is no 'where' clause
|
270 |
if ((pos = subExpression.indexOf("where")) == -1) { |
271 |
return false; |
272 |
} |
273 |
|
274 |
// If there is no subexpression in the WHERE clause -> true
|
275 |
// ( + 5 is the length of the 'where')
|
276 |
subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
|
277 |
|
278 |
if (subExpression.length() == 0) { |
279 |
return true; |
280 |
} else {
|
281 |
return false; |
282 |
} |
283 |
} |
284 |
|
285 |
/**
|
286 |
* @param ex
|
287 |
* @return
|
288 |
*/
|
289 |
public static String getLastMessage(Throwable ex) { |
290 |
|
291 |
Throwable p = ex;
|
292 |
while (p.getCause() != null && p.getCause() != p) { |
293 |
p = p.getCause(); |
294 |
} |
295 |
return p.getMessage();
|
296 |
} |
297 |
} |