Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / SelectByAttributesInTableExtension.java @ 43521

History | View | Annotate | Download (9.22 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.app.extension;
25

    
26
import java.util.Iterator;
27

    
28
import javax.swing.JOptionPane;
29

    
30
import org.gvsig.andami.IconThemeHelper;
31
import org.gvsig.andami.PluginServices;
32
import org.gvsig.andami.messages.NotificationManager;
33
import org.gvsig.andami.plugins.Extension;
34
import org.gvsig.andami.ui.mdiManager.IWindow;
35
import org.gvsig.app.ApplicationLocator;
36
import org.gvsig.app.gui.filter.ExpressionListener;
37
import org.gvsig.app.gui.filter.FilterDialog;
38
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
39
import org.gvsig.fmap.dal.DALLocator;
40
import org.gvsig.fmap.dal.DataManager;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FeatureQuery;
44
import org.gvsig.fmap.dal.feature.FeatureSelection;
45
import org.gvsig.fmap.dal.feature.FeatureSet;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.i18n.Messages;
48
import org.gvsig.tools.dispose.DisposeUtils;
49
import org.gvsig.utils.exceptionHandling.ExceptionListener;
50

    
51
/**
52
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
53
 * 
54
 * @author Vicente Caballero Navarro
55
 */
56
public class SelectByAttributesInTableExtension extends Extension implements ExpressionListener {
57

    
58
    protected FeatureStore featureStore = null;
59
    protected FeatureTableDocumentPanel table;
60
    private String filterTitle;
61

    
62
    public void initialize() {
63
        registerIcons();
64
    }
65

    
66
    private void registerIcons() {
67
            IconThemeHelper.registerIcon("action", "selection-by-attributes", this);
68
    }
69

    
70
    public void execute(String actionCommand) {
71
        if ("selection-by-attributes-table".equals(actionCommand)) {
72
            IWindow v = PluginServices.getMDIManager().getActiveWindow();
73

    
74
            if (v instanceof FeatureTableDocumentPanel) {
75
                table = (FeatureTableDocumentPanel) v;
76

    
77
                featureStore = table.getModel().getStore();
78
                filterTitle = table.getModel().getName();
79
                table.getModel().setModified(true);
80
            }
81

    
82
            doExecute();
83
        }
84
    }
85

    
86
    protected void doExecute() {
87
        FilterDialog dlg = new FilterDialog(filterTitle);
88
        dlg.addExpressionListener(this);
89
        dlg.addExceptionListener(new ExceptionListener() {
90

    
91
            public void exceptionThrown(Throwable t) {
92
                NotificationManager.addError(t.getMessage(), t);
93
            }
94
        });
95
        dlg.setModel(featureStore);
96
        PluginServices.getMDIManager().addWindow(dlg);
97
    }
98

    
99
    public boolean isEnabled() {
100
        return isVisible();
101
    }
102

    
103
    public boolean isVisible() {
104
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
105
        return (v instanceof FeatureTableDocumentPanel);
106
    }
107

    
108
    // if no filter expression -> no element selected
109
    public void newSet(String expression) throws DataException {
110
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
111
            FeatureSet set = null;
112
            try {
113
                set = doSet(expression);
114
                if (set == null) {
115
                    return;
116
                }
117
                featureStore.setSelection(set);
118
                
119
            } catch (Exception e) {
120
                logger.warn("Problems executing query '"+expression+"' on store '"+featureStore+"'.",e);
121
                JOptionPane.showMessageDialog(
122
                    ApplicationLocator.getManager().getRootComponent(),
123
                    Messages.getText("_Invalid_expression") + ":\n"
124
                        + getLastMessage(e),
125
                    Messages.getText("_Invalid_expression"),
126
                    JOptionPane.ERROR_MESSAGE);
127
            } finally {
128
                DisposeUtils.disposeQuietly(set);
129
            }
130
        } else {
131
            // if no expression -> no element selected
132
            featureStore.getFeatureSelection().deselectAll();
133
        }
134
    }
135

    
136
    private FeatureSet doSet(String expression) throws DataException {
137
        FeatureQuery query = featureStore.createFeatureQuery();
138
        DataManager manager = DALLocator.getDataManager();
139
        query.setFilter(manager.createExpresion(expression));
140
        return featureStore.getFeatureSet(query);
141
    }
142

    
143
    @Override
144
    public void addToSet(String expression) throws DataException {
145
        // if no filter expression -> don't add more elements to set
146
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
147
            FeatureSet set = null;
148
            try {
149
                set = doSet(expression);
150
                if (set == null) {
151
                    return;
152
                }
153
                featureStore.getFeatureSelection().select(set);
154

    
155
            } catch (Exception e) {
156
                logger.warn("Problems executing query '"+expression+"' on store '"+featureStore+"'.",e);
157
                JOptionPane.showMessageDialog(
158
                    ApplicationLocator.getManager().getRootComponent(),
159
                    Messages.getText("_Invalid_expression") + ":\n"
160
                        + getLastMessage(e),
161
                    Messages.getText("_Invalid_expression"),
162
                    JOptionPane.ERROR_MESSAGE);
163
            } finally {
164
                DisposeUtils.disposeQuietly(set);
165
            }
166
        }
167
    }
168

    
169
    @Override
170
    public void fromSet(String expression) throws DataException {
171
        if ( !this.filterExpressionFromWhereIsEmpty(expression) ) {
172
            FeatureSet set = null;
173
            try {
174
                set = doSet(expression);
175
                if ( set == null ) {
176
                    return;
177
                }
178
                FeatureSelection oldSelection = featureStore.getFeatureSelection();
179
                FeatureSelection newSelection = featureStore.createFeatureSelection();
180
                Iterator iterator = set.iterator();
181
                while ( iterator.hasNext() ) {
182
                    Feature feature = (Feature) iterator.next();
183
                    if ( oldSelection.isSelected(feature) ) {
184
                        newSelection.select(feature);
185
                    }
186
                }
187
                featureStore.setSelection(newSelection);
188
            } catch (Exception e) {
189
                logger.warn("Problems executing query '"+expression+"' on store '"+featureStore+"'.",e);
190
                JOptionPane.showMessageDialog(
191
                    ApplicationLocator.getManager().getRootComponent(),
192
                    Messages.getText("_Invalid_expression") + ":\n"
193
                        + getLastMessage(e),
194
                    Messages.getText("_Invalid_expression"),
195
                    JOptionPane.ERROR_MESSAGE);
196
            } finally {
197
                DisposeUtils.disposeQuietly(set);
198
            }
199
        } else {
200
            featureStore.getFeatureSelection().deselectAll();
201
        }
202
    }
203
    
204
    /**
205
     * Returns true if the WHERE subconsultation of the filterExpression is
206
     * empty ("")
207
     * 
208
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
209
     * @param expression
210
     *            An string
211
     * @return A boolean value
212
     */
213
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
214
        
215
        if (expression == null) {
216
            return true;
217
        }
218
        
219
        String subExpression = expression.trim();
220
        
221
        if (subExpression.length() == 0) {
222
            return true;
223
        }
224
        
225
        int pos;
226

    
227
        // Remove last ';' if exists
228
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
229
            subExpression =
230
                subExpression.substring(0, subExpression.length() - 1).trim();
231
        }
232

    
233
        // If there is no 'where' clause
234
        if ((pos = subExpression.indexOf("where")) == -1) {
235
            return false;
236
        }
237

    
238
        // If there is no subexpression in the WHERE clause -> true
239
        // ( + 5 is the length of the 'where')
240
        subExpression =subExpression.substring(pos + 5, subExpression.length()).trim(); 
241
                                                                             
242
        if (subExpression.length() == 0) {
243
            return true;
244
        } else {
245
            return false;
246
        }
247
    }
248
    
249
    
250
    /**
251
     * @param ex
252
     * @return
253
     */
254
    public static String getLastMessage(Throwable ex) {
255
        
256
        Throwable p = ex;
257
        while (p.getCause() != null && p.getCause() != p) {
258
            p = p.getCause();
259
        }
260
        return p.getMessage();
261
    }    
262
}