svn-gvsig-desktop / tags / v2_0_0_Build_2051 / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / TableOperations.java @ 38747
History | View | Annotate | Download (15.7 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(sds2.getName(), field1, |
126 |
field2); |
127 |
|
128 |
} |
129 |
|
130 |
public void next(WizardEvent w) { |
131 |
WizardControl wiz = w.wizard; |
132 |
wiz.enableBack(true);
|
133 |
wiz.enableNext(((ObjectSelectionStep) wiz |
134 |
.getCurrentStep()).getSelectedItem() != null);
|
135 |
|
136 |
if (w.currentStep == 1) { |
137 |
TableDocument pt = |
138 |
(TableDocument) sourceTable.getSelected(); |
139 |
|
140 |
try {
|
141 |
firstTableField |
142 |
.setModel(new FieldSelectionModel(pt
|
143 |
.getStore(), PluginServices.getText( |
144 |
this, "seleccione_campo_enlace"))); |
145 |
} catch (SelectionException e) {
|
146 |
NotificationManager.addError( |
147 |
"Error obteniendo los campos de la tabla",
|
148 |
e); |
149 |
} |
150 |
} else
|
151 |
if (w.currentStep == 3) { |
152 |
try {
|
153 |
// tabla
|
154 |
TableDocument pt = |
155 |
(TableDocument) sourceTable |
156 |
.getSelected(); |
157 |
|
158 |
// ?ndice del campo
|
159 |
FeatureStore fs = pt.getStore(); |
160 |
String fieldName =
|
161 |
(String) firstTableField.getSelected();
|
162 |
int type =
|
163 |
((FeatureAttributeDescriptor) fs |
164 |
.getDefaultFeatureType().get( |
165 |
fieldName)).getType(); |
166 |
|
167 |
secondTableField |
168 |
.setModel(new FieldSelectionModel(
|
169 |
((TableDocument) targetTable |
170 |
.getSelected()).getStore(), |
171 |
PluginServices.getText(this,
|
172 |
"seleccione_campo_enlace"),
|
173 |
type)); |
174 |
} catch (SelectionException e) {
|
175 |
NotificationManager |
176 |
.addError( |
177 |
"Error obteniendo los campos de la tabla",
|
178 |
e); |
179 |
} catch (DataException e) {
|
180 |
NotificationManager |
181 |
.addError( |
182 |
"Error obteniendo los campos de la tabla",
|
183 |
e); |
184 |
} |
185 |
} |
186 |
} |
187 |
|
188 |
public void back(WizardEvent w) { |
189 |
WizardControl wiz = w.wizard; |
190 |
wiz.enableBack(true);
|
191 |
wiz.enableNext(((ObjectSelectionStep) wiz |
192 |
.getCurrentStep()).getSelectedItem() != null);
|
193 |
} |
194 |
}); |
195 |
project.setModified(true);
|
196 |
PluginServices.getMDIManager().addWindow(wiz); |
197 |
} catch (SelectionException e) {
|
198 |
NotificationManager.addError("Error abriendo el asistente", e);
|
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
/**
|
204 |
* @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String)
|
205 |
*/
|
206 |
public void newSet(String expression) throws DataException { |
207 |
// By Pablo: if no filter expression -> no element selected
|
208 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
209 |
FeatureSet set = null;
|
210 |
try {
|
211 |
set = doSet(expression); |
212 |
|
213 |
if (set == null) { |
214 |
throw new RuntimeException("Not a 'where' clause?"); |
215 |
} |
216 |
FeatureSelection newSel = featureStore.createFeatureSelection(); |
217 |
newSel.select(set); |
218 |
featureStore.setSelection(newSel); |
219 |
} catch (Exception e) { |
220 |
JOptionPane.showMessageDialog(
|
221 |
(Component) PluginServices.getMainFrame(),
|
222 |
"Asegurate de que la consulta es correcta.");
|
223 |
} finally {
|
224 |
if (set != null) { |
225 |
set.dispose(); |
226 |
} |
227 |
} |
228 |
} else {
|
229 |
// By Pablo: if no expression -> no element selected
|
230 |
featureStore.getFeatureSelection().deselectAll(); |
231 |
} |
232 |
} |
233 |
|
234 |
/**
|
235 |
* @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String)
|
236 |
*/
|
237 |
private FeatureSet doSet(String expression) throws DataException { |
238 |
FeatureQuery query = featureStore.createFeatureQuery(); |
239 |
query |
240 |
.setFilter(DALLocator.getDataManager().createExpresion(expression)); |
241 |
return featureStore.getFeatureSet(query);
|
242 |
} |
243 |
|
244 |
/**
|
245 |
* @see org.gvsig.app.gui.filter.ExpressionListener#addToSet(java.lang.String)
|
246 |
*/
|
247 |
public void addToSet(String expression) throws DataException { |
248 |
// By Pablo: if no filter expression -> don't add more elements to set
|
249 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
250 |
FeatureSet set = null;
|
251 |
try {
|
252 |
set = doSet(expression); |
253 |
|
254 |
featureStore.getFeatureSelection().select(set); |
255 |
} finally {
|
256 |
if (set != null) { |
257 |
set.dispose(); |
258 |
} |
259 |
} |
260 |
} |
261 |
} |
262 |
|
263 |
/**
|
264 |
* @see org.gvsig.app.gui.filter.ExpressionListener#fromSet(java.lang.String)
|
265 |
*/
|
266 |
public void fromSet(String expression) throws DataException { |
267 |
// By Pablo: if no filter expression -> no element selected
|
268 |
if (!this.filterExpressionFromWhereIsEmpty(expression)) { |
269 |
|
270 |
FeatureSet set = null;
|
271 |
DisposableIterator iterator = null;
|
272 |
try {
|
273 |
set = doSet(expression); |
274 |
|
275 |
if (set == null) { |
276 |
throw new RuntimeException("Not a 'where' clause?"); |
277 |
} |
278 |
FeatureSelection oldSelection = |
279 |
featureStore.getFeatureSelection(); |
280 |
|
281 |
FeatureSelection newSelection = |
282 |
featureStore.createFeatureSelection(); |
283 |
iterator = set.fastIterator(); |
284 |
while (iterator.hasNext()) {
|
285 |
Feature feature = (Feature) iterator.next(); |
286 |
if (oldSelection.isSelected(feature)) {
|
287 |
newSelection.select(feature); |
288 |
} |
289 |
} |
290 |
featureStore.setSelection(newSelection); |
291 |
} finally {
|
292 |
DisposeUtils.dispose(iterator); |
293 |
DisposeUtils.dispose(set); |
294 |
} |
295 |
} else {
|
296 |
// By Pablo: if no expression -> no element selected
|
297 |
// featureStore.setSelection(featureStore.createSelection());
|
298 |
featureStore.getFeatureSelection().deselectAll(); |
299 |
} |
300 |
} |
301 |
|
302 |
/**
|
303 |
* Returns true if the WHERE subconsultation of the filterExpression is
|
304 |
* empty ("")
|
305 |
*
|
306 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
307 |
* @param expression
|
308 |
* An string
|
309 |
* @return A boolean value
|
310 |
*/
|
311 |
private boolean filterExpressionFromWhereIsEmpty(String expression) { |
312 |
String subExpression = expression.trim();
|
313 |
int pos;
|
314 |
|
315 |
// Remove last ';' if exists
|
316 |
if (subExpression.charAt(subExpression.length() - 1) == ';') { |
317 |
subExpression = |
318 |
subExpression.substring(0, subExpression.length() - 1).trim(); |
319 |
} |
320 |
|
321 |
// If there is no 'where' clause
|
322 |
if ((pos = subExpression.indexOf("where")) == -1) { |
323 |
return false; |
324 |
} |
325 |
|
326 |
// If there is no subexpression in the WHERE clause -> true
|
327 |
subExpression = |
328 |
subExpression.substring(pos + 5, subExpression.length()).trim(); // + |
329 |
// 5
|
330 |
// is
|
331 |
// the
|
332 |
// length
|
333 |
// of
|
334 |
// 'where'
|
335 |
if (subExpression.length() == 0) { |
336 |
return true; |
337 |
} else {
|
338 |
return false; |
339 |
} |
340 |
} |
341 |
|
342 |
/**
|
343 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
344 |
*/
|
345 |
public boolean isVisible() { |
346 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
347 |
|
348 |
if (v == null) { |
349 |
return false; |
350 |
} |
351 |
|
352 |
if (v instanceof FeatureTableDocumentPanel) { |
353 |
return true; |
354 |
} /*
|
355 |
* else {
|
356 |
* if (v instanceof com.iver.cit.gvsig.gui.View) {
|
357 |
* com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
|
358 |
* ProjectView pv = view.getModel();
|
359 |
* FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
360 |
* .getActives();
|
361 |
*
|
362 |
* if (seleccionadas.length == 1) {
|
363 |
* if (seleccionadas[0] instanceof AlphanumericData) {
|
364 |
* return true;
|
365 |
* }
|
366 |
* }
|
367 |
* }
|
368 |
*/
|
369 |
return false; |
370 |
// }
|
371 |
} |
372 |
|
373 |
/**
|
374 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
375 |
*/
|
376 |
public void initialize() { |
377 |
IconThemeHelper.registerIcon("action", "table-create-link", this); |
378 |
|
379 |
} |
380 |
|
381 |
/**
|
382 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
383 |
*/
|
384 |
public boolean isEnabled() { |
385 |
return true; |
386 |
} |
387 |
|
388 |
/**
|
389 |
* Ensure that field name only has 'safe' characters
|
390 |
* (no spaces, special characters, etc).
|
391 |
*/
|
392 |
public String sanitizeFieldName(String fieldName) { |
393 |
return fieldName.replaceAll("\\W", "_"); // replace any non-word |
394 |
// character by an underscore
|
395 |
} |
396 |
|
397 |
} |