Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / adapter / GeometryAdapter.java @ 9641

History | View | Annotate | Download (7.49 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.core.adapter;
42

    
43
import java.awt.Graphics2D;
44
import java.awt.geom.AffineTransform;
45
import java.awt.geom.Point2D;
46
import java.awt.geom.Rectangle2D;
47
import java.util.ArrayList;
48

    
49
import com.iver.cit.gvsig.fmap.core.FShape;
50
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
51
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
52
import com.iver.utiles.XMLEntity;
53

    
54

    
55
/**
56
 * Abstract adaptor to relate the geometries with the fframes and to be able to
57
 * integrate them in the Layout.
58
 *
59
 * @author Vicente Caballero Navarro
60
 */
61
public abstract class GeometryAdapter {
62
    private ArrayList points = new ArrayList();
63
    private GeneralPathX shape;
64

    
65
    /**
66
     * Add a point to de geometry.
67
     *
68
     * @param point Point that is added.
69
     *
70
     * @return Number of points that contains the geometry.
71
     */
72
    public int addPoint(Point2D point) {
73
        points.add(point);
74

    
75
        return points.size();
76
    }
77

    
78
    /**
79
     * End the creation of the geometry with the last point added.
80
     */
81
    public void end() {
82
        obtainShape((Point2D) points.get(points.size() - 1));
83
    }
84

    
85
    /**
86
     * Adds the GeneralPathX with all the points of the geometry.
87
     *
88
     * @param gpx GeneralPathX
89
     */
90
    protected void setGPX(GeneralPathX gpx) {
91
        shape = gpx;
92
    }
93

    
94
    /**
95
     * Returns the GeneralPathX.
96
     *
97
     * @return GeneralPathX.
98
     */
99
    protected GeneralPathX getGPX() {
100
        return shape;
101
    }
102

    
103
    /**
104
     * Obtains the geometry passing him as parameter the last point.
105
     *
106
     * @param p Last Point.
107
     */
108
    public abstract void obtainShape(Point2D p);
109

    
110
    /**
111
     * Applies the transformation to all the points of the geometry.
112
     *
113
     * @param at AffineTransform
114
     */
115
    public void applyTransform(AffineTransform at) {
116
        for (int i = 0; i < points.size(); i++) {
117
            at.transform((Point2D) points.get(i), (Point2D) points.get(i));
118
        }
119

    
120
        shape.transform(at);
121
    }
122

    
123
    /**
124
     * It draws the geometry on the Graphics that is passed like parameter.
125
     *
126
     * @param g Graphics
127
     * @param at AffineTransform
128
     * @param symbol FSymbol
129
     */
130
    public abstract void draw(Graphics2D g, AffineTransform at, ISymbol symbol);
131

    
132
    /**
133
     * Paints the geometry on the Graphics adding him the last point if the
134
     * parameter andLastPoint is true.
135
     *
136
     * @param g Graphics
137
     * @param at AffineTransform
138
     * @param andLastPoint If true add last point.
139
     */
140
    public abstract void paint(Graphics2D g, AffineTransform at,
141
        boolean andLastPoint);
142

    
143
    /**
144
     * Set the point of cursor.
145
     *
146
     * @param p Point of cursor.
147
     */
148
    public abstract void pointPosition(Point2D p);
149

    
150
    /**
151
     * Obtains the shape of the Geometry.
152
     *
153
     * @return FShape.
154
     */
155
    protected abstract FShape getShape(AffineTransform at);
156

    
157
    /**
158
     * Returns all the points of Geometry.
159
     *
160
     * @return Array of points.
161
     */
162
    public Point2D[] getPoints() {
163
        return (Point2D[]) points.toArray(new Point2D[0]);
164
    }
165

    
166
    /**
167
     * Draws a handler in each vertex of the Geometry.
168
     *
169
     * @param g Graphics
170
     * @param at AffineTransform.
171
     */
172
    public void drawVertex(Graphics2D g, AffineTransform at) {
173
        Point2D[] ps = getPoints();
174
        Point2D[] pointRes = new Point2D[ps.length];
175
        at.transform(ps, 0, pointRes, 0, ps.length);
176

    
177
        int d = 3;
178

    
179
        for (int i = 0; i < pointRes.length; i++) {
180
            g.fillRect((int) pointRes[i].getX() - d,
181
                (int) pointRes[i].getY() - d, d * 2, d * 2);
182
        }
183
    }
184

    
185
    /**
186
     * Modifies a point of the Geometry from an index by the one that is passed
187
     * like parameter.
188
     *
189
     * @param pos Index
190
     * @param point Point
191
     */
192
    public void changePoint(int pos, Point2D point) {
193
        this.points.set(pos, point);
194
    }
195

    
196
    /**
197
     * Add all the points of Geometry.
198
     *
199
     * @param points All points.
200
     */
201
    public void setPoints(Point2D[] points) {
202
        this.points.clear();
203

    
204
        for (int i = 0; i < points.length; i++) {
205
            this.points.add(points[i]);
206
        }
207
    }
208

    
209
    /**
210
     * DOCUMENT ME!
211
     *
212
     * @return DOCUMENT ME!
213
     */
214
    public XMLEntity getXMLEntity() {
215
        XMLEntity xml = new XMLEntity();
216
        double[] ps = new double[points.size() * 2];
217
        int j = 0;
218

    
219
        for (int i = 0; i < points.size(); i++) {
220
            ps[j] = (((Point2D) (points.get(i))).getX());
221
            ps[j + 1] = (((Point2D) (points.get(i))).getY());
222
            j = j + 2;
223
        }
224

    
225
        xml.putProperty("points", ps);
226
        xml.putProperty("className", this.getClass().getName());
227

    
228
        return xml;
229
    }
230

    
231
    /**
232
     * DOCUMENT ME!
233
     *
234
     * @param xml DOCUMENT ME!
235
     *
236
     * @return DOCUMENT ME!
237
     */
238
    public static GeometryAdapter createFromXML(XMLEntity xml) {
239
        GeometryAdapter geometry = null;
240

    
241
        try {
242
            Class clase = Class.forName(xml.getStringProperty("className"));
243
            geometry = (GeometryAdapter) clase.newInstance();
244
        } catch (Exception e) {
245
        }
246

    
247
        double[] ps = xml.getDoubleArrayProperty("points");
248
        Point2D[] pointsAux = new Point2D[ps.length / 2];
249
        int j = 0;
250

    
251
        for (int i = 0; i < ps.length; i = i + 2) {
252
            pointsAux[j] = new Point2D.Double(ps[i], ps[i + 1]);
253
            j++;
254
        }
255

    
256
        geometry.setPoints(pointsAux);
257
        geometry.end();
258

    
259
        return geometry;
260
    }
261

    
262
    /**
263
     * Remove last point of Geometry.
264
     */
265
    public void delLastPoint() {
266
        if (points.size() > 0) {
267
            points.remove(points.size() - 1);
268
        }
269
    }
270
    public GeometryAdapter cloneAdapter(){
271
            GeometryAdapter cloneAdapter=null;
272
                try {
273
                        cloneAdapter = (GeometryAdapter)this.getClass().newInstance();
274
                } catch (InstantiationException e) {
275
                } catch (IllegalAccessException e) {
276
                }
277
            cloneAdapter.points=(ArrayList)this.points.clone();
278
            cloneAdapter.shape=(GeneralPathX)this.shape.clone();
279
            return cloneAdapter;
280
    }
281
    public Rectangle2D getBounds2D(){
282
            Rectangle2D r=shape.getBounds2D();
283
            if (r.getWidth()<0.5) {
284
                    return new Rectangle2D.Double(r.getX()-0.25,r.getY(),0.5,r.getHeight());
285
            }else if(r.getHeight()<0.5) {
286
                    return new Rectangle2D.Double(r.getX(),r.getY()-0.25,r.getWidth(),0.5);
287
            }
288
            return shape.getBounds2D();
289
    }
290
}