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