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 / FiltroExtension.java @ 40558
History | View | Annotate | Download (7.48 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 |
import java.awt.Component; |
27 |
|
28 |
import javax.swing.JOptionPane; |
29 |
|
30 |
import org.gvsig.andami.IconThemeHelper; |
31 |
import org.gvsig.andami.PluginServices; |
32 |
import org.gvsig.andami.messages.NotificationManager; |
33 |
import org.gvsig.andami.plugins.Extension; |
34 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
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.FeatureQuery; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.utils.exceptionHandling.ExceptionListener; |
45 |
|
46 |
/**
|
47 |
* Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
|
48 |
*
|
49 |
* @author Vicente Caballero Navarro
|
50 |
*/
|
51 |
public class FiltroExtension extends Extension implements ExpressionListener { |
52 |
|
53 |
protected FeatureStore featureStore = null; |
54 |
protected FeatureTableDocumentPanel table;
|
55 |
private String filterTitle; |
56 |
|
57 |
public void initialize() { |
58 |
registerIcons(); |
59 |
} |
60 |
|
61 |
private void registerIcons() { |
62 |
IconThemeHelper.registerIcon("action", "table-filter", this); |
63 |
} |
64 |
|
65 |
public void execute(String actionCommand) { |
66 |
if ("table-filter".equals(actionCommand)) { |
67 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
68 |
|
69 |
if (v instanceof FeatureTableDocumentPanel) { |
70 |
table = (FeatureTableDocumentPanel) v; |
71 |
|
72 |
featureStore = table.getModel().getStore(); |
73 |
filterTitle = table.getModel().getName(); |
74 |
table.getModel().setModified(true);
|
75 |
} |
76 |
|
77 |
doExecute(); |
78 |
} |
79 |
} |
80 |
|
81 |
protected void doExecute() { |
82 |
FilterDialog dlg = new FilterDialog(filterTitle);
|
83 |
dlg.addExpressionListener(this);
|
84 |
dlg.addExceptionListener(new ExceptionListener() { |
85 |
|
86 |
public void exceptionThrown(Throwable t) { |
87 |
NotificationManager.addError(t.getMessage(), t); |
88 |
} |
89 |
}); |
90 |
dlg.setModel(featureStore); |
91 |
PluginServices.getMDIManager().addWindow(dlg); |
92 |
} |
93 |
|
94 |
public boolean isEnabled() { |
95 |
return isVisible();
|
96 |
} |
97 |
|
98 |
public boolean isVisible() { |
99 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
100 |
return (v instanceof FeatureTableDocumentPanel); |
101 |
} |
102 |
|
103 |
// By Pablo: if no filter expression -> no element selected
|
104 |
public void newSet(String expression) throws DataException { |
105 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
106 |
FeatureSet set = null;
|
107 |
try {
|
108 |
set = doSet(expression); |
109 |
|
110 |
if (set == null) { |
111 |
return;
|
112 |
} |
113 |
featureStore.setSelection(set); |
114 |
|
115 |
} catch (Exception e) { |
116 |
JOptionPane.showMessageDialog(
|
117 |
(Component) PluginServices.getMainFrame(),
|
118 |
"Asegurate de que la consulta es correcta.");
|
119 |
} finally {
|
120 |
if (set != null) { |
121 |
set.dispose(); |
122 |
} |
123 |
} |
124 |
} else {
|
125 |
// By Pablo: if no expression -> no element selected
|
126 |
featureStore.getFeatureSelection().deselectAll(); |
127 |
} |
128 |
} |
129 |
|
130 |
private FeatureSet doSet(String expression) throws DataException { |
131 |
FeatureQuery query = featureStore.createFeatureQuery(); |
132 |
DataManager manager = DALLocator.getDataManager(); |
133 |
query.setFilter(manager.createExpresion(expression)); |
134 |
return featureStore.getFeatureSet(query);
|
135 |
} |
136 |
|
137 |
public void addToSet(String expression) throws DataException { |
138 |
// By Pablo: if no filter expression -> don't add more elements to set
|
139 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
140 |
FeatureSet set = null;
|
141 |
try {
|
142 |
set = doSet(expression); |
143 |
|
144 |
if (set == null) { |
145 |
return;
|
146 |
} |
147 |
featureStore.getFeatureSelection().select(set); |
148 |
} finally {
|
149 |
if (set != null) { |
150 |
set.dispose(); |
151 |
} |
152 |
} |
153 |
} |
154 |
} |
155 |
|
156 |
public void fromSet(String expression) throws DataException { |
157 |
// By Pablo: if no filter expression -> no element selected
|
158 |
try {
|
159 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
160 |
NotificationManager.showMessageInfo("Falta por implementar",
|
161 |
null);
|
162 |
} else {
|
163 |
// By Pablo: if no expression -> no element selected
|
164 |
featureStore.getFeatureSelection().deselectAll(); |
165 |
} |
166 |
} catch (DataException e) {
|
167 |
NotificationManager.addError(e); |
168 |
} |
169 |
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* Returns true if the WHERE subconsultation of the filterExpression is
|
174 |
* empty ("")
|
175 |
*
|
176 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
177 |
* @param expression
|
178 |
* An string
|
179 |
* @return A boolean value
|
180 |
*/
|
181 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
182 |
|
183 |
if (expression == null) { |
184 |
return true; |
185 |
} |
186 |
|
187 |
String subExpression = expression.trim();
|
188 |
|
189 |
if (subExpression.length() == 0) { |
190 |
return true; |
191 |
} |
192 |
|
193 |
int pos;
|
194 |
|
195 |
// Remove last ';' if exists
|
196 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
197 |
subExpression = |
198 |
subExpression.substring(0, subExpression.length() - 1).trim(); |
199 |
} |
200 |
|
201 |
// If there is no 'where' clause
|
202 |
if ((pos = subExpression.indexOf("where")) == -1) { |
203 |
return false; |
204 |
} |
205 |
|
206 |
// If there is no subexpression in the WHERE clause -> true
|
207 |
subExpression = |
208 |
subExpression.substring(pos + 5, subExpression.length()).trim(); // + |
209 |
// 5
|
210 |
// is
|
211 |
// the
|
212 |
// length
|
213 |
// of
|
214 |
// 'where'
|
215 |
if (subExpression.length() == 0) { |
216 |
return true; |
217 |
} else {
|
218 |
return false; |
219 |
} |
220 |
} |
221 |
} |