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 @ 41663

History | View | Annotate | Download (12.2 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
    }
124

    
125
    private void doShowFilterDialog() {
126
        
127
    }
128

    
129
    protected void doSelectFilter() {
130
        FilterItem item = (FilterItem) this.lstFilters.getSelectedValue();
131
        if( item!=null ) {
132
            this.txtExpresion.setText(item.value);
133
            this.txtName.setText(item.label);
134
        }
135
    }
136
    
137
    private static class Data implements EvaluatorData {
138
        private final DynObject data;
139
        
140
        public Data(DynObject data) {
141
            this.data = data;
142
        }
143
        public Object getDataValue(String name) {
144
            if( this.data.getDynClass().getDynField(name)!=null ) {
145
                return this.data.getDynValue(name);
146
            }
147
            if( "defaultgeometry".equalsIgnoreCase(name) ) {
148
                // FIXME: deberia crear una geometris de agun tipo
149
                return null;
150
            }
151
            throw new RuntimeException("Identifier '"+name+"'not defined.");
152
        }
153

    
154
        public Object getContextValue(String name) {
155
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
156
        }
157

    
158
        public Iterator getDataValues() {
159
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
160
        }
161

    
162
        public Iterator getDataNames() {
163
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
164
        }
165

    
166
        public boolean hasDataValue(String name) {
167
            if( "defaultgeometry".equalsIgnoreCase(name) ) {
168
                return true;
169
            }
170
            return  this.data.hasDynValue(name);
171
        }
172

    
173
        public boolean hasContextValue(String name) {
174
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
175
        }
176
    }
177
            
178
    protected void doTest() {
179
        try {
180
            this.checkPanel(false);
181
            I18nManager i18nManager = ToolsLocator.getI18nManager();
182
            JOptionPane.showMessageDialog(this,
183
                    i18nManager.getTranslation("_Expresion_correcta"),
184
                    i18nManager.getTranslation("_Filter"),
185
                    JOptionPane.INFORMATION_MESSAGE
186
            );
187
        } catch (NotContinueWizardException ex) {
188
            I18nManager i18nManager = ToolsLocator.getI18nManager();
189
            JOptionPane.showMessageDialog(this,
190
                    ex.getMessage(), 
191
                    i18nManager.getTranslation("_Warning"),
192
                    JOptionPane.WARNING_MESSAGE
193
            );
194
        }
195
    }
196
    
197
    protected void doChangeOption(){
198
        if( this.rdbFilteredRows.isSelected() ) {
199
            this.rdoUseNewExpresion.setEnabled(true);
200
            this.rdoUseSavedExpresion.setEnabled(true);
201
        
202
            if( this.rdoUseSavedExpresion.isSelected() ) {
203
                this.lstFilters.setEnabled(true);
204
                this.txtName.setEnabled(false);
205
                this.txtExpresion.setEnabled(false);
206
                this.butTest.setEnabled(false);
207
                this.butFilterDialog.setEnabled(false);
208
            } else {
209
                this.rdoUseNewExpresion.setSelected(false);
210
                
211
                this.lstFilters.setEnabled(false);
212
                this.txtName.setEnabled(true);
213
                this.txtExpresion.setEnabled(true);
214
                this.butTest.setEnabled(true);
215
                this.butFilterDialog.setEnabled(true);
216
            }
217

    
218
        } else if( this.rdbSelectedRows.isSelected() ) {
219
            this.lstFilters.setEnabled(false);
220
            this.txtName.setEnabled(false);
221
            this.txtExpresion.setEnabled(false);
222
            this.butTest.setEnabled(false);
223
            this.butFilterDialog.setEnabled(false);
224
            this.rdoUseNewExpresion.setEnabled(false);
225
            this.rdoUseSavedExpresion.setEnabled(false);
226

    
227
        } else {
228
            this.rdbAllRows.setSelected(true);
229
            
230
            this.lstFilters.setEnabled(false);
231
            this.txtName.setEnabled(false);
232
            this.txtExpresion.setEnabled(false);
233
            this.butTest.setEnabled(false);
234
            this.butFilterDialog.setEnabled(false);
235
            this.rdoUseNewExpresion.setEnabled(false);
236
            this.rdoUseSavedExpresion.setEnabled(false);
237
            
238
        }
239
    } 
240
    
241
    public boolean isFullLayerSelected() {
242
        return this.rdbAllRows.isSelected();
243
    }
244

    
245
    public boolean isSelectedFeaturesSelected() {
246
        return this.rdbSelectedRows.isSelected();
247
    }
248

    
249
    public boolean isPersonalizedFilterSelected() {
250
        return this.rdbFilteredRows.isSelected();
251
    }
252

    
253
    public void checkPanel()  throws NotContinueWizardException {
254
        checkPanel(true);
255
    }
256
    
257
    public void checkPanel(boolean save)  throws NotContinueWizardException {
258
        if( this.rdbFilteredRows.isSelected() ) {
259
            I18nManager i18nManager = ToolsLocator.getI18nManager();
260
            String expresion = this.getFilterExpresion();
261
            if( expresion == null ) {
262
                throw new NotContinueWizardException(
263
                        i18nManager.getTranslation("_The_expresion_is_empty_Check_other_option_or_enter_a_expresion"), 
264
                        this, true
265
                );
266
            }
267
            Evaluator filter = this.getFilter();
268
            if( filter == null ) {
269
                throw new NotContinueWizardException(
270
                        i18nManager.getTranslation("_Problems_compiling_the_expesion_Check_the_sintax_Remember_use_SQL_expresion_sintax"), 
271
                        this, true
272
                );
273
            }
274
            DynObjectManager dynobjmanager = ToolsLocator.getDynObjectManager();
275
            try {
276
                DynObject values = dynobjmanager.createDynObject(this.store.getDefaultFeatureType());
277
                filter.evaluate(new Data(values));
278
            } catch (EvaluatorException ex) {
279
                throw new NotContinueWizardException(
280
                        i18nManager.getTranslation("_Check_the_sintax_Remember_use_SQL_expresion_sintax")
281
                        + "\n"
282
                        + ex.getCause().getMessage(), 
283
                        this, true
284
                );
285
            } catch (DataException ex) {
286
                logger.warn("Can't test expresion",ex);
287
                throw new NotContinueWizardException(
288
                        i18nManager.getTranslation("_Problems_to_create_a_data_set_to_test_the_expresion_See_register_for_more_information"), 
289
                        this, true
290
                );
291
            }
292
            if( save ) {
293
                String filterName = this.getFilterName();
294
                if( filterName!=null ) {
295
                    ExporttoManager manager = ExporttoLocator.getManager();
296
                    manager.addFilter(filterName, expresion);
297
                }
298
            }
299
        }
300
    }
301

    
302
    public Evaluator getFilter() {
303
        String filter = this.getFilterExpresion();
304
        if( filter == null ) {
305
            return null;
306
        }
307
        DataManager datamanager = DALLocator.getDataManager();
308
        try {
309
            Evaluator evaluator = datamanager.createExpresion(filter);
310
            return evaluator;
311
        } catch (InitializeException ex) {
312
            // Error de sintaxis en la expresion ???
313
            return null;
314
        }
315
    }
316

    
317
    public String getFilterExpresion() {
318
        String s = this.txtExpresion.getText();
319
        if( StringUtils.isBlank(s) ) {
320
            return null;
321
        }
322
        return s.trim();
323
    }
324

    
325
    public String getFilterName() {
326
        String s = this.txtName.getText();
327
        if( StringUtils.isBlank(s) ) {
328
            return null;
329
        }
330
        return s.trim();
331
    }
332

    
333
}