Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extAnnotations / src / com / iver / cit / gvsig / project / documents / gui / Annotation_Create.java @ 11743

History | View | Annotate | Download (9.52 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42

    
43
package com.iver.cit.gvsig.project.documents.gui;
44

    
45
import java.awt.Component;
46
import java.io.File;
47
import java.io.IOException;
48

    
49
import javax.swing.JFileChooser;
50
import javax.swing.JOptionPane;
51

    
52
import jwizardcomponent.FinishAction;
53
import jwizardcomponent.JWizardComponents;
54

    
55
import com.hardcode.driverManager.Driver;
56
import com.hardcode.gdbms.engine.data.driver.DriverException;
57
import com.iver.andami.PluginServices;
58
import com.iver.cit.gvsig.fmap.MapContext;
59
import com.iver.cit.gvsig.fmap.core.FShape;
60
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
63
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
64
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
65
import com.iver.cit.gvsig.fmap.edition.EditionException;
66
import com.iver.cit.gvsig.fmap.edition.IWriter;
67
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
68
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
69
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
70
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
71
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
72
import com.iver.cit.gvsig.gui.panels.annotation.ConfigureLabel;
73
import com.iver.utiles.SimpleFileFilter;
74

    
75

    
76
/**
77
 * Dialog to create a new annotation layer.
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class Annotation_Create extends FinishAction {
82
    private JWizardComponents myWizardComponents;
83
    private MapContext map;
84
    private Annotation_Layer layerAnnotation;
85

    
86
    public Annotation_Create(JWizardComponents wizardComponents,
87
        MapContext map, Annotation_Layer layerAnnotation) {
88
        super(wizardComponents);
89
        this.map = map;
90
        this.layerAnnotation = layerAnnotation;
91
        myWizardComponents = wizardComponents;
92
    }
93

    
94
    /**
95
     * DOCUMENT ME!
96
     */
97
    public void performAction() {
98
        myWizardComponents.getFinishButton().setEnabled(false);
99

    
100
        Annotation_FieldSelect panel1 = (Annotation_FieldSelect) myWizardComponents.getWizardPanel(0);
101
        Annotation_ConfigureLabel panel2 = (Annotation_ConfigureLabel) myWizardComponents.getWizardPanel(1);
102

    
103
        SelectableDataSource source;
104
        Annotation_Mapping mapping = new Annotation_Mapping();
105

    
106
        try {
107
            source = this.layerAnnotation.getRecordset();
108

    
109
            mapping.setColumnText(source.getFieldIndexByName(panel1.getField()));
110

    
111
            if (!panel2.getAngleFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
112
                mapping.setColumnRotate(source.getFieldIndexByName(
113
                        panel2.getAngleFieldName()));
114
            }
115

    
116
            if (!panel2.getColorFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
117
                mapping.setColumnColor(source.getFieldIndexByName(
118
                        panel2.getColorFieldName()));
119
            }
120

    
121
            if (!panel2.getSizeFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
122
                mapping.setColumnHeight(source.getFieldIndexByName(
123
                        panel2.getSizeFieldName()));
124
            }
125

    
126
            if (!panel2.getFontFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
127
                mapping.setColumnTypeFont(source.getFieldIndexByName(
128
                        panel2.getFontFieldName()));
129
            }
130
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
131
            e.printStackTrace();
132

    
133
            return;
134
        } catch (DriverException e) {
135
            e.printStackTrace();
136
        }
137

    
138
        // this.layerAnnotation.setName(panel1.getNewLayerName());
139
        this.layerAnnotation.setMapping(mapping);
140
        ((FSymbol) this.layerAnnotation.getLegend().getDefaultSymbol()).setFontSizeInPixels(panel2.sizeUnitsInPixels());
141

    
142
        try {
143
            saveToShp(map, this.layerAnnotation, panel1.getDuplicate());
144
        } catch (EditionException e) {
145
            e.printStackTrace();
146
        } catch (DriverIOException e) {
147
            e.printStackTrace();
148
        }
149

    
150
        this.myWizardComponents.getCancelAction().performAction();
151
    }
152

    
153
    /**
154
     * DOCUMENT ME!
155
     *
156
     * @param mapContext DOCUMENT ME!
157
     * @param layer DOCUMENT ME!
158
     * @param duplicate DOCUMENT ME!
159
     *
160
     * @throws EditionException DOCUMENT ME!
161
     * @throws DriverIOException DOCUMENT ME!
162
     */
163
    public void saveToShp(MapContext mapContext, Annotation_Layer layer,
164
        String duplicate) throws EditionException, DriverIOException {
165
        try {
166
            JFileChooser jfc = new JFileChooser();
167
            SimpleFileFilter filterShp = new SimpleFileFilter("shp",
168
                    PluginServices.getText(this, "shp_files"));
169
            jfc.setFileFilter(filterShp);
170

    
171
            if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
172
                File newFile = jfc.getSelectedFile();
173
                String path = newFile.getAbsolutePath();
174

    
175
                if (newFile.exists()) {
176
                    int resp = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
177
                            PluginServices.getText(this,
178
                                "fichero_ya_existe_seguro_desea_guardarlo"),
179
                            PluginServices.getText(this, "guardar"),
180
                            JOptionPane.YES_NO_OPTION);
181

    
182
                    if (resp != JOptionPane.YES_OPTION) {
183
                        return;
184
                    }
185
                }
186

    
187
                if (!(path.toLowerCase().endsWith(".shp"))) {
188
                    path = path + ".shp";
189
                }
190

    
191
                newFile = new File(path);
192

    
193
                SelectableDataSource sds = layer.getRecordset();
194
                FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
195

    
196
                ShpWriter writer = (ShpWriter) LayerFactory.getWM().getWriter("Shape Writer");
197
                Driver driver = null;
198

    
199
                SHPLayerDefinition lyrDefPoint = new SHPLayerDefinition();
200
                lyrDefPoint.setFieldsDesc(fieldsDescrip);
201

    
202
                File filePoints = new File(path);
203
                lyrDefPoint.setFile(filePoints);
204
                lyrDefPoint.setName(filePoints.getName());
205
                lyrDefPoint.setShapeType(FShape.POINT);
206
                writer.setFile(filePoints);
207
                writer.initialize(lyrDefPoint);
208
                driver = getOpenAnnotationDriver(filePoints);
209
                writeFeatures(mapContext, layer, writer, driver, duplicate);
210
            }
211
        } catch (DriverException e) {
212
            e.printStackTrace();
213
            throw new EditionException(e);
214
        } catch (IOException e) {
215
            e.printStackTrace();
216
        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
217
            e.printStackTrace();
218
        }
219
    }
220

    
221
    /**
222
     * DOCUMENT ME!
223
     *
224
     * @param filePoints DOCUMENT ME!
225
     *
226
     * @return DOCUMENT ME!
227
     *
228
     * @throws IOException DOCUMENT ME!
229
     */
230
    private Driver getOpenAnnotationDriver(File filePoints)
231
        throws IOException {
232
        IndexedShpDriver drv = new IndexedShpDriver();
233

    
234
        if (!filePoints.exists()) {
235
            filePoints.createNewFile();
236

    
237
            File newFileSHX = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
238
                        ".shx"));
239
            newFileSHX.createNewFile();
240

    
241
            File newFileDBF = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
242
                        ".dbf"));
243
            newFileDBF.createNewFile();
244
        }
245

    
246
        File newFileGVA = new File(filePoints.getAbsolutePath().replaceAll("[.]shp",
247
                    ".gva"));
248

    
249
        if (!newFileGVA.exists()) {
250
            newFileGVA.createNewFile();
251
        }
252

    
253
        drv.open(filePoints);
254

    
255
        return drv;
256
    }
257

    
258
    /**
259
     * DOCUMENT ME!
260
     *
261
     * @param mapContext DOCUMENT ME!
262
     * @param layer DOCUMENT ME!
263
     * @param writer DOCUMENT ME!
264
     * @param reader DOCUMENT ME!
265
     * @param duplicate DOCUMENT ME!
266
     *
267
     * @throws DriverIOException DOCUMENT ME!
268
     * @throws com.iver.cit.gvsig.fmap.DriverException DOCUMENT ME!
269
     */
270
    private void writeFeatures(MapContext mapContext, Annotation_Layer layer,
271
        IWriter writer, Driver reader, String duplicate)
272
        throws DriverIOException, com.iver.cit.gvsig.fmap.DriverException {
273
        PluginServices.cancelableBackgroundExecution(new Annotation_TaskCreate(
274
                mapContext, layer, writer, reader, duplicate));
275
    }
276
}