Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAnnotations / src / com / iver / cit / gvsig / fmap / edition / Annotation_EditableAdapter.java @ 28605

History | View | Annotate | Download (13.8 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.fmap.edition;
44

    
45
import java.awt.Shape;
46
import java.awt.geom.NoninvertibleTransformException;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.io.IOException;
50
import java.util.List;
51

    
52
import org.cresques.cts.ICoordTrans;
53

    
54
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
55
import com.hardcode.gdbms.engine.values.NullValue;
56
import com.hardcode.gdbms.engine.values.NumericValue;
57
import com.hardcode.gdbms.engine.values.StringValue;
58
import com.hardcode.gdbms.engine.values.Value;
59
import com.hardcode.gdbms.engine.values.ValueFactory;
60
import com.iver.andami.PluginServices;
61
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
62
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
63
import com.iver.cit.gvsig.fmap.ViewPort;
64
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
65
import com.iver.cit.gvsig.fmap.core.FPoint2D;
66
import com.iver.cit.gvsig.fmap.core.Handler;
67
import com.iver.cit.gvsig.fmap.core.IFeature;
68
import com.iver.cit.gvsig.fmap.core.IGeometry;
69
import com.iver.cit.gvsig.fmap.core.IRow;
70
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
71
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
72
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
73
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
74
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend;
75
import com.iver.utiles.StringUtilities;
76
import com.iver.utiles.XMLEntity;
77

    
78

    
79
/**
80
 * Annotation Editable Adapter
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84
public class Annotation_EditableAdapter extends VectorialEditableAdapter {
85
        private static String DEFAULT_ANNOTATION_TEXT = "default_annotation_text";
86

    
87
        private static String DEFAULT_ANNOTATION_TYPEFONT = "default_annotation_typefont";
88

    
89
        private static String DEFAULT_ANNOTATION_ROTATE = "default_annotation_rotate";
90

    
91
        private static String DEFAULT_ANNOTATION_STYLEFONT = "default_annotation_stylefont";
92

    
93
        private static String DEFAULT_ANNOTATION_HEIGHT = "default_annotation_height";
94

    
95
        private static String DEFAULT_ANNOTATION_COLOR = "default_annotation_color";
96

    
97

    
98

    
99
        private Annotation_Mapping mapping;
100

    
101
    private Annotation_Layer lyrAnnotation;
102

    
103
     public int doAddRow(IRow feat, int sourceType) throws ExpansionFileWriteException, ReadDriverException {
104
                int position = super.doAddRow(feat, sourceType);
105
                PluginServices ps = PluginServices
106
                                .getPluginServices("com.iver.cit.gvsig.annotation");
107
                XMLEntity xml = ps.getPersistentXML();
108
                String newFID=getNewFID();
109
                boolean cancel = fireBeforeRowAdded(sourceType,newFID);
110
                if (cancel)
111
                        return -1;
112
                Value[] values = feat.getAttributes();
113
                if (!(values[0] instanceof NullValue &&
114
                                values[1] instanceof NullValue &&
115
                                values[2] instanceof NullValue &&
116
                                values[3] instanceof NullValue &&
117
                                values[4] instanceof NullValue &&
118
                                values[5] instanceof NullValue)) {
119
                        IGeometry geom;
120
                        geom=((DefaultFeature)feat).getGeometry();
121
                        Shape shape=geom.getInternalShape();
122
                        Handler[] handlers=geom.getHandlers(IGeometry.SELECTHANDLER);
123
                        Point2D h0=handlers[0].getPoint();
124
                        if (!(shape instanceof FPoint2D)) {
125
                                Point2D h1=handlers[1].getPoint();
126
                                double rotation=Math.toDegrees(UtilFunctions.getAngle(h0,h1));
127
                                values[mapping.getColumnRotate()] = ValueFactory.createValue(rotation);
128
                        }
129
                        geom = ShapeFactory.createPoint2D(h0.getX(),h0.getY());
130
                        ((DefaultFeature)feat).setGeometry(geom);
131
                        return position;
132
                }
133

    
134
                int intColor = Annotation_Mapping.DEFAULTCOLOR;
135
                String text = Annotation_Mapping.DEFAULTTEXT;
136
                String type = Annotation_Mapping.DEFAULTTYPEFONT;
137
                int style = Annotation_Mapping.DEFAULTSTYLEFONT;
138
                int height = Annotation_Mapping.DEFAULTHEIGHT;
139
                int rotate = Annotation_Mapping.DEFAULTROTATE;
140

    
141
                if (xml.contains(DEFAULT_ANNOTATION_COLOR)){
142
                        intColor = StringUtilities.string2Color(xml.getStringProperty(DEFAULT_ANNOTATION_COLOR)).getRGB();
143
                        text = xml.getStringProperty(DEFAULT_ANNOTATION_TEXT);
144
                        type = xml.getStringProperty(DEFAULT_ANNOTATION_TYPEFONT);
145
                        style = xml.getIntProperty(DEFAULT_ANNOTATION_STYLEFONT);
146
                        height = xml.getIntProperty(DEFAULT_ANNOTATION_HEIGHT);
147
                        rotate = xml.getIntProperty(DEFAULT_ANNOTATION_ROTATE);
148
                }
149
                values[mapping.getColumnText()] = ValueFactory.createValue(text);
150
                values[mapping.getColumnTypeFont()] = ValueFactory.createValue(type);
151
                values[mapping.getColumnStyleFont()] = ValueFactory.createValue(style);
152
                values[mapping.getColumnHeight()] = ValueFactory.createValue(height);
153
                values[mapping.getColumnRotate()] = ValueFactory.createValue(rotate);
154
                values[mapping.getColumnColor()] = ValueFactory.createValue(intColor);
155
//                IGeometry geom;
156
//                ViewPort vp=lyrAnnotation.getMapContext().getViewPort();
157
//                geom = lyrAnnotation.getTextWrappingGeometry(height, text, rotate,
158
//                                        position,vp);
159
//                feat = new DefaultFeature(geom, values, feat.getID());
160
                return position;
161
        }
162

    
163
    public Annotation_EditableAdapter(Annotation_Layer lyrAnnotation) {
164
        super();
165
        this.mapping = lyrAnnotation.getAnnotatonMapping();
166
        this.lyrAnnotation = lyrAnnotation;
167
    }
168

    
169
    public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) throws ReadDriverException, ExpansionFileReadException{
170
        // En esta clase suponemos random access.
171
        // Luego tendremos otra clase que sea VectorialEditableDBAdapter
172
        // que reescribir? este m?todo.
173
//        Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
174
        List l = fmapSpatialIndex.query(r);
175
        IRowEdited[] feats = new IRowEdited[l.size()];
176

    
177
        try {
178
            for (int index = 0; index < l.size(); index++) {
179
                Integer i = (Integer) l.get(index);
180
                int inverse = getInversedIndex(i.intValue());
181
                feats[index] = getRowAnnotation(inverse);
182
            }
183
        } catch (DriverIOException ex) {
184
                 throw new ReadDriverException(lyrAnnotation.getName(),ex);
185
        }
186

    
187
        return feats;
188
    }
189

    
190
    /*
191
     * (non-Javadoc)
192
     *
193
     * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
194
     */
195
    public IRowEdited getRowAnnotation(int index) throws DriverIOException, ExpansionFileReadException, ReadDriverException {
196
        int calculatedIndex = getCalculatedIndex(index);
197
        Integer integer = new Integer(calculatedIndex);
198

    
199
        // Si no est? en el fichero de expansi?n
200
        DefaultRowEdited edRow = null;
201
        ViewPort vp=lyrAnnotation.getMapContext().getViewPort();
202
               IFeature f = null;
203
        if (!relations.containsKey(integer)) {
204
            f = ova.getFeature(calculatedIndex);
205
        } else {
206
            int num = ((Integer) relations.get(integer)).intValue();
207
            IRowEdited aux = expansionFile.getRow(num);
208
            f = (IFeature) aux.getLinkedRow().cloneRow();
209
        }
210
        String s = f.getID();
211
        Annotation_Mapping mapping = lyrAnnotation.getAnnotatonMapping();
212
        NumericValue vRotation = (NumericValue) f.getAttribute(mapping.getColumnRotate());
213
        NumericValue vHeight = (NumericValue) f.getAttribute(mapping.getColumnHeight());
214
        NumericValue vStyle = (NumericValue) f.getAttribute(mapping.getColumnStyleFont());
215
        StringValue vType = (StringValue) f.getAttribute(mapping.getColumnTypeFont());
216
        Value vText = f.getAttribute(mapping.getColumnText());
217
        start();
218
        IGeometry geom = getShape(index);
219
        stop();
220
        ICoordTrans ct = lyrAnnotation.getCoordTrans();
221
        boolean bMustClone = false;
222
        if (ct != null) {
223
            if (bMustClone) {
224
                geom = geom.cloneGeometry();
225
            }
226
            geom.reProject(ct);
227
        }
228
        geom.transform(vp.getAffineTransform());
229
        int unit=-1;
230
        if (lyrAnnotation.getLegend() instanceof Annotation_Legend) {
231
                unit = ((Annotation_Legend)lyrAnnotation.getLegend()).getUnits();
232
        }
233
//        if (lyrAnnotation.getLabelingStrategy()!=null)
234
//                unit=((AttrInTableLabelingStrategy)lyrAnnotation.getLabelingStrategy()).getUnit();
235

    
236
        IGeometry geom1 = lyrAnnotation.getTextWrappingGeometryInPixels(unit,vHeight.floatValue(), //*FConstant.FONT_HEIGHT_SCALE_FACTOR,
237
               vText.toString(), vRotation.doubleValue(),vType.getValue(),vStyle.intValue(), index, vp,geom);
238
              try {
239
                        geom1.transform(vp.getAffineTransform().createInverse());
240
                } catch (NoninvertibleTransformException e) {
241
                         throw new ReadDriverException(lyrAnnotation.getName(),e);
242
                }
243
              f = new DefaultFeature(geom1, f.getAttributes(), s);
244
        edRow = new DefaultRowEdited(f,
245
               DefaultRowEdited.STATUS_ORIGINAL, index);
246

    
247
        return edRow;
248

    
249
    }
250
        /**
251
         * Si se intenta modificar una geometr?a original de la capa en edici?n se
252
         * a?ade al fichero de expansi?n y se registra la posici?n en la que se
253
         * a?adi?. Si se intenta modificar una geometria que se encuentra en el
254
         * fichero de expansi?n (por ser nueva o original pero modificada) se invoca
255
         * el m?todo modifyGeometry y se actualiza el ?ndice de la geometria en el
256
         * fichero.
257
         *
258
         * @param calculatedIndex
259
         *            DOCUMENT ME!
260
         * @param feat
261
         *            DOCUMENT ME!
262
         *
263
         * @return position inside ExpansionFile
264
         * @throws ReadDriverException
265
         * @throws ExpansionFileWriteException
266
         * @throws ExpansionFileReadException
267
         *
268
         * @throws IOException
269
         * @throws DriverIOException
270
         */
271
        public int doModifyRow(int calculatedIndex, IRow feat, int sourceType) throws ReadDriverException, ExpansionFileWriteException, ExpansionFileReadException {
272
                boolean cancel = fireBeforeModifyRow(feat, calculatedIndex, sourceType);
273
                if (cancel)
274
                        return -1;
275
                int posAnteriorInExpansionFile = -1;
276
                Integer integer = new Integer(calculatedIndex);
277

    
278
                IFeature featAnt = null;
279
                IGeometry geom;
280
                Value[] values=feat.getAttributes();
281
                geom=((DefaultFeature)feat).getGeometry();
282
                Shape shape=geom.getInternalShape();
283
                Handler[] handlers=geom.getHandlers(IGeometry.SELECTHANDLER);
284
                Point2D h0=handlers[0].getPoint();
285
                if (!(shape instanceof FPoint2D)) {
286
                        Point2D h1=handlers[1].getPoint();
287
                        double rotation=-Math.toDegrees(UtilFunctions.getAngle(h0,h1));
288
                        boolean rotate=false;
289
                        if (((NumericValue)values[mapping.getColumnRotate()]).intValue() != (int)rotation ) {
290
                                values[mapping.getColumnRotate()] = ValueFactory.createValue(rotation);
291
                                rotate=true;
292
                        }
293

    
294
                        Point2D h2=handlers[2].getPoint();
295
                        if (!rotate) {
296
                                double height=Math.sqrt(Math.pow(h2.getX()-h1.getX(),2D)+Math.pow(h2.getY()-h1.getY(),2D));
297
                                boolean isInPixels=((Annotation_Legend)lyrAnnotation.getLegend()).isFontSizeInPixels();
298
                                double distance=height;
299
                                if (isInPixels) {
300
                                        distance=lyrAnnotation.getMapContext().getViewPort().fromMapDistance(distance);
301
                                }
302
                                values[mapping.getColumnHeight()] = ValueFactory.createValue(distance);
303
                        }
304
                        Point2D h3=handlers[3].getPoint();
305
//                        geom = ShapeFactory.createPoint2D(h3.getX(),h3.getY());
306
                        geom = ShapeFactory.createPoint2D(h0.getX(),h0.getY());
307
                }else {
308
                        geom = ShapeFactory.createPoint2D(h0.getX(),h0.getY());
309
                }
310

    
311
                feat = new DefaultFeature(geom, values, feat.getID());
312
                if (!relations.containsKey(integer)) {
313
                        int newPosition = expansionFile.addRow(feat,
314
                                        IRowEdited.STATUS_MODIFIED, actualIndexFields);
315
                        relations.put(integer, new Integer(newPosition));
316

    
317
                        if (sourceType == EditionEvent.GRAPHIC) {
318
                                // Se actualiza el ?ndice espacial
319
                                featAnt = (DefaultFeature) (ova.getFeature(calculatedIndex));
320
                                IGeometry g = featAnt.getGeometry();
321
                                Rectangle2D rAnt = g.getBounds2D();
322
                                Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
323
                                this.fmapSpatialIndex.delete(rAnt, new Integer(calculatedIndex));
324
                                this.fmapSpatialIndex.insert(r, new Integer(calculatedIndex));
325
                        }
326
                } else {
327
                        // Obtenemos el ?ndice en el fichero de expansi?n
328
                        int num = ((Integer) relations.get(integer)).intValue();
329
                        posAnteriorInExpansionFile = num;
330

    
331
                        // Obtenemos la geometr?a para actualiza el ?ndice
332
                        // espacialposteriormente
333
                        featAnt = (IFeature) expansionFile.getRow(num).getLinkedRow();
334

    
335
                        /*
336
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del
337
                         * fichero de expansi?n en el que se encuentra la geometr?a
338
                         * modificada
339
                         */
340
                        num = expansionFile.modifyRow(num, feat, actualIndexFields);
341

    
342
                        /*
343
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
344
                         * fichero de expansi?n.
345
                         */
346
                        relations.put(integer, new Integer(num));
347
                        if (sourceType == EditionEvent.GRAPHIC) {
348
                                // Se modifica el ?ndice espacial
349
                                Rectangle2D rAnt = featAnt.getGeometry().getBounds2D();
350
                                Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
351
                                this.fmapSpatialIndex.delete(rAnt, new Integer(calculatedIndex));
352
                                this.fmapSpatialIndex.insert(r, new Integer(calculatedIndex));
353
                        }
354
                }
355
                isFullExtentDirty = true;
356
                fireAfterModifyRow(calculatedIndex, sourceType);
357
                return posAnteriorInExpansionFile;
358
        }
359

    
360
}