Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / StopEditing.java @ 5807

History | View | Annotate | Download (6.43 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.awt.Component;
4
import java.io.IOException;
5

    
6
import javax.swing.JOptionPane;
7

    
8
import com.iver.andami.PluginServices;
9
import com.iver.andami.messages.NotificationManager;
10
import com.iver.andami.plugins.Extension;
11
import com.iver.cit.gvsig.fmap.DriverException;
12
import com.iver.cit.gvsig.fmap.FMap;
13
import com.iver.cit.gvsig.fmap.MapControl;
14
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
15
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
16
import com.iver.cit.gvsig.fmap.edition.EditionException;
17
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
18
import com.iver.cit.gvsig.fmap.edition.IWriteable;
19
import com.iver.cit.gvsig.fmap.edition.IWriter;
20
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
21
import com.iver.cit.gvsig.fmap.layers.FLayer;
22
import com.iver.cit.gvsig.fmap.layers.FLayers;
23
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
24
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
25
import com.iver.cit.gvsig.gui.Table;
26
import com.iver.cit.gvsig.gui.View;
27
import com.iver.cit.gvsig.project.ProjectView;
28

    
29
/**
30
 * @author Francisco Jos?
31
 *
32
 * Cuando un tema se pone en edici?n, puede que su driver implemente
33
 * ISpatialWriter. En ese caso, es capaz de guardarse sobre s? mismo. Si no lo
34
 * implementa, esta opci?n estar? deshabilitada y la ?nica posibilidad de
35
 * guardar este tema ser? "Guardando como..."
36
 */
37
public class StopEditing extends Extension {
38
        private View vista;
39

    
40
        /**
41
         * @see com.iver.andami.plugins.IExtension#initialize()
42
         */
43
        public void initialize() {
44
        }
45

    
46
        /**
47
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
48
         */
49
        public void execute(String s) {
50
                com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
51
                                .getActiveView();
52

    
53
                vista = (View) f;
54
                boolean isStop=false;
55
                ProjectView model = vista.getModel();
56
                FMap mapa = model.getMapContext();
57
                FLayers layers = mapa.getLayers();
58
                EditionManager edMan = CADExtension.getEditionManager();
59
                if (s.equals("STOPEDITING")) {
60
                        FLayer[] actives = layers.getActives();
61
                        // TODO: Comprobar que solo hay una activa, o al menos
62
                        // que solo hay una en edici?n que est? activa, etc, etc
63
                        for (int i = 0; i < actives.length; i++) {
64
                                if (actives[i] instanceof FLyrVect && actives[i].isEditing()) {
65
                                        FLyrVect lv = (FLyrVect) actives[i];
66
                                        MapControl mapControl = (MapControl) vista.getMapControl();
67
                                        // VectorialLayerEdited lyrEd = (VectorialLayerEdited)
68
                                        // edMan.getActiveLayerEdited();
69
                                        // lyrEd.clearSelection();
70
                                        isStop=stopEditing(lv, mapControl);
71

    
72
                                        // return;
73
                                }
74
                        }
75
                        if (isStop) {
76
                                vista.getMapControl().setTool("zoomIn");
77
                                vista.hideConsole();
78
                        }
79
                }
80
                PluginServices.getMainFrame().enableControls();
81
        }
82

    
83
        /**
84
         * @see com.iver.andami.plugins.IExtension#isEnabled()
85
         */
86
        public boolean isEnabled() {
87
                FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
88
                if (lyrs == null)
89
                        return false;
90
                FLyrVect lyrVect = (FLyrVect) lyrs[0];
91
                if (lyrVect.getSource() instanceof VectorialEditableAdapter) {
92
                        return true;
93
                }
94
                return false;
95
        }
96
        private boolean isWritable(FLyrVect lyrVect) {
97
                if (!lyrVect.getSource().getDriver().isWritable())
98
                        return false;
99
                VectorialEditableAdapter vea = (VectorialEditableAdapter) lyrVect
100
                                .getSource();
101
                IWriter writer = vea.getWriter();
102
                if (writer != null)
103
                {
104
                        if (writer instanceof ISpatialWriter)
105
                                return true;
106
                }
107
                return false;
108
        }
109
        /**
110
         * DOCUMENT ME!
111
         */
112
        public boolean stopEditing(FLyrVect layer, MapControl mapControl) {
113
                VectorialEditableAdapter vea = (VectorialEditableAdapter) layer
114
                                .getSource();
115
                int resp = JOptionPane.NO_OPTION;
116

    
117
                try {
118
                        if (isWritable(layer)) {
119
                                resp = JOptionPane.showConfirmDialog((Component) PluginServices
120
                                                .getMainFrame(), PluginServices.getText(this,
121
                                                "realmente_desea_guardar_la_capa")
122
                                                + " : " + layer.getName(), PluginServices.getText(this,
123
                                                "guardar"), JOptionPane.YES_NO_OPTION);
124
                                if (resp != JOptionPane.YES_OPTION) { // CANCEL EDITING
125
                                        cancelEdition(layer);
126
                                } else { // GUARDAMOS EL TEMA
127
                                        saveLayer(layer);
128
                                }
129

    
130
                                vea.getCommandRecord().removeCommandListener(mapControl);
131
                                layer.setEditing(false);
132
                                return true;
133
                        } else {// Si no existe writer para la capa que tenemos en edici?n
134
                                resp = JOptionPane
135
                                                .showConfirmDialog(
136
                                                                (Component) PluginServices.getMainFrame(),
137
                                                                PluginServices
138
                                                                                .getText(
139
                                                                                                this,
140
                                                                                                "no_existe_writer_para_este_formato_de_capa_puede_exportar_o_cancelar_desea_cancelar_la_edicion")
141
                                                                                + " : " + layer.getName(),
142
                                                                PluginServices.getText(this, "cancelar"),
143
                                                                JOptionPane.YES_NO_OPTION);
144
                                if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING
145
                                        cancelEdition(layer);
146
                                        vea.getCommandRecord().removeCommandListener(mapControl);
147
                                        layer.setEditing(false);
148
                                        return true;
149
                                }
150
                        }
151
                } catch (EditionException e) {
152
                        NotificationManager.addError(e);
153
                } catch (IOException e) {
154
                        NotificationManager.addError(e);
155
                } catch (DriverException e) {
156
                        NotificationManager.addError(e);
157
                }
158
                return false;
159

    
160
        }
161

    
162

    
163
        private void saveLayer(FLyrVect layer) throws DriverException,
164
                        EditionException {
165
                VectorialEditableAdapter vea = (VectorialEditableAdapter) layer
166
                                .getSource();
167

    
168
                ISpatialWriter writer = (ISpatialWriter) vea.getWriter();
169
                com.iver.andami.ui.mdiManager.View[] views = PluginServices
170
                                .getMDIManager().getAllViews();
171
                for (int j = 0; j < views.length; j++) {
172
                        if (views[j] instanceof Table) {
173
                                Table table = (Table) views[j];
174
                                if (table.getModel().getAssociatedTable() != null
175
                                                && table.getModel().getAssociatedTable().equals(layer)) {
176
                                        table.stopEditingCell();
177
                                }
178
                        }
179
                }
180
                ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(layer);
181
                writer.initialize(lyrDef);
182
                vea.stopEdition(writer, EditionEvent.GRAPHIC);
183

    
184
        }
185

    
186
        private void cancelEdition(FLyrVect layer) throws IOException {
187
                com.iver.andami.ui.mdiManager.View[] views = PluginServices
188
                                .getMDIManager().getAllViews();
189
                for (int j = 0; j < views.length; j++) {
190
                        if (views[j] instanceof Table) {
191
                                Table table = (Table) views[j];
192
                                if (table.getModel().getAssociatedTable() != null
193
                                                && table.getModel().getAssociatedTable().equals(layer)) {
194
                                        table.cancelEditing();
195
                                }
196
                        }
197
                }
198
        }
199
        /**
200
         * @see com.iver.andami.plugins.IExtension#isVisible()
201
         */
202
        public boolean isVisible() {
203
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
204
                        return true;
205
                else
206
                        return false;
207

    
208
        }
209
}