Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 1774

History | View | Annotate | Download (6.55 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig;
42

    
43
import com.hardcode.driverManager.DriverLoadException;
44

    
45
import com.hardcode.gdbms.engine.data.DataSource;
46
import com.hardcode.gdbms.engine.data.DataSourceFactory;
47
import com.hardcode.gdbms.engine.instruction.SemanticException;
48
import com.hardcode.gdbms.parser.ParseException;
49

    
50
import com.iver.andami.PluginServices;
51
import com.iver.andami.messages.NotificationManager;
52
import com.iver.andami.plugins.Extension;
53
import com.iver.andami.ui.mdiManager.View;
54

    
55
import com.iver.cit.gvsig.fmap.DriverException;
56
import com.iver.cit.gvsig.fmap.layers.FBitSet;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
59
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
60
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
61
import com.iver.cit.gvsig.gui.Table;
62
import com.iver.cit.gvsig.gui.filter.DefaultExpressionDataSource;
63
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
64
import com.iver.cit.gvsig.gui.filter.FilterDialog;
65
import com.iver.cit.gvsig.project.ProjectView;
66
import com.iver.utiles.exceptionHandling.ExceptionListener;
67

    
68
import java.io.IOException;
69

    
70

    
71
/**
72
 * Extensi?n que controla las operaciones realizadas sobre las tablas.
73
 *
74
 * @author Fernando Gonz?lez Cort?s
75
 */
76
public class TableOperations implements Extension, ExpressionListener {
77
        private SelectableDataSource dataSource = null;
78
        private Table vista;
79

    
80
        /**
81
         * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
82
         */
83
        public void execute(String actionCommand) {
84
                View v = PluginServices.getMDIManager().getActiveView();
85

    
86
                if (v instanceof Table) {
87
                        vista = (Table) v;
88
                        dataSource = vista.getModel().getModelo();
89
                } else if (v instanceof com.iver.cit.gvsig.gui.View) {
90
                        ProjectView pv = ((com.iver.cit.gvsig.gui.View) v).getModel();
91

    
92
                        try {
93
                                dataSource = ((AlphanumericData) pv.getMapContext().getLayers()
94
                                                                                                   .getActives()[0]).getRecordset();
95
                        } catch (DriverException e) {
96
                                NotificationManager.addError(e.getMessage(), e);
97
                        }
98
                }
99

    
100
                DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
101
                ds.setTable(dataSource);
102

    
103
                FilterDialog dlg = new FilterDialog();
104
                dlg.addExpressionListener(this);
105
                dlg.addExceptionListener(new ExceptionListener() {
106
                                public void exceptionThrown(Throwable t) {
107
                                        NotificationManager.addError(t.getMessage(), t);
108
                                }
109
                        });
110

    
111
                dlg.setModel(ds);
112
                PluginServices.getMDIManager().addView(dlg);
113
        }
114

    
115
        /**
116
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
117
         */
118
        public void newSet(String expression) {
119
                long[] sel = doSet(expression);
120

    
121
                if (sel == null) {
122
                        throw new RuntimeException("Not a 'where' clause?");
123
                }
124

    
125
                FBitSet selection = new FBitSet();
126

    
127
                for (int i = 0; i < sel.length; i++) {
128
                        selection.set((int) sel[i]);
129
                }
130

    
131
                dataSource.clearSelection();
132
                dataSource.setSelection(selection);
133
        }
134

    
135
        /**
136
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
137
         */
138
        private long[] doSet(String expression) {
139
                try {
140
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression);
141

    
142
                        return ds.getWhereFilter();
143
                } catch (DriverLoadException e) {
144
                        NotificationManager.addError("Error cargando el driver", e);
145
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
146
                        NotificationManager.addError("Error accediendo al driver", e);
147
                } catch (ParseException e) {
148
                        NotificationManager.addError("Parse error", e);
149
                } catch (SemanticException e) {
150
                        NotificationManager.addError(e.getMessage(), e);
151
                } catch (IOException e) {
152
                        NotificationManager.addError("GDBMS internal error", e);
153
                }
154

    
155
                return null;
156
        }
157

    
158
        /**
159
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
160
         */
161
        public void addToSet(String expression) {
162
                long[] sel = doSet(expression);
163

    
164
                if (sel == null) {
165
                        throw new RuntimeException("Not a 'where' clause?");
166
                }
167

    
168
                FBitSet selection = new FBitSet();
169

    
170
                for (int i = 0; i < sel.length; i++) {
171
                        selection.set((int) sel[i]);
172
                }
173

    
174
                FBitSet fbs = dataSource.getSelection();
175
                fbs.or(selection);
176
                dataSource.setSelection(fbs);
177
        }
178

    
179
        /**
180
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
181
         */
182
        public void fromSet(String expression) {
183
                long[] sel = doSet(expression);
184

    
185
                if (sel == null) {
186
                        throw new RuntimeException("Not a 'where' clause?");
187
                }
188

    
189
                FBitSet selection = new FBitSet();
190

    
191
                for (int i = 0; i < sel.length; i++) {
192
                        selection.set((int) sel[i]);
193
                }
194

    
195
                FBitSet fbs = dataSource.getSelection();
196
                fbs.and(selection);
197
                dataSource.setSelection(fbs);
198
        }
199

    
200
        /**
201
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
202
         */
203
        public boolean isVisible() {
204
                View v = PluginServices.getMDIManager().getActiveView();
205

    
206
                if (v == null) {
207
                        return false;
208
                }
209

    
210
                if (v.getClass() == Table.class) {
211
                        return true;
212
                } else {
213
                        if (v instanceof com.iver.cit.gvsig.gui.View) {
214
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
215
                                ProjectView pv = view.getModel();
216
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
217
                                                                                   .getActives();
218

    
219
                                if (seleccionadas.length == 1) {
220
                                        if (seleccionadas[0] instanceof AlphanumericData) {
221
                                                return true;
222
                                        }
223
                                }
224
                        }
225

    
226
                        return false;
227
                }
228
        }
229

    
230
        /**
231
         * @see com.iver.andami.plugins.Extension#inicializar()
232
         */
233
        public void inicializar() {
234
        }
235

    
236
        /**
237
         * @see com.iver.andami.plugins.Extension#isEnabled()
238
         */
239
        public boolean isEnabled() {
240
                return true;
241
        }
242
}