svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / StopEditing.java @ 38869
History | View | Annotate | Download (16.2 KB)
1 |
package org.gvsig.editing; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
5 |
import java.awt.Font; |
6 |
import java.awt.GridBagConstraints; |
7 |
import java.awt.GridBagLayout; |
8 |
import java.awt.Insets; |
9 |
import java.util.ArrayList; |
10 |
|
11 |
import javax.swing.JLabel; |
12 |
import javax.swing.JOptionPane; |
13 |
import javax.swing.JPanel; |
14 |
|
15 |
import org.cresques.cts.IProjection; |
16 |
import org.gvsig.andami.PluginServices; |
17 |
import org.gvsig.andami.PluginsLocator; |
18 |
import org.gvsig.andami.messages.NotificationManager; |
19 |
import org.gvsig.andami.plugins.Extension; |
20 |
import org.gvsig.andami.plugins.IExtension; |
21 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
22 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
23 |
import org.gvsig.andami.plugins.status.UnsavedData; |
24 |
import org.gvsig.app.project.Project; |
25 |
import org.gvsig.app.project.ProjectManager; |
26 |
import org.gvsig.app.project.documents.view.DefaultViewDocument; |
27 |
import org.gvsig.app.project.documents.view.ViewDocument; |
28 |
import org.gvsig.app.project.documents.view.ViewManager; |
29 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
30 |
import org.gvsig.editing.gui.cad.CADToolAdapter; |
31 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
32 |
import org.gvsig.exportto.app.extension.ExporttoLayerExtension; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.exception.ReadException; |
35 |
import org.gvsig.fmap.dal.exception.WriteException; |
36 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
37 |
import org.gvsig.fmap.mapcontext.MapContext; |
38 |
import org.gvsig.fmap.mapcontext.exceptions.CancelEditingLayerException; |
39 |
import org.gvsig.fmap.mapcontext.exceptions.StartEditionLayerException; |
40 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
41 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
42 |
import org.gvsig.fmap.mapcontext.layers.LayersIterator; |
43 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
44 |
import org.gvsig.fmap.mapcontrol.MapControl; |
45 |
import org.gvsig.i18n.Messages; |
46 |
import org.gvsig.utils.swing.threads.IMonitorableTask; |
47 |
|
48 |
/**
|
49 |
* @author Francisco Jos?
|
50 |
*
|
51 |
* Cuando un tema se pone en edici?n, puede que su driver implemente
|
52 |
* ISpatialWriter. En ese caso, es capaz de guardarse sobre s? mismo. Si
|
53 |
* no lo implementa, esta opci?n estar? deshabilitada y la ?nica
|
54 |
* posibilidad de guardar este tema ser? "Guardando como..."
|
55 |
*/
|
56 |
public class StopEditing extends Extension { |
57 |
private DefaultViewPanel vista;
|
58 |
|
59 |
/**
|
60 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
61 |
*/
|
62 |
public void initialize() { |
63 |
} |
64 |
|
65 |
/**
|
66 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
67 |
*/
|
68 |
public void execute(String s) { |
69 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices |
70 |
.getMDIManager().getActiveWindow(); |
71 |
|
72 |
vista = (DefaultViewPanel) f; |
73 |
boolean isStop = false; |
74 |
ViewDocument model = vista.getModel(); |
75 |
MapContext mapa = model.getMapContext(); |
76 |
FLayers layers = mapa.getLayers(); |
77 |
EditionManager edMan = CADExtension.getEditionManager(); |
78 |
|
79 |
if (s.equals("layer-stop-editing")) { |
80 |
vista.getMapControl().getCanceldraw().setCanceled(true);
|
81 |
FLayer[] actives = layers.getActives();
|
82 |
// TODO: Comprobar que solo hay una activa, o al menos
|
83 |
// que solo hay una en edici?n que est? activa, etc, etc
|
84 |
for (int i = 0; i < actives.length; i++) { |
85 |
if (actives[i] instanceof FLyrVect && actives[i].isEditing()) { |
86 |
FLyrVect lv = (FLyrVect) actives[i]; |
87 |
MapControl mapControl = vista.getMapControl(); |
88 |
VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan |
89 |
.getActiveLayerEdited(); |
90 |
lv.getFeatureStore().deleteObserver(lyrEd); |
91 |
isStop = stopEditing(lv, mapControl); |
92 |
if (isStop) {
|
93 |
lv.removeLayerListener(edMan); |
94 |
mapControl.getMapContext().removeLayerDrawListener( |
95 |
lyrEd); |
96 |
// if (lv instanceof FLyrAnnotation){
|
97 |
// FLyrAnnotation lva=(FLyrAnnotation)lv;
|
98 |
// lva.setMapping(lva.getMapping());
|
99 |
// }
|
100 |
} else {
|
101 |
lv.getFeatureStore().addObserver(lyrEd); |
102 |
|
103 |
} |
104 |
} |
105 |
} |
106 |
if (isStop) {
|
107 |
vista.getMapControl().setTool("zoomIn");
|
108 |
vista.hideConsole(); |
109 |
vista.repaintMap(); |
110 |
CADExtension.clearView(); |
111 |
|
112 |
} |
113 |
} |
114 |
PluginServices.getMainFrame().enableControls(); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
119 |
*/
|
120 |
public boolean isEnabled() { |
121 |
FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
|
122 |
if (lyrs == null) { |
123 |
return false; |
124 |
} |
125 |
FLyrVect lyrVect = (FLyrVect) lyrs[0];
|
126 |
if (lyrVect.isEditing()) {
|
127 |
return true; |
128 |
} |
129 |
return false; |
130 |
} |
131 |
|
132 |
/**
|
133 |
* DOCUMENT ME!
|
134 |
*/
|
135 |
public boolean stopEditing(FLyrVect layer, MapControl mapControl) { |
136 |
int resp = JOptionPane.CANCEL_OPTION; |
137 |
|
138 |
try {
|
139 |
if (layer.isWritable()) {
|
140 |
Object[] options = { |
141 |
PluginServices.getText(this, "_Guardar"), |
142 |
" " + PluginServices.getText(this, "_Descartar") + " ", |
143 |
PluginServices.getText(this, "_Continuar") }; |
144 |
|
145 |
JPanel explanation_panel = getExplanationPanel(layer.getName());
|
146 |
|
147 |
resp = JOptionPane
|
148 |
.showOptionDialog( |
149 |
(Component) PluginServices.getMainFrame(),
|
150 |
explanation_panel, |
151 |
PluginServices.getText(this, "stop_edition"), |
152 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
153 |
JOptionPane.QUESTION_MESSAGE, null, options, |
154 |
options[2]);
|
155 |
|
156 |
if (resp == JOptionPane.YES_OPTION) { |
157 |
// SAVE
|
158 |
saveLayer(layer); |
159 |
layer.setEditing(false);
|
160 |
return true; |
161 |
} else if (resp == JOptionPane.NO_OPTION) { |
162 |
// CANCEL EDITING
|
163 |
cancelEdition(layer); |
164 |
layer.setEditing(false);
|
165 |
return true; |
166 |
} else if (resp == JOptionPane.CANCEL_OPTION) { |
167 |
// CONTINUE EDITING
|
168 |
return false; |
169 |
} else {
|
170 |
// This happens when user clicks on [x]
|
171 |
// to abruptly close previous JOptionPane dialog
|
172 |
// We make it equivalent to CONTINUE EDITING
|
173 |
return false; |
174 |
} |
175 |
|
176 |
} |
177 |
// ========================================
|
178 |
// Layer cannot save changes:
|
179 |
|
180 |
Object[] options = { |
181 |
PluginServices.getText(this, "export"), |
182 |
PluginServices.getText(this,
|
183 |
"finish_editing_without_saving_changes"),
|
184 |
PluginServices.getText(this, "continue_editing") }; |
185 |
|
186 |
resp = JOptionPane
|
187 |
.showOptionDialog( |
188 |
(Component) PluginServices.getMainFrame(),
|
189 |
PluginServices |
190 |
.getText( |
191 |
this,
|
192 |
"no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer")
|
193 |
+ " : " + layer.getName(), PluginServices
|
194 |
.getText(this, "stop_edition"), |
195 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
196 |
JOptionPane.QUESTION_MESSAGE, null, options, |
197 |
options[2]);
|
198 |
if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING |
199 |
cancelEdition(layer); |
200 |
layer.setEditing(false);
|
201 |
return true; |
202 |
} else if (resp == JOptionPane.YES_OPTION) { |
203 |
int status = exportLayer(layer);
|
204 |
if (status == JOptionPane.OK_OPTION) { |
205 |
cancelEdition(layer); |
206 |
layer.setEditing(false);
|
207 |
} |
208 |
} |
209 |
|
210 |
} catch (StartEditionLayerException e) {
|
211 |
NotificationManager.addError(e); |
212 |
} catch (ReadException e) {
|
213 |
NotificationManager.addError(e); |
214 |
} catch (CancelEditingLayerException e) {
|
215 |
NotificationManager.addError(e); |
216 |
} |
217 |
return false; |
218 |
|
219 |
} |
220 |
|
221 |
|
222 |
private JPanel getExplanationPanel( |
223 |
String layer_name) {
|
224 |
|
225 |
JPanel resp = new JPanel(new BorderLayout(10, 10)); |
226 |
|
227 |
String msg = Messages.getText("realmente_desea_guardar_la_capa"); |
228 |
msg = msg + " '" + layer_name + "'?"; |
229 |
JLabel topLabel = new JLabel(msg); |
230 |
|
231 |
JPanel mainPanel = new JPanel(new GridBagLayout()); |
232 |
GridBagConstraints cc = new GridBagConstraints(); |
233 |
|
234 |
cc.gridx = 0;
|
235 |
cc.gridy = 0;
|
236 |
cc.anchor = GridBagConstraints.WEST;
|
237 |
|
238 |
cc.insets = new Insets(3, 6, 3, 6); |
239 |
|
240 |
Font boldf = mainPanel.getFont().deriveFont(Font.BOLD); |
241 |
|
242 |
JLabel lbl = new JLabel(Messages.getText("_Guardar")); |
243 |
lbl.setFont(boldf); |
244 |
mainPanel.add(lbl, cc); |
245 |
cc.gridx = 1;
|
246 |
mainPanel.add(new JLabel(Messages.getText("_Save_changes_performed")), cc); |
247 |
|
248 |
cc.gridx = 0;
|
249 |
cc.gridy = 1;
|
250 |
lbl = new JLabel(Messages.getText("_Descartar")); |
251 |
lbl.setFont(boldf); |
252 |
mainPanel.add(lbl, cc); |
253 |
cc.gridx = 1;
|
254 |
mainPanel.add(new JLabel(Messages.getText("_Discard_and_lose_changes")), cc); |
255 |
|
256 |
cc.gridx = 0;
|
257 |
cc.gridy = 2;
|
258 |
lbl = new JLabel(Messages.getText("_Continuar")); |
259 |
lbl.setFont(boldf); |
260 |
mainPanel.add(lbl, cc); |
261 |
cc.gridx = 1;
|
262 |
mainPanel.add(new JLabel(Messages.getText("_Do_not_save_yet_Stay_in_editing_mode")), cc); |
263 |
|
264 |
resp.add(mainPanel, BorderLayout.CENTER);
|
265 |
resp.add(topLabel, BorderLayout.NORTH);
|
266 |
return resp;
|
267 |
} |
268 |
|
269 |
|
270 |
private void saveLayer(FLyrVect layer) throws ReadException { |
271 |
FeatureStore featureStore = layer.getFeatureStore(); |
272 |
try {
|
273 |
featureStore.finishEditing(); |
274 |
} catch (WriteException e) {
|
275 |
throw new ReadException(featureStore.getName(), e); |
276 |
} catch (DataException e) {
|
277 |
throw new ReadException(featureStore.getName(), e); |
278 |
} |
279 |
} |
280 |
|
281 |
private void cancelEdition(FLyrVect layer) |
282 |
throws CancelEditingLayerException {
|
283 |
FeatureStore featureStore = null;
|
284 |
try {
|
285 |
featureStore = layer.getFeatureStore(); |
286 |
|
287 |
featureStore.cancelEditing(); |
288 |
} catch (ReadException e) {
|
289 |
throw new CancelEditingLayerException(layer.getName(), e); |
290 |
} catch (DataException e) {
|
291 |
throw new CancelEditingLayerException(layer.getName(), e); |
292 |
} |
293 |
} |
294 |
|
295 |
private int exportLayer(FLyrVect layer) throws ReadException { |
296 |
ViewDocument model = vista.getModel(); |
297 |
MapContext mapContext = model.getMapContext(); |
298 |
IProjection projection = mapContext.getProjection(); |
299 |
|
300 |
ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator |
301 |
.getManager().getExtension(ExporttoLayerExtension.class); |
302 |
return extension.showExportto(layer, projection, mapContext);
|
303 |
} |
304 |
|
305 |
/**
|
306 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
307 |
*/
|
308 |
public boolean isVisible() { |
309 |
if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
|
310 |
return true; |
311 |
} |
312 |
return false; |
313 |
|
314 |
} |
315 |
|
316 |
public IExtensionStatus getStatus() {
|
317 |
return new StopEditingStatus(); |
318 |
} |
319 |
|
320 |
/**
|
321 |
* Show the dialogs to save the layer without ask if don't like to save.
|
322 |
*
|
323 |
* @param layer
|
324 |
* Layer to save.
|
325 |
*/
|
326 |
public boolean executeSaveLayer(FLyrVect layer) { |
327 |
CADToolAdapter cadtoolAdapter = CADExtension.getCADToolAdapter(layer); |
328 |
EditionManager edMan = cadtoolAdapter.getEditionManager(); |
329 |
VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan |
330 |
.getLayerEdited(layer); |
331 |
boolean isStop = false; |
332 |
try {
|
333 |
lyrEd.clearSelection(); |
334 |
|
335 |
if (layer.isWritable()) {
|
336 |
saveLayer(layer); |
337 |
layer.setEditing(false);
|
338 |
// if (layer.isSpatiallyIndexed())
|
339 |
// {
|
340 |
// if(layer.getISpatialIndex() != null)
|
341 |
// {
|
342 |
// PluginServices.
|
343 |
// cancelableBackgroundExecution(new
|
344 |
// CreateSpatialIndexMonitorableTask((FLyrVect)layer));
|
345 |
// }
|
346 |
// }
|
347 |
|
348 |
isStop = true;
|
349 |
} else {
|
350 |
// Si no existe writer para la capa que tenemos en edici?n
|
351 |
int resp = JOptionPane |
352 |
.showConfirmDialog( |
353 |
(Component) PluginServices.getMainFrame(),
|
354 |
PluginServices |
355 |
.getText( |
356 |
this,
|
357 |
"no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
|
358 |
+ " : " + layer.getName(),
|
359 |
PluginServices |
360 |
.getText(this, "cancelar_edicion"), |
361 |
JOptionPane.YES_NO_OPTION);
|
362 |
if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING |
363 |
try {
|
364 |
cancelEdition(layer); |
365 |
layer.setEditing(false);
|
366 |
// if (!(layer.getSource().getDriver() instanceof
|
367 |
// IndexedShpDriver)){
|
368 |
// VectorialLayerEdited
|
369 |
// vle=(VectorialLayerEdited)CADExtension.getEditionManager().getLayerEdited(layer);
|
370 |
// layer.setLegend((IVectorLegend)vle.getLegend());
|
371 |
// }
|
372 |
} catch (CancelEditingLayerException e) {
|
373 |
PluginServices.getLogger().error(e.getMessage(), e); |
374 |
return isStop;
|
375 |
} |
376 |
isStop = true;
|
377 |
} |
378 |
|
379 |
} |
380 |
if (isStop) {
|
381 |
layer.removeLayerListener(edMan); |
382 |
// if (layer instanceof FLyrAnnotation){
|
383 |
// FLyrAnnotation lva=(FLyrAnnotation)layer;
|
384 |
// lva.setMapping(lva.getMapping());
|
385 |
// }
|
386 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices |
387 |
.getMDIManager().getActiveWindow(); |
388 |
if (f instanceof DefaultViewPanel) { |
389 |
vista = (DefaultViewPanel) f; |
390 |
FLayer auxLayer = vista.getMapControl().getMapContext() |
391 |
.getLayers().getLayer(layer.getName()); |
392 |
if (auxLayer != null && auxLayer.equals(layer)) { |
393 |
vista.getMapControl().setTool("zoomIn");
|
394 |
vista.hideConsole(); |
395 |
vista.repaintMap(); |
396 |
CADExtension.clearView(); |
397 |
} |
398 |
} |
399 |
} |
400 |
} catch (ReadException e1) {
|
401 |
NotificationManager.showMessageError(e1.getMessage(), e1); |
402 |
} catch (StartEditionLayerException e) {
|
403 |
NotificationManager.showMessageError(e.getMessage(), e); |
404 |
} catch (DataException e) {
|
405 |
NotificationManager.showMessageError(e.getMessage(), e); |
406 |
} |
407 |
return isStop;
|
408 |
} |
409 |
|
410 |
private class UnsavedLayer extends UnsavedData { |
411 |
|
412 |
private FLayer layer;
|
413 |
|
414 |
public UnsavedLayer(IExtension extension) {
|
415 |
super(extension);
|
416 |
} |
417 |
|
418 |
public String getDescription() { |
419 |
return PluginServices.getText(this, "editing_layer_unsaved"); |
420 |
} |
421 |
|
422 |
public String getResourceName() { |
423 |
return layer.getName();
|
424 |
} |
425 |
|
426 |
public boolean saveData() { |
427 |
return executeSaveLayer((FLyrVect) layer);
|
428 |
} |
429 |
|
430 |
public void setLayer(FLayer layer) { |
431 |
this.layer = layer;
|
432 |
|
433 |
} |
434 |
|
435 |
public String getIcon() { |
436 |
return layer.getTocImageIcon();
|
437 |
} |
438 |
|
439 |
} |
440 |
|
441 |
/**
|
442 |
* <p>
|
443 |
* This class provides the status of extensions. If this extension has some
|
444 |
* unsaved editing layer (and save them), and methods to check if the
|
445 |
* extension has some associated background tasks.
|
446 |
*
|
447 |
* @author Vicente Caballero Navarro
|
448 |
*
|
449 |
*/
|
450 |
private class StopEditingStatus implements IExtensionStatus { |
451 |
/**
|
452 |
* This method is used to check if this extension has some unsaved
|
453 |
* editing layer.
|
454 |
*
|
455 |
* @return true if the extension has some unsaved editing layer, false
|
456 |
* otherwise.
|
457 |
*/
|
458 |
public boolean hasUnsavedData() { |
459 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
460 |
DefaultViewDocument[] views = project.getDocuments(
|
461 |
ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]); |
462 |
for (int i = 0; i < views.length; i++) { |
463 |
FLayers layers = views[i].getMapContext().getLayers(); |
464 |
LayersIterator iter = getEditingLayer(layers); |
465 |
if (iter.hasNext()) {
|
466 |
return true; |
467 |
} |
468 |
} |
469 |
return false; |
470 |
} |
471 |
|
472 |
/**
|
473 |
* This method is used to check if the extension has some associated
|
474 |
* background process which is currently running.
|
475 |
*
|
476 |
* @return true if the extension has some associated background process,
|
477 |
* false otherwise.
|
478 |
*/
|
479 |
public boolean hasRunningProcesses() { |
480 |
return false; |
481 |
} |
482 |
|
483 |
/**
|
484 |
* <p>
|
485 |
* Gets an array of the traceable background tasks associated with this
|
486 |
* extension. These tasks may be tracked, canceled, etc.
|
487 |
* </p>
|
488 |
*
|
489 |
* @return An array of the associated background tasks, or null in case
|
490 |
* there is no associated background tasks.
|
491 |
*/
|
492 |
public IMonitorableTask[] getRunningProcesses() { |
493 |
return null; |
494 |
} |
495 |
|
496 |
/**
|
497 |
* <p>
|
498 |
* Gets an array of the UnsavedData objects, which contain information
|
499 |
* about the unsaved editing layers and allows to save it.
|
500 |
* </p>
|
501 |
*
|
502 |
* @return An array of the associated unsaved editing layers, or null in
|
503 |
* case the extension has not unsaved editing layers.
|
504 |
*/
|
505 |
public IUnsavedData[] getUnsavedData() { |
506 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
507 |
DefaultViewDocument[] views = project.getDocuments(
|
508 |
ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]); |
509 |
ArrayList unsavedLayers = new ArrayList(); |
510 |
for (int i = 0; i < views.length; i++) { |
511 |
FLayers layers = views[i].getMapContext().getLayers(); |
512 |
LayersIterator iter = getEditingLayer(layers); |
513 |
while (iter.hasNext()) {
|
514 |
UnsavedLayer ul = new UnsavedLayer(StopEditing.this);
|
515 |
ul.setLayer(iter.nextLayer()); |
516 |
unsavedLayers.add(ul); |
517 |
} |
518 |
} |
519 |
return (IUnsavedData[]) unsavedLayers.toArray(new IUnsavedData[0]); |
520 |
} |
521 |
} |
522 |
|
523 |
private LayersIterator getEditingLayer(FLayers layers) {
|
524 |
return new LayersIterator(layers) { |
525 |
public boolean evaluate(FLayer layer) { |
526 |
return layer.isEditing();
|
527 |
} |
528 |
}; |
529 |
} |
530 |
} |