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 3541 caballero
/* 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 3684 caballero
import java.awt.geom.Rectangle2D;
47 3541 caballero
import java.util.ArrayList;
48
49 7659 fjp
import com.iver.cit.gvsig.fmap.core.FShape;
50
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
51 9641 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
52 7659 fjp
import com.iver.utiles.XMLEntity;
53 3541 caballero
54 7659 fjp
55 3541 caballero
/**
56 3684 caballero
 * Abstract adaptor to relate the geometries with the fframes and to be able to
57
 * integrate them in the Layout.
58 3541 caballero
 *
59
 * @author Vicente Caballero Navarro
60
 */
61
public abstract class GeometryAdapter {
62
    private ArrayList points = new ArrayList();
63
    private GeneralPathX shape;
64
65
    /**
66 3684 caballero
     * Add a point to de geometry.
67 3541 caballero
     *
68 3684 caballero
     * @param point Point that is added.
69 3541 caballero
     *
70 3684 caballero
     * @return Number of points that contains the geometry.
71 3541 caballero
     */
72
    public int addPoint(Point2D point) {
73
        points.add(point);
74
75
        return points.size();
76
    }
77
78
    /**
79 3684 caballero
     * End the creation of the geometry with the last point added.
80 3541 caballero
     */
81
    public void end() {
82
        obtainShape((Point2D) points.get(points.size() - 1));
83
    }
84
85
    /**
86 3684 caballero
     * Adds the GeneralPathX with all the points of the geometry.
87 3541 caballero
     *
88 3684 caballero
     * @param gpx GeneralPathX
89 3541 caballero
     */
90
    protected void setGPX(GeneralPathX gpx) {
91
        shape = gpx;
92
    }
93
94
    /**
95 3684 caballero
     * Returns the GeneralPathX.
96 3541 caballero
     *
97 3684 caballero
     * @return GeneralPathX.
98 3541 caballero
     */
99
    protected GeneralPathX getGPX() {
100
        return shape;
101
    }
102
103
    /**
104 3684 caballero
     * Obtains the geometry passing him as parameter the last point.
105 3541 caballero
     *
106 3684 caballero
     * @param p Last Point.
107 3541 caballero
     */
108
    public abstract void obtainShape(Point2D p);
109
110
    /**
111 3684 caballero
     * Applies the transformation to all the points of the geometry.
112 3541 caballero
     *
113 3684 caballero
     * @param at AffineTransform
114 3541 caballero
     */
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 3684 caballero
     * It draws the geometry on the Graphics that is passed like parameter.
125 3541 caballero
     *
126 3684 caballero
     * @param g Graphics
127
     * @param at AffineTransform
128
     * @param symbol FSymbol
129 3541 caballero
     */
130 7659 fjp
    public abstract void draw(Graphics2D g, AffineTransform at, ISymbol symbol);
131 3541 caballero
132
    /**
133 3684 caballero
     * Paints the geometry on the Graphics adding him the last point if the
134
     * parameter andLastPoint is true.
135 3541 caballero
     *
136 3684 caballero
     * @param g Graphics
137
     * @param at AffineTransform
138
     * @param andLastPoint If true add last point.
139 3541 caballero
     */
140 3684 caballero
    public abstract void paint(Graphics2D g, AffineTransform at,
141
        boolean andLastPoint);
142 3541 caballero
143
    /**
144 3684 caballero
     * Set the point of cursor.
145 3541 caballero
     *
146 3684 caballero
     * @param p Point of cursor.
147 3541 caballero
     */
148
    public abstract void pointPosition(Point2D p);
149 3684 caballero
150 3541 caballero
    /**
151 3684 caballero
     * Obtains the shape of the Geometry.
152 3541 caballero
     *
153 3684 caballero
     * @return FShape.
154 3541 caballero
     */
155 3684 caballero
    protected abstract FShape getShape(AffineTransform at);
156
157
    /**
158
     * Returns all the points of Geometry.
159
     *
160
     * @return Array of points.
161
     */
162 3541 caballero
    public Point2D[] getPoints() {
163
        return (Point2D[]) points.toArray(new Point2D[0]);
164
    }
165
166
    /**
167 3684 caballero
     * Draws a handler in each vertex of the Geometry.
168 3541 caballero
     *
169 3684 caballero
     * @param g Graphics
170
     * @param at AffineTransform.
171 3541 caballero
     */
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 3684 caballero
     * Modifies a point of the Geometry from an index by the one that is passed
187
     * like parameter.
188 3541 caballero
     *
189 3684 caballero
     * @param pos Index
190
     * @param point Point
191 3541 caballero
     */
192 3684 caballero
    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 3541 caballero
    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 3684 caballero
225 3541 caballero
        xml.putProperty("points", ps);
226
        xml.putProperty("className", this.getClass().getName());
227 3684 caballero
228 3541 caballero
        return xml;
229
    }
230
231
    /**
232
     * DOCUMENT ME!
233
     *
234
     * @param xml DOCUMENT ME!
235 3684 caballero
     *
236
     * @return DOCUMENT ME!
237 3541 caballero
     */
238
    public static GeometryAdapter createFromXML(XMLEntity xml) {
239 3684 caballero
        GeometryAdapter geometry = null;
240
241 3541 caballero
        try {
242
            Class clase = Class.forName(xml.getStringProperty("className"));
243
            geometry = (GeometryAdapter) clase.newInstance();
244
        } catch (Exception e) {
245
        }
246 3684 caballero
247
        double[] ps = xml.getDoubleArrayProperty("points");
248 3541 caballero
        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 3684 caballero
259 3541 caballero
        return geometry;
260
    }
261 3579 caballero
262 3684 caballero
    /**
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 5766 caballero
            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 3684 caballero
            return shape.getBounds2D();
289
    }
290 3541 caballero
}