Statistics
| Revision:

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

History | View | Annotate | Download (7.14 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.hardcode.gdbms.engine.instruction.FieldNotFoundException;
9
import com.iver.andami.PluginServices;
10
import com.iver.andami.messages.NotificationManager;
11
import com.iver.andami.plugins.Extension;
12
import com.iver.cit.gvsig.fmap.DriverException;
13
import com.iver.cit.gvsig.fmap.MapContext;
14
import com.iver.cit.gvsig.fmap.MapControl;
15
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
16
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
17
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
18
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
19
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
20
import com.iver.cit.gvsig.fmap.edition.EditionException;
21
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
22
import com.iver.cit.gvsig.fmap.edition.IWriter;
23
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
24
import com.iver.cit.gvsig.fmap.layers.FLayer;
25
import com.iver.cit.gvsig.fmap.layers.FLayers;
26
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
27
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
28
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
29
import com.iver.cit.gvsig.project.documents.table.gui.Table;
30
import com.iver.cit.gvsig.project.documents.view.ProjectView;
31
import com.iver.cit.gvsig.project.documents.view.gui.View;
32

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

    
44
        /**
45
         * @see com.iver.andami.plugins.IExtension#initialize()
46
         */
47
        public void initialize() {
48
        }
49

    
50
        /**
51
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
52
         */
53
        public void execute(String s) {
54
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
55
                                .getActiveWindow();
56

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

    
75
                                        // return;
76
                                }
77
                        }
78
                        if (isStop) {
79
                                vista.getMapControl().setTool("zoomIn");
80
                                vista.hideConsole();
81
                                vista.repaintMap();
82
                        }
83
                }
84
                PluginServices.getMainFrame().enableControls();
85
        }
86

    
87
        /**
88
         * @see com.iver.andami.plugins.IExtension#isEnabled()
89
         */
90
        public boolean isEnabled() {
91
                FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
92
                if (lyrs == null)
93
                        return false;
94
                FLyrVect lyrVect = (FLyrVect) lyrs[0];
95
                if (lyrVect.getSource() instanceof VectorialEditableAdapter) {
96
                        return true;
97
                }
98
                return false;
99
        }
100
        /**
101
         * DOCUMENT ME!
102
         */
103
        public boolean stopEditing(FLyrVect layer, MapControl mapControl) {
104
                VectorialEditableAdapter vea = (VectorialEditableAdapter) layer
105
                                .getSource();
106
                int resp = JOptionPane.NO_OPTION;
107

    
108
                try {
109
                        if (layer.isWritable()) {
110
                                resp = JOptionPane.showConfirmDialog((Component) PluginServices
111
                                                .getMainFrame(), PluginServices.getText(this,
112
                                                "realmente_desea_guardar_la_capa")
113
                                                + " : " + layer.getName(), PluginServices.getText(this,
114
                                                "guardar"), JOptionPane.YES_NO_OPTION);
115
                                if (resp != JOptionPane.YES_OPTION) { // CANCEL EDITING
116
                                        cancelEdition(layer);
117
                                } else { // GUARDAMOS EL TEMA
118
                                        saveLayer(layer);
119
                                }
120

    
121
                                vea.getCommandRecord().removeCommandListener(mapControl);
122
                                layer.setEditing(false);
123
                                return true;
124
                        }
125
                        // Si no existe writer para la capa que tenemos en edici?n
126
                                resp = JOptionPane
127
                                                .showConfirmDialog(
128
                                                                (Component) PluginServices.getMainFrame(),
129
                                                                PluginServices
130
                                                                                .getText(
131
                                                                                                this,
132
                                                                                                "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
133
                                                                                + " : " + layer.getName(),
134
                                                                PluginServices.getText(this, "cancelar_edicion"),
135
                                                                JOptionPane.YES_NO_OPTION);
136
                                if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING
137
                                        cancelEdition(layer);
138
                                        vea.getCommandRecord().removeCommandListener(mapControl);
139
                                        if (!(layer.getSource().getDriver() instanceof IndexedShpDriver)){
140
                                                VectorialLayerEdited vle=(VectorialLayerEdited)CADExtension.getEditionManager().getLayerEdited(layer);
141
                                                layer.setLegend((VectorialLegend)vle.getLegend());
142
                                        }
143
                                        layer.setEditing(false);
144
                                        return true;
145
                                }
146

    
147
                } catch (EditionException e) {
148
                        NotificationManager.addError(e);
149
                } catch (IOException e) {
150
                        NotificationManager.addError(e);
151
                } catch (DriverException e) {
152
                        NotificationManager.addError(e);
153
                } catch (FieldNotFoundException e) {
154
                        e.printStackTrace();
155
                }
156
                return false;
157

    
158
        }
159

    
160

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

    
166
                ISpatialWriter writer = (ISpatialWriter) vea.getWriter();
167
                com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices
168
                                .getMDIManager().getAllWindows();
169
                for (int j = 0; j < views.length; j++) {
170
                        if (views[j] instanceof Table) {
171
                                Table table = (Table) views[j];
172
                                if (table.getModel().getAssociatedTable() != null
173
                                                && table.getModel().getAssociatedTable().equals(layer)) {
174
                                        table.stopEditingCell();
175
                                }
176
                        }
177
                }
178
                vea.cleanSelectableDatasource();
179
                layer.setRecordset(vea.getRecordset()); // Queremos que el recordset del layer
180
                // refleje los cambios en los campos.
181
                ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(layer);
182
                String aux="FIELDS:";
183
                FieldDescription[] flds = lyrDef.getFieldsDesc();
184
                for (int i=0; i < flds.length; i++)
185
                {
186
                        aux = aux + ", " + flds[i].getFieldAlias();
187
                }
188
                System.err.println("Escribiendo la capa " + lyrDef.getName() +
189
                                " con los campos " + aux);
190
                writer.initialize(lyrDef);
191
                vea.stopEdition(writer, EditionEvent.GRAPHIC);
192

    
193
        }
194

    
195
        private void cancelEdition(FLyrVect layer) throws IOException {
196
                com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices
197
                                .getMDIManager().getAllWindows();
198
                for (int j = 0; j < views.length; j++) {
199
                        if (views[j] instanceof Table) {
200
                                Table table = (Table) views[j];
201
                                if (table.getModel().getAssociatedTable() != null
202
                                                && table.getModel().getAssociatedTable().equals(layer)) {
203
                                        table.cancelEditing();
204
                                }
205
                        }
206
                }
207
        }
208
        /**
209
         * @see com.iver.andami.plugins.IExtension#isVisible()
210
         */
211
        public boolean isVisible() {
212
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
213
                        return true;
214
                return false;
215

    
216
        }
217
}