Revision 43987 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
SelectByAttributesInTableExtension.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.app.extension; |
25 | 25 |
|
26 |
import java.awt.event.ActionEvent; |
|
27 |
import java.awt.event.ActionListener; |
|
26 | 28 |
import java.util.Iterator; |
27 | 29 |
|
28 | 30 |
import javax.swing.JOptionPane; |
... | ... | |
36 | 38 |
import org.gvsig.app.gui.filter.ExpressionListener; |
37 | 39 |
import org.gvsig.app.gui.filter.FilterDialog; |
38 | 40 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
41 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
|
42 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
43 |
import org.gvsig.expressionevaluator.swing.Element; |
|
44 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
|
45 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
|
46 |
import org.gvsig.expressionevaluator.swing.JExpressionBuilder; |
|
39 | 47 |
import org.gvsig.fmap.dal.DALLocator; |
40 | 48 |
import org.gvsig.fmap.dal.DataManager; |
41 | 49 |
import org.gvsig.fmap.dal.exception.DataException; |
... | ... | |
46 | 54 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
47 | 55 |
import org.gvsig.i18n.Messages; |
48 | 56 |
import org.gvsig.tools.dispose.DisposeUtils; |
57 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
|
58 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
|
59 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
|
60 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
|
49 | 61 |
import org.gvsig.utils.exceptionHandling.ExceptionListener; |
50 | 62 |
|
51 | 63 |
/** |
52 | 64 |
* Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla. |
53 |
*
|
|
65 |
* |
|
54 | 66 |
* @author Vicente Caballero Navarro |
55 | 67 |
*/ |
56 | 68 |
public class SelectByAttributesInTableExtension extends Extension implements ExpressionListener { |
... | ... | |
64 | 76 |
} |
65 | 77 |
|
66 | 78 |
private void registerIcons() { |
67 |
IconThemeHelper.registerIcon("action", "selection-by-attributes", this);
|
|
79 |
IconThemeHelper.registerIcon("action", "selection-by-attributes", this);
|
|
68 | 80 |
} |
69 | 81 |
|
70 | 82 |
public void execute(String actionCommand) { |
71 |
if ("selection-by-attributes-table".equals(actionCommand)) { |
|
72 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
|
83 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
|
73 | 84 |
|
74 |
if (v instanceof FeatureTableDocumentPanel) { |
|
75 |
table = (FeatureTableDocumentPanel) v; |
|
85 |
if (!(v instanceof FeatureTableDocumentPanel)) { |
|
86 |
return; |
|
87 |
} |
|
88 |
if ("selection-by-attributes-table2".equals(actionCommand)) { |
|
89 |
table = (FeatureTableDocumentPanel) v; |
|
76 | 90 |
|
77 |
featureStore = table.getModel().getStore(); |
|
78 |
filterTitle = table.getModel().getName(); |
|
79 |
table.getModel().setModified(true); |
|
80 |
} |
|
91 |
featureStore = table.getModel().getStore(); |
|
92 |
filterTitle = table.getModel().getName(); |
|
93 |
table.getModel().setModified(true); |
|
81 | 94 |
|
95 |
doSelectByAttributes2(filterTitle, featureStore); |
|
96 |
|
|
97 |
} else if ("selection-by-attributes-table".equals(actionCommand)) { |
|
98 |
table = (FeatureTableDocumentPanel) v; |
|
99 |
|
|
100 |
featureStore = table.getModel().getStore(); |
|
101 |
filterTitle = table.getModel().getName(); |
|
102 |
table.getModel().setModified(true); |
|
103 |
|
|
82 | 104 |
doExecute(); |
83 | 105 |
} |
84 | 106 |
} |
... | ... | |
115 | 137 |
return; |
116 | 138 |
} |
117 | 139 |
featureStore.setSelection(set); |
118 |
|
|
140 |
|
|
119 | 141 |
} catch (Exception e) { |
120 |
logger.warn("Problems executing query '"+expression+"' on store '"+featureStore+"'.",e);
|
|
142 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
|
|
121 | 143 |
JOptionPane.showMessageDialog( |
122 |
ApplicationLocator.getManager().getRootComponent(), |
|
123 |
Messages.getText("_Invalid_expression") + ":\n" |
|
144 |
ApplicationLocator.getManager().getRootComponent(),
|
|
145 |
Messages.getText("_Invalid_expression") + ":\n"
|
|
124 | 146 |
+ getLastMessage(e), |
125 |
Messages.getText("_Invalid_expression"), |
|
126 |
JOptionPane.ERROR_MESSAGE); |
|
147 |
Messages.getText("_Invalid_expression"),
|
|
148 |
JOptionPane.ERROR_MESSAGE);
|
|
127 | 149 |
} finally { |
128 | 150 |
DisposeUtils.disposeQuietly(set); |
129 | 151 |
} |
... | ... | |
153 | 175 |
featureStore.getFeatureSelection().select(set); |
154 | 176 |
|
155 | 177 |
} catch (Exception e) { |
156 |
logger.warn("Problems executing query '"+expression+"' on store '"+featureStore+"'.",e);
|
|
178 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
|
|
157 | 179 |
JOptionPane.showMessageDialog( |
158 |
ApplicationLocator.getManager().getRootComponent(), |
|
159 |
Messages.getText("_Invalid_expression") + ":\n" |
|
180 |
ApplicationLocator.getManager().getRootComponent(),
|
|
181 |
Messages.getText("_Invalid_expression") + ":\n"
|
|
160 | 182 |
+ getLastMessage(e), |
161 |
Messages.getText("_Invalid_expression"), |
|
162 |
JOptionPane.ERROR_MESSAGE); |
|
183 |
Messages.getText("_Invalid_expression"),
|
|
184 |
JOptionPane.ERROR_MESSAGE);
|
|
163 | 185 |
} finally { |
164 | 186 |
DisposeUtils.disposeQuietly(set); |
165 | 187 |
} |
... | ... | |
168 | 190 |
|
169 | 191 |
@Override |
170 | 192 |
public void fromSet(String expression) throws DataException { |
171 |
if ( !this.filterExpressionFromWhereIsEmpty(expression) ) {
|
|
193 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) {
|
|
172 | 194 |
FeatureSet set = null; |
173 | 195 |
try { |
174 | 196 |
set = doSet(expression); |
175 |
if ( set == null ) {
|
|
197 |
if (set == null) {
|
|
176 | 198 |
return; |
177 | 199 |
} |
178 | 200 |
FeatureSelection oldSelection = featureStore.getFeatureSelection(); |
179 | 201 |
FeatureSelection newSelection = featureStore.createFeatureSelection(); |
180 | 202 |
Iterator iterator = set.iterator(); |
181 |
while ( iterator.hasNext() ) {
|
|
203 |
while (iterator.hasNext()) {
|
|
182 | 204 |
Feature feature = (Feature) iterator.next(); |
183 |
if ( oldSelection.isSelected(feature) ) {
|
|
205 |
if (oldSelection.isSelected(feature)) {
|
|
184 | 206 |
newSelection.select(feature); |
185 | 207 |
} |
186 | 208 |
} |
187 | 209 |
featureStore.setSelection(newSelection); |
188 | 210 |
} catch (Exception e) { |
189 |
logger.warn("Problems executing query '"+expression+"' on store '"+featureStore+"'.",e);
|
|
211 |
logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
|
|
190 | 212 |
JOptionPane.showMessageDialog( |
191 |
ApplicationLocator.getManager().getRootComponent(), |
|
192 |
Messages.getText("_Invalid_expression") + ":\n" |
|
213 |
ApplicationLocator.getManager().getRootComponent(),
|
|
214 |
Messages.getText("_Invalid_expression") + ":\n"
|
|
193 | 215 |
+ getLastMessage(e), |
194 |
Messages.getText("_Invalid_expression"), |
|
195 |
JOptionPane.ERROR_MESSAGE); |
|
216 |
Messages.getText("_Invalid_expression"),
|
|
217 |
JOptionPane.ERROR_MESSAGE);
|
|
196 | 218 |
} finally { |
197 | 219 |
DisposeUtils.disposeQuietly(set); |
198 | 220 |
} |
... | ... | |
200 | 222 |
featureStore.getFeatureSelection().deselectAll(); |
201 | 223 |
} |
202 | 224 |
} |
203 |
|
|
225 |
|
|
204 | 226 |
/** |
205 | 227 |
* Returns true if the WHERE subconsultation of the filterExpression is |
206 | 228 |
* empty ("") |
207 |
*
|
|
229 |
* |
|
208 | 230 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com) |
209 |
* @param expression |
|
210 |
* An string |
|
231 |
* @param expression An string |
|
211 | 232 |
* @return A boolean value |
212 | 233 |
*/ |
213 | 234 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
214 |
|
|
235 |
|
|
215 | 236 |
if (expression == null) { |
216 | 237 |
return true; |
217 | 238 |
} |
218 |
|
|
239 |
|
|
219 | 240 |
String subExpression = expression.trim(); |
220 |
|
|
241 |
|
|
221 | 242 |
if (subExpression.length() == 0) { |
222 | 243 |
return true; |
223 | 244 |
} |
224 |
|
|
245 |
|
|
225 | 246 |
int pos; |
226 | 247 |
|
227 | 248 |
// Remove last ';' if exists |
228 | 249 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
229 |
subExpression =
|
|
230 |
subExpression.substring(0, subExpression.length() - 1).trim(); |
|
250 |
subExpression |
|
251 |
= subExpression.substring(0, subExpression.length() - 1).trim();
|
|
231 | 252 |
} |
232 | 253 |
|
233 | 254 |
// If there is no 'where' clause |
... | ... | |
237 | 258 |
|
238 | 259 |
// If there is no subexpression in the WHERE clause -> true |
239 | 260 |
// ( + 5 is the length of the 'where') |
240 |
subExpression =subExpression.substring(pos + 5, subExpression.length()).trim();
|
|
241 |
|
|
261 |
subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
|
|
262 |
|
|
242 | 263 |
if (subExpression.length() == 0) { |
243 | 264 |
return true; |
244 | 265 |
} else { |
245 | 266 |
return false; |
246 | 267 |
} |
247 | 268 |
} |
248 |
|
|
249 |
|
|
269 |
|
|
250 | 270 |
/** |
251 | 271 |
* @param ex |
252 | 272 |
* @return |
253 | 273 |
*/ |
254 | 274 |
public static String getLastMessage(Throwable ex) { |
255 |
|
|
275 |
|
|
256 | 276 |
Throwable p = ex; |
257 | 277 |
while (p.getCause() != null && p.getCause() != p) { |
258 | 278 |
p = p.getCause(); |
259 | 279 |
} |
260 | 280 |
return p.getMessage(); |
261 |
} |
|
281 |
} |
|
282 |
|
|
283 |
private void doSelectByAttributes2(String title, final FeatureStore store) { |
|
284 |
WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
|
285 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
|
286 |
ExpressionEvaluatorSwingManager swingManager = ExpressionEvaluatorSwingLocator.getManager(); |
|
287 |
|
|
288 |
final JExpressionBuilder panel = swingManager.createJExpressionBuilder(); |
|
289 |
Element element = swingManager.createElement("DAL.FeatureStoreElement", panel, store); |
|
290 |
if (element != null) { |
|
291 |
panel.getElements().add(element); |
|
292 |
} |
|
293 |
final Dialog dialog = windowManager.createDialog( |
|
294 |
panel.asJComponent(), |
|
295 |
title, |
|
296 |
null, |
|
297 |
WindowManager_v2.BUTTONS_OK_CANCEL |
|
298 |
); |
|
299 |
dialog.addActionListener(new ActionListener() { |
|
300 |
@Override |
|
301 |
public void actionPerformed(ActionEvent e) { |
|
302 |
if (dialog.getAction() == WindowManager_v2.BUTTON_OK) { |
|
303 |
try { |
|
304 |
DataManager manager = DALLocator.getDataManager(); |
|
305 |
|
|
306 |
FeatureQuery query = store.createFeatureQuery(); |
|
307 |
query.setFilter(manager.createExpresion(panel.getExpression())); |
|
308 |
FeatureSet selection = store.getFeatureSet(query); |
|
309 |
store.getFeatureSelection().select(selection); |
|
310 |
} catch (Exception ex) { |
|
311 |
logger.warn("Can't build selecction from filter expression.", ex); |
|
312 |
} |
|
313 |
} |
|
314 |
} |
|
315 |
}); |
|
316 |
dialog.show(WindowManager.MODE.WINDOW); |
|
317 |
|
|
318 |
} |
|
262 | 319 |
} |
Also available in: Unified diff