Statistics
| Revision:

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

History | View | Annotate | Download (7.83 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 javax.print.attribute.PrintRequestAttributeSet;
50

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

    
56

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

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

    
77
        return points.size();
78
    }
79

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

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

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

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

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

    
122
        shape.transform(at);
123
    }
124

    
125
    /**
126
     * It draws the geometry on the Graphics that is passed like parameter.
127
     *
128
     * @param g Graphics
129
     * @param at AffineTransform
130
     * @param symbol FSymbol
131
     */
132
    public abstract void draw(Graphics2D g, AffineTransform at, ISymbol symbol);
133
    public abstract void print(Graphics2D g, AffineTransform at, ISymbol symbol, PrintRequestAttributeSet properties);
134
    /**
135
     * Paints the geometry on the Graphics adding him the last point if the
136
     * parameter andLastPoint is true.
137
     *
138
     * @param g Graphics
139
     * @param at AffineTransform
140
     * @param andLastPoint If true add last point.
141
     */
142
    public abstract void paint(Graphics2D g, AffineTransform at,
143
        boolean andLastPoint);
144

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

    
152
    /**
153
     * Obtains the shape of the Geometry.
154
     *
155
     * @return FShape.
156
     */
157
    public abstract FShape getShape(AffineTransform at);
158

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

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

    
179
        int d = 3;
180

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

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

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

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

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

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

    
227
        xml.putProperty("points", ps);
228
        xml.putProperty("className", this.getClass().getName());
229

    
230
        return xml;
231
    }
232

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

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

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

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

    
258
        geometry.setPoints(pointsAux);
259
        geometry.end();
260

    
261
        return geometry;
262
    }
263

    
264
    /**
265
     * Remove last point of Geometry.
266
     */
267
    public void delLastPoint() {
268
        if (points.size() > 0) {
269
            points.remove(points.size() - 1);
270
        }
271
    }
272
    public GeometryAdapter cloneAdapter(){
273
            GeometryAdapter cloneAdapter=null;
274
                try {
275
                        cloneAdapter = (GeometryAdapter)this.getClass().newInstance();
276
                } catch (InstantiationException e) {
277
                } catch (IllegalAccessException e) {
278
                }
279
            cloneAdapter.points=(ArrayList)this.points.clone();
280
            cloneAdapter.shape=(GeneralPathX)this.shape.clone();
281
            return cloneAdapter;
282
    }
283
    public Rectangle2D getBounds2D(){
284
        Rectangle2D r=shape.getBounds2D();
285
        double w=r.getWidth();
286
        double h=r.getHeight();
287
        double x=r.getX();
288
        double y=r.getY();
289
        boolean modified=false;
290
        if (r.getWidth()<0.5) {
291
         modified=true;
292
         w=0.5;
293
         x=x-0.25;
294
        }
295
        if(r.getHeight()<0.5) {
296
         modified=true;
297
         h=0.5;
298
         y=y-0.25;
299
        }
300
        if (modified)
301
         return new Rectangle2D.Double(x,y,w,h);
302
        return r;
303
   }
304
}