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