svn-gvsig-desktop / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 1871
History | View | Annotate | Download (11.8 KB)
1 | 1103 | fjp | /* 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 | 312 | fernando | package com.iver.cit.gvsig; |
42 | |||
43 | 1830 | fernando | import java.io.IOException; |
44 | |||
45 | 1219 | vcaballero | import com.hardcode.driverManager.DriverLoadException; |
46 | 884 | fernando | import com.hardcode.gdbms.engine.data.DataSource; |
47 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
48 | import com.hardcode.gdbms.engine.instruction.SemanticException; |
||
49 | import com.hardcode.gdbms.parser.ParseException; |
||
50 | 855 | fernando | import com.iver.andami.PluginServices; |
51 | import com.iver.andami.messages.NotificationManager; |
||
52 | 596 | fernando | import com.iver.andami.plugins.Extension; |
53 | 1830 | fernando | import com.iver.andami.ui.mdiManager.SingletonView; |
54 | 855 | fernando | import com.iver.andami.ui.mdiManager.View; |
55 | import com.iver.cit.gvsig.fmap.DriverException; |
||
56 | 884 | fernando | import com.iver.cit.gvsig.fmap.layers.FBitSet; |
57 | 855 | fernando | import com.iver.cit.gvsig.fmap.layers.FLayer; |
58 | 1774 | fernando | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
59 | 855 | fernando | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
60 | import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
||
61 | 312 | fernando | import com.iver.cit.gvsig.gui.Table; |
62 | 855 | fernando | 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 | 1830 | fernando | import com.iver.cit.gvsig.gui.tables.AndamiWizard; |
66 | import com.iver.cit.gvsig.gui.tables.FieldSelectionModel; |
||
67 | import com.iver.cit.gvsig.gui.tables.ObjectSelectionStep; |
||
68 | import com.iver.cit.gvsig.gui.tables.TableSelectionModel; |
||
69 | import com.iver.cit.gvsig.project.ProjectTable; |
||
70 | 1221 | fernando | import com.iver.cit.gvsig.project.ProjectView; |
71 | 855 | fernando | import com.iver.utiles.exceptionHandling.ExceptionListener; |
72 | 1830 | fernando | import com.iver.utiles.swing.objectSelection.SelectionException; |
73 | import com.iver.utiles.swing.wizard.WizardControl; |
||
74 | import com.iver.utiles.swing.wizard.WizardEvent; |
||
75 | import com.iver.utiles.swing.wizard.WizardListener; |
||
76 | 312 | fernando | |
77 | |||
78 | /**
|
||
79 | 1219 | vcaballero | * Extensi?n que controla las operaciones realizadas sobre las tablas.
|
80 | 312 | fernando | *
|
81 | * @author Fernando Gonz?lez Cort?s
|
||
82 | */
|
||
83 | 596 | fernando | public class TableOperations implements Extension, ExpressionListener { |
84 | 855 | fernando | private SelectableDataSource dataSource = null; |
85 | 1219 | vcaballero | private Table vista;
|
86 | 312 | fernando | |
87 | 1219 | vcaballero | /**
|
88 | * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
|
||
89 | */
|
||
90 | public void execute(String actionCommand) { |
||
91 | 1830 | fernando | if ("FILTRO".equals(actionCommand)) { |
92 | try {
|
||
93 | View v = PluginServices.getMDIManager().getActiveView();
|
||
94 | 1219 | vcaballero | |
95 | 1830 | fernando | if (v instanceof Table) { |
96 | vista = (Table) v; |
||
97 | dataSource = vista.getModel().getModelo(); |
||
98 | } else if (v instanceof com.iver.cit.gvsig.gui.View) { |
||
99 | ProjectView pv = ((com.iver.cit.gvsig.gui.View) v).getModel(); |
||
100 | 1219 | vcaballero | |
101 | 1830 | fernando | dataSource = ((AlphanumericData) pv.getMapContext() |
102 | .getLayers().getActives()[0]).getRecordset();
|
||
103 | } |
||
104 | |||
105 | DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
|
||
106 | ds.setTable(dataSource); |
||
107 | |||
108 | FilterDialog dlg = new FilterDialog();
|
||
109 | dlg.addExpressionListener(this);
|
||
110 | dlg.addExceptionListener(new ExceptionListener() { |
||
111 | public void exceptionThrown(Throwable t) { |
||
112 | NotificationManager.addError(t.getMessage(), t); |
||
113 | } |
||
114 | }); |
||
115 | |||
116 | dlg.setModel(ds); |
||
117 | PluginServices.getMDIManager().addView(dlg); |
||
118 | 855 | fernando | } catch (DriverException e) {
|
119 | 1830 | fernando | NotificationManager.addError("Error filtrando", e);
|
120 | 855 | fernando | } |
121 | 1830 | fernando | } else if ("JOIN".equals(actionCommand)) { |
122 | ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
||
123 | ProjectTable[] pts = (ProjectTable[]) pe.getProject().getTables() |
||
124 | .toArray(new ProjectTable[0]); |
||
125 | 855 | fernando | |
126 | 1830 | fernando | try {
|
127 | final ObjectSelectionStep sourceTable = new ObjectSelectionStep(); |
||
128 | sourceTable.setModel(new TableSelectionModel(pts,
|
||
129 | "Selecciona la tabla origen"));
|
||
130 | 312 | fernando | |
131 | 1830 | fernando | final ObjectSelectionStep targetTable = new ObjectSelectionStep(); |
132 | targetTable.setModel(new TableSelectionModel(pts,
|
||
133 | "Selecciona la tabla a enlazar"));
|
||
134 | 312 | fernando | |
135 | 1830 | fernando | final ObjectSelectionStep firstTableField = new ObjectSelectionStep(); |
136 | final ObjectSelectionStep secondTableField = new ObjectSelectionStep(); |
||
137 | final AndamiWizard wiz = new AndamiWizard(); |
||
138 | |||
139 | wiz.addStep(sourceTable); |
||
140 | wiz.addStep(firstTableField); |
||
141 | wiz.addStep(targetTable); |
||
142 | wiz.addStep(secondTableField); |
||
143 | |||
144 | wiz.addWizardListener(new WizardListener() {
|
||
145 | public void cancel(WizardEvent w) { |
||
146 | PluginServices.getMDIManager().closeView(wiz); |
||
147 | } |
||
148 | |||
149 | public void finished(WizardEvent w) { |
||
150 | PluginServices.getMDIManager().closeView(wiz); |
||
151 | |||
152 | ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected(); |
||
153 | SelectableDataSource sds = sourceProjectTable.getModelo(); |
||
154 | String tableName1 = sds.getName();
|
||
155 | |||
156 | ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected(); |
||
157 | sds = targetProjectTable.getModelo(); |
||
158 | |||
159 | String tableName2 = sds.getName();
|
||
160 | |||
161 | String field1 = (String) firstTableField.getSelected(); |
||
162 | String field2 = (String) secondTableField.getSelected(); |
||
163 | |||
164 | 1836 | fernando | String sql = "custom com_iver_cit_gvsig_arcjoin tables '" + tableName1 + |
165 | 1830 | fernando | "', '" + tableName2 + "' values(" + field1 + ", " + |
166 | field2 + ");";
|
||
167 | System.out.println(sql);
|
||
168 | |||
169 | try {
|
||
170 | SelectableDataSource result = new SelectableDataSource(
|
||
171 | LayerFactory.getDataSourceFactory() |
||
172 | .executeSQL(sql, DataSourceFactory.MANUAL_MODE)); |
||
173 | ProjectTable projectTable = (ProjectTable) sourceTable.getSelected(); |
||
174 | projectTable.replaceDataSource(result); |
||
175 | } catch (ParseException e) { |
||
176 | throw new RuntimeException(e); |
||
177 | } catch (DriverLoadException e) {
|
||
178 | NotificationManager.addError("Error con la carga de drivers",
|
||
179 | e); |
||
180 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
181 | NotificationManager.addError("Error leyendo del driver",
|
||
182 | e); |
||
183 | } catch (SemanticException e) {
|
||
184 | throw new RuntimeException(e); |
||
185 | } catch (IOException e) { |
||
186 | NotificationManager.addError("Error de entrada/salida",
|
||
187 | e); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | public void next(WizardEvent w) { |
||
192 | WizardControl wiz = w.wizard; |
||
193 | wiz.enableBack(true);
|
||
194 | wiz.enableNext(((ObjectSelectionStep)wiz.getCurrentStep()).getSelectedItem() != null);
|
||
195 | if (w.currentStep == 1) { |
||
196 | ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
||
197 | |||
198 | try {
|
||
199 | firstTableField.setModel(new FieldSelectionModel(
|
||
200 | pt.getModelo(), |
||
201 | "Selecciona el campo sobre el que hacer el join",
|
||
202 | -1));
|
||
203 | } catch (SelectionException e) {
|
||
204 | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
||
205 | e); |
||
206 | } |
||
207 | } else if (w.currentStep == 3) { |
||
208 | try {
|
||
209 | //tabla
|
||
210 | ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
||
211 | |||
212 | //?ndice del campo
|
||
213 | SelectableDataSource sds = pt.getModelo(); |
||
214 | String fieldName = (String) firstTableField.getSelected(); |
||
215 | int fieldIndex = sds.getFieldIndexByName(fieldName);
|
||
216 | int type = sds.getFieldType(fieldIndex);
|
||
217 | |||
218 | secondTableField.setModel(new FieldSelectionModel(
|
||
219 | ((ProjectTable) targetTable.getSelected()).getModelo(), |
||
220 | "Selecciona el campo sobre el que hacer el join",
|
||
221 | type)); |
||
222 | } catch (SelectionException e) {
|
||
223 | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
||
224 | e); |
||
225 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
226 | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
||
227 | e); |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | public void back(WizardEvent w) { |
||
233 | WizardControl wiz = w.wizard; |
||
234 | wiz.enableBack(true);
|
||
235 | wiz.enableNext(((ObjectSelectionStep)wiz.getCurrentStep()).getSelectedItem() != null);
|
||
236 | } |
||
237 | }); |
||
238 | PluginServices.getMDIManager().addView(wiz); |
||
239 | } catch (SelectionException e) {
|
||
240 | NotificationManager.addError("Error abriendo el asistente", e);
|
||
241 | } |
||
242 | } |
||
243 | 1219 | vcaballero | } |
244 | 312 | fernando | |
245 | 1219 | vcaballero | /**
|
246 | 855 | fernando | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
247 | 312 | fernando | */
|
248 | public void newSet(String expression) { |
||
249 | 884 | fernando | long[] sel = doSet(expression); |
250 | 1219 | vcaballero | |
251 | if (sel == null) { |
||
252 | throw new RuntimeException("Not a 'where' clause?"); |
||
253 | } |
||
254 | |||
255 | 884 | fernando | FBitSet selection = new FBitSet();
|
256 | 1219 | vcaballero | |
257 | 884 | fernando | for (int i = 0; i < sel.length; i++) { |
258 | selection.set((int) sel[i]);
|
||
259 | } |
||
260 | 1219 | vcaballero | |
261 | 312 | fernando | dataSource.clearSelection(); |
262 | 884 | fernando | dataSource.setSelection(selection); |
263 | 312 | fernando | } |
264 | 1219 | vcaballero | |
265 | 312 | fernando | /**
|
266 | 855 | fernando | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
267 | 312 | fernando | */
|
268 | 884 | fernando | private long[] doSet(String expression) { |
269 | try {
|
||
270 | 1830 | fernando | DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression, DataSourceFactory.MANUAL_MODE);
|
271 | 1219 | vcaballero | |
272 | 884 | fernando | return ds.getWhereFilter();
|
273 | } catch (DriverLoadException e) {
|
||
274 | NotificationManager.addError("Error cargando el driver", e);
|
||
275 | 1830 | fernando | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
276 | 884 | fernando | NotificationManager.addError("Error accediendo al driver", e);
|
277 | } catch (ParseException e) { |
||
278 | NotificationManager.addError("Parse error", e);
|
||
279 | } catch (SemanticException e) {
|
||
280 | NotificationManager.addError(e.getMessage(), e); |
||
281 | } catch (IOException e) { |
||
282 | NotificationManager.addError("GDBMS internal error", e);
|
||
283 | 312 | fernando | } |
284 | 1219 | vcaballero | |
285 | 884 | fernando | return null; |
286 | 312 | fernando | } |
287 | |||
288 | 1219 | vcaballero | /**
|
289 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
|
||
290 | */
|
||
291 | public void addToSet(String expression) { |
||
292 | 884 | fernando | long[] sel = doSet(expression); |
293 | 1219 | vcaballero | |
294 | if (sel == null) { |
||
295 | throw new RuntimeException("Not a 'where' clause?"); |
||
296 | } |
||
297 | |||
298 | 884 | fernando | FBitSet selection = new FBitSet();
|
299 | 1219 | vcaballero | |
300 | 884 | fernando | for (int i = 0; i < sel.length; i++) { |
301 | selection.set((int) sel[i]);
|
||
302 | } |
||
303 | 1219 | vcaballero | |
304 | 884 | fernando | FBitSet fbs = dataSource.getSelection(); |
305 | fbs.or(selection); |
||
306 | dataSource.setSelection(fbs); |
||
307 | 1219 | vcaballero | } |
308 | 312 | fernando | |
309 | 1219 | vcaballero | /**
|
310 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
|
||
311 | */
|
||
312 | public void fromSet(String expression) { |
||
313 | 884 | fernando | long[] sel = doSet(expression); |
314 | 1219 | vcaballero | |
315 | if (sel == null) { |
||
316 | throw new RuntimeException("Not a 'where' clause?"); |
||
317 | } |
||
318 | |||
319 | 884 | fernando | FBitSet selection = new FBitSet();
|
320 | 1219 | vcaballero | |
321 | 884 | fernando | for (int i = 0; i < sel.length; i++) { |
322 | selection.set((int) sel[i]);
|
||
323 | 312 | fernando | } |
324 | 1219 | vcaballero | |
325 | 884 | fernando | FBitSet fbs = dataSource.getSelection(); |
326 | fbs.and(selection); |
||
327 | dataSource.setSelection(fbs); |
||
328 | 1219 | vcaballero | } |
329 | 312 | fernando | |
330 | /**
|
||
331 | * @see com.iver.mdiApp.plugins.Extension#isVisible()
|
||
332 | */
|
||
333 | public boolean isVisible() { |
||
334 | 855 | fernando | View v = PluginServices.getMDIManager().getActiveView();
|
335 | 312 | fernando | |
336 | 855 | fernando | if (v == null) { |
337 | 312 | fernando | return false; |
338 | } |
||
339 | 1219 | vcaballero | |
340 | 855 | fernando | if (v.getClass() == Table.class) {
|
341 | 312 | fernando | return true; |
342 | 1219 | vcaballero | } else {
|
343 | if (v instanceof com.iver.cit.gvsig.gui.View) { |
||
344 | 855 | fernando | com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v; |
345 | ProjectView pv = view.getModel(); |
||
346 | 1219 | vcaballero | FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
347 | .getActives(); |
||
348 | |||
349 | if (seleccionadas.length == 1) { |
||
350 | if (seleccionadas[0] instanceof AlphanumericData) { |
||
351 | 855 | fernando | return true; |
352 | } |
||
353 | 312 | fernando | } |
354 | } |
||
355 | 1219 | vcaballero | |
356 | 312 | fernando | return false; |
357 | } |
||
358 | } |
||
359 | |||
360 | 596 | fernando | /**
|
361 | * @see com.iver.andami.plugins.Extension#inicializar()
|
||
362 | */
|
||
363 | public void inicializar() { |
||
364 | } |
||
365 | |||
366 | /**
|
||
367 | * @see com.iver.andami.plugins.Extension#isEnabled()
|
||
368 | */
|
||
369 | public boolean isEnabled() { |
||
370 | 855 | fernando | return true; |
371 | 596 | fernando | } |
372 | 312 | fernando | } |