root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 22932
History | View | Annotate | Download (16.6 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.awt.Component; |
44 |
import java.awt.Dimension; |
45 |
import java.util.Iterator; |
46 |
|
47 |
import javax.swing.JOptionPane; |
48 |
|
49 |
import org.gvsig.fmap.data.DataException; |
50 |
import org.gvsig.fmap.data.DataManager; |
51 |
import org.gvsig.fmap.data.ReadException; |
52 |
import org.gvsig.fmap.data.feature.Feature; |
53 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
54 |
import org.gvsig.fmap.data.feature.FeatureStore; |
55 |
import org.gvsig.fmap.data.feature.MemoryFeatureCollection; |
56 |
import org.gvsig.fmap.data.feature.joinstore.JoinFeatureStore; |
57 |
import org.gvsig.fmap.data.feature.joinstore.JoinFeatureStoreParameters; |
58 |
|
59 |
import com.iver.andami.PluginServices; |
60 |
import com.iver.andami.messages.NotificationManager; |
61 |
import com.iver.andami.plugins.Extension; |
62 |
import com.iver.andami.ui.mdiManager.IWindow; |
63 |
import com.iver.cit.gvsig.gui.filter.ExpressionListener; |
64 |
import com.iver.cit.gvsig.project.Project; |
65 |
import com.iver.cit.gvsig.project.documents.table.FieldSelectionModel; |
66 |
import com.iver.cit.gvsig.project.documents.table.ObjectSelectionStep; |
67 |
import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
68 |
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory; |
69 |
import com.iver.cit.gvsig.project.documents.table.TableSelectionModel; |
70 |
import com.iver.cit.gvsig.project.documents.table.gui.AndamiWizard; |
71 |
import com.iver.cit.gvsig.project.documents.table.gui.Table; |
72 |
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 |
|
77 |
|
78 |
/**
|
79 |
* Extensi?n que controla las operaciones realizadas sobre las tablas.
|
80 |
*
|
81 |
* @author Fernando Gonz?lez Cort?s
|
82 |
*/
|
83 |
public class TableOperations extends Extension implements ExpressionListener { |
84 |
private FeatureStore featureStore = null; |
85 |
//private Table vista;
|
86 |
|
87 |
/**
|
88 |
* @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
|
89 |
*/
|
90 |
public void execute(String actionCommand) { |
91 |
ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
92 |
Project project=pe.getProject(); |
93 |
ProjectTable[] pts = project.getDocumentsByType(ProjectTableFactory.registerName)
|
94 |
.toArray(new ProjectTable[0]); |
95 |
if ("JOIN".equals(actionCommand)) { |
96 |
try {
|
97 |
final ObjectSelectionStep sourceTable = new ObjectSelectionStep(); |
98 |
sourceTable.setModel(new TableSelectionModel(pts,
|
99 |
PluginServices.getText(this, "seleccione_tabla_origen"))); |
100 |
|
101 |
final ObjectSelectionStep targetTable = new ObjectSelectionStep(); |
102 |
targetTable.setModel(new TableSelectionModel(pts,
|
103 |
PluginServices.getText(this, "seleccione_tabla_a_enlazar"))); |
104 |
|
105 |
final ObjectSelectionStep firstTableField = new ObjectSelectionStep(); |
106 |
final ObjectSelectionStep secondTableField = new ObjectSelectionStep(); |
107 |
final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel")); |
108 |
wiz.setSize(new Dimension(450,200)); |
109 |
wiz.addStep(sourceTable); |
110 |
wiz.addStep(firstTableField); |
111 |
wiz.addStep(targetTable); |
112 |
wiz.addStep(secondTableField); |
113 |
|
114 |
wiz.addWizardListener(new WizardListener() {
|
115 |
public void cancel(WizardEvent w) { |
116 |
PluginServices.getMDIManager().closeWindow(wiz); |
117 |
} |
118 |
|
119 |
public void finished(WizardEvent w) { |
120 |
PluginServices.getMDIManager().closeWindow(wiz); |
121 |
|
122 |
ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected(); |
123 |
FeatureStore fs1 = sourceProjectTable.getModel(); |
124 |
String tableName1 = fs1.getName();
|
125 |
|
126 |
ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected(); |
127 |
FeatureStore fs2 = targetProjectTable.getModel(); |
128 |
|
129 |
String tableName2 = fs2.getName();
|
130 |
|
131 |
String field1 = (String) firstTableField.getSelected(); |
132 |
String field2 = (String) secondTableField.getSelected(); |
133 |
|
134 |
String sql =
|
135 |
"custom com_iver_cit_gvsig_arcjoin tables '" +
|
136 |
tableName1 + "', '" + tableName2 + "' values(" + |
137 |
field1 + ", " + field2 + ");"; |
138 |
System.out.println(sql);
|
139 |
|
140 |
try {
|
141 |
DataManager dm=DataManager.getManager(); |
142 |
JoinFeatureStoreParameters jfsp=(JoinFeatureStoreParameters)dm.createDataStoreParameters(JoinFeatureStore.DATASTORE_NAME); |
143 |
jfsp.setStorePrimary(fs1.getParameters()); |
144 |
jfsp.setStoreSecondary(fs2.getParameters()); |
145 |
jfsp.setDefaultGeometry(fs1.getDefaultFeatureType().getDefaultGeometry()); |
146 |
jfsp.setFieldNamePrefix("Join_");
|
147 |
jfsp.setLinkFieldPrimary(field1); |
148 |
jfsp.setLinkFieldSecondary(field2); |
149 |
|
150 |
JoinFeatureStore jfs=(JoinFeatureStore)dm.createDataStore(jfsp); |
151 |
featureStore=jfs; |
152 |
ProjectTable projectTable = (ProjectTable) sourceTable.getSelected(); |
153 |
FeatureStore original = projectTable.getModel(); |
154 |
projectTable.setModel(jfs); |
155 |
|
156 |
original.dispose(); |
157 |
|
158 |
IWindow[] windows=PluginServices.getMDIManager().getAllWindows();
|
159 |
Table tableWindow; |
160 |
for (int i = 0; i < windows.length; i++) { |
161 |
if (windows[i] instanceof Table){ |
162 |
tableWindow = (Table) windows[i]; |
163 |
if (tableWindow.getModel().equals(
|
164 |
projectTable)) { |
165 |
tableWindow.setModel(projectTable); |
166 |
} |
167 |
} |
168 |
} |
169 |
} catch (ReadException e) {
|
170 |
NotificationManager.addError("Error leyendo del driver",
|
171 |
e); |
172 |
} |
173 |
} |
174 |
|
175 |
public void next(WizardEvent w) { |
176 |
WizardControl wiz = w.wizard; |
177 |
wiz.enableBack(true);
|
178 |
wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
|
179 |
|
180 |
if (w.currentStep == 1) { |
181 |
ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
182 |
|
183 |
try {
|
184 |
firstTableField.setModel(new FieldSelectionModel(
|
185 |
pt.getModel(), |
186 |
PluginServices.getText(this, "seleccione_campo_enlace"), |
187 |
null));
|
188 |
} catch (SelectionException e) {
|
189 |
NotificationManager.addError("Error obteniendo los campos de la tabla",
|
190 |
e); |
191 |
} |
192 |
} else if (w.currentStep == 3) { |
193 |
try {
|
194 |
//tabla
|
195 |
ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
196 |
|
197 |
//?ndice del campo
|
198 |
FeatureStore fs = pt.getModel(); |
199 |
String fieldName = (String) firstTableField.getSelected(); |
200 |
String type = fs.getDefaultFeatureType().get(fieldName).getDataType();
|
201 |
|
202 |
|
203 |
secondTableField.setModel(new FieldSelectionModel(
|
204 |
((ProjectTable) targetTable |
205 |
.getSelected()) |
206 |
.getModel(), |
207 |
PluginServices.getText(this, "seleccione_campo_enlace"), |
208 |
type)); |
209 |
} catch (SelectionException e) {
|
210 |
NotificationManager.addError("Error obteniendo los campos de la tabla",
|
211 |
e); |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
public void back(WizardEvent w) { |
217 |
WizardControl wiz = w.wizard; |
218 |
wiz.enableBack(true);
|
219 |
wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
|
220 |
} |
221 |
}); |
222 |
project.setModified(true);
|
223 |
PluginServices.getMDIManager().addWindow(wiz); |
224 |
} catch (SelectionException e) {
|
225 |
NotificationManager.addError("Error abriendo el asistente", e);
|
226 |
} |
227 |
}else if ("LINK".equals(actionCommand)) { |
228 |
try {
|
229 |
final ObjectSelectionStep sourceTable = new ObjectSelectionStep(); |
230 |
sourceTable.setModel(new TableSelectionModel(pts,
|
231 |
PluginServices.getText(this, "seleccione_tabla_origen"))); |
232 |
|
233 |
final ObjectSelectionStep targetTable = new ObjectSelectionStep(); |
234 |
targetTable.setModel(new TableSelectionModel(pts,
|
235 |
PluginServices.getText(this, "seleccione_tabla_a_enlazar"))); |
236 |
|
237 |
final ObjectSelectionStep firstTableField = new ObjectSelectionStep(); |
238 |
final ObjectSelectionStep secondTableField = new ObjectSelectionStep(); |
239 |
final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel")); |
240 |
wiz.setSize(new Dimension(450,200)); |
241 |
wiz.addStep(sourceTable); |
242 |
wiz.addStep(firstTableField); |
243 |
wiz.addStep(targetTable); |
244 |
wiz.addStep(secondTableField); |
245 |
|
246 |
wiz.addWizardListener(new WizardListener() {
|
247 |
public void cancel(WizardEvent w) { |
248 |
PluginServices.getMDIManager().closeWindow(wiz); |
249 |
} |
250 |
|
251 |
public void finished(WizardEvent w) { |
252 |
PluginServices.getMDIManager().closeWindow(wiz); |
253 |
|
254 |
ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected(); |
255 |
FeatureStore sds1 = sourceProjectTable.getModel(); |
256 |
|
257 |
ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected(); |
258 |
FeatureStore sds2 = targetProjectTable.getModel(); |
259 |
|
260 |
String field1 = (String) firstTableField.getSelected(); |
261 |
String field2 = (String) secondTableField.getSelected(); |
262 |
sourceProjectTable.setLinkTable(sds2.getName(),field1,field2); |
263 |
((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject().setLinkTable(); |
264 |
|
265 |
} |
266 |
|
267 |
public void next(WizardEvent w) { |
268 |
WizardControl wiz = w.wizard; |
269 |
wiz.enableBack(true);
|
270 |
wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
|
271 |
|
272 |
if (w.currentStep == 1) { |
273 |
ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
274 |
|
275 |
try {
|
276 |
firstTableField.setModel(new FieldSelectionModel(
|
277 |
pt.getModel(), |
278 |
PluginServices.getText(this, "seleccione_campo_enlace"), |
279 |
null));
|
280 |
} catch (SelectionException e) {
|
281 |
NotificationManager.addError("Error obteniendo los campos de la tabla",
|
282 |
e); |
283 |
} |
284 |
} else if (w.currentStep == 3) { |
285 |
try {
|
286 |
//tabla
|
287 |
ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
288 |
|
289 |
//?ndice del campo
|
290 |
FeatureStore fs = pt.getModel(); |
291 |
String fieldName = (String) firstTableField.getSelected(); |
292 |
String type = fs.getDefaultFeatureType().get(fieldName).getDataType();
|
293 |
|
294 |
secondTableField.setModel(new FieldSelectionModel(
|
295 |
((ProjectTable) targetTable |
296 |
.getSelected()) |
297 |
.getModel(), |
298 |
PluginServices.getText(this, "seleccione_campo_enlace"), |
299 |
type)); |
300 |
} catch (SelectionException e) {
|
301 |
NotificationManager.addError("Error obteniendo los campos de la tabla",
|
302 |
e); |
303 |
} |
304 |
} |
305 |
} |
306 |
|
307 |
public void back(WizardEvent w) { |
308 |
WizardControl wiz = w.wizard; |
309 |
wiz.enableBack(true);
|
310 |
wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
|
311 |
} |
312 |
}); |
313 |
project.setModified(true);
|
314 |
PluginServices.getMDIManager().addWindow(wiz); |
315 |
} catch (SelectionException e) {
|
316 |
NotificationManager.addError("Error abriendo el asistente", e);
|
317 |
} |
318 |
} |
319 |
} |
320 |
|
321 |
/**
|
322 |
* @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
323 |
*/
|
324 |
public void newSet(String expression) throws DataException { |
325 |
// By Pablo: if no filter expression -> no element selected
|
326 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
327 |
try {
|
328 |
FeatureCollection sel = doSet(expression); |
329 |
|
330 |
if (sel == null) { |
331 |
throw new RuntimeException("Not a 'where' clause?"); |
332 |
} |
333 |
featureStore.setSelection(sel); |
334 |
}catch(Exception e){ |
335 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta."); |
336 |
} |
337 |
} |
338 |
else {
|
339 |
// By Pablo: if no expression -> no element selected
|
340 |
featureStore.getSelection().clear(); |
341 |
} |
342 |
} |
343 |
|
344 |
/**
|
345 |
* @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
346 |
*/
|
347 |
private FeatureCollection doSet(String expression) throws ReadException { |
348 |
return (FeatureCollection)featureStore.getDataCollection(featureStore.getDefaultFeatureType(),expression,null); |
349 |
} |
350 |
/**
|
351 |
* @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
|
352 |
*/
|
353 |
public void addToSet(String expression) throws DataException { |
354 |
// By Pablo: if no filter expression -> don't add more elements to set
|
355 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
356 |
FeatureCollection sel=null;
|
357 |
try {
|
358 |
sel = doSet(expression); |
359 |
} catch (ReadException e) {
|
360 |
throw new DataException("Expression",e); |
361 |
} |
362 |
|
363 |
if (sel == null) { |
364 |
throw new RuntimeException("Not a 'where' clause?"); |
365 |
} |
366 |
FeatureCollection oldSelection=(FeatureCollection)featureStore.getSelection(); |
367 |
sel.addAll(oldSelection); |
368 |
featureStore.setSelection(sel); |
369 |
} |
370 |
} |
371 |
|
372 |
/**
|
373 |
* @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
|
374 |
*/
|
375 |
public void fromSet(String expression) throws DataException { |
376 |
// By Pablo: if no filter expression -> no element selected
|
377 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
378 |
|
379 |
FeatureCollection sel=null;
|
380 |
sel = doSet(expression); |
381 |
|
382 |
if (sel == null) { |
383 |
throw new RuntimeException("Not a 'where' clause?"); |
384 |
} |
385 |
FeatureCollection oldSelection=(FeatureCollection)featureStore.getSelection(); |
386 |
|
387 |
MemoryFeatureCollection mfc=new MemoryFeatureCollection(featureStore);
|
388 |
Iterator iterator=sel.iterator();
|
389 |
while (iterator.hasNext()) {
|
390 |
Feature feature = (Feature) iterator.next(); |
391 |
if (oldSelection.contains(feature)){
|
392 |
mfc.add(feature); |
393 |
} |
394 |
} |
395 |
featureStore.setSelection(mfc); |
396 |
} else {
|
397 |
// By Pablo: if no expression -> no element selected
|
398 |
featureStore.setSelection(featureStore.createSelection()); |
399 |
} |
400 |
} |
401 |
|
402 |
/**
|
403 |
* Returns true if the WHERE subconsultation of the filterExpression is empty ("")
|
404 |
*
|
405 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
406 |
* @param expression An string
|
407 |
* @return A boolean value
|
408 |
*/
|
409 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
410 |
String subExpression = expression.trim();
|
411 |
int pos;
|
412 |
|
413 |
// Remove last ';' if exists
|
414 |
if (subExpression.charAt(subExpression.length() -1) == ';') { |
415 |
subExpression = subExpression.substring(0, subExpression.length() -1).trim(); |
416 |
} |
417 |
|
418 |
// If there is no 'where' clause
|
419 |
if ((pos = subExpression.indexOf("where")) == -1) { |
420 |
return false; |
421 |
} |
422 |
|
423 |
// If there is no subexpression in the WHERE clause -> true
|
424 |
subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where' |
425 |
if ( subExpression.length() == 0 ) { |
426 |
return true; |
427 |
} else {
|
428 |
return false; |
429 |
} |
430 |
} |
431 |
|
432 |
/**
|
433 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
434 |
*/
|
435 |
public boolean isVisible() { |
436 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
437 |
|
438 |
if (v == null) { |
439 |
return false; |
440 |
} |
441 |
|
442 |
if (v instanceof Table) { |
443 |
return true; |
444 |
} /*else {
|
445 |
if (v instanceof com.iver.cit.gvsig.gui.View) {
|
446 |
com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
|
447 |
ProjectView pv = view.getModel();
|
448 |
FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
449 |
.getActives();
|
450 |
|
451 |
if (seleccionadas.length == 1) {
|
452 |
if (seleccionadas[0] instanceof AlphanumericData) {
|
453 |
return true;
|
454 |
}
|
455 |
}
|
456 |
}
|
457 |
*/
|
458 |
return false; |
459 |
//}
|
460 |
} |
461 |
|
462 |
/**
|
463 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
464 |
*/
|
465 |
public void initialize() { |
466 |
registerIcons(); |
467 |
org.gvsig.fmap.data.feature.joinstore.Register.selfRegister(); |
468 |
|
469 |
|
470 |
} |
471 |
|
472 |
private void registerIcons(){ |
473 |
PluginServices.getIconTheme().registerDefault( |
474 |
"table-join",
|
475 |
this.getClass().getClassLoader().getResource("images/tablejoin.png") |
476 |
); |
477 |
|
478 |
PluginServices.getIconTheme().registerDefault( |
479 |
"table-link",
|
480 |
this.getClass().getClassLoader().getResource("images/tablelink.png") |
481 |
); |
482 |
} |
483 |
|
484 |
/**
|
485 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
486 |
*/
|
487 |
public boolean isEnabled() { |
488 |
return true; |
489 |
} |
490 |
|
491 |
|
492 |
} |