svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.impl / src / main / java / org / gvsig / exportto / swing / impl / panel / ExportFilterPanel.java @ 41878
History | View | Annotate | Download (13.4 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.exportto.swing.impl.panel; |
||
25 | |||
26 | 41537 | jjdelcerro | import java.awt.event.ActionEvent; |
27 | import java.awt.event.ActionListener; |
||
28 | import java.util.Iterator; |
||
29 | import javax.swing.DefaultListModel; |
||
30 | 41540 | jjdelcerro | import javax.swing.JOptionPane; |
31 | import javax.swing.event.ListSelectionEvent; |
||
32 | import javax.swing.event.ListSelectionListener; |
||
33 | import org.apache.commons.lang3.StringUtils; |
||
34 | 40435 | jjdelcerro | import org.gvsig.exportto.ExporttoLocator; |
35 | import org.gvsig.exportto.ExporttoManager; |
||
36 | import org.gvsig.exportto.swing.impl.DefaultJExporttoServicePanel; |
||
37 | 41537 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
38 | import org.gvsig.fmap.dal.DataManager; |
||
39 | 41540 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
40 | 41538 | jjdelcerro | import org.gvsig.fmap.dal.exception.InitializeException; |
41 | 41537 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
42 | 41540 | jjdelcerro | import org.gvsig.gui.beans.wizard.panel.NotContinueWizardException; |
43 | 41537 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
44 | import org.gvsig.tools.dynobject.DynObject; |
||
45 | import org.gvsig.tools.dynobject.DynObjectManager; |
||
46 | import org.gvsig.tools.evaluator.Evaluator; |
||
47 | import org.gvsig.tools.evaluator.EvaluatorData; |
||
48 | 41538 | jjdelcerro | import org.gvsig.tools.evaluator.EvaluatorException; |
49 | 41540 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
50 | import org.slf4j.Logger; |
||
51 | import org.slf4j.LoggerFactory; |
||
52 | 40435 | jjdelcerro | |
53 | /**
|
||
54 | * @author gvSIG Team
|
||
55 | * @version $Id$
|
||
56 | *
|
||
57 | */
|
||
58 | 41537 | jjdelcerro | public class ExportFilterPanel extends ExportFilterPanelLayout { |
59 | 41540 | jjdelcerro | private static final long serialVersionUID = -2450969034747572624L; |
60 | |||
61 | private static final Logger logger = LoggerFactory.getLogger(ExportFilterPanel.class); |
||
62 | 40435 | jjdelcerro | |
63 | 41537 | jjdelcerro | private FeatureStore store;
|
64 | |||
65 | 40435 | jjdelcerro | public ExportFilterPanel(DefaultJExporttoServicePanel exporttoServicePanel) {
|
66 | 41540 | jjdelcerro | this.store = exporttoServicePanel.getFeatureStore();
|
67 | 41537 | jjdelcerro | this.initComponents();
|
68 | 40435 | jjdelcerro | } |
69 | |||
70 | 41537 | jjdelcerro | private static class FilterItem { |
71 | private final String label; |
||
72 | private final String value; |
||
73 | public FilterItem(String label, String value) { |
||
74 | this.label=label;
|
||
75 | this.value=value;
|
||
76 | } |
||
77 | @Override
|
||
78 | public String toString() { |
||
79 | return this.label; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | private void initComponents() { |
||
84 | ExporttoManager manager = ExporttoLocator.getManager(); |
||
85 | |||
86 | DefaultListModel model = new DefaultListModel(); |
||
87 | Iterator filterNames = manager.getPredefinedFilters();
|
||
88 | while( filterNames.hasNext() ) {
|
||
89 | String name = (String) filterNames.next(); |
||
90 | String expresion = manager.getFilter(name);
|
||
91 | model.addElement(new FilterItem(name, expresion));
|
||
92 | } |
||
93 | this.lstFilters.setModel(model);
|
||
94 | 41540 | jjdelcerro | this.lstFilters.addListSelectionListener(new ListSelectionListener() { |
95 | public void valueChanged(ListSelectionEvent lse) { |
||
96 | doSelectFilter(); |
||
97 | } |
||
98 | }); |
||
99 | |||
100 | 41655 | jjdelcerro | ActionListener changeOptionAction = new ActionListener() { |
101 | 41537 | jjdelcerro | public void actionPerformed(ActionEvent e) { |
102 | doChangeOption(); |
||
103 | } |
||
104 | 41655 | jjdelcerro | }; |
105 | this.rdbAllRows.addActionListener(changeOptionAction);
|
||
106 | this.rdbFilteredRows.addActionListener(changeOptionAction);
|
||
107 | this.rdbSelectedRows.addActionListener(changeOptionAction);
|
||
108 | this.rdoUseNewExpresion.addActionListener(changeOptionAction);
|
||
109 | this.rdoUseSavedExpresion.addActionListener(changeOptionAction);
|
||
110 | |||
111 | 41537 | jjdelcerro | this.butTest.addActionListener(new ActionListener() { |
112 | public void actionPerformed(ActionEvent e) { |
||
113 | doTest(); |
||
114 | } |
||
115 | }); |
||
116 | 41540 | jjdelcerro | this.rdbAllRows.setSelected(true); |
117 | doChangeOption(); |
||
118 | 41657 | jjdelcerro | this.butFilterDialog.addActionListener(new ActionListener() { |
119 | public void actionPerformed(ActionEvent e) { |
||
120 | doShowFilterDialog(); |
||
121 | } |
||
122 | }); |
||
123 | 41878 | jjdelcerro | this.translate();
|
124 | 41537 | jjdelcerro | } |
125 | 41657 | jjdelcerro | |
126 | 41878 | jjdelcerro | private void translate() { |
127 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
128 | |||
129 | this.rdbAllRows.setText(i18nManager.getTranslation("_Todos_los_registros")); |
||
130 | this.rdbSelectedRows.setText(i18nManager.getTranslation("_Los_registros_seleccionados")); |
||
131 | this.rdbFilteredRows.setText(i18nManager.getTranslation("_Los_registros_que_cumplan_el_critrio_seleccionado")); |
||
132 | this.lblName.setText(i18nManager.getTranslation("_Nombre_de_la_expresion_opcinal")); |
||
133 | this.lblExpresion.setText(i18nManager.getTranslation("_Expresion")); |
||
134 | this.txtName.setToolTipText(i18nManager.getTranslation("_Type_here_the_name_of_the_expression_if_you_want_to_save_it_otherwise_leave_it_blank")); |
||
135 | this.butTest.setText(i18nManager.getTranslation("_Test")); |
||
136 | this.lblHeader.setText(i18nManager.getTranslation("_Indique_que_registros_desea_exportar")); |
||
137 | this.rdoUseSavedExpresion.setText(i18nManager.getTranslation("_Usar_una_expresion_guardada")); |
||
138 | this.rdoUseNewExpresion.setText(i18nManager.getTranslation("_Nueva_expresion")); |
||
139 | this.butFilterDialog.setText(i18nManager.getTranslation("_Filtro")); |
||
140 | } |
||
141 | |||
142 | 41657 | jjdelcerro | private void doShowFilterDialog() { |
143 | |||
144 | } |
||
145 | |||
146 | 41540 | jjdelcerro | protected void doSelectFilter() { |
147 | FilterItem item = (FilterItem) this.lstFilters.getSelectedValue();
|
||
148 | if( item!=null ) { |
||
149 | this.txtExpresion.setText(item.value);
|
||
150 | this.txtName.setText(item.label);
|
||
151 | } |
||
152 | } |
||
153 | |||
154 | 41537 | jjdelcerro | private static class Data implements EvaluatorData { |
155 | private final DynObject data; |
||
156 | |||
157 | public Data(DynObject data) {
|
||
158 | this.data = data;
|
||
159 | } |
||
160 | public Object getDataValue(String name) { |
||
161 | 41540 | jjdelcerro | if( this.data.getDynClass().getDynField(name)!=null ) { |
162 | return this.data.getDynValue(name); |
||
163 | } |
||
164 | if( "defaultgeometry".equalsIgnoreCase(name) ) { |
||
165 | 41610 | jjdelcerro | // FIXME: deberia crear una geometris de agun tipo
|
166 | return null; |
||
167 | 41540 | jjdelcerro | } |
168 | throw new RuntimeException("Identifier '"+name+"'not defined."); |
||
169 | 41537 | jjdelcerro | } |
170 | 40435 | jjdelcerro | |
171 | 41537 | jjdelcerro | public Object getContextValue(String name) { |
172 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
173 | } |
||
174 | 40435 | jjdelcerro | |
175 | 41537 | jjdelcerro | public Iterator getDataValues() { |
176 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
177 | } |
||
178 | 40435 | jjdelcerro | |
179 | 41537 | jjdelcerro | public Iterator getDataNames() { |
180 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
181 | } |
||
182 | 40435 | jjdelcerro | |
183 | 41537 | jjdelcerro | public boolean hasDataValue(String name) { |
184 | 41540 | jjdelcerro | if( "defaultgeometry".equalsIgnoreCase(name) ) { |
185 | return true; |
||
186 | } |
||
187 | return this.data.hasDynValue(name); |
||
188 | 41537 | jjdelcerro | } |
189 | 40435 | jjdelcerro | |
190 | 41537 | jjdelcerro | public boolean hasContextValue(String name) { |
191 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
192 | } |
||
193 | 40435 | jjdelcerro | } |
194 | 41537 | jjdelcerro | |
195 | protected void doTest() { |
||
196 | 41538 | jjdelcerro | try {
|
197 | 41540 | jjdelcerro | this.checkPanel(false); |
198 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
199 | JOptionPane.showMessageDialog(this, |
||
200 | i18nManager.getTranslation("_Expresion_correcta"),
|
||
201 | i18nManager.getTranslation("_Filter"),
|
||
202 | JOptionPane.INFORMATION_MESSAGE
|
||
203 | ); |
||
204 | } catch (NotContinueWizardException ex) {
|
||
205 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
206 | JOptionPane.showMessageDialog(this, |
||
207 | ex.getMessage(), |
||
208 | i18nManager.getTranslation("_Warning"),
|
||
209 | JOptionPane.WARNING_MESSAGE
|
||
210 | ); |
||
211 | 41538 | jjdelcerro | } |
212 | 41537 | jjdelcerro | } |
213 | |||
214 | protected void doChangeOption(){ |
||
215 | 41655 | jjdelcerro | if( this.rdbFilteredRows.isSelected() ) { |
216 | this.rdoUseNewExpresion.setEnabled(true); |
||
217 | this.rdoUseSavedExpresion.setEnabled(true); |
||
218 | |||
219 | 41663 | jjdelcerro | if( this.rdoUseSavedExpresion.isSelected() ) { |
220 | this.lstFilters.setEnabled(true); |
||
221 | this.txtName.setEnabled(false); |
||
222 | this.txtExpresion.setEnabled(false); |
||
223 | this.butTest.setEnabled(false); |
||
224 | this.butFilterDialog.setEnabled(false); |
||
225 | } else {
|
||
226 | this.rdoUseNewExpresion.setSelected(false); |
||
227 | |||
228 | 41655 | jjdelcerro | this.lstFilters.setEnabled(false); |
229 | this.txtName.setEnabled(true); |
||
230 | this.txtExpresion.setEnabled(true); |
||
231 | this.butTest.setEnabled(true); |
||
232 | this.butFilterDialog.setEnabled(true); |
||
233 | } |
||
234 | |||
235 | } else if( this.rdbSelectedRows.isSelected() ) { |
||
236 | 41537 | jjdelcerro | this.lstFilters.setEnabled(false); |
237 | this.txtName.setEnabled(false); |
||
238 | this.txtExpresion.setEnabled(false); |
||
239 | 41540 | jjdelcerro | this.butTest.setEnabled(false); |
240 | 41655 | jjdelcerro | this.butFilterDialog.setEnabled(false); |
241 | this.rdoUseNewExpresion.setEnabled(false); |
||
242 | this.rdoUseSavedExpresion.setEnabled(false); |
||
243 | |||
244 | } else {
|
||
245 | this.rdbAllRows.setSelected(true); |
||
246 | |||
247 | 41537 | jjdelcerro | this.lstFilters.setEnabled(false); |
248 | this.txtName.setEnabled(false); |
||
249 | this.txtExpresion.setEnabled(false); |
||
250 | 41540 | jjdelcerro | this.butTest.setEnabled(false); |
251 | 41655 | jjdelcerro | this.butFilterDialog.setEnabled(false); |
252 | this.rdoUseNewExpresion.setEnabled(false); |
||
253 | this.rdoUseSavedExpresion.setEnabled(false); |
||
254 | |||
255 | 41537 | jjdelcerro | } |
256 | } |
||
257 | |||
258 | 40435 | jjdelcerro | public boolean isFullLayerSelected() { |
259 | 41537 | jjdelcerro | return this.rdbAllRows.isSelected(); |
260 | 40435 | jjdelcerro | } |
261 | |||
262 | public boolean isSelectedFeaturesSelected() { |
||
263 | 41537 | jjdelcerro | return this.rdbSelectedRows.isSelected(); |
264 | 40435 | jjdelcerro | } |
265 | |||
266 | public boolean isPersonalizedFilterSelected() { |
||
267 | 41537 | jjdelcerro | return this.rdbFilteredRows.isSelected(); |
268 | 40435 | jjdelcerro | } |
269 | |||
270 | 41540 | jjdelcerro | public void checkPanel() throws NotContinueWizardException { |
271 | checkPanel(true);
|
||
272 | 40435 | jjdelcerro | } |
273 | 41540 | jjdelcerro | |
274 | public void checkPanel(boolean save) throws NotContinueWizardException { |
||
275 | 41553 | jjdelcerro | if( this.rdbFilteredRows.isSelected() ) { |
276 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
277 | String expresion = this.getFilterExpresion(); |
||
278 | if( expresion == null ) { |
||
279 | throw new NotContinueWizardException( |
||
280 | i18nManager.getTranslation("_The_expresion_is_empty_Check_other_option_or_enter_a_expresion"),
|
||
281 | this, true |
||
282 | ); |
||
283 | 41540 | jjdelcerro | } |
284 | 41553 | jjdelcerro | Evaluator filter = this.getFilter();
|
285 | if( filter == null ) { |
||
286 | throw new NotContinueWizardException( |
||
287 | i18nManager.getTranslation("_Problems_compiling_the_expesion_Check_the_sintax_Remember_use_SQL_expresion_sintax"),
|
||
288 | this, true |
||
289 | ); |
||
290 | } |
||
291 | DynObjectManager dynobjmanager = ToolsLocator.getDynObjectManager(); |
||
292 | try {
|
||
293 | DynObject values = dynobjmanager.createDynObject(this.store.getDefaultFeatureType());
|
||
294 | filter.evaluate(new Data(values));
|
||
295 | } catch (EvaluatorException ex) {
|
||
296 | throw new NotContinueWizardException( |
||
297 | i18nManager.getTranslation("_Check_the_sintax_Remember_use_SQL_expresion_sintax")
|
||
298 | + "\n"
|
||
299 | + ex.getCause().getMessage(), |
||
300 | this, true |
||
301 | ); |
||
302 | } catch (DataException ex) {
|
||
303 | logger.warn("Can't test expresion",ex);
|
||
304 | throw new NotContinueWizardException( |
||
305 | i18nManager.getTranslation("_Problems_to_create_a_data_set_to_test_the_expresion_See_register_for_more_information"),
|
||
306 | this, true |
||
307 | ); |
||
308 | } |
||
309 | if( save ) {
|
||
310 | String filterName = this.getFilterName(); |
||
311 | if( filterName!=null ) { |
||
312 | ExporttoManager manager = ExporttoLocator.getManager(); |
||
313 | manager.addFilter(filterName, expresion); |
||
314 | } |
||
315 | } |
||
316 | 41540 | jjdelcerro | } |
317 | } |
||
318 | 40435 | jjdelcerro | |
319 | 41540 | jjdelcerro | public Evaluator getFilter() {
|
320 | String filter = this.getFilterExpresion(); |
||
321 | if( filter == null ) { |
||
322 | return null; |
||
323 | } |
||
324 | DataManager datamanager = DALLocator.getDataManager(); |
||
325 | try {
|
||
326 | Evaluator evaluator = datamanager.createExpresion(filter); |
||
327 | return evaluator;
|
||
328 | } catch (InitializeException ex) {
|
||
329 | // Error de sintaxis en la expresion ???
|
||
330 | return null; |
||
331 | } |
||
332 | } |
||
333 | |||
334 | public String getFilterExpresion() { |
||
335 | String s = this.txtExpresion.getText(); |
||
336 | if( StringUtils.isBlank(s) ) {
|
||
337 | return null; |
||
338 | } |
||
339 | return s.trim();
|
||
340 | } |
||
341 | |||
342 | public String getFilterName() { |
||
343 | String s = this.txtName.getText(); |
||
344 | if( StringUtils.isBlank(s) ) {
|
||
345 | return null; |
||
346 | } |
||
347 | return s.trim();
|
||
348 | } |
||
349 | |||
350 | 40435 | jjdelcerro | } |