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