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 @ 44168
History | View | Annotate | Download (13.4 KB)
1 | 43925 | jjdelcerro | 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 | 43968 | jjdelcerro | public void previousPanel() { |
82 | |||
83 | } |
||
84 | |||
85 | @Override
|
||
86 | public void nextPanel() { |
||
87 | 43925 | jjdelcerro | this.parameters.setFilterExpresion(this.getFilterExpresion()); |
88 | if( this.isFullLayerSelected() ) { |
||
89 | this.parameters.setFeaturesToUse(ExportParameters.USE_ALL_FEATURES);
|
||
90 | } else if( this.isPersonalizedFilterSelected() ) { |
||
91 | this.parameters.setFeaturesToUse(ExportParameters.USE_FILTERED_FEATURES);
|
||
92 | } else {
|
||
93 | this.parameters.setFeaturesToUse(ExportParameters.USE_SELECTED_FEATURES);
|
||
94 | } |
||
95 | } |
||
96 | |||
97 | @Override
|
||
98 | public JComponent asJComponent() { |
||
99 | return this; |
||
100 | } |
||
101 | |||
102 | private void initComponents() { |
||
103 | ExportManager manager = ExportLocator.getManager(); |
||
104 | |||
105 | DefaultListModel model = new DefaultListModel(); |
||
106 | List<ExportManager.ExportFilter> filters = manager.getFilters();
|
||
107 | for (ExportFilter filter : filters) {
|
||
108 | model.addElement(new ListElement(filter.getName(),filter.getExpression()));
|
||
109 | } |
||
110 | this.lstFilters.setModel(model);
|
||
111 | this.lstFilters.addListSelectionListener(new ListSelectionListener() { |
||
112 | @Override
|
||
113 | public void valueChanged(ListSelectionEvent lse) { |
||
114 | doSelectFilter(); |
||
115 | } |
||
116 | }); |
||
117 | |||
118 | ActionListener changeOptionAction = new ActionListener() { |
||
119 | @Override
|
||
120 | public void actionPerformed(ActionEvent e) { |
||
121 | doChangeOption(); |
||
122 | } |
||
123 | }; |
||
124 | this.rdbAllRows.addActionListener(changeOptionAction);
|
||
125 | this.rdbFilteredRows.addActionListener(changeOptionAction);
|
||
126 | this.rdbSelectedRows.addActionListener(changeOptionAction);
|
||
127 | this.rdoUseNewExpresion.addActionListener(changeOptionAction);
|
||
128 | this.rdoUseSavedExpresion.addActionListener(changeOptionAction);
|
||
129 | |||
130 | this.butTest.addActionListener(new ActionListener() { |
||
131 | @Override
|
||
132 | public void actionPerformed(ActionEvent e) { |
||
133 | doTest(); |
||
134 | } |
||
135 | }); |
||
136 | this.rdbAllRows.setSelected(true); |
||
137 | doChangeOption(); |
||
138 | this.butFilterDialog.addActionListener(new ActionListener() { |
||
139 | @Override
|
||
140 | public void actionPerformed(ActionEvent e) { |
||
141 | doShowFilterDialog(); |
||
142 | } |
||
143 | }); |
||
144 | this.translate();
|
||
145 | } |
||
146 | |||
147 | private void translate() { |
||
148 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
149 | |||
150 | this.rdbAllRows.setText(i18nManager.getTranslation("_Todos_los_registros")); |
||
151 | this.rdbSelectedRows.setText(i18nManager.getTranslation("_Los_registros_seleccionados")); |
||
152 | this.rdbFilteredRows.setText(i18nManager.getTranslation("_Los_registros_que_cumplan_el_critrio_seleccionado")); |
||
153 | this.lblName.setText(i18nManager.getTranslation("_Nombre_de_la_expresion_opcinal")); |
||
154 | this.lblExpresion.setText(i18nManager.getTranslation("_Expresion")); |
||
155 | this.txtName.setToolTipText(i18nManager.getTranslation("_Type_here_the_name_of_the_expression_if_you_want_to_save_it_otherwise_leave_it_blank")); |
||
156 | this.butTest.setText(i18nManager.getTranslation("_Test")); |
||
157 | this.lblHeader.setText(i18nManager.getTranslation("_Indique_que_registros_desea_exportar")); |
||
158 | this.rdoUseSavedExpresion.setText(i18nManager.getTranslation("_Usar_una_expresion_guardada")); |
||
159 | this.rdoUseNewExpresion.setText(i18nManager.getTranslation("_Nueva_expresion")); |
||
160 | this.butFilterDialog.setText(i18nManager.getTranslation("_Filtro")); |
||
161 | } |
||
162 | |||
163 | private void doShowFilterDialog() { |
||
164 | |||
165 | } |
||
166 | |||
167 | protected void doSelectFilter() { |
||
168 | ExportFilter item = (ExportFilter) ListElement.getSelected(this.lstFilters);
|
||
169 | if( item!=null ) { |
||
170 | this.txtExpresion.setText(item.getExpression());
|
||
171 | this.txtName.setText(item.getName());
|
||
172 | } |
||
173 | } |
||
174 | |||
175 | private static class Data implements EvaluatorData { |
||
176 | private final DynObject data; |
||
177 | |||
178 | public Data(DynObject data) {
|
||
179 | this.data = data;
|
||
180 | } |
||
181 | @Override
|
||
182 | public Object getDataValue(String name) { |
||
183 | if( this.data.getDynClass().getDynField(name)!=null ) { |
||
184 | return this.data.getDynValue(name); |
||
185 | } |
||
186 | if( "defaultgeometry".equalsIgnoreCase(name) ) { |
||
187 | // FIXME: deberia crear una geometris de agun tipo
|
||
188 | return null; |
||
189 | } |
||
190 | throw new RuntimeException("Identifier '"+name+"'not defined."); |
||
191 | } |
||
192 | |||
193 | @Override
|
||
194 | public Object getContextValue(String name) { |
||
195 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
196 | } |
||
197 | |||
198 | @Override
|
||
199 | public Iterator getDataValues() { |
||
200 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
201 | } |
||
202 | |||
203 | @Override
|
||
204 | public Iterator getDataNames() { |
||
205 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
206 | } |
||
207 | |||
208 | @Override
|
||
209 | public boolean hasDataValue(String name) { |
||
210 | if( "defaultgeometry".equalsIgnoreCase(name) ) { |
||
211 | return true; |
||
212 | } |
||
213 | return this.data.hasDynValue(name); |
||
214 | } |
||
215 | |||
216 | @Override
|
||
217 | public boolean hasContextValue(String name) { |
||
218 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
219 | } |
||
220 | } |
||
221 | |||
222 | protected void doTest() { |
||
223 | try {
|
||
224 | this.checkPanel(false); |
||
225 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
226 | JOptionPane.showMessageDialog(this, |
||
227 | i18nManager.getTranslation("_Expresion_correcta"),
|
||
228 | i18nManager.getTranslation("_Filter"),
|
||
229 | JOptionPane.INFORMATION_MESSAGE
|
||
230 | ); |
||
231 | } catch (ExportPanelValidationException ex) {
|
||
232 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
233 | JOptionPane.showMessageDialog(this, |
||
234 | ex.getMessage(), |
||
235 | i18nManager.getTranslation("_Warning"),
|
||
236 | JOptionPane.WARNING_MESSAGE
|
||
237 | ); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | protected void doChangeOption(){ |
||
242 | if( this.rdbFilteredRows.isSelected() ) { |
||
243 | this.rdoUseNewExpresion.setEnabled(true); |
||
244 | this.rdoUseSavedExpresion.setEnabled(true); |
||
245 | |||
246 | if( this.rdoUseSavedExpresion.isSelected() ) { |
||
247 | this.lstFilters.setEnabled(true); |
||
248 | this.txtName.setEnabled(false); |
||
249 | this.txtExpresion.setEnabled(false); |
||
250 | this.butTest.setEnabled(false); |
||
251 | this.butFilterDialog.setEnabled(false); |
||
252 | } else {
|
||
253 | this.rdoUseNewExpresion.setSelected(false); |
||
254 | |||
255 | this.lstFilters.setEnabled(false); |
||
256 | this.txtName.setEnabled(true); |
||
257 | this.txtExpresion.setEnabled(true); |
||
258 | this.butTest.setEnabled(true); |
||
259 | this.butFilterDialog.setEnabled(true); |
||
260 | } |
||
261 | |||
262 | } else if( this.rdbSelectedRows.isSelected() ) { |
||
263 | this.lstFilters.setEnabled(false); |
||
264 | this.txtName.setEnabled(false); |
||
265 | this.txtExpresion.setEnabled(false); |
||
266 | this.butTest.setEnabled(false); |
||
267 | this.butFilterDialog.setEnabled(false); |
||
268 | this.rdoUseNewExpresion.setEnabled(false); |
||
269 | this.rdoUseSavedExpresion.setEnabled(false); |
||
270 | |||
271 | } else {
|
||
272 | this.rdbAllRows.setSelected(true); |
||
273 | |||
274 | this.lstFilters.setEnabled(false); |
||
275 | this.txtName.setEnabled(false); |
||
276 | this.txtExpresion.setEnabled(false); |
||
277 | this.butTest.setEnabled(false); |
||
278 | this.butFilterDialog.setEnabled(false); |
||
279 | this.rdoUseNewExpresion.setEnabled(false); |
||
280 | this.rdoUseSavedExpresion.setEnabled(false); |
||
281 | |||
282 | } |
||
283 | } |
||
284 | |||
285 | public boolean isFullLayerSelected() { |
||
286 | return this.rdbAllRows.isSelected(); |
||
287 | } |
||
288 | |||
289 | public boolean isSelectedFeaturesSelected() { |
||
290 | return this.rdbSelectedRows.isSelected(); |
||
291 | } |
||
292 | |||
293 | public boolean isPersonalizedFilterSelected() { |
||
294 | return this.rdbFilteredRows.isSelected(); |
||
295 | } |
||
296 | |||
297 | public void checkPanel(boolean save) throws ExportPanelValidationException { |
||
298 | if( this.rdbFilteredRows.isSelected() ) { |
||
299 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
300 | String expresion = this.getFilterExpresion(); |
||
301 | if( expresion == null ) { |
||
302 | throw new ExportPanelValidationException( |
||
303 | i18nManager.getTranslation("_The_expresion_is_empty_Check_other_option_or_enter_a_expresion")
|
||
304 | ); |
||
305 | } |
||
306 | Evaluator filter = this.getFilter();
|
||
307 | if( filter == null ) { |
||
308 | throw new ExportPanelValidationException( |
||
309 | i18nManager.getTranslation("_Problems_compiling_the_expesion_Check_the_sintax_Remember_use_SQL_expresion_sintax")
|
||
310 | ); |
||
311 | } |
||
312 | DynObjectManager dynobjmanager = ToolsLocator.getDynObjectManager(); |
||
313 | try {
|
||
314 | DynObject values = dynobjmanager.createDynObject(this.parameters.getSourceFeatureType());
|
||
315 | filter.evaluate(new Data(values));
|
||
316 | } catch (EvaluatorException ex) {
|
||
317 | throw new ExportPanelValidationException( |
||
318 | i18nManager.getTranslation("_Check_the_sintax_Remember_use_SQL_expresion_sintax")
|
||
319 | + "\n"
|
||
320 | + ex.getCause().getMessage() |
||
321 | ); |
||
322 | } catch (Exception ex) { |
||
323 | LOG.warn("Can't test expresion",ex);
|
||
324 | throw new ExportPanelValidationException( |
||
325 | i18nManager.getTranslation("_Problems_to_create_a_data_set_to_test_the_expresion_See_register_for_more_information")
|
||
326 | ); |
||
327 | } |
||
328 | if( save ) {
|
||
329 | String filterName = this.getFilterName(); |
||
330 | if( filterName!=null ) { |
||
331 | ExportManager manager = ExportLocator.getManager(); |
||
332 | manager.addFilter(filterName, expresion); |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 | |||
338 | private Evaluator getFilter() {
|
||
339 | String filter = this.getFilterExpresion(); |
||
340 | if( filter == null ) { |
||
341 | return null; |
||
342 | } |
||
343 | DataManager datamanager = DALLocator.getDataManager(); |
||
344 | try {
|
||
345 | Evaluator evaluator = datamanager.createExpresion(filter); |
||
346 | return evaluator;
|
||
347 | } catch (InitializeException ex) {
|
||
348 | // Error de sintaxis en la expresion ???
|
||
349 | return null; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | public String getFilterExpresion() { |
||
354 | String s = this.txtExpresion.getText(); |
||
355 | if( StringUtils.isBlank(s) ) {
|
||
356 | return null; |
||
357 | } |
||
358 | return s.trim();
|
||
359 | } |
||
360 | |||
361 | public String getFilterName() { |
||
362 | String s = this.txtName.getText(); |
||
363 | if( StringUtils.isBlank(s) ) {
|
||
364 | return null; |
||
365 | } |
||
366 | return s.trim();
|
||
367 | } |
||
368 | |||
369 | |||
370 | } |