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