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