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 / export / swing / impl / panels / SelectFilterPanel.java @ 43925
History | View | Annotate | Download (13.4 KB)
1 |
package org.gvsig.export.swing.impl.panels; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.awt.event.ActionListener; |
5 |
import java.util.Iterator; |
6 |
import java.util.List; |
7 |
import javax.swing.DefaultListModel; |
8 |
import javax.swing.JComponent; |
9 |
import javax.swing.JOptionPane; |
10 |
import javax.swing.event.ListSelectionEvent; |
11 |
import javax.swing.event.ListSelectionListener; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.gvsig.export.ExportLocator; |
14 |
import org.gvsig.export.ExportManager; |
15 |
import org.gvsig.export.ExportManager.ExportFilter; |
16 |
import org.gvsig.export.ExportParameters; |
17 |
import org.gvsig.export.swing.JExportProcessPanel; |
18 |
import org.gvsig.export.swing.spi.ExportPanel; |
19 |
import org.gvsig.export.swing.spi.ExportPanelValidationException; |
20 |
import org.gvsig.export.swing.spi.ExportPanelsManager; |
21 |
import org.gvsig.fmap.dal.DALLocator; |
22 |
import org.gvsig.fmap.dal.DataManager; |
23 |
import org.gvsig.fmap.dal.exception.InitializeException; |
24 |
import org.gvsig.tools.ToolsLocator; |
25 |
import org.gvsig.tools.dynobject.DynObject; |
26 |
import org.gvsig.tools.dynobject.DynObjectManager; |
27 |
import org.gvsig.tools.evaluator.Evaluator; |
28 |
import org.gvsig.tools.evaluator.EvaluatorData; |
29 |
import org.gvsig.tools.evaluator.EvaluatorException; |
30 |
import org.gvsig.tools.i18n.I18nManager; |
31 |
import org.gvsig.tools.swing.api.ListElement; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
/**
|
36 |
*
|
37 |
* @author jjdelcerro
|
38 |
*/
|
39 |
public class SelectFilterPanel |
40 |
extends SelectFilterPanelView
|
41 |
implements ExportPanel
|
42 |
{ |
43 |
|
44 |
private static final Logger LOG = LoggerFactory.getLogger(SelectFilterPanel.class); |
45 |
|
46 |
private final ExportParameters parameters; |
47 |
private final JExportProcessPanel processPanel; |
48 |
|
49 |
public SelectFilterPanel(
|
50 |
JExportProcessPanel processPanel, |
51 |
ExportParameters parameters |
52 |
) { |
53 |
this.processPanel = processPanel;
|
54 |
this.parameters = parameters;
|
55 |
this.initComponents();
|
56 |
} |
57 |
|
58 |
@Override
|
59 |
public String getIdPanel() { |
60 |
return ExportPanelsManager.PANEL_SELECT_FILTER;
|
61 |
} |
62 |
|
63 |
@Override
|
64 |
public String getTitlePanel() { |
65 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
66 |
return i18n.getTranslation("_Filter"); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public boolean validatePanel() throws ExportPanelValidationException { |
71 |
checkPanel(true);
|
72 |
return true; |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public void enterPanel() { |
77 |
|
78 |
} |
79 |
|
80 |
@Override
|
81 |
public void exitPanel() { |
82 |
this.parameters.setFilterExpresion(this.getFilterExpresion()); |
83 |
if( this.isFullLayerSelected() ) { |
84 |
this.parameters.setFeaturesToUse(ExportParameters.USE_ALL_FEATURES);
|
85 |
} else if( this.isPersonalizedFilterSelected() ) { |
86 |
this.parameters.setFeaturesToUse(ExportParameters.USE_FILTERED_FEATURES);
|
87 |
} else {
|
88 |
this.parameters.setFeaturesToUse(ExportParameters.USE_SELECTED_FEATURES);
|
89 |
} |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public JComponent asJComponent() { |
94 |
return this; |
95 |
} |
96 |
|
97 |
private void initComponents() { |
98 |
ExportManager manager = ExportLocator.getManager(); |
99 |
|
100 |
DefaultListModel model = new DefaultListModel(); |
101 |
List<ExportManager.ExportFilter> filters = manager.getFilters();
|
102 |
for (ExportFilter filter : filters) {
|
103 |
model.addElement(new ListElement(filter.getName(),filter.getExpression()));
|
104 |
} |
105 |
this.lstFilters.setModel(model);
|
106 |
this.lstFilters.addListSelectionListener(new ListSelectionListener() { |
107 |
@Override
|
108 |
public void valueChanged(ListSelectionEvent lse) { |
109 |
doSelectFilter(); |
110 |
} |
111 |
}); |
112 |
|
113 |
ActionListener changeOptionAction = new ActionListener() { |
114 |
@Override
|
115 |
public void actionPerformed(ActionEvent e) { |
116 |
doChangeOption(); |
117 |
} |
118 |
}; |
119 |
this.rdbAllRows.addActionListener(changeOptionAction);
|
120 |
this.rdbFilteredRows.addActionListener(changeOptionAction);
|
121 |
this.rdbSelectedRows.addActionListener(changeOptionAction);
|
122 |
this.rdoUseNewExpresion.addActionListener(changeOptionAction);
|
123 |
this.rdoUseSavedExpresion.addActionListener(changeOptionAction);
|
124 |
|
125 |
this.butTest.addActionListener(new ActionListener() { |
126 |
@Override
|
127 |
public void actionPerformed(ActionEvent e) { |
128 |
doTest(); |
129 |
} |
130 |
}); |
131 |
this.rdbAllRows.setSelected(true); |
132 |
doChangeOption(); |
133 |
this.butFilterDialog.addActionListener(new ActionListener() { |
134 |
@Override
|
135 |
public void actionPerformed(ActionEvent e) { |
136 |
doShowFilterDialog(); |
137 |
} |
138 |
}); |
139 |
this.translate();
|
140 |
} |
141 |
|
142 |
private void translate() { |
143 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
144 |
|
145 |
this.rdbAllRows.setText(i18nManager.getTranslation("_Todos_los_registros")); |
146 |
this.rdbSelectedRows.setText(i18nManager.getTranslation("_Los_registros_seleccionados")); |
147 |
this.rdbFilteredRows.setText(i18nManager.getTranslation("_Los_registros_que_cumplan_el_critrio_seleccionado")); |
148 |
this.lblName.setText(i18nManager.getTranslation("_Nombre_de_la_expresion_opcinal")); |
149 |
this.lblExpresion.setText(i18nManager.getTranslation("_Expresion")); |
150 |
this.txtName.setToolTipText(i18nManager.getTranslation("_Type_here_the_name_of_the_expression_if_you_want_to_save_it_otherwise_leave_it_blank")); |
151 |
this.butTest.setText(i18nManager.getTranslation("_Test")); |
152 |
this.lblHeader.setText(i18nManager.getTranslation("_Indique_que_registros_desea_exportar")); |
153 |
this.rdoUseSavedExpresion.setText(i18nManager.getTranslation("_Usar_una_expresion_guardada")); |
154 |
this.rdoUseNewExpresion.setText(i18nManager.getTranslation("_Nueva_expresion")); |
155 |
this.butFilterDialog.setText(i18nManager.getTranslation("_Filtro")); |
156 |
} |
157 |
|
158 |
private void doShowFilterDialog() { |
159 |
|
160 |
} |
161 |
|
162 |
protected void doSelectFilter() { |
163 |
ExportFilter item = (ExportFilter) ListElement.getSelected(this.lstFilters);
|
164 |
if( item!=null ) { |
165 |
this.txtExpresion.setText(item.getExpression());
|
166 |
this.txtName.setText(item.getName());
|
167 |
} |
168 |
} |
169 |
|
170 |
private static class Data implements EvaluatorData { |
171 |
private final DynObject data; |
172 |
|
173 |
public Data(DynObject data) {
|
174 |
this.data = data;
|
175 |
} |
176 |
@Override
|
177 |
public Object getDataValue(String name) { |
178 |
if( this.data.getDynClass().getDynField(name)!=null ) { |
179 |
return this.data.getDynValue(name); |
180 |
} |
181 |
if( "defaultgeometry".equalsIgnoreCase(name) ) { |
182 |
// FIXME: deberia crear una geometris de agun tipo
|
183 |
return null; |
184 |
} |
185 |
throw new RuntimeException("Identifier '"+name+"'not defined."); |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public Object getContextValue(String name) { |
190 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public Iterator getDataValues() { |
195 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
196 |
} |
197 |
|
198 |
@Override
|
199 |
public Iterator getDataNames() { |
200 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public boolean hasDataValue(String name) { |
205 |
if( "defaultgeometry".equalsIgnoreCase(name) ) { |
206 |
return true; |
207 |
} |
208 |
return this.data.hasDynValue(name); |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public boolean hasContextValue(String name) { |
213 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
214 |
} |
215 |
} |
216 |
|
217 |
protected void doTest() { |
218 |
try {
|
219 |
this.checkPanel(false); |
220 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
221 |
JOptionPane.showMessageDialog(this, |
222 |
i18nManager.getTranslation("_Expresion_correcta"),
|
223 |
i18nManager.getTranslation("_Filter"),
|
224 |
JOptionPane.INFORMATION_MESSAGE
|
225 |
); |
226 |
} catch (ExportPanelValidationException ex) {
|
227 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
228 |
JOptionPane.showMessageDialog(this, |
229 |
ex.getMessage(), |
230 |
i18nManager.getTranslation("_Warning"),
|
231 |
JOptionPane.WARNING_MESSAGE
|
232 |
); |
233 |
} |
234 |
} |
235 |
|
236 |
protected void doChangeOption(){ |
237 |
if( this.rdbFilteredRows.isSelected() ) { |
238 |
this.rdoUseNewExpresion.setEnabled(true); |
239 |
this.rdoUseSavedExpresion.setEnabled(true); |
240 |
|
241 |
if( this.rdoUseSavedExpresion.isSelected() ) { |
242 |
this.lstFilters.setEnabled(true); |
243 |
this.txtName.setEnabled(false); |
244 |
this.txtExpresion.setEnabled(false); |
245 |
this.butTest.setEnabled(false); |
246 |
this.butFilterDialog.setEnabled(false); |
247 |
} else {
|
248 |
this.rdoUseNewExpresion.setSelected(false); |
249 |
|
250 |
this.lstFilters.setEnabled(false); |
251 |
this.txtName.setEnabled(true); |
252 |
this.txtExpresion.setEnabled(true); |
253 |
this.butTest.setEnabled(true); |
254 |
this.butFilterDialog.setEnabled(true); |
255 |
} |
256 |
|
257 |
} else if( this.rdbSelectedRows.isSelected() ) { |
258 |
this.lstFilters.setEnabled(false); |
259 |
this.txtName.setEnabled(false); |
260 |
this.txtExpresion.setEnabled(false); |
261 |
this.butTest.setEnabled(false); |
262 |
this.butFilterDialog.setEnabled(false); |
263 |
this.rdoUseNewExpresion.setEnabled(false); |
264 |
this.rdoUseSavedExpresion.setEnabled(false); |
265 |
|
266 |
} else {
|
267 |
this.rdbAllRows.setSelected(true); |
268 |
|
269 |
this.lstFilters.setEnabled(false); |
270 |
this.txtName.setEnabled(false); |
271 |
this.txtExpresion.setEnabled(false); |
272 |
this.butTest.setEnabled(false); |
273 |
this.butFilterDialog.setEnabled(false); |
274 |
this.rdoUseNewExpresion.setEnabled(false); |
275 |
this.rdoUseSavedExpresion.setEnabled(false); |
276 |
|
277 |
} |
278 |
} |
279 |
|
280 |
public boolean isFullLayerSelected() { |
281 |
return this.rdbAllRows.isSelected(); |
282 |
} |
283 |
|
284 |
public boolean isSelectedFeaturesSelected() { |
285 |
return this.rdbSelectedRows.isSelected(); |
286 |
} |
287 |
|
288 |
public boolean isPersonalizedFilterSelected() { |
289 |
return this.rdbFilteredRows.isSelected(); |
290 |
} |
291 |
|
292 |
public void checkPanel(boolean save) throws ExportPanelValidationException { |
293 |
if( this.rdbFilteredRows.isSelected() ) { |
294 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
295 |
String expresion = this.getFilterExpresion(); |
296 |
if( expresion == null ) { |
297 |
throw new ExportPanelValidationException( |
298 |
i18nManager.getTranslation("_The_expresion_is_empty_Check_other_option_or_enter_a_expresion")
|
299 |
); |
300 |
} |
301 |
Evaluator filter = this.getFilter();
|
302 |
if( filter == null ) { |
303 |
throw new ExportPanelValidationException( |
304 |
i18nManager.getTranslation("_Problems_compiling_the_expesion_Check_the_sintax_Remember_use_SQL_expresion_sintax")
|
305 |
); |
306 |
} |
307 |
DynObjectManager dynobjmanager = ToolsLocator.getDynObjectManager(); |
308 |
try {
|
309 |
DynObject values = dynobjmanager.createDynObject(this.parameters.getSourceFeatureType());
|
310 |
filter.evaluate(new Data(values));
|
311 |
} catch (EvaluatorException ex) {
|
312 |
throw new ExportPanelValidationException( |
313 |
i18nManager.getTranslation("_Check_the_sintax_Remember_use_SQL_expresion_sintax")
|
314 |
+ "\n"
|
315 |
+ ex.getCause().getMessage() |
316 |
); |
317 |
} catch (Exception ex) { |
318 |
LOG.warn("Can't test expresion",ex);
|
319 |
throw new ExportPanelValidationException( |
320 |
i18nManager.getTranslation("_Problems_to_create_a_data_set_to_test_the_expresion_See_register_for_more_information")
|
321 |
); |
322 |
} |
323 |
if( save ) {
|
324 |
String filterName = this.getFilterName(); |
325 |
if( filterName!=null ) { |
326 |
ExportManager manager = ExportLocator.getManager(); |
327 |
manager.addFilter(filterName, expresion); |
328 |
} |
329 |
} |
330 |
} |
331 |
} |
332 |
|
333 |
private Evaluator getFilter() {
|
334 |
String filter = this.getFilterExpresion(); |
335 |
if( filter == null ) { |
336 |
return null; |
337 |
} |
338 |
DataManager datamanager = DALLocator.getDataManager(); |
339 |
try {
|
340 |
Evaluator evaluator = datamanager.createExpresion(filter); |
341 |
return evaluator;
|
342 |
} catch (InitializeException ex) {
|
343 |
// Error de sintaxis en la expresion ???
|
344 |
return null; |
345 |
} |
346 |
} |
347 |
|
348 |
public String getFilterExpresion() { |
349 |
String s = this.txtExpresion.getText(); |
350 |
if( StringUtils.isBlank(s) ) {
|
351 |
return null; |
352 |
} |
353 |
return s.trim();
|
354 |
} |
355 |
|
356 |
public String getFilterName() { |
357 |
String s = this.txtName.getText(); |
358 |
if( StringUtils.isBlank(s) ) {
|
359 |
return null; |
360 |
} |
361 |
return s.trim();
|
362 |
} |
363 |
|
364 |
|
365 |
} |