Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / StopEditing.java @ 38608

History | View | Annotate | Download (14.4 KB)

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