Statistics
| Revision:

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
/**
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.exportto.swing.impl.panel;
25

    
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.util.Iterator;
29
import javax.swing.DefaultListModel;
30
import javax.swing.JOptionPane;
31
import javax.swing.event.ListSelectionEvent;
32
import javax.swing.event.ListSelectionListener;
33
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.exportto.ExporttoLocator;
35
import org.gvsig.exportto.ExporttoManager;
36
import org.gvsig.exportto.swing.impl.DefaultJExporttoServicePanel;
37
import org.gvsig.fmap.dal.DALLocator;
38
import org.gvsig.fmap.dal.DataManager;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.InitializeException;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.gui.beans.wizard.panel.NotContinueWizardException;
43
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
import org.gvsig.tools.evaluator.EvaluatorException;
49
import org.gvsig.tools.i18n.I18nManager;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
/**
54
 * @author gvSIG Team
55
 * @version $Id$
56
 * 
57
 */
58
public class ExportFilterPanel extends ExportFilterPanelLayout {
59
    private static final long serialVersionUID = -2450969034747572624L;
60
    
61
    private static final Logger logger = LoggerFactory.getLogger(ExportFilterPanel.class);
62

    
63
    private FeatureStore store; 
64
    
65
    public ExportFilterPanel(DefaultJExporttoServicePanel exporttoServicePanel) {
66
        this.store = exporttoServicePanel.getFeatureStore();
67
        this.initComponents();
68
    }
69

    
70
    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
        this.lstFilters.addListSelectionListener(new ListSelectionListener() {
95
            public void valueChanged(ListSelectionEvent lse) {
96
                doSelectFilter();
97
            }
98
        });
99
        
100
        ActionListener changeOptionAction = new ActionListener() {
101
            public void actionPerformed(ActionEvent e) {
102
                doChangeOption();
103
            }
104
        };
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
        this.butTest.addActionListener(new ActionListener() {
112
            public void actionPerformed(ActionEvent e) {
113
                doTest();
114
            }
115
        });
116
        this.rdbAllRows.setSelected(true);
117
        doChangeOption();
118
        this.butFilterDialog.addActionListener(new ActionListener() {
119
            public void actionPerformed(ActionEvent e) {
120
                doShowFilterDialog();
121
            }
122
        });
123
        this.translate();
124
    }
125

    
126
    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
    private void doShowFilterDialog() {
143
        
144
    }
145

    
146
    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
    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
            if( this.data.getDynClass().getDynField(name)!=null ) {
162
                return this.data.getDynValue(name);
163
            }
164
            if( "defaultgeometry".equalsIgnoreCase(name) ) {
165
                // FIXME: deberia crear una geometris de agun tipo
166
                return null;
167
            }
168
            throw new RuntimeException("Identifier '"+name+"'not defined.");
169
        }
170

    
171
        public Object getContextValue(String name) {
172
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
173
        }
174

    
175
        public Iterator getDataValues() {
176
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
177
        }
178

    
179
        public Iterator getDataNames() {
180
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
181
        }
182

    
183
        public boolean hasDataValue(String name) {
184
            if( "defaultgeometry".equalsIgnoreCase(name) ) {
185
                return true;
186
            }
187
            return  this.data.hasDynValue(name);
188
        }
189

    
190
        public boolean hasContextValue(String name) {
191
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
192
        }
193
    }
194
            
195
    protected void doTest() {
196
        try {
197
            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
        }
212
    }
213
    
214
    protected void doChangeOption(){
215
        if( this.rdbFilteredRows.isSelected() ) {
216
            this.rdoUseNewExpresion.setEnabled(true);
217
            this.rdoUseSavedExpresion.setEnabled(true);
218
        
219
            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
                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
            this.lstFilters.setEnabled(false);
237
            this.txtName.setEnabled(false);
238
            this.txtExpresion.setEnabled(false);
239
            this.butTest.setEnabled(false);
240
            this.butFilterDialog.setEnabled(false);
241
            this.rdoUseNewExpresion.setEnabled(false);
242
            this.rdoUseSavedExpresion.setEnabled(false);
243

    
244
        } else {
245
            this.rdbAllRows.setSelected(true);
246
            
247
            this.lstFilters.setEnabled(false);
248
            this.txtName.setEnabled(false);
249
            this.txtExpresion.setEnabled(false);
250
            this.butTest.setEnabled(false);
251
            this.butFilterDialog.setEnabled(false);
252
            this.rdoUseNewExpresion.setEnabled(false);
253
            this.rdoUseSavedExpresion.setEnabled(false);
254
            
255
        }
256
    } 
257
    
258
    public boolean isFullLayerSelected() {
259
        return this.rdbAllRows.isSelected();
260
    }
261

    
262
    public boolean isSelectedFeaturesSelected() {
263
        return this.rdbSelectedRows.isSelected();
264
    }
265

    
266
    public boolean isPersonalizedFilterSelected() {
267
        return this.rdbFilteredRows.isSelected();
268
    }
269

    
270
    public void checkPanel()  throws NotContinueWizardException {
271
        checkPanel(true);
272
    }
273
    
274
    public void checkPanel(boolean save)  throws NotContinueWizardException {
275
        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
            }
284
            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
        }
317
    }
318

    
319
    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
}