Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2056 / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / FiltroExtension.java @ 39030

History | View | Annotate | Download (7.26 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.app.extension;
23

    
24
import java.awt.Component;
25

    
26
import javax.swing.JOptionPane;
27

    
28
import org.gvsig.andami.IconThemeHelper;
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.messages.NotificationManager;
31
import org.gvsig.andami.plugins.Extension;
32
import org.gvsig.andami.ui.mdiManager.IWindow;
33
import org.gvsig.app.gui.filter.ExpressionListener;
34
import org.gvsig.app.gui.filter.FilterDialog;
35
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataManager;
38
import org.gvsig.fmap.dal.exception.DataException;
39
import org.gvsig.fmap.dal.feature.FeatureQuery;
40
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.utils.exceptionHandling.ExceptionListener;
43

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

    
51
    protected FeatureStore featureStore = null;
52
    protected FeatureTableDocumentPanel table;
53
    private String filterTitle;
54

    
55
    public void initialize() {
56
        registerIcons();
57
    }
58

    
59
    private void registerIcons() {
60
            IconThemeHelper.registerIcon("action", "table-filter", this);
61
    }
62

    
63
    public void execute(String actionCommand) {
64
        if ("table-filter".equals(actionCommand)) {
65
            IWindow v = PluginServices.getMDIManager().getActiveWindow();
66

    
67
            if (v instanceof FeatureTableDocumentPanel) {
68
                table = (FeatureTableDocumentPanel) v;
69

    
70
                featureStore = table.getModel().getStore();
71
                filterTitle = table.getModel().getName();
72
                table.getModel().setModified(true);
73
            }
74

    
75
            doExecute();
76
        }
77
    }
78

    
79
    protected void doExecute() {
80
        FilterDialog dlg = new FilterDialog(filterTitle);
81
        dlg.addExpressionListener(this);
82
        dlg.addExceptionListener(new ExceptionListener() {
83

    
84
            public void exceptionThrown(Throwable t) {
85
                NotificationManager.addError(t.getMessage(), t);
86
            }
87
        });
88
        dlg.setModel(featureStore);
89
        PluginServices.getMDIManager().addWindow(dlg);
90
    }
91

    
92
    public boolean isEnabled() {
93
        return isVisible();
94
    }
95

    
96
    public boolean isVisible() {
97
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
98
        return (v instanceof FeatureTableDocumentPanel);
99
    }
100

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

    
108
                if (set == null) {
109
                    return;
110
                }
111
                featureStore.setSelection(set);
112

    
113
            } catch (Exception e) {
114
                JOptionPane.showMessageDialog(
115
                    (Component) PluginServices.getMainFrame(),
116
                    "Asegurate de que la consulta es correcta.");
117
            } finally {
118
                if (set != null) {
119
                    set.dispose();
120
                }
121
            }
122
        } else {
123
            // By Pablo: if no expression -> no element selected
124
            featureStore.getFeatureSelection().deselectAll();
125
        }
126
    }
127

    
128
    private FeatureSet doSet(String expression) throws DataException {
129
        FeatureQuery query = featureStore.createFeatureQuery();
130
        DataManager manager = DALLocator.getDataManager();
131
        query.setFilter(manager.createExpresion(expression));
132
        return featureStore.getFeatureSet(query);
133
    }
134

    
135
    public void addToSet(String expression) throws DataException {
136
        // By Pablo: if no filter expression -> don't add more elements to set
137
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
138
            FeatureSet set = null;
139
            try {
140
                set = doSet(expression);
141

    
142
                if (set == null) {
143
                    return;
144
                }
145
                featureStore.getFeatureSelection().select(set);
146
            } finally {
147
                if (set != null) {
148
                    set.dispose();
149
                }
150
            }
151
        }
152
    }
153

    
154
    public void fromSet(String expression) throws DataException {
155
        // By Pablo: if no filter expression -> no element selected
156
        try {
157
            if (!this.filterExpressionFromWhereIsEmpty(expression)) {
158
                NotificationManager.showMessageInfo("Falta por implementar",
159
                    null);
160
            } else {
161
                // By Pablo: if no expression -> no element selected
162
                featureStore.getFeatureSelection().deselectAll();
163
            }
164
        } catch (DataException e) {
165
            NotificationManager.addError(e);
166
        }
167

    
168
    }
169

    
170
    /**
171
     * Returns true if the WHERE subconsultation of the filterExpression is
172
     * empty ("")
173
     * 
174
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
175
     * @param expression
176
     *            An string
177
     * @return A boolean value
178
     */
179
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
180
        String subExpression = expression.trim();
181
        int pos;
182

    
183
        // Remove last ';' if exists
184
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
185
            subExpression =
186
                subExpression.substring(0, subExpression.length() - 1).trim();
187
        }
188

    
189
        // If there is no 'where' clause
190
        if ((pos = subExpression.indexOf("where")) == -1) {
191
            return false;
192
        }
193

    
194
        // If there is no subexpression in the WHERE clause -> true
195
        subExpression =
196
            subExpression.substring(pos + 5, subExpression.length()).trim(); // +
197
                                                                             // 5
198
                                                                             // is
199
                                                                             // the
200
                                                                             // length
201
                                                                             // of
202
                                                                             // 'where'
203
        if (subExpression.length() == 0) {
204
            return true;
205
        } else {
206
            return false;
207
        }
208
    }
209
}