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