Statistics
| Revision:

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

History | View | Annotate | Download (19.5 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.geom.Point2D;
46
import java.awt.geom.Rectangle2D;
47
import java.sql.Types;
48
import java.text.ParseException;
49
import java.util.HashMap;
50
import java.util.Iterator;
51

    
52
import javax.swing.JComponent;
53
import javax.swing.JOptionPane;
54

    
55
import org.cresques.cts.ICoordTrans;
56

    
57
import com.hardcode.driverManager.Driver;
58
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
59
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
60
import com.hardcode.gdbms.engine.data.driver.DriverException;
61
import com.hardcode.gdbms.engine.values.NumericValue;
62
import com.hardcode.gdbms.engine.values.Value;
63
import com.hardcode.gdbms.engine.values.ValueFactory;
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.messages.NotificationManager;
66
import com.iver.cit.gvsig.AddLayer;
67
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
68
import com.iver.cit.gvsig.fmap.MapContext;
69
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
70
import com.iver.cit.gvsig.fmap.core.FShape;
71
import com.iver.cit.gvsig.fmap.core.IFeature;
72
import com.iver.cit.gvsig.fmap.core.IGeometry;
73
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
74
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
75
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
76
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
77
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
78
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
79
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
80
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
81
import com.iver.cit.gvsig.fmap.edition.IWriter;
82
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
83
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
84
import com.iver.cit.gvsig.fmap.layers.FBitSet;
85
import com.iver.cit.gvsig.fmap.layers.FLayer;
86
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
87
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
88
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
89
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
90
import com.iver.utiles.PostProcessSupport;
91
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
92

    
93

    
94
/**
95
 * Task to create the annotation layer.
96
 *
97
 * @author Vicente Caballero Navarro
98
 */
99
public class Annotation_TaskCreate extends AbstractMonitorableTask {
100
    Annotation_Layer lyrVect;
101
    IWriter writer;
102
    int rowCount;
103
    ReadableVectorial va;
104
    SelectableDataSource sds;
105
    FBitSet bitSet;
106
    MapContext mapContext;
107
    VectorialDriver reader;
108
    private Annotation_Mapping mapping;
109
    private String duplicate;
110
    private HashMap mapGeom = new HashMap();
111
    private HashMap mapAttri = new HashMap();
112

    
113
    public Annotation_TaskCreate(MapContext mapContext, Annotation_Layer lyr,
114
        IWriter writer, Driver reader, String duplicate) throws ReadDriverException{
115
        this.duplicate = duplicate;
116
        this.mapping = lyr.getAnnotatonMapping();
117
        this.mapContext = mapContext;
118
        this.lyrVect = lyr;
119
        this.writer = writer;
120
        this.reader = (VectorialDriver) reader;
121

    
122
        setInitialStep(0);
123
        setDeterminatedProcess(true);
124
        setStatusMessage(PluginServices.getText(this, "exporting_") + ": " +
125
            PluginServices.getText(this, "annotations"));
126

    
127
        va = lyrVect.getSource();
128
        sds = lyrVect.getRecordset();
129

    
130
        bitSet = sds.getSelection();
131

    
132
        if (bitSet.cardinality() == 0) {
133
            rowCount = va.getShapeCount();
134
        } else {
135
            rowCount = bitSet.cardinality();
136
        }
137

    
138
        setFinalStep(rowCount);
139
    }
140

    
141
    /**
142
     * DOCUMENT ME!
143
     *
144
     * @throws Exception DOCUMENT ME!
145
     */
146
    public void run() throws Exception {
147
        if (duplicate.equals(PluginServices.getText(this, "centered"))) {
148
            duplicate();
149
        }
150

    
151
        // va.start();
152
        ICoordTrans ct = lyrVect.getCoordTrans();
153
        DriverAttributes attr = va.getDriverAttributes();
154
        boolean bMustClone = false;
155

    
156
        if (attr != null) {
157
            if (attr.isLoadedInMemory()) {
158
                bMustClone = attr.isLoadedInMemory();
159
            }
160
        }
161

    
162
        SHPLayerDefinition lyrDef = (SHPLayerDefinition) writer.getTableDefinition();
163
        lyrDef.setShapeType(FShape.POINT);
164

    
165
        FieldDescription[] fields = getFieldDescriptions();
166
        lyrDef.setFieldsDesc(fields);
167
        writer.initialize(lyrDef);
168

    
169
        // Creamos la tabla.
170
        writer.preProcess();
171

    
172
        if (duplicate.equals(PluginServices.getText(this, "centered"))) {
173
            int pos = 0;
174
            Iterator iter = mapGeom.keySet().iterator();
175

    
176
            while (iter.hasNext()) {
177
                String textValue = (String) iter.next();
178
                Rectangle2D rectangle = (Rectangle2D) mapGeom.get(textValue);
179
                IGeometry geom = ShapeFactory.createPoint2D(rectangle.getCenterX(),
180
                        rectangle.getCenterY());
181

    
182
                if (ct != null) {
183
                    if (bMustClone) {
184
                        geom = geom.cloneGeometry();
185
                    }
186

    
187
                    geom.reProject(ct);
188
                }
189

    
190
                reportStep();
191
                setNote(PluginServices.getText(this, "exporting_") + " " + pos +
192
                    "    " + mapGeom.size());
193

    
194
                Value[] values = (Value[]) mapAttri.get(textValue);
195
                IFeature feat = new DefaultFeature(geom, values, "" + pos);
196
                DefaultRowEdited edRow = new DefaultRowEdited(feat,
197
                        DefaultRowEdited.STATUS_ADDED, pos);
198
                writer.process(edRow);
199
                pos++;
200
            }
201
        } else if (duplicate.equals(PluginServices.getText(this, "duplicate.none"))) {
202
            va.start();
203

    
204
            if (bitSet.cardinality() == 0) {
205
                rowCount = va.getShapeCount();
206

    
207
                for (int i = 0; i < rowCount; i++) {
208
                    IGeometry geom = va.getShape(i);
209
                    if (geom != null) {
210
                                                if (geom.getGeometryType() != FShape.POINT) {
211
                                                        Point2D p = FLabel.createLabelPoint((FShape) geom
212
                                                                        .getInternalShape());
213
                                                        geom = ShapeFactory.createPoint2D(p.getX(), p
214
                                                                        .getY());
215
                                                }
216

    
217
                                                if (ct != null) {
218
                                                        if (bMustClone) {
219
                                                                geom = geom.cloneGeometry();
220
                                                        }
221

    
222
                                                        geom.reProject(ct);
223
                                                }
224

    
225
                                                reportStep();
226
                                                setNote(PluginServices.getText(this, "exporting_")
227
                                                                + " " + i + "    " + rowCount);
228

    
229
                                                if (isCanceled()) {
230
                                                        break;
231
                                                }
232

    
233
                                                if (geom != null) {
234
                                                        Value[] valuesAnnotation = getValuesAnnotation(i);
235

    
236
                                                        IFeature feat = new DefaultFeature(geom,
237
                                                                        valuesAnnotation, "" + i);
238
                                                        DefaultRowEdited edRow = new DefaultRowEdited(feat,
239
                                                                        DefaultRowEdited.STATUS_ADDED, i);
240
                                                        writer.process(edRow);
241
                                                }
242
                                        }
243
                }
244
            } else {
245
                int counter = 0;
246

    
247
                for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet
248
                                                .nextSetBit(i + 1)) {
249
                                        IGeometry geom = va.getShape(i);
250
                                        if (geom != null) {
251
                                                if (geom.getGeometryType() != FShape.POINT) {
252
                                                        Point2D p = FLabel.createLabelPoint((FShape) geom
253
                                                                        .getInternalShape());
254
                                                        geom = ShapeFactory.createPoint2D(p.getX(), p
255
                                                                        .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_")
268
                                                                + " " + counter + "    " + bitSet.cardinality());
269

    
270
                                                if (isCanceled()) {
271
                                                        break;
272
                                                }
273

    
274
                                                if (geom != null) {
275
                                                        Value[] valuesAnnotation = getValuesAnnotation(i);
276
                                                        IFeature feat = new DefaultFeature(geom,
277
                                                                        valuesAnnotation, "" + i);
278
                                                        DefaultRowEdited edRow = new DefaultRowEdited(feat,
279
                                                                        DefaultRowEdited.STATUS_ADDED, i);
280

    
281
                                                        writer.process(edRow);
282
                                                }
283
                                        }
284

    
285
                                }
286
            }
287
            va.stop();
288
        }
289

    
290
        writer.postProcess();
291
        setCurrentStep(getFinalStep());
292

    
293
        mapAttri.clear();
294
        mapGeom.clear();
295

    
296
        if (reader != null) {
297
            int res = JOptionPane.showConfirmDialog((JComponent) PluginServices.getMDIManager()
298
                                                                               .getActiveWindow(),
299
                    PluginServices.getText(this,
300
                        "insertar_en_la_vista_la_capa_creada"),
301
                    PluginServices.getText(this, "insertar_capa"),
302
                    JOptionPane.YES_NO_OPTION);
303

    
304
            if (res == JOptionPane.YES_OPTION) {
305
                PostProcessSupport.executeCalls();
306
                lyrDef = (SHPLayerDefinition) writer.getTableDefinition();
307

    
308
                FLayer newLayer = LayerFactory.createLayer(lyrDef.getName(),
309
                        reader, lyrVect.getProjection());
310

    
311
                Annotation_Layer la = Annotation_Layer.createLayerFromVect((FLyrVect)newLayer);
312
//                LayerListener[] layerListeners = newLayer.getLayerListeners();
313
//
314
//                for (int i = 0; i < layerListeners.length; i++) {
315
//                    la.addLayerListener(layerListeners[i]);
316
//                }
317

    
318
//                la.setSource(((FLyrVect) newLayer).getSource());
319
//                la.setProjection(newLayer.getProjection());
320
                la.setName(newLayer.getName());
321
//                la.setLegend((VectorialLegend) lyrVect.getLegend());
322
                Annotation_Mapping.addAnnotationMapping(la);
323
                AddLayer.checkProjection(la,mapContext.getViewPort());
324
                mapContext.getLayers().addLayer(la);
325
            }
326
        }
327
    }
328

    
329
    /**
330
     * DOCUMENT ME!
331
     *
332
     * @return DOCUMENT ME!
333
     * @throws ReadDriverException
334
     */
335
    private FieldDescription[] getFieldDescriptions() throws ReadDriverException {
336
        SHPLayerDefinition lyrDef = (SHPLayerDefinition) writer.getTableDefinition();
337
        FieldDescription[] fields = new FieldDescription[Annotation_Mapping.NUMCOLUMNS]; // lyrDef.getFieldsDesc();
338

    
339
        int posText = mapping.getColumnText();
340
        int posTypeFont = mapping.getColumnTypeFont();
341
        int posStyleFont = mapping.getColumnStyleFont();
342
        int posColor = mapping.getColumnColor();
343
        int posHeight = mapping.getColumnHeight();
344
        int posRotate = mapping.getColumnRotate();
345

    
346
        FieldDescription[] fieldsDescriptions = lyrDef.getFieldsDesc();
347
        setField(Annotation_Mapping.TEXT, fieldsDescriptions, fields, posText, 0);
348
        setField(Annotation_Mapping.TYPEFONT, fieldsDescriptions, fields,
349
            posTypeFont, 1);
350
        setField(Annotation_Mapping.STYLEFONT, fieldsDescriptions, fields,
351
            posStyleFont, 2);
352
        setField(Annotation_Mapping.COLOR, fieldsDescriptions, fields,
353
            posColor, 3);
354
        setField(Annotation_Mapping.HEIGHT, fieldsDescriptions, fields,
355
            posHeight, 4);
356
        setField(Annotation_Mapping.ROTATE, fieldsDescriptions, fields,
357
            posRotate, 5);
358

    
359
        return fields;
360
    }
361

    
362
    /**
363
     * DOCUMENT ME!
364
     *
365
     * @param name DOCUMENT ME!
366
     * @param antFields DOCUMENT ME!
367
     * @param fields DOCUMENT ME!
368
     * @param pos DOCUMENT ME!
369
     * @param i DOCUMENT ME!
370
     */
371
    private void setField(String name, FieldDescription[] antFields,
372
        FieldDescription[] fields, int pos, int i) {
373
        int type = Annotation_Mapping.getType(name);
374

    
375
        if (pos != -1) {
376
            fields[i] = antFields[pos];
377
        } else {
378
            fields[i] = new FieldDescription();
379

    
380
            if (type != Types.VARCHAR) {
381
                if (name.equals("Color")) {
382
                    fields[i].setFieldLength(10);
383
                }
384
            }else if (name.equals("TypeFont")){
385
                    fields[i].setFieldLength(15);
386
            }
387
        }
388

    
389
        fields[i].setFieldType(type);
390
        fields[i].setFieldName(name);
391
    }
392

    
393
    /**
394
     * DOCUMENT ME!
395
     *
396
     * @param i DOCUMENT ME!
397
     *
398
     * @return DOCUMENT ME!
399
     * @throws ReadDriverException
400
     *
401
     * @throws DriverException DOCUMENT ME!
402
     * @throws EditionException DOCUMENT ME!
403
     */
404
    private Value[] getValuesAnnotation(int i) throws ReadDriverException{
405
        int posText = mapping.getColumnText();
406
        int posTypeFont = mapping.getColumnTypeFont();
407
        int posStyleFont = mapping.getColumnStyleFont();
408
        int posColor = mapping.getColumnColor();
409
        int posHeight = mapping.getColumnHeight();
410
        int posRotate = mapping.getColumnRotate();
411

    
412
        Value[] values = sds.getRow(i);
413
        Value[] valuesAnnotation = new Value[Annotation_Mapping.NUMCOLUMNS];
414

    
415
        setValue(Annotation_Mapping.TEXT, values, valuesAnnotation, posText, 0);
416
        setValue(Annotation_Mapping.TYPEFONT, values, valuesAnnotation,
417
            posTypeFont, 1);
418
        setValue(Annotation_Mapping.STYLEFONT, values, valuesAnnotation,
419
            posStyleFont, 2);
420
        setValue(Annotation_Mapping.COLOR, values, valuesAnnotation, posColor, 3);
421
        setValue(Annotation_Mapping.HEIGHT, values, valuesAnnotation,
422
            posHeight, 4);
423
        setValue(Annotation_Mapping.ROTATE, values, valuesAnnotation,
424
            posRotate, 5);
425

    
426
        return valuesAnnotation;
427
    }
428

    
429
    /**
430
     * DOCUMENT ME!
431
     *
432
     * @param name DOCUMENT ME!
433
     * @param antValues DOCUMENT ME!
434
     * @param values DOCUMENT ME!
435
     * @param pos DOCUMENT ME!
436
     * @param i DOCUMENT ME!
437
     */
438
    private void setValue(String name, Value[] antValues, Value[] values,
439
        int pos, int i) {
440
        int type = Annotation_Mapping.getType(name);
441
            try {
442
                if (name == Annotation_Mapping.TEXT) {
443
                        if (pos!=-1){
444
                                values[i] = ValueFactory.createValue(antValues[pos].toString());
445
                        }else{
446
                                values[i] = ValueFactory.createValueByType(String.valueOf(
447
                                Annotation_Mapping.DEFAULTTEXT), type);
448
                        }
449
                } else if (name == Annotation_Mapping.COLOR) {
450
                        if (pos!=-1){
451
                                values[i] = ValueFactory.createValue(((NumericValue)antValues[pos]).intValue());
452
                        }else{
453
                                values[i] = ValueFactory.createValueByType(String.valueOf(
454
                                Annotation_Mapping.DEFAULTCOLOR), type);
455
                        }
456
                } else if (name == Annotation_Mapping.HEIGHT) {
457
                        if (pos!=-1){
458
                                values[i] = ValueFactory.createValue(((NumericValue)antValues[pos]).intValue());
459
                        }else{
460
                                values[i] = ValueFactory.createValueByType(String.valueOf(
461
                                Annotation_Mapping.DEFAULTHEIGHT), type);
462
                        }
463
                } else if (name == Annotation_Mapping.TYPEFONT) {
464
                        if (pos!=-1){
465
                                values[i] = ValueFactory.createValue(antValues[pos].toString());
466
                        }else{
467
                                values[i] = ValueFactory.createValueByType(String.valueOf(
468
                                Annotation_Mapping.DEFAULTTYPEFONT), type);
469
                        }
470
                } else if (name == Annotation_Mapping.STYLEFONT) {
471
                        if (pos!=-1){
472
                                values[i] = ValueFactory.createValue(((NumericValue)antValues[pos]).intValue());
473
                        }else{
474
                                values[i] = ValueFactory.createValueByType(String.valueOf(
475
                                Annotation_Mapping.DEFAULTSTYLEFONT), type);
476
                        }
477
                } else if (name == Annotation_Mapping.ROTATE) {
478
                        if (pos!=-1){
479
                                values[i] = ValueFactory.createValue(((NumericValue)antValues[pos]).intValue());
480
                        }else{
481
                                values[i] = ValueFactory.createValueByType(String.valueOf(
482
                                Annotation_Mapping.DEFAULTROTATE), type);
483
                        }
484
                }
485
            } catch (ParseException e) {
486
                    NotificationManager.addError(e);
487
            }
488
    }
489

    
490
    /**
491
     * DOCUMENT ME!
492
     * @throws ReadDriverException
493
     * @throws InitializeDriverException
494
     * @throws ExpansionFileReadException
495
     *
496
     * @throws DriverIOException DOCUMENT ME!
497
     * @throws DriverException DOCUMENT ME!
498
     * @throws EditionException DOCUMENT ME!
499
     */
500
    private void duplicate() throws InitializeDriverException, ReadDriverException, ExpansionFileReadException {
501
        va.start();
502

    
503
        if (bitSet.cardinality() == 0) {
504
            rowCount = va.getShapeCount();
505

    
506
            for (int i = 0; i < rowCount; i++) {
507
                IGeometry geom = va.getShape(i);
508
                modifyMap(geom, i);
509

    
510
                // reportStep();
511
                // setNote(PluginServices.getText(this, "exporting_") +" "+ i+ "
512
                // "+rowCount);
513
                if (isCanceled()) {
514
                    break;
515
                }
516
            }
517
        } else {
518
            for (int i = bitSet.nextSetBit(0); i >= 0;
519
                    i = bitSet.nextSetBit(i + 1)) {
520
                IGeometry geom = va.getShape(i);
521
                modifyMap(geom, i);
522

    
523
                // reportStep();
524
                // setNote(PluginServices.getText(this, "exporting_") +" "+
525
                // counter+ " "+bitSet.cardinality());
526
                if (isCanceled()) {
527
                    break;
528
                }
529
            }
530
        }
531

    
532
        va.stop();
533
    }
534

    
535
    /**
536
     * DOCUMENT ME!
537
     *
538
     * @param geom DOCUMENT ME!
539
     * @param index DOCUMENT ME!
540
     * @throws ReadDriverException
541
     *
542
     * @throws DriverException DOCUMENT ME!
543
     * @throws EditionException DOCUMENT ME!
544
     */
545
    private void modifyMap(IGeometry geom, int index) throws ReadDriverException {
546
        if (geom==null)
547
                return;
548
            if (geom.getGeometryType() != FShape.POINT) {
549
            Point2D p = FLabel.createLabelPoint(geom.getInternalShape());
550
            geom = ShapeFactory.createPoint2D(p.getX(), p.getY());
551
        }
552

    
553
        Value[] valuesAnnotation = getValuesAnnotation(index);
554
        String textValue = valuesAnnotation[0].toString();
555
        if (!mapAttri.containsKey(textValue)) {
556
           mapAttri.put(textValue, valuesAnnotation);
557
        }
558
        if (mapGeom.containsKey(textValue)) {
559
           IGeometry geometry2 = geom;
560
           Rectangle2D rectangle1 = (Rectangle2D) mapGeom.get(textValue);
561
           mapGeom.put(textValue,
562
           rectangle1.createUnion(geometry2.getBounds2D()));
563
           reportStep();
564
           setNote(PluginServices.getText(this, "exporting_"));
565
        } else {
566
           mapGeom.put(textValue, geom.getBounds2D());
567
        }
568
    }
569

    
570
        public void finished() {
571
                // TODO Auto-generated method stub
572

    
573
        }
574
}