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 / FiltroExtension.java @ 40955

History | View | Annotate | Download (8.03 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.awt.Component;
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.FeatureQuery;
43
import org.gvsig.fmap.dal.feature.FeatureSet;
44
import org.gvsig.fmap.dal.feature.FeatureStore;
45
import org.gvsig.i18n.Messages;
46
import org.gvsig.utils.exceptionHandling.ExceptionListener;
47

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

    
55
    protected FeatureStore featureStore = null;
56
    protected FeatureTableDocumentPanel table;
57
    private String filterTitle;
58

    
59
    public void initialize() {
60
        registerIcons();
61
    }
62

    
63
    private void registerIcons() {
64
            IconThemeHelper.registerIcon("action", "table-filter", this);
65
    }
66

    
67
    public void execute(String actionCommand) {
68
        if ("table-filter".equals(actionCommand)) {
69
            IWindow v = PluginServices.getMDIManager().getActiveWindow();
70

    
71
            if (v instanceof FeatureTableDocumentPanel) {
72
                table = (FeatureTableDocumentPanel) v;
73

    
74
                featureStore = table.getModel().getStore();
75
                filterTitle = table.getModel().getName();
76
                table.getModel().setModified(true);
77
            }
78

    
79
            doExecute();
80
        }
81
    }
82

    
83
    protected void doExecute() {
84
        FilterDialog dlg = new FilterDialog(filterTitle);
85
        dlg.addExpressionListener(this);
86
        dlg.addExceptionListener(new ExceptionListener() {
87

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

    
96
    public boolean isEnabled() {
97
        return isVisible();
98
    }
99

    
100
    public boolean isVisible() {
101
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
102
        return (v instanceof FeatureTableDocumentPanel);
103
    }
104

    
105
    // By Pablo: if no filter expression -> no element selected
106
    public void newSet(String expression) throws DataException {
107
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
108
            FeatureSet set = null;
109
            try {
110
                set = doSet(expression);
111

    
112
                if (set == null) {
113
                    return;
114
                }
115
                featureStore.setSelection(set);
116

    
117
            } catch (Exception e) {
118
                
119
                JOptionPane.showMessageDialog(
120
                    ApplicationLocator.getManager().getRootComponent(),
121
                    Messages.getText("_Invalid_expression") + ":\n"
122
                        + getLastMessage(e),
123
                    Messages.getText("_Invalid_expression"),
124
                    JOptionPane.ERROR_MESSAGE);
125
                
126
            } finally {
127
                if (set != null) {
128
                    set.dispose();
129
                }
130
            }
131
        } else {
132
            // By Pablo: if no expression -> no element selected
133
            featureStore.getFeatureSelection().deselectAll();
134
        }
135
    }
136

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

    
144
    public void addToSet(String expression) throws DataException {
145
        // By Pablo: 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

    
151
                if (set == null) {
152
                    return;
153
                }
154
                featureStore.getFeatureSelection().select(set);
155
            } finally {
156
                if (set != null) {
157
                    set.dispose();
158
                }
159
            }
160
        }
161
    }
162

    
163
    public void fromSet(String expression) throws DataException {
164
        // By Pablo: if no filter expression -> no element selected
165
        try {
166
            if (!this.filterExpressionFromWhereIsEmpty(expression)) {
167
                NotificationManager.showMessageInfo("Falta por implementar",
168
                    null);
169
            } else {
170
                // By Pablo: if no expression -> no element selected
171
                featureStore.getFeatureSelection().deselectAll();
172
            }
173
        } catch (DataException e) {
174
            NotificationManager.addError(e);
175
        }
176

    
177
    }
178

    
179
    /**
180
     * Returns true if the WHERE subconsultation of the filterExpression is
181
     * empty ("")
182
     * 
183
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
184
     * @param expression
185
     *            An string
186
     * @return A boolean value
187
     */
188
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
189
        
190
        if (expression == null) {
191
            return true;
192
        }
193
        
194
        String subExpression = expression.trim();
195
        
196
        if (subExpression.length() == 0) {
197
            return true;
198
        }
199
        
200
        int pos;
201

    
202
        // Remove last ';' if exists
203
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
204
            subExpression =
205
                subExpression.substring(0, subExpression.length() - 1).trim();
206
        }
207

    
208
        // If there is no 'where' clause
209
        if ((pos = subExpression.indexOf("where")) == -1) {
210
            return false;
211
        }
212

    
213
        // If there is no subexpression in the WHERE clause -> true
214
        subExpression =
215
            subExpression.substring(pos + 5, subExpression.length()).trim(); // +
216
                                                                             // 5
217
                                                                             // is
218
                                                                             // the
219
                                                                             // length
220
                                                                             // of
221
                                                                             // 'where'
222
        if (subExpression.length() == 0) {
223
            return true;
224
        } else {
225
            return false;
226
        }
227
    }
228
    
229
    
230
    /**
231
     * @param ex
232
     * @return
233
     */
234
    public static String getLastMessage(Throwable ex) {
235
        
236
        Throwable p = ex;
237
        while (p.getCause() != null && p.getCause() != p) {
238
            p = p.getCause();
239
        }
240
        return p.getMessage();
241
    }    
242
}