Statistics
| Revision:

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

History | View | Annotate | Download (4.22 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.awt.Component;
4
import java.io.File;
5
import java.net.URL;
6

    
7
import javax.swing.JFileChooser;
8

    
9
import org.geotools.data.FeatureStore;
10
import org.geotools.data.FeatureWriter;
11
import org.geotools.data.shapefile.ShapefileDataStore;
12
import org.geotools.feature.FeatureType;
13

    
14
import com.iver.andami.PluginServices;
15
import com.iver.andami.plugins.Extension;
16
import com.iver.cit.gvsig.fmap.MapContext;
17
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
18
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
19
import com.iver.cit.gvsig.fmap.layers.FLayers;
20
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
21
import com.iver.cit.gvsig.project.documents.view.ProjectView;
22
import com.iver.cit.gvsig.project.documents.view.gui.View;
23
import com.iver.cit.gvsig.writers.WriterGT2;
24
import com.iver.utiles.SimpleFileFilter;
25

    
26

    
27
/**
28
 * DOCUMENT ME!
29
 *
30
 * @author Vicente Caballero Navarro
31
 */
32
public class StopEditingToGT2Shp extends Extension {
33
    /**
34
         * @see com.iver.andami.plugins.IExtension#initialize()
35
         */
36
    public void initialize() {
37
    }
38

    
39
    /**
40
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
41
         */
42
    public void execute(String s) {
43
        com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
44
                                                             .getActiveWindow();
45

    
46
        View vista = (View) f;
47
        ProjectView model = vista.getModel();
48
        MapContext mapa = model.getMapContext();
49
            FLayers layers = mapa.getLayers();
50
            if (s.equals("STOPEDITING")){
51
            for (int i = 0; i < layers.getLayersCount(); i++) {
52
                if (layers.getLayer(i) instanceof FLyrVect &&
53
                        layers.getLayer(i).isEditing()) {
54
                    FLyrVect lv = (FLyrVect) layers.getLayer(i);
55
                    stopEditing(lv);
56

    
57
                    return;
58
                }
59
            }
60
            }
61
            PluginServices.getMainFrame().enableControls();
62
    }
63

    
64
    /**
65
         * @see com.iver.andami.plugins.IExtension#isEnabled()
66
         */
67
    public boolean isEnabled() {
68
        return true;
69
    }
70

    
71

    
72

    
73

    
74
    /**
75
         * DOCUMENT ME!
76
         */
77
    public void stopEditing(FLyrVect layer) {
78
        try {
79
            // WriterGT2Shp writer = new WriterGT2Shp(layer);
80

    
81

    
82
            JFileChooser jfc = new JFileChooser();
83
            // if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
84
             /*
85
                                 * FLyrVect layer = (FLyrVect) test.createLayer("prueba",
86
                                 * (VectorialFileDriver) driverManager.getDriver( "gvSIG shp
87
                                 * driver"), original, ProjectionPool.get("EPSG:23030"));
88
                                 */
89
            SimpleFileFilter filterShp = new SimpleFileFilter(".shp", "Ficheros .shp");
90
            jfc.setFileFilter(filterShp);
91
             if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION)
92
             {
93
                             File newFile = jfc.getSelectedFile();
94
                             FeatureType featType = WriterGT2.getFeatureType(layer, "the_geom",
95
                                             newFile.getName());
96
                                        URL theUrl = newFile.toURL();
97
                                        ShapefileDataStore dataStore = new ShapefileDataStore(theUrl);
98
                                        dataStore.createSchema(featType);
99

    
100
                                        String featureName = dataStore.getTypeNames()[0];
101
                                        FeatureStore featStore = (FeatureStore) dataStore.getFeatureSource(featureName);
102

    
103
                                        // Necesitamos crear de verdad los ficheros antes de usarlos
104
                                        // para meter las features
105
                                        FeatureWriter featWriter = dataStore.getFeatureWriterAppend(featureName, featStore.getTransaction());
106
                                        featWriter.close();
107
                                        // Aqu? ya tenemos un fichero vac?o, listo para usar.
108

    
109

    
110
                                        WriterGT2 writer = new WriterGT2(featStore, true);
111

    
112
                            VectorialEditableAdapter vea = (VectorialEditableAdapter) layer.getSource();
113
                            vea.stopEdition(writer,EditionEvent.GRAPHIC);
114
                            layer.setSource(vea.getOriginalAdapter());
115
                            layer.setEditing(false);
116
             }
117
        } catch (Exception e) {
118
            e.printStackTrace();
119
        }
120
    }
121

    
122

    
123
    /**
124
         * @see com.iver.andami.plugins.IExtension#isVisible()
125
         */
126
    public boolean isVisible() {
127
        if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
128
                return true;
129
              return false;
130

    
131
    }
132
}
133