Statistics
| Revision:

root / trunk / extensions / extAnnotations / src / com / iver / cit / gvsig / project / documents / gui / Annotation_Create.java @ 11536

History | View | Annotate | Download (7.4 KB)

1
package com.iver.cit.gvsig.project.documents.gui;
2

    
3
import com.hardcode.driverManager.Driver;
4

    
5
import com.hardcode.gdbms.engine.data.driver.DriverException;
6

    
7
import com.iver.andami.PluginServices;
8

    
9
import com.iver.cit.gvsig.fmap.MapContext;
10
import com.iver.cit.gvsig.fmap.core.FShape;
11
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
12
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
13
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
14
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
15
import com.iver.cit.gvsig.fmap.edition.EditionException;
16
import com.iver.cit.gvsig.fmap.edition.IWriter;
17
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
18
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
19
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
20
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
21
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
22
import com.iver.cit.gvsig.gui.panels.annotation.ConfigureLabel;
23

    
24
import com.iver.utiles.SimpleFileFilter;
25

    
26
import jwizardcomponent.FinishAction;
27
import jwizardcomponent.JWizardComponents;
28

    
29
import java.awt.Component;
30

    
31
import java.io.File;
32
import java.io.IOException;
33

    
34
import javax.swing.JFileChooser;
35
import javax.swing.JOptionPane;
36

    
37

    
38
/**
39
 * Dialog to create a new annotation layer.
40
 *
41
 * @author Vicente Caballero Navarro
42
 */
43
public class Annotation_Create extends FinishAction {
44
    private JWizardComponents myWizardComponents;
45
    private MapContext map;
46
    private Annotation_Layer layerAnnotation;
47

    
48
    public Annotation_Create(JWizardComponents wizardComponents,
49
        MapContext map, Annotation_Layer layerAnnotation) {
50
        super(wizardComponents);
51
        this.map = map;
52
        this.layerAnnotation = layerAnnotation;
53
        myWizardComponents = wizardComponents;
54
    }
55

    
56
    public void performAction() {
57
        myWizardComponents.getFinishButton().setEnabled(false);
58

    
59
        Annotation_FieldSelect panel1 = (Annotation_FieldSelect) myWizardComponents.getWizardPanel(0);
60
        Annotation_ConfigureLabel panel2 = (Annotation_ConfigureLabel) myWizardComponents.getWizardPanel(1);
61

    
62
        SelectableDataSource source;
63
        Annotation_Mapping mapping = new Annotation_Mapping();
64

    
65
        try {
66
            source = this.layerAnnotation.getRecordset();
67

    
68
            mapping.setColumnText(source.getFieldIndexByName(panel1.getField()));
69

    
70
            if (!panel2.getAngleFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
71
                mapping.setColumnRotate(source.getFieldIndexByName(
72
                        panel2.getAngleFieldName()));
73
            }
74

    
75
            if (!panel2.getColorFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
76
                mapping.setColumnColor(source.getFieldIndexByName(
77
                        panel2.getColorFieldName()));
78
            }
79

    
80
            if (!panel2.getSizeFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
81
                mapping.setColumnHeight(source.getFieldIndexByName(
82
                        panel2.getSizeFieldName()));
83
            }
84

    
85
            this.layerAnnotation.setInPixels(panel2.sizeUnitsInPixels());
86

    
87
            if (!panel2.getFontFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
88
                mapping.setColumnTypeFont(source.getFieldIndexByName(
89
                        panel2.getFontFieldName()));
90
            }
91
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
92
            e.printStackTrace();
93

    
94
            return;
95
        } catch (DriverException e) {
96
            e.printStackTrace();
97
        }
98

    
99
        //                this.layerAnnotation.setName(panel1.getNewLayerName());
100
        this.layerAnnotation.setMapping(mapping);
101

    
102
        try {
103
            saveToShp(map, this.layerAnnotation);
104
        } catch (EditionException e) {
105
            e.printStackTrace();
106
        } catch (DriverIOException e) {
107
            e.printStackTrace();
108
        }
109

    
110
        this.myWizardComponents.getCancelAction().performAction();
111
    }
112

    
113
    public void saveToShp(MapContext mapContext, Annotation_Layer layer)
114
        throws EditionException, DriverIOException {
115
        try {
116
            JFileChooser jfc = new JFileChooser();
117
            SimpleFileFilter filterShp = new SimpleFileFilter("shp",
118
                    PluginServices.getText(this, "shp_files"));
119
            jfc.setFileFilter(filterShp);
120

    
121
            if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
122
                File newFile = jfc.getSelectedFile();
123
                String path = newFile.getAbsolutePath();
124

    
125
                if (newFile.exists()) {
126
                    int resp = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
127
                            PluginServices.getText(this,
128
                                "fichero_ya_existe_seguro_desea_guardarlo"),
129
                            PluginServices.getText(this, "guardar"),
130
                            JOptionPane.YES_NO_OPTION);
131

    
132
                    if (resp != JOptionPane.YES_OPTION) {
133
                        return;
134
                    }
135
                }
136

    
137
                if (!(path.toLowerCase().endsWith(".shp"))) {
138
                    path = path + ".shp";
139
                }
140

    
141
                newFile = new File(path);
142

    
143
                SelectableDataSource sds = layer.getRecordset();
144
                FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
145

    
146
                ShpWriter writer = (ShpWriter) LayerFactory.getWM().getWriter("Shape Writer");
147
                Driver driver = null;
148

    
149
                SHPLayerDefinition lyrDefPoint = new SHPLayerDefinition();
150
                lyrDefPoint.setFieldsDesc(fieldsDescrip);
151

    
152
                File filePoints = new File(path);
153
                lyrDefPoint.setFile(filePoints);
154
                lyrDefPoint.setName(filePoints.getName());
155
                lyrDefPoint.setShapeType(FShape.POINT);
156
                writer.setFile(filePoints);
157
                writer.initialize(lyrDefPoint);
158
                driver = getOpenAnnotationDriver(filePoints);
159
                writeFeatures(mapContext, layer, writer, driver);
160
            }
161
        } catch (DriverException e) {
162
            e.printStackTrace();
163
            throw new EditionException(e);
164
        } catch (IOException e) {
165
            e.printStackTrace();
166
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
167
            e.printStackTrace();
168
        }
169
    }
170

    
171
    private Driver getOpenAnnotationDriver(File filePoints)
172
        throws IOException {
173
        IndexedShpDriver drv = new IndexedShpDriver();
174

    
175
        if (!filePoints.exists()) {
176
            filePoints.createNewFile();
177

    
178
            File newFileSHX = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
179
                        ".shx"));
180
            newFileSHX.createNewFile();
181

    
182
            File newFileDBF = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
183
                        ".dbf"));
184
            newFileDBF.createNewFile();
185
        }
186

    
187
        File newFileGVA = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
188
                    ".gva"));
189

    
190
        if (!newFileGVA.exists()) {
191
            newFileGVA.createNewFile();
192
        }
193

    
194
        drv.open(filePoints);
195

    
196
        return drv;
197
    }
198

    
199
    private void writeFeatures(MapContext mapContext, Annotation_Layer layer,
200
        IWriter writer, Driver reader)
201
        throws DriverIOException, com.iver.cit.gvsig.fmap.DriverException {
202
        PluginServices.cancelableBackgroundExecution(new Annotation_TaskCreate(
203
                mapContext, layer, writer, reader));
204
    }
205
}