Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAnnotations / src / com / iver / cit / gvsig / project / documents / gui / Annotation_Create.java @ 28606

History | View | Annotate | Download (9.95 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.driverManager.DriverLoadException;
57
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
58
import com.hardcode.gdbms.driver.exceptions.OpenDriverException;
59
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
63
import com.iver.cit.gvsig.fmap.MapContext;
64
import com.iver.cit.gvsig.fmap.core.FShape;
65
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
66
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
67
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
68
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
69
import com.iver.cit.gvsig.fmap.edition.IWriter;
70
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
71
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
72
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
73
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
74
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
75
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend;
76
import com.iver.cit.gvsig.gui.panels.annotation.ConfigureLabel;
77
import com.iver.utiles.SimpleFileFilter;
78

    
79

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

    
90
    public Annotation_Create(JWizardComponents wizardComponents,
91
        MapContext map, Annotation_Layer layerAnnotation) {
92
        super(wizardComponents);
93
        this.map = map;
94
        this.layerAnnotation = layerAnnotation;
95
        myWizardComponents = wizardComponents;
96
    }
97

    
98
    /**
99
     * DOCUMENT ME!
100
     */
101
    public void performAction() {
102
        myWizardComponents.getFinishButton().setEnabled(false);
103

    
104
        Annotation_FieldSelect panel1 = (Annotation_FieldSelect) myWizardComponents.getWizardPanel(0);
105
        Annotation_ConfigureLabel panel2 = (Annotation_ConfigureLabel) myWizardComponents.getWizardPanel(1);
106

    
107
        SelectableDataSource source;
108
        Annotation_Mapping mapping = new Annotation_Mapping();
109

    
110
        try {
111
            source = this.layerAnnotation.getRecordset();
112

    
113
            mapping.setColumnText(source.getFieldIndexByName(panel1.getField()));
114

    
115
            if (!panel2.getAngleFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
116
                mapping.setColumnRotate(source.getFieldIndexByName(
117
                        panel2.getAngleFieldName()));
118
            }
119

    
120
            if (!panel2.getColorFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
121
                mapping.setColumnColor(source.getFieldIndexByName(
122
                        panel2.getColorFieldName()));
123
            }
124

    
125
            if (!panel2.getSizeFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
126
                mapping.setColumnHeight(source.getFieldIndexByName(
127
                        panel2.getSizeFieldName()));
128
            }
129

    
130
            if (!panel2.getFontFieldName().equals(ConfigureLabel.TEXT_FOR_DEFAULT_VALUE)) {
131
                mapping.setColumnTypeFont(source.getFieldIndexByName(
132
                        panel2.getFontFieldName()));
133
            }
134
        } catch (ReadDriverException e) {
135
                 NotificationManager.addError(e);
136
                }
137

    
138
        try {
139
                this.layerAnnotation.setMapping(mapping);
140
//                AttrInTableLabelingStrategy labeling = new AttrInTableLabelingStrategy();
141
                ((Annotation_Legend)layerAnnotation.getLegend()).setUnits(panel2.getCmbUnits().getSelectedUnitIndex());
142
//                this.layerAnnotation.setLabelingStrategy(labeling);
143
//                ((FSymbol) this.layerAnnotation.getLegend().getDefaultSymbol()).setFontSizeInPixels(panel2.sizeUnitsInPixels());
144
            saveToShp(map, this.layerAnnotation, panel1.getDuplicate());
145
        } catch (LegendLayerException e) {
146
                         NotificationManager.addError(e);
147
                } catch (ReadDriverException e) {
148
                         NotificationManager.addError(e);
149
                }
150

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

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

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

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

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

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

    
193
                newFile = new File(path);
194

    
195
                SelectableDataSource sds = layer.getRecordset();
196
                FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
197

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

    
201
                SHPLayerDefinition lyrDefPoint = new SHPLayerDefinition();
202
                lyrDefPoint.setFieldsDesc(fieldsDescrip);
203

    
204
                File filePoints = new File(path);
205
                lyrDefPoint.setFile(filePoints);
206
                lyrDefPoint.setName(filePoints.getName());
207
                lyrDefPoint.setShapeType(FShape.POINT);
208
                writer.setFile(filePoints);
209
                writer.initialize(lyrDefPoint);
210
                driver = getOpenAnnotationDriver(filePoints);
211
                writeFeatures(mapContext, layer, writer, driver, duplicate);
212
            }
213
        } catch (InitializeWriterException e) {
214
                        throw new ReadDriverException(layerAnnotation.getName(),e);
215
                } catch (DriverLoadException e) {
216
                        throw new ReadDriverException(layerAnnotation.getName(),e);
217
                } catch (IOException e) {
218
                        throw new ReadDriverException(layerAnnotation.getName(),e);
219
                }
220
    }
221

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

    
236
        if (!filePoints.exists()) {
237
            filePoints.createNewFile();
238

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

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

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

    
251
        if (!newFileGVA.exists()) {
252
            newFileGVA.createNewFile();
253
        }
254

    
255
        drv.open(filePoints);
256

    
257
        return drv;
258
    }
259

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