svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / TableOperations.java @ 40558
History | View | Annotate | Download (15.8 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.extension; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.awt.Dimension; |
28 |
import java.util.List; |
29 |
|
30 |
import javax.swing.JOptionPane; |
31 |
|
32 |
import org.gvsig.andami.IconThemeHelper; |
33 |
import org.gvsig.andami.PluginServices; |
34 |
import org.gvsig.andami.messages.NotificationManager; |
35 |
import org.gvsig.andami.plugins.Extension; |
36 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
37 |
import org.gvsig.app.gui.filter.ExpressionListener; |
38 |
import org.gvsig.app.project.Project; |
39 |
import org.gvsig.app.project.ProjectManager; |
40 |
import org.gvsig.app.project.documents.Document; |
41 |
import org.gvsig.app.project.documents.gui.AndamiWizard; |
42 |
import org.gvsig.app.project.documents.gui.ObjectSelectionStep; |
43 |
import org.gvsig.app.project.documents.table.FieldSelectionModel; |
44 |
import org.gvsig.app.project.documents.table.TableDocument; |
45 |
import org.gvsig.app.project.documents.table.TableManager; |
46 |
import org.gvsig.app.project.documents.table.TableSelectionModel; |
47 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
48 |
import org.gvsig.fmap.dal.DALLocator; |
49 |
import org.gvsig.fmap.dal.exception.DataException; |
50 |
import org.gvsig.fmap.dal.feature.Feature; |
51 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
52 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
53 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
54 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
55 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
56 |
import org.gvsig.tools.dispose.DisposableIterator; |
57 |
import org.gvsig.tools.dispose.DisposeUtils; |
58 |
import org.gvsig.utils.swing.objectSelection.SelectionException; |
59 |
import org.gvsig.utils.swing.wizard.WizardControl; |
60 |
import org.gvsig.utils.swing.wizard.WizardEvent; |
61 |
import org.gvsig.utils.swing.wizard.WizardListener; |
62 |
|
63 |
/**
|
64 |
* Extensi?n que controla las operaciones realizadas sobre las tablas.
|
65 |
*
|
66 |
* @author Fernando Gonz?lez Cort?s
|
67 |
*/
|
68 |
public class TableOperations extends Extension implements ExpressionListener { |
69 |
|
70 |
private FeatureStore featureStore = null; |
71 |
|
72 |
public void execute(String actionCommand) { |
73 |
final Project project =
|
74 |
ProjectManager.getInstance().getCurrentProject(); |
75 |
List<Document> tableDcouments = |
76 |
project.getDocuments(TableManager.TYPENAME); |
77 |
TableDocument[] pts =
|
78 |
tableDcouments |
79 |
.toArray(new TableDocument[tableDcouments.size()]);
|
80 |
|
81 |
if ("table-create-link".equals(actionCommand)) { |
82 |
try {
|
83 |
final ObjectSelectionStep sourceTable =
|
84 |
new ObjectSelectionStep();
|
85 |
sourceTable.setModel(new TableSelectionModel(pts,
|
86 |
PluginServices.getText(this, "seleccione_tabla_origen"))); |
87 |
|
88 |
final ObjectSelectionStep targetTable =
|
89 |
new ObjectSelectionStep();
|
90 |
targetTable |
91 |
.setModel(new TableSelectionModel(pts, PluginServices
|
92 |
.getText(this, "seleccione_tabla_a_enlazar"))); |
93 |
|
94 |
final ObjectSelectionStep firstTableField =
|
95 |
new ObjectSelectionStep();
|
96 |
final ObjectSelectionStep secondTableField =
|
97 |
new ObjectSelectionStep();
|
98 |
final AndamiWizard wiz =
|
99 |
new AndamiWizard(PluginServices.getText(this, "back"), |
100 |
PluginServices.getText(this, "next"), |
101 |
PluginServices.getText(this, "finish"), |
102 |
PluginServices.getText(this, "cancel")); |
103 |
wiz.setSize(new Dimension(450, 200)); |
104 |
wiz.addStep(sourceTable); |
105 |
wiz.addStep(firstTableField); |
106 |
wiz.addStep(targetTable); |
107 |
wiz.addStep(secondTableField); |
108 |
|
109 |
wiz.addWizardListener(new WizardListener() {
|
110 |
|
111 |
public void cancel(WizardEvent w) { |
112 |
PluginServices.getMDIManager().closeWindow(wiz); |
113 |
} |
114 |
|
115 |
public void finished(WizardEvent w) { |
116 |
PluginServices.getMDIManager().closeWindow(wiz); |
117 |
|
118 |
TableDocument sourceProjectTable = |
119 |
(TableDocument) sourceTable.getSelected(); |
120 |
|
121 |
TableDocument targetProjectTable = |
122 |
(TableDocument) targetTable.getSelected(); |
123 |
FeatureStore sds2 = targetProjectTable.getStore(); |
124 |
|
125 |
String field1 = (String) firstTableField.getSelected(); |
126 |
String field2 = (String) secondTableField.getSelected(); |
127 |
sourceProjectTable.addLinkTable( |
128 |
targetProjectTable.getName(), field1, |
129 |
field2); |
130 |
|
131 |
} |
132 |
|
133 |
public void next(WizardEvent w) { |
134 |
WizardControl wiz = w.wizard; |
135 |
wiz.enableBack(true);
|
136 |
wiz.enableNext(((ObjectSelectionStep) wiz |
137 |
.getCurrentStep()).getSelectedItem() != null);
|
138 |
|
139 |
if (w.currentStep == 1) { |
140 |
TableDocument pt = |
141 |
(TableDocument) sourceTable.getSelected(); |
142 |
|
143 |
try {
|
144 |
firstTableField |
145 |
.setModel(new FieldSelectionModel(pt
|
146 |
.getStore(), PluginServices.getText( |
147 |
this, "seleccione_campo_enlace"))); |
148 |
} catch (SelectionException e) {
|
149 |
NotificationManager.addError( |
150 |
"Error obteniendo los campos de la tabla",
|
151 |
e); |
152 |
} |
153 |
} else
|
154 |
if (w.currentStep == 3) { |
155 |
try {
|
156 |
// tabla
|
157 |
TableDocument pt = |
158 |
(TableDocument) sourceTable |
159 |
.getSelected(); |
160 |
|
161 |
// ?ndice del campo
|
162 |
FeatureStore fs = pt.getStore(); |
163 |
String fieldName =
|
164 |
(String) firstTableField.getSelected();
|
165 |
int type =
|
166 |
((FeatureAttributeDescriptor) fs |
167 |
.getDefaultFeatureType().get( |
168 |
fieldName)).getType(); |
169 |
|
170 |
secondTableField |
171 |
.setModel(new FieldSelectionModel(
|
172 |
((TableDocument) targetTable |
173 |
.getSelected()).getStore(), |
174 |
PluginServices.getText(this,
|
175 |
"seleccione_campo_enlace"),
|
176 |
type)); |
177 |
} catch (SelectionException e) {
|
178 |
NotificationManager |
179 |
.addError( |
180 |
"Error obteniendo los campos de la tabla",
|
181 |
e); |
182 |
} catch (DataException e) {
|
183 |
NotificationManager |
184 |
.addError( |
185 |
"Error obteniendo los campos de la tabla",
|
186 |
e); |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
public void back(WizardEvent w) { |
192 |
WizardControl wiz = w.wizard; |
193 |
wiz.enableBack(true);
|
194 |
wiz.enableNext(((ObjectSelectionStep) wiz |
195 |
.getCurrentStep()).getSelectedItem() != null);
|
196 |
} |
197 |
}); |
198 |
project.setModified(true);
|
199 |
PluginServices.getMDIManager().addWindow(wiz); |
200 |
} catch (SelectionException e) {
|
201 |
NotificationManager.addError("Error abriendo el asistente", e);
|
202 |
} |
203 |
} |
204 |
} |
205 |
|
206 |
/**
|
207 |
* @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String)
|
208 |
*/
|
209 |
public void newSet(String expression) throws DataException { |
210 |
// By Pablo: if no filter expression -> no element selected
|
211 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
212 |
FeatureSet set = null;
|
213 |
try {
|
214 |
set = doSet(expression); |
215 |
|
216 |
if (set == null) { |
217 |
throw new RuntimeException("Not a 'where' clause?"); |
218 |
} |
219 |
FeatureSelection newSel = featureStore.createFeatureSelection(); |
220 |
newSel.select(set); |
221 |
featureStore.setSelection(newSel); |
222 |
} catch (Exception e) { |
223 |
JOptionPane.showMessageDialog(
|
224 |
(Component) PluginServices.getMainFrame(),
|
225 |
"Asegurate de que la consulta es correcta.");
|
226 |
} finally {
|
227 |
if (set != null) { |
228 |
set.dispose(); |
229 |
} |
230 |
} |
231 |
} else {
|
232 |
// By Pablo: if no expression -> no element selected
|
233 |
featureStore.getFeatureSelection().deselectAll(); |
234 |
} |
235 |
} |
236 |
|
237 |
/**
|
238 |
* @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String)
|
239 |
*/
|
240 |
private FeatureSet doSet(String expression) throws DataException { |
241 |
FeatureQuery query = featureStore.createFeatureQuery(); |
242 |
query |
243 |
.setFilter(DALLocator.getDataManager().createExpresion(expression)); |
244 |
return featureStore.getFeatureSet(query);
|
245 |
} |
246 |
|
247 |
/**
|
248 |
* @see org.gvsig.app.gui.filter.ExpressionListener#addToSet(java.lang.String)
|
249 |
*/
|
250 |
public void addToSet(String expression) throws DataException { |
251 |
// By Pablo: if no filter expression -> don't add more elements to set
|
252 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
253 |
FeatureSet set = null;
|
254 |
try {
|
255 |
set = doSet(expression); |
256 |
|
257 |
featureStore.getFeatureSelection().select(set); |
258 |
} finally {
|
259 |
if (set != null) { |
260 |
set.dispose(); |
261 |
} |
262 |
} |
263 |
} |
264 |
} |
265 |
|
266 |
/**
|
267 |
* @see org.gvsig.app.gui.filter.ExpressionListener#fromSet(java.lang.String)
|
268 |
*/
|
269 |
public void fromSet(String expression) throws DataException { |
270 |
// By Pablo: if no filter expression -> no element selected
|
271 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
272 |
|
273 |
FeatureSet set = null;
|
274 |
DisposableIterator iterator = null;
|
275 |
try {
|
276 |
set = doSet(expression); |
277 |
|
278 |
if (set == null) { |
279 |
throw new RuntimeException("Not a 'where' clause?"); |
280 |
} |
281 |
FeatureSelection oldSelection = |
282 |
featureStore.getFeatureSelection(); |
283 |
|
284 |
FeatureSelection newSelection = |
285 |
featureStore.createFeatureSelection(); |
286 |
iterator = set.fastIterator(); |
287 |
while (iterator.hasNext()) {
|
288 |
Feature feature = (Feature) iterator.next(); |
289 |
if (oldSelection.isSelected(feature)) {
|
290 |
newSelection.select(feature); |
291 |
} |
292 |
} |
293 |
featureStore.setSelection(newSelection); |
294 |
} finally {
|
295 |
DisposeUtils.dispose(iterator); |
296 |
DisposeUtils.dispose(set); |
297 |
} |
298 |
} else {
|
299 |
// By Pablo: if no expression -> no element selected
|
300 |
// featureStore.setSelection(featureStore.createSelection());
|
301 |
featureStore.getFeatureSelection().deselectAll(); |
302 |
} |
303 |
} |
304 |
|
305 |
/**
|
306 |
* Returns true if the WHERE subconsultation of the filterExpression is
|
307 |
* empty ("")
|
308 |
*
|
309 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
310 |
* @param expression
|
311 |
* An string
|
312 |
* @return A boolean value
|
313 |
*/
|
314 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
315 |
String subExpression = expression.trim();
|
316 |
int pos;
|
317 |
|
318 |
// Remove last ';' if exists
|
319 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
320 |
subExpression = |
321 |
subExpression.substring(0, subExpression.length() - 1).trim(); |
322 |
} |
323 |
|
324 |
// If there is no 'where' clause
|
325 |
if ((pos = subExpression.indexOf("where")) == -1) { |
326 |
return false; |
327 |
} |
328 |
|
329 |
// If there is no subexpression in the WHERE clause -> true
|
330 |
subExpression = |
331 |
subExpression.substring(pos + 5, subExpression.length()).trim(); // + |
332 |
// 5
|
333 |
// is
|
334 |
// the
|
335 |
// length
|
336 |
// of
|
337 |
// 'where'
|
338 |
if (subExpression.length() == 0) { |
339 |
return true; |
340 |
} else {
|
341 |
return false; |
342 |
} |
343 |
} |
344 |
|
345 |
/**
|
346 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
347 |
*/
|
348 |
public boolean isVisible() { |
349 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
350 |
|
351 |
if (v == null) { |
352 |
return false; |
353 |
} |
354 |
|
355 |
if (v instanceof FeatureTableDocumentPanel) { |
356 |
return true; |
357 |
} /*
|
358 |
* else {
|
359 |
* if (v instanceof com.iver.cit.gvsig.gui.View) {
|
360 |
* com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
|
361 |
* ProjectView pv = view.getModel();
|
362 |
* FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
363 |
* .getActives();
|
364 |
*
|
365 |
* if (seleccionadas.length == 1) {
|
366 |
* if (seleccionadas[0] instanceof AlphanumericData) {
|
367 |
* return true;
|
368 |
* }
|
369 |
* }
|
370 |
* }
|
371 |
*/
|
372 |
return false; |
373 |
// }
|
374 |
} |
375 |
|
376 |
/**
|
377 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
378 |
*/
|
379 |
public void initialize() { |
380 |
IconThemeHelper.registerIcon("action", "table-create-link", this); |
381 |
|
382 |
} |
383 |
|
384 |
/**
|
385 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
386 |
*/
|
387 |
public boolean isEnabled() { |
388 |
return true; |
389 |
} |
390 |
|
391 |
/**
|
392 |
* Ensure that field name only has 'safe' characters
|
393 |
* (no spaces, special characters, etc).
|
394 |
*/
|
395 |
public String sanitizeFieldName(String fieldName) { |
396 |
return fieldName.replaceAll("\\W", "_"); // replace any non-word |
397 |
// character by an underscore
|
398 |
} |
399 |
|
400 |
} |