svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 28890
History | View | Annotate | Download (12.3 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 | 22477 | vcaballero | import java.awt.Component; |
44 | 25750 | vcaballero | import java.awt.Dimension; |
45 | 1830 | fernando | |
46 | 22477 | vcaballero | import javax.swing.JOptionPane; |
47 | |||
48 | 25068 | vcaballero | import org.gvsig.fmap.dal.DALLocator; |
49 | 25750 | vcaballero | import org.gvsig.fmap.dal.DataTypes; |
50 | 24759 | jmvivo | import org.gvsig.fmap.dal.exception.DataException; |
51 | 27525 | jmvivo | import org.gvsig.fmap.dal.feature.DisposableIterator; |
52 | 24759 | jmvivo | import org.gvsig.fmap.dal.feature.Feature; |
53 | 25750 | vcaballero | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
54 | 25089 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureQuery; |
55 | import org.gvsig.fmap.dal.feature.FeatureSelection; |
||
56 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
57 | 24759 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureStore; |
58 | 25750 | vcaballero | import org.gvsig.project.document.table.FeatureTableDocument; |
59 | import org.gvsig.project.document.table.FeatureTableDocumentFactory; |
||
60 | 25068 | vcaballero | import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel; |
61 | 24759 | jmvivo | import org.opengis.feature.FeatureCollection; |
62 | 20994 | jmvivo | |
63 | 855 | fernando | import com.iver.andami.PluginServices; |
64 | 25750 | vcaballero | import com.iver.andami.messages.NotificationManager; |
65 | 596 | fernando | import com.iver.andami.plugins.Extension; |
66 | 6877 | cesar | import com.iver.andami.ui.mdiManager.IWindow; |
67 | 855 | fernando | import com.iver.cit.gvsig.gui.filter.ExpressionListener; |
68 | 26325 | jmvivo | import com.iver.cit.gvsig.project.documents.gui.AndamiWizard; |
69 | import com.iver.cit.gvsig.project.documents.gui.ObjectSelectionStep; |
||
70 | 25750 | vcaballero | import com.iver.cit.gvsig.project.documents.table.FieldSelectionModel; |
71 | import com.iver.cit.gvsig.project.documents.table.TableSelectionModel; |
||
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 | 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 | 5005 | jorpiell | public class TableOperations extends Extension implements ExpressionListener { |
84 | 21665 | vcaballero | private FeatureStore featureStore = null; |
85 | 312 | fernando | |
86 | 1219 | vcaballero | /**
|
87 | 5005 | jorpiell | * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
|
88 | 1219 | vcaballero | */
|
89 | public void execute(String actionCommand) { |
||
90 | 28890 | jpiera | ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
91 | com.iver.cit.gvsig.project.Project project=pe.getProject(); |
||
92 | FeatureTableDocument[] pts = project.getDocumentsByType(FeatureTableDocumentFactory.registerName)
|
||
93 | .toArray(new FeatureTableDocument[0]); |
||
94 | if ("LINK".equals(actionCommand)) { |
||
95 | 25750 | vcaballero | try {
|
96 | final ObjectSelectionStep sourceTable = new ObjectSelectionStep(); |
||
97 | sourceTable.setModel(new TableSelectionModel(pts,
|
||
98 | PluginServices.getText(this, "seleccione_tabla_origen"))); |
||
99 | |||
100 | final ObjectSelectionStep targetTable = new ObjectSelectionStep(); |
||
101 | targetTable.setModel(new TableSelectionModel(pts,
|
||
102 | 28890 | jpiera | PluginServices.getText(this, "seleccione_tabla_a_enlazar"))); |
103 | 25750 | vcaballero | |
104 | final ObjectSelectionStep firstTableField = new ObjectSelectionStep(); |
||
105 | final ObjectSelectionStep secondTableField = new ObjectSelectionStep(); |
||
106 | final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel")); |
||
107 | wiz.setSize(new Dimension(450,200)); |
||
108 | wiz.addStep(sourceTable); |
||
109 | wiz.addStep(firstTableField); |
||
110 | wiz.addStep(targetTable); |
||
111 | wiz.addStep(secondTableField); |
||
112 | |||
113 | wiz.addWizardListener(new WizardListener() {
|
||
114 | 28890 | jpiera | public void cancel(WizardEvent w) { |
115 | PluginServices.getMDIManager().closeWindow(wiz); |
||
116 | } |
||
117 | 25750 | vcaballero | |
118 | 28890 | jpiera | public void finished(WizardEvent w) { |
119 | PluginServices.getMDIManager().closeWindow(wiz); |
||
120 | 25750 | vcaballero | |
121 | 28890 | jpiera | FeatureTableDocument sourceProjectTable = (FeatureTableDocument) sourceTable.getSelected(); |
122 | FeatureStore sds1 = sourceProjectTable.getStore(); |
||
123 | 25750 | vcaballero | |
124 | 28890 | jpiera | FeatureTableDocument targetProjectTable = (FeatureTableDocument) targetTable.getSelected(); |
125 | FeatureStore sds2 = targetProjectTable.getStore(); |
||
126 | 25750 | vcaballero | |
127 | 28890 | jpiera | String field1 = (String) firstTableField.getSelected(); |
128 | String field2 = (String) secondTableField.getSelected(); |
||
129 | sourceProjectTable.setLinkTable(sds2.getName(),field1,field2); |
||
130 | ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject().setLinkTable(); |
||
131 | 25750 | vcaballero | |
132 | 28890 | jpiera | } |
133 | 25750 | vcaballero | |
134 | 28890 | jpiera | public void next(WizardEvent w) { |
135 | WizardControl wiz = w.wizard; |
||
136 | wiz.enableBack(true);
|
||
137 | wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
|
||
138 | 25750 | vcaballero | |
139 | 28890 | jpiera | if (w.currentStep == 1) { |
140 | FeatureTableDocument pt = (FeatureTableDocument) sourceTable.getSelected(); |
||
141 | 25750 | vcaballero | |
142 | 28890 | jpiera | try {
|
143 | firstTableField.setModel(new FieldSelectionModel(
|
||
144 | pt.getStore(), |
||
145 | PluginServices.getText(this, "seleccione_campo_enlace"), |
||
146 | DataTypes.STRING)); |
||
147 | } catch (SelectionException e) {
|
||
148 | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
||
149 | 25750 | vcaballero | e); |
150 | 28890 | jpiera | } |
151 | } else if (w.currentStep == 3) { |
||
152 | try {
|
||
153 | //tabla
|
||
154 | FeatureTableDocument pt = (FeatureTableDocument) sourceTable.getSelected(); |
||
155 | 25750 | vcaballero | |
156 | 28890 | jpiera | //?ndice del campo
|
157 | FeatureStore fs = pt.getStore(); |
||
158 | String fieldName = (String) firstTableField.getSelected(); |
||
159 | int type = ((FeatureAttributeDescriptor)fs.getDefaultFeatureType().get(fieldName)).getDataType();
|
||
160 | 25750 | vcaballero | |
161 | 28890 | jpiera | secondTableField.setModel(new FieldSelectionModel(
|
162 | ((FeatureTableDocument) targetTable |
||
163 | .getSelected()) |
||
164 | .getStore(), |
||
165 | PluginServices.getText(this, "seleccione_campo_enlace"), |
||
166 | type)); |
||
167 | } catch (SelectionException e) {
|
||
168 | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
||
169 | 25750 | vcaballero | e); |
170 | 28890 | jpiera | } catch (DataException e) {
|
171 | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
||
172 | e); |
||
173 | 25750 | vcaballero | } |
174 | } |
||
175 | 28890 | jpiera | } |
176 | 25750 | vcaballero | |
177 | 28890 | jpiera | public void back(WizardEvent w) { |
178 | WizardControl wiz = w.wizard; |
||
179 | wiz.enableBack(true);
|
||
180 | wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
|
||
181 | } |
||
182 | }); |
||
183 | 25750 | vcaballero | project.setModified(true);
|
184 | PluginServices.getMDIManager().addWindow(wiz); |
||
185 | } catch (SelectionException e) {
|
||
186 | NotificationManager.addError("Error abriendo el asistente", e);
|
||
187 | } |
||
188 | 28890 | jpiera | } |
189 | 1219 | vcaballero | } |
190 | 312 | fernando | |
191 | 1219 | vcaballero | /**
|
192 | 855 | fernando | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
193 | 312 | fernando | */
|
194 | 22353 | jmvivo | public void newSet(String expression) throws DataException { |
195 | 22477 | vcaballero | // By Pablo: if no filter expression -> no element selected
|
196 | 9494 | ppiqueras | if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
197 | 22477 | vcaballero | try {
|
198 | 25089 | jmvivo | FeatureSet set = doSet(expression); |
199 | 9532 | caballero | |
200 | 25089 | jmvivo | if (set == null) { |
201 | 22477 | vcaballero | throw new RuntimeException("Not a 'where' clause?"); |
202 | } |
||
203 | 25089 | jmvivo | FeatureSelection newSel =featureStore.createFeatureSelection(); |
204 | newSel.select(set); |
||
205 | featureStore.setSelection(newSel); |
||
206 | set.dispose(); |
||
207 | 22477 | vcaballero | }catch(Exception e){ |
208 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta."); |
||
209 | 9494 | ppiqueras | } |
210 | 1219 | vcaballero | } |
211 | 9494 | ppiqueras | else {
|
212 | // By Pablo: if no expression -> no element selected
|
||
213 | 25089 | jmvivo | featureStore.getFeatureSelection().deselectAll(); |
214 | 884 | fernando | } |
215 | 312 | fernando | } |
216 | 1219 | vcaballero | |
217 | 312 | fernando | /**
|
218 | 855 | fernando | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
219 | 312 | fernando | */
|
220 | 25089 | jmvivo | private FeatureSet doSet(String expression) throws DataException { |
221 | FeatureQuery query = featureStore.createFeatureQuery(); |
||
222 | query |
||
223 | 28890 | jpiera | .setFilter(DALLocator.getDataManager().createExpresion( |
224 | expression)); |
||
225 | 25089 | jmvivo | return featureStore.getFeatureSet(query);
|
226 | 312 | fernando | } |
227 | 1219 | vcaballero | /**
|
228 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
|
||
229 | */
|
||
230 | 22353 | jmvivo | public void addToSet(String expression) throws DataException { |
231 | 22477 | vcaballero | // By Pablo: if no filter expression -> don't add more elements to set
|
232 | 9494 | ppiqueras | if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
233 | 25089 | jmvivo | FeatureSet set = null;
|
234 | set = doSet(expression); |
||
235 | 9532 | caballero | |
236 | 25089 | jmvivo | featureStore.getFeatureSelection().select(set); |
237 | set.dispose(); |
||
238 | 1219 | vcaballero | } |
239 | } |
||
240 | 312 | fernando | |
241 | 1219 | vcaballero | /**
|
242 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
|
||
243 | */
|
||
244 | 22353 | jmvivo | public void fromSet(String expression) throws DataException { |
245 | 22477 | vcaballero | // By Pablo: if no filter expression -> no element selected
|
246 | 27525 | jmvivo | if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
247 | 9532 | caballero | |
248 | 27525 | jmvivo | FeatureSet set = null;
|
249 | DisposableIterator iterator = null;
|
||
250 | try {
|
||
251 | set = doSet(expression); |
||
252 | 9532 | caballero | |
253 | 25089 | jmvivo | if (set == null) { |
254 | 22477 | vcaballero | throw new RuntimeException("Not a 'where' clause?"); |
255 | } |
||
256 | 27525 | jmvivo | FeatureCollection oldSelection = (FeatureCollection) featureStore |
257 | .getSelection(); |
||
258 | 9532 | caballero | |
259 | 25089 | jmvivo | FeatureSelection newSelection = featureStore |
260 | 28890 | jpiera | .createFeatureSelection(); |
261 | 27525 | jmvivo | iterator = set.iterator(); |
262 | 22477 | vcaballero | while (iterator.hasNext()) {
|
263 | 27525 | jmvivo | Feature feature = (Feature) iterator.next(); |
264 | if (oldSelection.contains(feature)) {
|
||
265 | 25089 | jmvivo | newSelection.select(feature); |
266 | 22477 | vcaballero | } |
267 | } |
||
268 | 25089 | jmvivo | featureStore.setSelection(newSelection); |
269 | 27525 | jmvivo | } finally {
|
270 | if (iterator != null){ |
||
271 | iterator.dispose(); |
||
272 | 22477 | vcaballero | } |
273 | 27525 | jmvivo | if (set != null){ |
274 | set.dispose(); |
||
275 | } |
||
276 | } |
||
277 | } else {
|
||
278 | // By Pablo: if no expression -> no element selected
|
||
279 | featureStore.setSelection(featureStore.createSelection()); |
||
280 | } |
||
281 | 1219 | vcaballero | } |
282 | 9532 | caballero | |
283 | 9494 | ppiqueras | /**
|
284 | * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
|
||
285 | 9532 | caballero | *
|
286 | 9494 | ppiqueras | * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
287 | * @param expression An string
|
||
288 | 9532 | caballero | * @return A boolean value
|
289 | 9494 | ppiqueras | */
|
290 | private boolean filterExpressionFromWhereIsEmpty(String expression) { |
||
291 | String subExpression = expression.trim();
|
||
292 | 9532 | caballero | int pos;
|
293 | |||
294 | 9494 | ppiqueras | // Remove last ';' if exists
|
295 | 22931 | jmvivo | if (subExpression.charAt(subExpression.length() -1) == ';') { |
296 | 9494 | ppiqueras | subExpression = subExpression.substring(0, subExpression.length() -1).trim(); |
297 | 22931 | jmvivo | } |
298 | 9532 | caballero | |
299 | 9494 | ppiqueras | // If there is no 'where' clause
|
300 | 22931 | jmvivo | if ((pos = subExpression.indexOf("where")) == -1) { |
301 | 9494 | ppiqueras | return false; |
302 | 22931 | jmvivo | } |
303 | 9532 | caballero | |
304 | 9494 | ppiqueras | // If there is no subexpression in the WHERE clause -> true
|
305 | subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where' |
||
306 | 22931 | jmvivo | if ( subExpression.length() == 0 ) { |
307 | 9494 | ppiqueras | return true; |
308 | 22931 | jmvivo | } else {
|
309 | 9494 | ppiqueras | return false; |
310 | 22931 | jmvivo | } |
311 | 9494 | ppiqueras | } |
312 | 312 | fernando | |
313 | /**
|
||
314 | 5005 | jorpiell | * @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
315 | 312 | fernando | */
|
316 | public boolean isVisible() { |
||
317 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
318 | 312 | fernando | |
319 | 855 | fernando | if (v == null) { |
320 | 312 | fernando | return false; |
321 | } |
||
322 | 1219 | vcaballero | |
323 | 25068 | vcaballero | if (v instanceof FeatureTableDocumentPanel) { |
324 | 312 | fernando | return true; |
325 | 2183 | fernando | } /*else {
|
326 | 1219 | vcaballero | if (v instanceof com.iver.cit.gvsig.gui.View) {
|
327 | 855 | fernando | com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
|
328 | ProjectView pv = view.getModel();
|
||
329 | 1219 | vcaballero | FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
330 | .getActives();
|
||
331 | |||
332 | if (seleccionadas.length == 1) {
|
||
333 | if (seleccionadas[0] instanceof AlphanumericData) {
|
||
334 | 855 | fernando | return true;
|
335 | }
|
||
336 | 312 | fernando | }
|
337 | }
|
||
338 | 28890 | jpiera | */
|
339 | return false; |
||
340 | 2183 | fernando | //}
|
341 | 312 | fernando | } |
342 | |||
343 | 596 | fernando | /**
|
344 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#initialize()
|
345 | 596 | fernando | */
|
346 | 5005 | jorpiell | public void initialize() { |
347 | 14821 | jmvivo | registerIcons(); |
348 | 28890 | jpiera | // FIXME
|
349 | // org.gvsig.fmap.data.feature.joinstore.Register.selfRegister();
|
||
350 | 22477 | vcaballero | |
351 | |||
352 | 596 | fernando | } |
353 | |||
354 | 14821 | jmvivo | private void registerIcons(){ |
355 | 15647 | jmvivo | PluginServices.getIconTheme().registerDefault( |
356 | 14821 | jmvivo | "table-join",
|
357 | this.getClass().getClassLoader().getResource("images/tablejoin.png") |
||
358 | 28890 | jpiera | ); |
359 | 14821 | jmvivo | |
360 | 15647 | jmvivo | PluginServices.getIconTheme().registerDefault( |
361 | 14821 | jmvivo | "table-link",
|
362 | this.getClass().getClassLoader().getResource("images/tablelink.png") |
||
363 | 28890 | jpiera | ); |
364 | 14821 | jmvivo | } |
365 | |||
366 | 596 | fernando | /**
|
367 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
368 | 596 | fernando | */
|
369 | public boolean isEnabled() { |
||
370 | 855 | fernando | return true; |
371 | 596 | fernando | } |
372 | 26325 | jmvivo | |
373 | 26241 | vcaballero | /**
|
374 | * Ensure that field name only has 'safe' characters
|
||
375 | * (no spaces, special characters, etc).
|
||
376 | */
|
||
377 | public String sanitizeFieldName(String fieldName) { |
||
378 | return fieldName.replaceAll("\\W", "_"); // replace any non-word character by an underscore |
||
379 | } |
||
380 | 3940 | caballero | |
381 | 26241 | vcaballero | |
382 | 312 | fernando | } |