Statistics
| Revision:

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

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 38398 jldominguez
                                } else if (resp == JOptionPane.NO_OPTION) {
152
                                    // CANCEL EDITING
153 5807 caballero
                                        cancelEdition(layer);
154 37144 fdiaz
                                        layer.setEditing(false);
155
                                        return true;
156 38398 jldominguez
                                } else if (resp == JOptionPane.CANCEL_OPTION) {
157
                                    // CONTINUE EDITING
158
                                    return false;
159
                                } else {
160
                                    // should not get here
161
                                    // cancel editing
162
                    cancelEdition(layer);
163
                    layer.setEditing(false);
164
                    return true;
165 5267 fjp
                                }
166 38398 jldominguez
167 6604 caballero
                        }
168 38398 jldominguez
                        // ========================================
169
                        // Layer cannot save changes:
170
171 37148 fdiaz
                        Object[] options = {
172
                                        PluginServices.getText(this, "export"),
173
                                        PluginServices.getText(this,
174
                                                        "finish_editing_without_saving_changes"),
175
                                        PluginServices.getText(this, "continue_editing") };
176 37144 fdiaz
177 37148 fdiaz
                        resp = JOptionPane
178
                                        .showOptionDialog(
179
                                                        (Component) PluginServices.getMainFrame(),
180
                                                        PluginServices
181
                                                                        .getText(
182
                                                                                        this,
183
                                                                                        "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer")
184
                                                                        + " : " + layer.getName(), PluginServices
185
                                                                        .getText(this, "stop_edition"),
186
                                                        JOptionPane.YES_NO_CANCEL_OPTION,
187
                                                        JOptionPane.QUESTION_MESSAGE, null, options,
188
                                                        options[2]);
189
                        if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
190
                                cancelEdition(layer);
191
                                layer.setEditing(false);
192
                                return true;
193
                        } else if (resp == JOptionPane.YES_OPTION) {
194
                                int status = exportLayer(layer);
195
                                if (status == JOptionPane.OK_OPTION) {
196 5807 caballero
                                        cancelEdition(layer);
197 15803 vcaballero
                                        layer.setEditing(false);
198 5571 jmvivo
                                }
199 37148 fdiaz
                        }
200 6604 caballero
201 10626 caballero
                } catch (StartEditionLayerException e) {
202 5267 fjp
                        NotificationManager.addError(e);
203 21666 vcaballero
                } catch (ReadException e) {
204 5807 caballero
                        NotificationManager.addError(e);
205 11468 caballero
                } catch (CancelEditingLayerException e) {
206
                        NotificationManager.addError(e);
207 4421 fjp
                }
208 5807 caballero
                return false;
209 4950 caballero
210 5267 fjp
        }
211
212 37148 fdiaz
        private void saveLayer(FLyrVect layer) throws ReadException {
213
                FeatureStore featureStore = layer.getFeatureStore();
214 23424 vcaballero
                try {
215
                        featureStore.finishEditing();
216
                } catch (WriteException e) {
217 34978 fdiaz
                        throw new ReadException(featureStore.getName(), e);
218 24263 vcaballero
                } catch (DataException e) {
219 34978 fdiaz
                        throw new ReadException(featureStore.getName(), e);
220 6483 fjp
                }
221 5807 caballero
        }
222
223 37148 fdiaz
        private void cancelEdition(FLyrVect layer)
224
                        throws CancelEditingLayerException {
225
                FeatureStore featureStore = null;
226 23424 vcaballero
                try {
227
                        featureStore = layer.getFeatureStore();
228 24263 vcaballero
229
                        featureStore.cancelEditing();
230 23424 vcaballero
                } catch (ReadException e) {
231 34978 fdiaz
                        throw new CancelEditingLayerException(layer.getName(), e);
232 24263 vcaballero
                } catch (DataException e) {
233 34978 fdiaz
                        throw new CancelEditingLayerException(layer.getName(), e);
234 23424 vcaballero
                }
235 5807 caballero
        }
236 37144 fdiaz
237 37148 fdiaz
        private int exportLayer(FLyrVect layer) throws ReadException {
238 37144 fdiaz
                ViewDocument model = vista.getModel();
239
                MapContext mapContext = model.getMapContext();
240
                IProjection projection = mapContext.getProjection();
241 37148 fdiaz
242
                ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator
243
                                .getManager().getExtension(ExporttoLayerExtension.class);
244 37144 fdiaz
                return extension.showExportto(layer, projection, mapContext);
245
        }
246 37148 fdiaz
247
        /**
248 29616 jpiera
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
249 4430 fjp
         */
250
        public boolean isVisible() {
251 27420 jmvivo
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
252 4430 fjp
                        return true;
253 27420 jmvivo
                }
254 6604 caballero
                return false;
255 4421 fjp
256 4430 fjp
        }
257 37148 fdiaz
258 12739 caballero
        public IExtensionStatus getStatus() {
259
                return new StopEditingStatus();
260
        }
261 37148 fdiaz
262 12739 caballero
        /**
263
         * Show the dialogs to save the layer without ask if don't like to save.
264 37148 fdiaz
         *
265
         * @param layer
266
         *            Layer to save.
267 12739 caballero
         */
268 37148 fdiaz
        public boolean executeSaveLayer(FLyrVect layer) {
269
                CADToolAdapter cadtoolAdapter = CADExtension.getCADToolAdapter(layer);
270
                EditionManager edMan = cadtoolAdapter.getEditionManager();
271
                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan
272
                                .getLayerEdited(layer);
273
                boolean isStop = false;
274 12739 caballero
                try {
275 27268 vcaballero
                        lyrEd.clearSelection();
276 12739 caballero
277 37148 fdiaz
                        if (layer.isWritable()) {
278 12739 caballero
                                saveLayer(layer);
279
                                layer.setEditing(false);
280 37148 fdiaz
                                // if (layer.isSpatiallyIndexed())
281
                                // {
282
                                // if(layer.getISpatialIndex() != null)
283
                                // {
284
                                // PluginServices.
285
                                // cancelableBackgroundExecution(new
286
                                // CreateSpatialIndexMonitorableTask((FLyrVect)layer));
287
                                // }
288
                                // }
289 12739 caballero
290 37148 fdiaz
                                isStop = true;
291
                        } else {
292
                                // Si no existe writer para la capa que tenemos en edici?n
293
                                int resp = JOptionPane
294
                                                .showConfirmDialog(
295
                                                                (Component) PluginServices.getMainFrame(),
296
                                                                PluginServices
297
                                                                                .getText(
298
                                                                                                this,
299
                                                                                                "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
300
                                                                                + " : " + layer.getName(),
301
                                                                PluginServices
302
                                                                                .getText(this, "cancelar_edicion"),
303
                                                                JOptionPane.YES_NO_OPTION);
304
                                if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING
305
                                        try {
306
                                                cancelEdition(layer);
307
                                                layer.setEditing(false);
308
                                                // if (!(layer.getSource().getDriver() instanceof
309
                                                // IndexedShpDriver)){
310
                                                // VectorialLayerEdited
311
                                                // vle=(VectorialLayerEdited)CADExtension.getEditionManager().getLayerEdited(layer);
312
                                                // layer.setLegend((IVectorLegend)vle.getLegend());
313
                                                // }
314
                                        } catch (CancelEditingLayerException e) {
315
                                                PluginServices.getLogger().error(e.getMessage(), e);
316
                                                return isStop;
317
                                        }
318
                                        isStop = true;
319 12739 caballero
                                }
320
321 37148 fdiaz
                        }
322
                        if (isStop) {
323
                                layer.removeLayerListener(edMan);
324
                                // if (layer instanceof FLyrAnnotation){
325
                                // FLyrAnnotation lva=(FLyrAnnotation)layer;
326
                                // lva.setMapping(lva.getMapping());
327
                                // }
328
                                org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices
329
                                                .getMDIManager().getActiveWindow();
330
                                if (f instanceof DefaultViewPanel) {
331
                                        vista = (DefaultViewPanel) f;
332
                                        FLayer auxLayer = vista.getMapControl().getMapContext()
333
                                                        .getLayers().getLayer(layer.getName());
334
                                        if (auxLayer != null && auxLayer.equals(layer)) {
335
                                                vista.getMapControl().setTool("zoomIn");
336
                                                vista.hideConsole();
337
                                                vista.repaintMap();
338
                                                CADExtension.clearView();
339
                                        }
340 12739 caballero
                                }
341
                        }
342 21666 vcaballero
                } catch (ReadException e1) {
343 37148 fdiaz
                        NotificationManager.showMessageError(e1.getMessage(), e1);
344 12739 caballero
                } catch (StartEditionLayerException e) {
345 37148 fdiaz
                        NotificationManager.showMessageError(e.getMessage(), e);
346 24465 vcaballero
                } catch (DataException e) {
347 37148 fdiaz
                        NotificationManager.showMessageError(e.getMessage(), e);
348 12739 caballero
                }
349
                return isStop;
350
        }
351
352 37148 fdiaz
        private class UnsavedLayer extends UnsavedData {
353 12739 caballero
354
                private FLayer layer;
355
356
                public UnsavedLayer(IExtension extension) {
357
                        super(extension);
358
                }
359
360
                public String getDescription() {
361 37148 fdiaz
                        return PluginServices.getText(this, "editing_layer_unsaved");
362 12739 caballero
                }
363
364
                public String getResourceName() {
365
                        return layer.getName();
366
                }
367
368
                public boolean saveData() {
369 37148 fdiaz
                        return executeSaveLayer((FLyrVect) layer);
370 12739 caballero
                }
371
372
                public void setLayer(FLayer layer) {
373 37148 fdiaz
                        this.layer = layer;
374 12739 caballero
375
                }
376
377 29616 jpiera
                public String getIcon() {
378 12739 caballero
                        return layer.getTocImageIcon();
379
                }
380
381
        }
382
383
        /**
384 37148 fdiaz
         * <p>
385
         * This class provides the status of extensions. If this extension has some
386
         * unsaved editing layer (and save them), and methods to check if the
387
         * extension has some associated background tasks.
388
         *
389 12739 caballero
         * @author Vicente Caballero Navarro
390 37148 fdiaz
         *
391 12739 caballero
         */
392
        private class StopEditingStatus implements IExtensionStatus {
393
                /**
394 37148 fdiaz
                 * This method is used to check if this extension has some unsaved
395
                 * editing layer.
396
                 *
397
                 * @return true if the extension has some unsaved editing layer, false
398
                 *         otherwise.
399
                 */
400 12739 caballero
                public boolean hasUnsavedData() {
401 31496 jjdelcerro
                        Project project = ProjectManager.getInstance().getCurrentProject();
402 37148 fdiaz
                        DefaultViewDocument[] views = project.getDocuments(
403
                                        ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
404
                        for (int i = 0; i < views.length; i++) {
405
                                FLayers layers = views[i].getMapContext().getLayers();
406 12739 caballero
                                LayersIterator iter = getEditingLayer(layers);
407
                                if (iter.hasNext()) {
408
                                        return true;
409
                                }
410
                        }
411
                        return false;
412
                }
413 37148 fdiaz
414 12739 caballero
                /**
415 37148 fdiaz
                 * This method is used to check if the extension has some associated
416
                 * background process which is currently running.
417
                 *
418
                 * @return true if the extension has some associated background process,
419
                 *         false otherwise.
420
                 */
421 12739 caballero
                public boolean hasRunningProcesses() {
422
                        return false;
423
                }
424 37148 fdiaz
425
                /**
426
                 * <p>
427
                 * Gets an array of the traceable background tasks associated with this
428
                 * extension. These tasks may be tracked, canceled, etc.
429
                 * </p>
430
                 *
431
                 * @return An array of the associated background tasks, or null in case
432
                 *         there is no associated background tasks.
433
                 */
434 12739 caballero
                public IMonitorableTask[] getRunningProcesses() {
435
                        return null;
436
                }
437 37148 fdiaz
438 12739 caballero
                /**
439 37148 fdiaz
                 * <p>
440
                 * Gets an array of the UnsavedData objects, which contain information
441
                 * about the unsaved editing layers and allows to save it.
442
                 * </p>
443
                 *
444
                 * @return An array of the associated unsaved editing layers, or null in
445
                 *         case the extension has not unsaved editing layers.
446
                 */
447 12739 caballero
                public IUnsavedData[] getUnsavedData() {
448 31496 jjdelcerro
                        Project project = ProjectManager.getInstance().getCurrentProject();
449 37148 fdiaz
                        DefaultViewDocument[] views = project.getDocuments(
450
                                        ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
451
                        ArrayList unsavedLayers = new ArrayList();
452
                        for (int i = 0; i < views.length; i++) {
453 12739 caballero
                                FLayers layers = views[i].getMapContext().getLayers();
454
                                LayersIterator iter = getEditingLayer(layers);
455 37148 fdiaz
                                while (iter.hasNext()) {
456
                                        UnsavedLayer ul = new UnsavedLayer(StopEditing.this);
457 12739 caballero
                                        ul.setLayer(iter.nextLayer());
458
                                        unsavedLayers.add(ul);
459
                                }
460
                        }
461 37148 fdiaz
                        return (IUnsavedData[]) unsavedLayers.toArray(new IUnsavedData[0]);
462 12739 caballero
                }
463
        }
464 37148 fdiaz
465
        private LayersIterator getEditingLayer(FLayers layers) {
466
                return new LayersIterator(layers) {
467 12739 caballero
                        public boolean evaluate(FLayer layer) {
468
                                return layer.isEditing();
469
                        }
470
                };
471
        }
472 4421 fjp
}