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

History | View | Annotate | Download (9.62 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

    
27
import javax.swing.JOptionPane;
28

    
29
import org.gvsig.andami.IconThemeHelper;
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.messages.NotificationManager;
32
import org.gvsig.andami.plugins.Extension;
33
import org.gvsig.andami.ui.mdiManager.IWindow;
34
import org.gvsig.app.ApplicationLocator;
35
import org.gvsig.app.gui.filter.ExpressionListener;
36
import org.gvsig.app.gui.filter.FilterDialog;
37
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
38
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataManager;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.feature.Feature;
42
import org.gvsig.fmap.dal.feature.FeatureQuery;
43
import org.gvsig.fmap.dal.feature.FeatureSelection;
44
import org.gvsig.fmap.dal.feature.FeatureSet;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46
import org.gvsig.i18n.Messages;
47
import org.gvsig.tools.dispose.DisposeUtils;
48
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
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
@SuppressWarnings("UseSpecificCatch")
57
public class SelectByAttributesInTableExtension extends Extension implements ExpressionListener {
58

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

    
63
    @Override
64
    public void initialize() {
65
        registerIcons();
66
    }
67

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

    
72
    @Override
73
    public void execute(String actionCommand) {
74
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
75

    
76
        if (!(v instanceof FeatureTableDocumentPanel)) {
77
            return;
78
        }
79
        if ("selection-by-attributes-table".equals(actionCommand)) {
80
            table = (FeatureTableDocumentPanel) v;
81

    
82
            
83
            featureStore = table.getFeatureStore();
84
            filterTitle = featureStore.getName();
85
//            table.getModel().setModified(true);
86
            SelectByAttributes selector = new SelectByAttributes();
87
            selector.showWindow(filterTitle, featureStore, WindowManager.MODE.TOOL);
88
            
89
        } else if ("selection-by-attributes-table-old".equals(actionCommand)) {
90
            table = (FeatureTableDocumentPanel) v;
91

    
92
            featureStore = table.getFeatureStore();
93
            filterTitle = featureStore.getName();
94
//            table.getModel().setModified(true);
95

    
96
            doExecute();
97
        }
98
    }
99

    
100
    protected void doExecute() {
101
        FilterDialog dlg = new FilterDialog(filterTitle);
102
        dlg.addExpressionListener(this);
103
        dlg.addExceptionListener(new ExceptionListener() {
104

    
105
            @Override
106
            public void exceptionThrown(Throwable t) {
107
                NotificationManager.addError(t.getMessage(), t);
108
            }
109
        });
110
        dlg.setModel(featureStore);
111
        PluginServices.getMDIManager().addWindow(dlg);
112
    }
113

    
114
    @Override
115
    public boolean isEnabled() {
116
        return isVisible();
117
    }
118

    
119
    @Override
120
    public boolean isVisible() {
121
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
122
        return (v instanceof FeatureTableDocumentPanel);
123
    }
124

    
125
    // if no filter expression -> no element selected
126
    @Override
127
    public void newSet(String expression) throws DataException {
128
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
129
            FeatureSet set = null;
130
            try {
131
                set = doSet(expression);
132
                if (set == null) {
133
                    return;
134
                }
135
                featureStore.setSelection(set);
136

    
137
            } catch (Exception e) {
138
                logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
139
                JOptionPane.showMessageDialog(
140
                        ApplicationLocator.getManager().getRootComponent(),
141
                        Messages.getText("_Invalid_expression") + ":\n"
142
                        + getLastMessage(e),
143
                        Messages.getText("_Invalid_expression"),
144
                        JOptionPane.ERROR_MESSAGE);
145
            } finally {
146
                DisposeUtils.disposeQuietly(set);
147
            }
148
        } else {
149
            // if no expression -> no element selected
150
            featureStore.getFeatureSelection().deselectAll();
151
        }
152
    }
153

    
154
    private FeatureSet doSet(String expression) throws DataException {
155
        FeatureQuery query = featureStore.createFeatureQuery();
156
        DataManager manager = DALLocator.getDataManager();
157
        query.setFilter(manager.createExpresion(expression));
158
        return featureStore.getFeatureSet(query);
159
    }
160

    
161
    @Override
162
    public void addToSet(String expression) throws DataException {
163
        // if no filter expression -> don't add more elements to set
164
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
165
            FeatureSet set = null;
166
            try {
167
                set = doSet(expression);
168
                if (set == null) {
169
                    return;
170
                }
171
                featureStore.getFeatureSelection().select(set);
172

    
173
            } catch (Exception e) {
174
                logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
175
                JOptionPane.showMessageDialog(
176
                        ApplicationLocator.getManager().getRootComponent(),
177
                        Messages.getText("_Invalid_expression") + ":\n"
178
                        + getLastMessage(e),
179
                        Messages.getText("_Invalid_expression"),
180
                        JOptionPane.ERROR_MESSAGE);
181
            } finally {
182
                DisposeUtils.disposeQuietly(set);
183
            }
184
        }
185
    }
186

    
187
    @Override
188
    public void fromSet(String expression) throws DataException {
189
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
190
            FeatureSet set = null;
191
            try {
192
                set = doSet(expression);
193
                if (set == null) {
194
                    return;
195
                }
196
                FeatureSelection oldSelection = featureStore.getFeatureSelection();
197
                FeatureSelection newSelection = featureStore.createFeatureSelection();
198
                for (Feature feature : set) {
199
                    if (oldSelection.isSelected(feature)) {
200
                        newSelection.select(feature);
201
                    }
202
                }
203
                featureStore.setSelection(newSelection);
204
            } catch (Exception e) {
205
                logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
206
                JOptionPane.showMessageDialog(
207
                        ApplicationLocator.getManager().getRootComponent(),
208
                        Messages.getText("_Invalid_expression") + ":\n"
209
                        + getLastMessage(e),
210
                        Messages.getText("_Invalid_expression"),
211
                        JOptionPane.ERROR_MESSAGE);
212
            } finally {
213
                DisposeUtils.disposeQuietly(set);
214
            }
215
        } else {
216
            featureStore.getFeatureSelection().deselectAll();
217
        }
218
    }
219

    
220
    /**
221
     * Returns true if the WHERE subconsultation of the filterExpression is
222
     * empty ("")
223
     *
224
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
225
     * @param expression An string
226
     * @return A boolean value
227
     */
228
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
229

    
230
        if (expression == null) {
231
            return true;
232
        }
233

    
234
        String subExpression = expression.trim();
235

    
236
        if (subExpression.length() == 0) {
237
            return true;
238
        }
239

    
240
        int pos;
241

    
242
        // Remove last ';' if exists
243
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
244
            subExpression
245
                    = subExpression.substring(0, subExpression.length() - 1).trim();
246
        }
247

    
248
        // If there is no 'where' clause
249
        if ((pos = subExpression.indexOf("where")) == -1) {
250
            return false;
251
        }
252

    
253
        // If there is no subexpression in the WHERE clause -> true
254
        // ( + 5 is the length of the 'where')
255
        subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
256

    
257
        if (subExpression.length() == 0) {
258
            return true;
259
        } else {
260
            return false;
261
        }
262
    }
263

    
264
    /**
265
     * @param ex
266
     * @return
267
     */
268
    public static String getLastMessage(Throwable ex) {
269

    
270
        Throwable p = ex;
271
        while (p.getCause() != null && p.getCause() != p) {
272
            p = p.getCause();
273
        }
274
        return p.getMessage();
275
    }
276
}