Statistics
| Revision:

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

History | View | Annotate | Download (15.6 KB)

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

    
3
import java.awt.geom.Point2D;
4
import java.awt.geom.Rectangle2D;
5
import java.sql.Types;
6
import java.text.ParseException;
7
import java.util.HashMap;
8
import java.util.Iterator;
9

    
10
import javax.swing.JComponent;
11
import javax.swing.JOptionPane;
12

    
13
import org.cresques.cts.ICoordTrans;
14

    
15
import com.hardcode.driverManager.Driver;
16
import com.hardcode.gdbms.engine.data.driver.DriverException;
17
import com.hardcode.gdbms.engine.values.Value;
18
import com.hardcode.gdbms.engine.values.ValueFactory;
19
import com.iver.andami.PluginServices;
20
import com.iver.cit.gvsig.fmap.MapContext;
21
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
22
import com.iver.cit.gvsig.fmap.core.FShape;
23
import com.iver.cit.gvsig.fmap.core.IFeature;
24
import com.iver.cit.gvsig.fmap.core.IGeometry;
25
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
26
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
27
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
28
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
29
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
30
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
31
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
32
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
33
import com.iver.cit.gvsig.fmap.edition.EditionException;
34
import com.iver.cit.gvsig.fmap.edition.IWriter;
35
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
36
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
37
import com.iver.cit.gvsig.fmap.layers.FBitSet;
38
import com.iver.cit.gvsig.fmap.layers.FLayer;
39
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
40
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
41
import com.iver.cit.gvsig.fmap.layers.LayerListener;
42
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
43
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
44
import com.iver.utiles.PostProcessSupport;
45
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
46

    
47
/**
48
 * Task to create the annotation layer.
49
 *
50
 * @author Vicente Caballero Navarro
51
 */
52
public class Annotation_TaskCreate extends AbstractMonitorableTask {
53
        Annotation_Layer lyrVect;
54

    
55
        IWriter writer;
56

    
57
        int rowCount;
58

    
59
        ReadableVectorial va;
60

    
61
        SelectableDataSource sds;
62

    
63
        FBitSet bitSet;
64

    
65
        MapContext mapContext;
66

    
67
        VectorialDriver reader;
68

    
69
        private Annotation_Mapping mapping;
70

    
71
        private String duplicate;
72

    
73
        private HashMap mapGeom = new HashMap();
74

    
75
        private HashMap mapAttri = new HashMap();
76

    
77
        public Annotation_TaskCreate(MapContext mapContext, Annotation_Layer lyr,
78
                        IWriter writer, Driver reader, String duplicate)
79
                        throws DriverIOException, com.iver.cit.gvsig.fmap.DriverException {
80
                this.duplicate = duplicate;
81
                this.mapping = lyr.getAnnotatonMapping();
82
                this.mapContext = mapContext;
83
                this.lyrVect = lyr;
84
                this.writer = writer;
85
                this.reader = (VectorialDriver) reader;
86

    
87
                setInitialStep(0);
88
                setDeterminatedProcess(true);
89
                setStatusMessage(PluginServices.getText(this, "exporting_") + ": "
90
                                + PluginServices.getText(this, "annotations"));
91

    
92
                va = lyrVect.getSource();
93
                sds = lyrVect.getRecordset();
94

    
95
                bitSet = sds.getSelection();
96

    
97
                if (bitSet.cardinality() == 0) {
98
                        rowCount = va.getShapeCount();
99
                } else {
100
                        rowCount = bitSet.cardinality();
101
                }
102

    
103
                setFinalStep(rowCount);
104
        }
105

    
106
        public void run() throws Exception {
107
                if (duplicate.equals(PluginServices.getText(this, "centered"))) {
108
                        duplicate();
109
                }
110
                // va.start();
111

    
112
                ICoordTrans ct = lyrVect.getCoordTrans();
113
                DriverAttributes attr = va.getDriverAttributes();
114
                boolean bMustClone = false;
115

    
116
                if (attr != null) {
117
                        if (attr.isLoadedInMemory()) {
118
                                bMustClone = attr.isLoadedInMemory();
119
                        }
120
                }
121

    
122
                SHPLayerDefinition lyrDef = (SHPLayerDefinition) writer
123
                                .getTableDefinition();
124
                lyrDef.setShapeType(FShape.POINT);
125

    
126
                FieldDescription[] fields = getFieldDescriptions();
127
                lyrDef.setFieldsDesc(fields);
128
                writer.initialize(lyrDef);
129

    
130
                // Creamos la tabla.
131
                writer.preProcess();
132

    
133
                if (duplicate.equals(PluginServices.getText(this, "centered"))) {
134

    
135
                        int pos = 0;
136
                        Iterator iter = mapGeom.keySet().iterator();
137
                        while (iter.hasNext()) {
138
                                String textValue = (String) iter.next();
139
                                Rectangle2D rectangle = (Rectangle2D) mapGeom.get(textValue);
140
                                IGeometry geom = ShapeFactory.createPoint2D(rectangle
141
                                                .getCenterX(), rectangle.getCenterY());
142
                                if (ct != null) {
143
                                        if (bMustClone) {
144
                                                geom = geom.cloneGeometry();
145
                                        }
146

    
147
                                        geom.reProject(ct);
148
                                }
149
                                reportStep();
150
                                setNote(PluginServices.getText(this, "exporting_") + " " + pos
151
                                                + "    " + mapGeom.size());
152
                                Value[] values = (Value[]) mapAttri.get(textValue);
153
                                IFeature feat = new DefaultFeature(geom, values, "" + pos);
154
                                DefaultRowEdited edRow = new DefaultRowEdited(feat,
155
                                                DefaultRowEdited.STATUS_ADDED, pos);
156
                                writer.process(edRow);
157
                                pos++;
158
                        }
159

    
160
                } else if (duplicate.equals(PluginServices.getText(this, "none"))) {
161
                        va.start();
162
                        if (bitSet.cardinality() == 0) {
163
                                rowCount = va.getShapeCount();
164

    
165
                                for (int i = 0; i < rowCount; i++) {
166
                                        IGeometry geom = va.getShape(i);
167

    
168
                                        if (geom.getGeometryType() != FShape.POINT) {
169
                                                Point2D p = FLabel.createLabelPoint((FShape) geom
170
                                                                .getInternalShape());
171
                                                geom = ShapeFactory.createPoint2D(p.getX(), p.getY());
172
                                        }
173

    
174
                                        if (ct != null) {
175
                                                if (bMustClone) {
176
                                                        geom = geom.cloneGeometry();
177
                                                }
178

    
179
                                                geom.reProject(ct);
180
                                        }
181

    
182
                                        reportStep();
183
                                        setNote(PluginServices.getText(this, "exporting_") + " "
184
                                                        + i + "    " + rowCount);
185

    
186
                                        if (isCanceled()) {
187
                                                break;
188
                                        }
189

    
190
                                        if (geom != null) {
191
                                                Value[] valuesAnnotation = getValuesAnnotation(i);
192

    
193
                                                IFeature feat = new DefaultFeature(geom,
194
                                                                valuesAnnotation, "" + i);
195
                                                DefaultRowEdited edRow = new DefaultRowEdited(feat,
196
                                                                DefaultRowEdited.STATUS_ADDED, i);
197
                                                writer.process(edRow);
198
                                        }
199
                                }
200
                        } else {
201
                                int counter = 0;
202

    
203
                                for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet
204
                                                .nextSetBit(i + 1)) {
205
                                        IGeometry geom = va.getShape(i);
206

    
207
                                        if (geom.getGeometryType() != FShape.POINT) {
208
                                                Point2D p = FLabel.createLabelPoint((FShape) geom
209
                                                                .getInternalShape());
210
                                                geom = ShapeFactory.createPoint2D(p.getX(), p.getY());
211
                                        }
212

    
213
                                        if (ct != null) {
214
                                                if (bMustClone) {
215
                                                        geom = geom.cloneGeometry();
216
                                                }
217

    
218
                                                geom.reProject(ct);
219
                                        }
220

    
221
                                        reportStep();
222
                                        setNote(PluginServices.getText(this, "exporting_") + " "
223
                                                        + counter + "    " + bitSet.cardinality());
224

    
225
                                        if (isCanceled()) {
226
                                                break;
227
                                        }
228

    
229
                                        if (geom != null) {
230
                                                Value[] valuesAnnotation = getValuesAnnotation(i);
231
                                                IFeature feat = new DefaultFeature(geom,
232
                                                                valuesAnnotation, "" + i);
233
                                                DefaultRowEdited edRow = new DefaultRowEdited(feat,
234
                                                                DefaultRowEdited.STATUS_ADDED, i);
235

    
236
                                                writer.process(edRow);
237
                                        }
238
                                        va.stop();
239
                                }
240
                        }
241
                }
242

    
243
                writer.postProcess();
244

    
245
                //
246
                // if (bitSet.cardinality() == 0) {
247
                // rowCount = va.getShapeCount();
248
                //
249
                // for (int i = 0; i < rowCount; i++) {
250
                // IGeometry geom = va.getShape(i);
251
                //
252
                // if (geom.getGeometryType() != FShape.POINT) {
253
                // Point2D p = FLabel.createLabelPoint((FShape)
254
                // geom.getInternalShape());
255
                // geom = ShapeFactory.createPoint2D(p.getX(), p.getY());
256
                // }
257
                //
258
                // if (ct != null) {
259
                // if (bMustClone) {
260
                // geom = geom.cloneGeometry();
261
                // }
262
                //
263
                // geom.reProject(ct);
264
                // }
265
                //
266
                // reportStep();
267
                // setNote(PluginServices.getText(this, "exporting_") +" "+ i+ "
268
                // "+rowCount);
269
                //
270
                // if (isCanceled()) {
271
                // break;
272
                // }
273
                //
274
                // if (geom != null) {
275
                // Value[] valuesAnnotation = getValuesAnnotation(i);
276
                //
277
                // IFeature feat = new DefaultFeature(geom, valuesAnnotation,
278
                // "" + i);
279
                // DefaultRowEdited edRow = new DefaultRowEdited(feat,
280
                // DefaultRowEdited.STATUS_ADDED, i);
281
                // writer.process(edRow);
282
                // }
283
                // }
284
                // } else {
285
                // int counter = 0;
286
                //
287
                // for (int i = bitSet.nextSetBit(0); i >= 0;
288
                // i = bitSet.nextSetBit(i + 1)) {
289
                // IGeometry geom = va.getShape(i);
290
                //
291
                // if (geom.getGeometryType() != FShape.POINT) {
292
                // Point2D p = FLabel.createLabelPoint((FShape)
293
                // geom.getInternalShape());
294
                // geom = ShapeFactory.createPoint2D(p.getX(), p.getY());
295
                // }
296
                //
297
                // if (ct != null) {
298
                // if (bMustClone) {
299
                // geom = geom.cloneGeometry();
300
                // }
301
                //
302
                // geom.reProject(ct);
303
                // }
304
                //
305
                // reportStep();
306
                // setNote(PluginServices.getText(this, "exporting_") +" "+ counter+ "
307
                // "+bitSet.cardinality());
308
                //
309
                // if (isCanceled()) {
310
                // break;
311
                // }
312
                //
313
                // if (geom != null) {
314
                // Value[] valuesAnnotation = getValuesAnnotation(i);
315
                // IFeature feat = new DefaultFeature(geom, valuesAnnotation,
316
                // "" + i);
317
                // DefaultRowEdited edRow = new DefaultRowEdited(feat,
318
                // DefaultRowEdited.STATUS_ADDED, i);
319
                //
320
                // writer.process(edRow);
321
                // }
322
                // }
323
                // }
324
                //
325
                // writer.postProcess();
326
                // va.stop();
327
                mapAttri.clear();
328
                mapGeom.clear();
329
                if (reader != null) {
330
                        int res = JOptionPane.showConfirmDialog((JComponent) PluginServices
331
                                        .getMDIManager().getActiveWindow(), PluginServices.getText(
332
                                        this, "insertar_en_la_vista_la_capa_creada"),
333
                                        PluginServices.getText(this, "insertar_capa"),
334
                                        JOptionPane.YES_NO_OPTION);
335

    
336
                        if (res == JOptionPane.YES_OPTION) {
337
                                PostProcessSupport.executeCalls();
338
                                lyrDef = (SHPLayerDefinition) writer.getTableDefinition();
339

    
340
                                FLayer newLayer = LayerFactory.createLayer(lyrDef.getName(),
341
                                                reader, mapContext.getProjection());
342

    
343
                                Annotation_Layer la = new Annotation_Layer();
344
                                LayerListener[] layerListeners = newLayer.getLayerListeners();
345

    
346
                                for (int i = 0; i < layerListeners.length; i++) {
347
                                        la.addLayerListener(layerListeners[i]);
348
                                }
349

    
350
                                la.setSource(((FLyrVect) newLayer).getSource());
351
                                la.setProjection(newLayer.getProjection());
352
                                la.setName(newLayer.getName());
353
                                la.setInPixels(lyrVect.isInPixels());
354
                                Annotation_Mapping.addAnnotationMapping(la);
355
                                mapContext.getLayers().addLayer(la);
356
                        }
357
                }
358
        }
359

    
360
        private FieldDescription[] getFieldDescriptions() {
361
                SHPLayerDefinition lyrDef = (SHPLayerDefinition) writer
362
                                .getTableDefinition();
363
                FieldDescription[] fields = new FieldDescription[Annotation_Mapping.NUMCOLUMNS]; // lyrDef.getFieldsDesc();
364

    
365
                int posText = mapping.getColumnText();
366
                int posTypeFont = mapping.getColumnTypeFont();
367
                int posStyleFont = mapping.getColumnStyleFont();
368
                int posColor = mapping.getColumnColor();
369
                int posHeight = mapping.getColumnHeight();
370
                int posRotate = mapping.getColumnRotate();
371

    
372
                FieldDescription[] fieldsDescriptions = lyrDef.getFieldsDesc();
373
                setField(Annotation_Mapping.TEXT, fieldsDescriptions, fields, posText,
374
                                0);
375
                setField(Annotation_Mapping.TYPEFONT, fieldsDescriptions, fields,
376
                                posTypeFont, 1);
377
                setField(Annotation_Mapping.STYLEFONT, fieldsDescriptions, fields,
378
                                posStyleFont, 2);
379
                setField(Annotation_Mapping.COLOR, fieldsDescriptions, fields,
380
                                posColor, 3);
381
                setField(Annotation_Mapping.HEIGHT, fieldsDescriptions, fields,
382
                                posHeight, 4);
383
                setField(Annotation_Mapping.ROTATE, fieldsDescriptions, fields,
384
                                posRotate, 5);
385

    
386
                return fields;
387
        }
388

    
389
        private void setField(String name, FieldDescription[] antFields,
390
                        FieldDescription[] fields, int pos, int i) {
391
                int type = Annotation_Mapping.getType(name);
392

    
393
                if (pos != -1) {
394
                        fields[i] = antFields[pos];
395
                } else {
396
                        fields[i] = new FieldDescription();
397

    
398
                        if (type != Types.VARCHAR) {
399
                                if (name.equals("Color")) {
400
                                        fields[i].setFieldLength(10);
401
                                }
402
                        }
403
//                        else {
404
//                                fields[i].setFieldLength(10);
405
//                                fields[i].setFieldDecimalCount(3);
406
//                        }
407
                }
408

    
409
                fields[i].setFieldType(type);
410
                fields[i].setFieldName(name);
411
        }
412

    
413
        private Value[] getValuesAnnotation(int i) throws DriverException,
414
                        EditionException {
415
                int posText = mapping.getColumnText();
416
                int posTypeFont = mapping.getColumnTypeFont();
417
                int posStyleFont = mapping.getColumnStyleFont();
418
                int posColor = mapping.getColumnColor();
419
                int posHeight = mapping.getColumnHeight();
420
                int posRotate = mapping.getColumnRotate();
421

    
422
                Value[] values = sds.getRow(i);
423
                Value[] valuesAnnotation = new Value[Annotation_Mapping.NUMCOLUMNS];
424

    
425
                setValue(Annotation_Mapping.TEXT, values, valuesAnnotation, posText, 0);
426
                setValue(Annotation_Mapping.TYPEFONT, values, valuesAnnotation,
427
                                posTypeFont, 1);
428
                setValue(Annotation_Mapping.STYLEFONT, values, valuesAnnotation,
429
                                posStyleFont, 2);
430
                setValue(Annotation_Mapping.COLOR, values, valuesAnnotation, posColor,
431
                                3);
432
                setValue(Annotation_Mapping.HEIGHT, values, valuesAnnotation,
433
                                posHeight, 4);
434
                setValue(Annotation_Mapping.ROTATE, values, valuesAnnotation,
435
                                posRotate, 5);
436

    
437
                return valuesAnnotation;
438
        }
439

    
440
        private void setValue(String name, Value[] antValues, Value[] values,
441
                        int pos, int i) {
442
                int type = Annotation_Mapping.getType(name);
443

    
444
                if (pos != -1) {
445
                        try {
446
                                values[i] = ValueFactory.createValueByType(antValues[pos]
447
                                                .toString(), type);
448
                        } catch (ParseException e) {
449
                                e.printStackTrace();
450
                        }
451
                } else {
452
                        // if (type == Types.VARCHAR) {
453
                        // values[i] = ValueFactory.createNullValue();
454
                        // } else {
455
                        try {
456
                                if (name == Annotation_Mapping.TEXT) {
457
                                        values[i] = ValueFactory.createValueByType(String
458
                                                        .valueOf(Annotation_Mapping.DEFAULTTEXT), type);
459
                                } else if (name == Annotation_Mapping.COLOR) {
460
                                        values[i] = ValueFactory.createValueByType(String
461
                                                        .valueOf(Annotation_Mapping.DEFAULTCOLOR), type);
462
                                } else if (name == Annotation_Mapping.HEIGHT) {
463
                                        values[i] = ValueFactory.createValueByType(String
464
                                                        .valueOf(Annotation_Mapping.DEFAULTHEIGHT), type);
465
                                } else if (name == Annotation_Mapping.TYPEFONT) {
466
                                        values[i] = ValueFactory.createValueByType(String
467
                                                        .valueOf(Annotation_Mapping.DEFAULTTYPEFONT), type);
468
                                } else if (name == Annotation_Mapping.STYLEFONT) {
469
                                        values[i] = ValueFactory
470
                                                        .createValueByType(
471
                                                                        String
472
                                                                                        .valueOf(Annotation_Mapping.DEFAULTSTYLEFONT),
473
                                                                        type);
474
                                } else if (name == Annotation_Mapping.ROTATE) {
475
                                        values[i] = ValueFactory.createValueByType(String
476
                                                        .valueOf(Annotation_Mapping.DEFAULTROTATE), type);
477
                                }
478
                        } catch (ParseException e) {
479
                                e.printStackTrace();
480
                        }
481
                        // }
482
                }
483
        }
484

    
485
        private void duplicate() throws DriverIOException, DriverException,
486
                        EditionException {
487
                va.start();
488
                if (bitSet.cardinality() == 0) {
489
                        rowCount = va.getShapeCount();
490

    
491
                        for (int i = 0; i < rowCount; i++) {
492
                                IGeometry geom = va.getShape(i);
493
                                modifyMap(geom, i);
494
                                // reportStep();
495
                                // setNote(PluginServices.getText(this, "exporting_") +" "+ i+ "
496
                                // "+rowCount);
497

    
498
                                if (isCanceled()) {
499
                                        break;
500
                                }
501
                        }
502
                } else {
503
                        for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet
504
                                        .nextSetBit(i + 1)) {
505
                                IGeometry geom = va.getShape(i);
506
                                modifyMap(geom, i);
507
                                // reportStep();
508
                                // setNote(PluginServices.getText(this, "exporting_") +" "+
509
                                // counter+ " "+bitSet.cardinality());
510

    
511
                                if (isCanceled()) {
512
                                        break;
513
                                }
514
                        }
515
                }
516

    
517
                va.stop();
518

    
519
        }
520

    
521
        private void modifyMap(IGeometry geom, int index) throws DriverException,
522
                        EditionException {
523
                if (geom.getGeometryType() != FShape.POINT) {
524
                        Point2D p = FLabel.createLabelPoint((FShape) geom
525
                                        .getInternalShape());
526
                        geom = ShapeFactory.createPoint2D(p.getX(), p.getY());
527
                }
528
                if (geom != null) {
529
                        Value[] valuesAnnotation = getValuesAnnotation(index);
530
                        String textValue = valuesAnnotation[0].toString();
531
                        if (!mapAttri.containsKey(textValue)) {
532
                                mapAttri.put(textValue, valuesAnnotation);
533
                        }
534
                        if (mapGeom.containsKey(textValue)) {
535
                                IGeometry geometry2 = geom;
536
                                Rectangle2D rectangle1 = (Rectangle2D) mapGeom.get(textValue);
537
                                // FPoint2D pointNew = new
538
                                // FPoint2D((point1.getX()+point2.getX())/2,(point1.getY()+point2.getY())/2);
539

    
540
                                mapGeom.put(textValue, rectangle1.createUnion(geometry2
541
                                                .getBounds2D()));
542
                                reportStep();
543
                                setNote(PluginServices.getText(this, "exporting_"));
544
                        } else {
545
                                mapGeom.put(textValue, geom.getBounds2D());
546
                        }
547
                }
548
        }
549
}