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