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 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40558 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40558 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40558 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40558 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40558 jjdelcerro
 *
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 40435 jjdelcerro
 */
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 40955 jldominguez
import org.gvsig.app.ApplicationLocator;
35 40435 jjdelcerro
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 41747 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
42 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
43 41747 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureSelection;
44 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureSet;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46 40955 jldominguez
import org.gvsig.i18n.Messages;
47 43521 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
48 43987 jjdelcerro
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
49 40435 jjdelcerro
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 43987 jjdelcerro
 *
54 40435 jjdelcerro
 * @author Vicente Caballero Navarro
55
 */
56 44011 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
57 43521 jjdelcerro
public class SelectByAttributesInTableExtension extends Extension implements ExpressionListener {
58 40435 jjdelcerro
59
    protected FeatureStore featureStore = null;
60
    protected FeatureTableDocumentPanel table;
61
    private String filterTitle;
62
63 44011 jjdelcerro
    @Override
64 40435 jjdelcerro
    public void initialize() {
65
        registerIcons();
66
    }
67
68
    private void registerIcons() {
69 43987 jjdelcerro
        IconThemeHelper.registerIcon("action", "selection-by-attributes", this);
70 40435 jjdelcerro
    }
71
72 44011 jjdelcerro
    @Override
73 40435 jjdelcerro
    public void execute(String actionCommand) {
74 43987 jjdelcerro
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
75 40435 jjdelcerro
76 43987 jjdelcerro
        if (!(v instanceof FeatureTableDocumentPanel)) {
77
            return;
78
        }
79 44011 jjdelcerro
        if ("selection-by-attributes-table".equals(actionCommand)) {
80 43987 jjdelcerro
            table = (FeatureTableDocumentPanel) v;
81 40435 jjdelcerro
82 44259 jjdelcerro
83
            featureStore = table.getFeatureStore();
84
            filterTitle = featureStore.getName();
85
//            table.getModel().setModified(true);
86 44011 jjdelcerro
            SelectByAttributes selector = new SelectByAttributes();
87
            selector.showWindow(filterTitle, featureStore, WindowManager.MODE.TOOL);
88 43987 jjdelcerro
89 44011 jjdelcerro
        } else if ("selection-by-attributes-table-old".equals(actionCommand)) {
90 43987 jjdelcerro
            table = (FeatureTableDocumentPanel) v;
91
92 44259 jjdelcerro
            featureStore = table.getFeatureStore();
93
            filterTitle = featureStore.getName();
94
//            table.getModel().setModified(true);
95 43987 jjdelcerro
96 40435 jjdelcerro
            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 44011 jjdelcerro
            @Override
106 40435 jjdelcerro
            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 44011 jjdelcerro
    @Override
115 40435 jjdelcerro
    public boolean isEnabled() {
116
        return isVisible();
117
    }
118
119 44011 jjdelcerro
    @Override
120 40435 jjdelcerro
    public boolean isVisible() {
121
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
122
        return (v instanceof FeatureTableDocumentPanel);
123
    }
124
125 41747 jjdelcerro
    // if no filter expression -> no element selected
126 44011 jjdelcerro
    @Override
127 40435 jjdelcerro
    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 43987 jjdelcerro
137 40435 jjdelcerro
            } catch (Exception e) {
138 43987 jjdelcerro
                logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
139 40435 jjdelcerro
                JOptionPane.showMessageDialog(
140 43987 jjdelcerro
                        ApplicationLocator.getManager().getRootComponent(),
141
                        Messages.getText("_Invalid_expression") + ":\n"
142 40955 jldominguez
                        + getLastMessage(e),
143 43987 jjdelcerro
                        Messages.getText("_Invalid_expression"),
144
                        JOptionPane.ERROR_MESSAGE);
145 40435 jjdelcerro
            } finally {
146 43521 jjdelcerro
                DisposeUtils.disposeQuietly(set);
147 40435 jjdelcerro
            }
148
        } else {
149 41747 jjdelcerro
            // if no expression -> no element selected
150 40435 jjdelcerro
            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 43521 jjdelcerro
    @Override
162 40435 jjdelcerro
    public void addToSet(String expression) throws DataException {
163 41747 jjdelcerro
        // if no filter expression -> don't add more elements to set
164 40435 jjdelcerro
        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 43521 jjdelcerro
173
            } catch (Exception e) {
174 43987 jjdelcerro
                logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
175 43521 jjdelcerro
                JOptionPane.showMessageDialog(
176 43987 jjdelcerro
                        ApplicationLocator.getManager().getRootComponent(),
177
                        Messages.getText("_Invalid_expression") + ":\n"
178 43521 jjdelcerro
                        + getLastMessage(e),
179 43987 jjdelcerro
                        Messages.getText("_Invalid_expression"),
180
                        JOptionPane.ERROR_MESSAGE);
181 40435 jjdelcerro
            } finally {
182 43521 jjdelcerro
                DisposeUtils.disposeQuietly(set);
183 40435 jjdelcerro
            }
184
        }
185
    }
186
187 43521 jjdelcerro
    @Override
188 40435 jjdelcerro
    public void fromSet(String expression) throws DataException {
189 43987 jjdelcerro
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
190 41747 jjdelcerro
            FeatureSet set = null;
191
            try {
192
                set = doSet(expression);
193 43987 jjdelcerro
                if (set == null) {
194 41747 jjdelcerro
                    return;
195
                }
196
                FeatureSelection oldSelection = featureStore.getFeatureSelection();
197
                FeatureSelection newSelection = featureStore.createFeatureSelection();
198 44011 jjdelcerro
                for (Feature feature : set) {
199 43987 jjdelcerro
                    if (oldSelection.isSelected(feature)) {
200 41747 jjdelcerro
                        newSelection.select(feature);
201
                    }
202
                }
203
                featureStore.setSelection(newSelection);
204 43521 jjdelcerro
            } catch (Exception e) {
205 43987 jjdelcerro
                logger.warn("Problems executing query '" + expression + "' on store '" + featureStore + "'.", e);
206 43521 jjdelcerro
                JOptionPane.showMessageDialog(
207 43987 jjdelcerro
                        ApplicationLocator.getManager().getRootComponent(),
208
                        Messages.getText("_Invalid_expression") + ":\n"
209 43521 jjdelcerro
                        + getLastMessage(e),
210 43987 jjdelcerro
                        Messages.getText("_Invalid_expression"),
211
                        JOptionPane.ERROR_MESSAGE);
212 41747 jjdelcerro
            } finally {
213 43521 jjdelcerro
                DisposeUtils.disposeQuietly(set);
214 40435 jjdelcerro
            }
215 41747 jjdelcerro
        } else {
216
            featureStore.getFeatureSelection().deselectAll();
217 40435 jjdelcerro
        }
218
    }
219 43987 jjdelcerro
220 40435 jjdelcerro
    /**
221
     * Returns true if the WHERE subconsultation of the filterExpression is
222
     * empty ("")
223 43987 jjdelcerro
     *
224 40435 jjdelcerro
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
225 43987 jjdelcerro
     * @param expression An string
226 40435 jjdelcerro
     * @return A boolean value
227
     */
228
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
229 43987 jjdelcerro
230 40435 jjdelcerro
        if (expression == null) {
231
            return true;
232
        }
233 43987 jjdelcerro
234 40435 jjdelcerro
        String subExpression = expression.trim();
235 43987 jjdelcerro
236 40435 jjdelcerro
        if (subExpression.length() == 0) {
237
            return true;
238
        }
239 43987 jjdelcerro
240 40435 jjdelcerro
        int pos;
241
242
        // Remove last ';' if exists
243
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
244 43987 jjdelcerro
            subExpression
245
                    = subExpression.substring(0, subExpression.length() - 1).trim();
246 40435 jjdelcerro
        }
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 41747 jjdelcerro
        // ( + 5 is the length of the 'where')
255 43987 jjdelcerro
        subExpression = subExpression.substring(pos + 5, subExpression.length()).trim();
256
257 40435 jjdelcerro
        if (subExpression.length() == 0) {
258
            return true;
259
        } else {
260
            return false;
261
        }
262
    }
263 43987 jjdelcerro
264 40955 jldominguez
    /**
265
     * @param ex
266
     * @return
267
     */
268
    public static String getLastMessage(Throwable ex) {
269 43987 jjdelcerro
270 40955 jldominguez
        Throwable p = ex;
271
        while (p.getCause() != null && p.getCause() != p) {
272
            p = p.getCause();
273
        }
274
        return p.getMessage();
275 43987 jjdelcerro
    }
276 40435 jjdelcerro
}