Statistics
| Revision:

svn-gvsig-desktop / tags / Anotaciones_Build_1 / extensions / extAnnotations / src / com / iver / cit / gvsig / project / documents / gui / Annotation_TaskCreate.java @ 11599

History | View | Annotate | Download (12.7 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
import com.hardcode.gdbms.engine.values.Value;
7
import com.hardcode.gdbms.engine.values.ValueFactory;
8

    
9
import com.iver.andami.PluginServices;
10

    
11
import com.iver.cit.gvsig.fmap.MapContext;
12
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
13
import com.iver.cit.gvsig.fmap.core.FShape;
14
import com.iver.cit.gvsig.fmap.core.IFeature;
15
import com.iver.cit.gvsig.fmap.core.IGeometry;
16
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
17
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
18
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
19
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
20
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
21
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
22
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
23
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
24
import com.iver.cit.gvsig.fmap.edition.EditionException;
25
import com.iver.cit.gvsig.fmap.edition.IWriter;
26
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
27
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
28
import com.iver.cit.gvsig.fmap.layers.FBitSet;
29
import com.iver.cit.gvsig.fmap.layers.FLayer;
30
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
31
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
32
import com.iver.cit.gvsig.fmap.layers.LayerListener;
33
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
34
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
35

    
36
import com.iver.utiles.PostProcessSupport;
37
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
38

    
39
import org.cresques.cts.ICoordTrans;
40

    
41
import java.awt.geom.Point2D;
42

    
43
import java.sql.Types;
44

    
45
import java.text.ParseException;
46

    
47
import javax.swing.JComponent;
48
import javax.swing.JOptionPane;
49

    
50

    
51
/**
52
 * Task to create the annotation layer.
53
 *
54
 * @author Vicente Caballero Navarro
55
 */
56
public class Annotation_TaskCreate extends AbstractMonitorableTask {
57
    Annotation_Layer lyrVect;
58
    IWriter writer;
59
    int rowCount;
60
    ReadableVectorial va;
61
    SelectableDataSource sds;
62
    FBitSet bitSet;
63
    MapContext mapContext;
64
    VectorialDriver reader;
65
    private Annotation_Mapping mapping;
66

    
67
    public Annotation_TaskCreate(MapContext mapContext, Annotation_Layer lyr,
68
        IWriter writer, Driver reader)
69
        throws DriverIOException, com.iver.cit.gvsig.fmap.DriverException {
70
        this.mapping = lyr.getAnnotatonMapping();
71
        this.mapContext = mapContext;
72
        this.lyrVect = lyr;
73
        this.writer = writer;
74
        this.reader = (VectorialDriver) reader;
75

    
76
        setInitialStep(0);
77
        setDeterminatedProcess(true);
78
        setStatusMessage(PluginServices.getText(this, "exportando_features"));
79

    
80
        va = lyrVect.getSource();
81
        sds = lyrVect.getRecordset();
82

    
83
        bitSet = sds.getSelection();
84

    
85
        if (bitSet.cardinality() == 0) {
86
            rowCount = va.getShapeCount();
87
        } else {
88
            rowCount = bitSet.cardinality();
89
        }
90

    
91
        setFinalStep(rowCount);
92
    }
93

    
94
    public void run() throws Exception {
95
        va.start();
96

    
97
        ICoordTrans ct = lyrVect.getCoordTrans();
98
        DriverAttributes attr = va.getDriverAttributes();
99
        boolean bMustClone = false;
100

    
101
        if (attr != null) {
102
            if (attr.isLoadedInMemory()) {
103
                bMustClone = attr.isLoadedInMemory();
104
            }
105
        }
106

    
107
        SHPLayerDefinition lyrDef = (SHPLayerDefinition) writer.getTableDefinition();
108
        lyrDef.setShapeType(FShape.POINT);
109

    
110
        FieldDescription[] fields = getFieldDescriptions();
111
        lyrDef.setFieldsDesc(fields);
112
        writer.initialize(lyrDef);
113

    
114
        // Creamos la tabla.
115
        writer.preProcess();
116

    
117
        if (bitSet.cardinality() == 0) {
118
            rowCount = va.getShapeCount();
119

    
120
            for (int i = 0; i < rowCount; i++) {
121
                IGeometry geom = va.getShape(i);
122

    
123
                if (geom.getGeometryType() != FShape.POINT) {
124
                    Point2D p = FLabel.createLabelPoint((FShape) geom.getInternalShape());
125
                    geom = ShapeFactory.createPoint2D(p.getX(), p.getY());
126
                }
127

    
128
                if (ct != null) {
129
                    if (bMustClone) {
130
                        geom = geom.cloneGeometry();
131
                    }
132

    
133
                    geom.reProject(ct);
134
                }
135

    
136
                reportStep();
137
                setNote(PluginServices.getText(this, "exporting_") +" "+ i+ "    "+rowCount);
138

    
139
                if (isCanceled()) {
140
                    break;
141
                }
142

    
143
                if (geom != null) {
144
                    Value[] valuesAnnotation = getValuesAnnotation(i);
145

    
146
                    IFeature feat = new DefaultFeature(geom, valuesAnnotation,
147
                            "" + i);
148
                    DefaultRowEdited edRow = new DefaultRowEdited(feat,
149
                            DefaultRowEdited.STATUS_ADDED, i);
150
                    writer.process(edRow);
151
                }
152
            }
153
        } else {
154
            int counter = 0;
155

    
156
            for (int i = bitSet.nextSetBit(0); i >= 0;
157
                    i = bitSet.nextSetBit(i + 1)) {
158
                IGeometry geom = va.getShape(i);
159

    
160
                if (geom.getGeometryType() != FShape.POINT) {
161
                    Point2D p = FLabel.createLabelPoint((FShape) geom.getInternalShape());
162
                    geom = ShapeFactory.createPoint2D(p.getX(), p.getY());
163
                }
164

    
165
                if (ct != null) {
166
                    if (bMustClone) {
167
                        geom = geom.cloneGeometry();
168
                    }
169

    
170
                    geom.reProject(ct);
171
                }
172

    
173
                reportStep();
174
                setNote(PluginServices.getText(this, "exporting_") +" "+ counter+ "    "+bitSet.cardinality());
175

    
176
                if (isCanceled()) {
177
                    break;
178
                }
179

    
180
                if (geom != null) {
181
                    Value[] valuesAnnotation = getValuesAnnotation(i);
182
                    IFeature feat = new DefaultFeature(geom, valuesAnnotation,
183
                            "" + i);
184
                    DefaultRowEdited edRow = new DefaultRowEdited(feat,
185
                            DefaultRowEdited.STATUS_ADDED, i);
186

    
187
                    writer.process(edRow);
188
                }
189
            }
190
        }
191

    
192
        writer.postProcess();
193
        va.stop();
194

    
195
        if (reader != null) {
196
            int res = JOptionPane.showConfirmDialog((JComponent) PluginServices.getMDIManager()
197
                                                                               .getActiveWindow(),
198
                    PluginServices.getText(this,
199
                        "insertar_en_la_vista_la_capa_creada"),
200
                    PluginServices.getText(this, "insertar_capa"),
201
                    JOptionPane.YES_NO_OPTION);
202

    
203
            if (res == JOptionPane.YES_OPTION) {
204
                PostProcessSupport.executeCalls();
205
                lyrDef = (SHPLayerDefinition) writer.getTableDefinition();
206

    
207
                FLayer newLayer = LayerFactory.createLayer(lyrDef.getName(),
208
                        reader, mapContext.getProjection());
209

    
210
                Annotation_Layer la = new Annotation_Layer();
211
                LayerListener[] layerListeners = newLayer.getLayerListeners();
212

    
213
                for (int i = 0; i < layerListeners.length; i++) {
214
                    la.addLayerListener(layerListeners[i]);
215
                }
216

    
217
                la.setSource(((FLyrVect) newLayer).getSource());
218
                la.setProjection(newLayer.getProjection());
219
                la.setName(newLayer.getName());
220
                la.setInPixels(lyrVect.isInPixels());
221
                Annotation_Mapping.addAnnotationMapping(la);
222
                mapContext.getLayers().addLayer(la);
223
            }
224
        }
225
    }
226

    
227
    private FieldDescription[] getFieldDescriptions() {
228
        SHPLayerDefinition lyrDef = (SHPLayerDefinition) writer.getTableDefinition();
229
        FieldDescription[] fields = new FieldDescription[Annotation_Mapping.NUMCOLUMNS]; //lyrDef.getFieldsDesc();
230

    
231
        int posText = mapping.getColumnText();
232
        int posTypeFont = mapping.getColumnTypeFont();
233
        int posStyleFont = mapping.getColumnStyleFont();
234
        int posColor = mapping.getColumnColor();
235
        int posHeight = mapping.getColumnHeight();
236
        int posRotate = mapping.getColumnRotate();
237

    
238
        FieldDescription[] fieldsDescriptions = lyrDef.getFieldsDesc();
239
        setField(Annotation_Mapping.TEXT, fieldsDescriptions, fields, posText, 0);
240
        setField(Annotation_Mapping.TYPEFONT, fieldsDescriptions, fields,
241
            posTypeFont, 1);
242
        setField(Annotation_Mapping.STYLEFONT, fieldsDescriptions, fields,
243
            posStyleFont, 2);
244
        setField(Annotation_Mapping.COLOR, fieldsDescriptions, fields,
245
            posColor, 3);
246
        setField(Annotation_Mapping.HEIGHT, fieldsDescriptions, fields,
247
            posHeight, 4);
248
        setField(Annotation_Mapping.ROTATE, fieldsDescriptions, fields,
249
            posRotate, 5);
250

    
251
        return fields;
252
    }
253

    
254
    private void setField(String name, FieldDescription[] antFields,
255
        FieldDescription[] fields, int pos, int i) {
256
        int type = Annotation_Mapping.getType(name);
257

    
258
        if (pos != -1) {
259
            fields[i] = antFields[pos];
260
        } else {
261
            fields[i] = new FieldDescription();
262

    
263
            if (type != Types.VARCHAR) {
264
                    if (name.equals("Color")){
265
                            fields[i].setFieldLength(10);
266
                    }
267
            } else {
268
                fields[i].setFieldLength(10);
269
                fields[i].setFieldDecimalCount(3);
270
            }
271
        }
272

    
273
        fields[i].setFieldType(type);
274
        fields[i].setFieldName(name);
275
    }
276

    
277
    private Value[] getValuesAnnotation(int i)
278
        throws DriverException, EditionException {
279
        int posText = mapping.getColumnText();
280
        int posTypeFont = mapping.getColumnTypeFont();
281
        int posStyleFont = mapping.getColumnStyleFont();
282
        int posColor = mapping.getColumnColor();
283
        int posHeight = mapping.getColumnHeight();
284
        int posRotate = mapping.getColumnRotate();
285

    
286
        Value[] values = sds.getRow(i);
287
        Value[] valuesAnnotation = new Value[Annotation_Mapping.NUMCOLUMNS];
288

    
289
        setValue(Annotation_Mapping.TEXT, values, valuesAnnotation, posText, 0);
290
        setValue(Annotation_Mapping.TYPEFONT, values, valuesAnnotation,
291
            posTypeFont, 1);
292
        setValue(Annotation_Mapping.STYLEFONT, values, valuesAnnotation,
293
            posStyleFont, 2);
294
        setValue(Annotation_Mapping.COLOR, values, valuesAnnotation,
295
                posColor, 3);
296
        setValue(Annotation_Mapping.HEIGHT, values, valuesAnnotation,
297
            posHeight, 4);
298
        setValue(Annotation_Mapping.ROTATE, values, valuesAnnotation,
299
            posRotate, 5);
300

    
301
        return valuesAnnotation;
302
    }
303

    
304
    private void setValue(String name, Value[] antValues, Value[] values,
305
        int pos, int i) {
306
        int type = Annotation_Mapping.getType(name);
307

    
308
        if (pos != -1) {
309
            try {
310
                values[i] = ValueFactory.createValueByType(antValues[pos].toString(),
311
                        type);
312
            } catch (ParseException e) {
313
                e.printStackTrace();
314
            }
315
        } else {
316
//            if (type == Types.VARCHAR) {
317
//                values[i] = ValueFactory.createNullValue();
318
//            } else {
319
                try {
320
                        if (name == Annotation_Mapping.TEXT) {
321
                        values[i] = ValueFactory.createValueByType(String.valueOf(
322
                                    Annotation_Mapping.DEFAULTTEXT), type);
323
                    }else if (name == Annotation_Mapping.COLOR) {
324
                        values[i] = ValueFactory.createValueByType(String.valueOf(
325
                                    Annotation_Mapping.DEFAULTCOLOR), type);
326
                    } else if (name == Annotation_Mapping.HEIGHT) {
327
                        values[i] = ValueFactory.createValueByType(String.valueOf(
328
                                    Annotation_Mapping.DEFAULTHEIGHT), type);
329
                    } else if (name == Annotation_Mapping.TYPEFONT) {
330
                        values[i] = ValueFactory.createValueByType(String.valueOf(
331
                                Annotation_Mapping.DEFAULTTYPEFONT), type);
332
                    } else if (name == Annotation_Mapping.STYLEFONT) {
333
                        values[i] = ValueFactory.createValueByType(String.valueOf(
334
                                    Annotation_Mapping.DEFAULTSTYLEFONT), type);
335
                    } else if (name == Annotation_Mapping.ROTATE) {
336
                        values[i] = ValueFactory.createValueByType(String.valueOf(
337
                                    Annotation_Mapping.DEFAULTROTATE), type);
338
                    }
339
                } catch (ParseException e) {
340
                    e.printStackTrace();
341
                }
342
//            }
343
        }
344
    }
345
}