Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / geometryadapters / GeometryAdapter.java @ 28546

History | View | Annotate | Download (8.32 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.project.documents.layout.geometryadapters;
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 org.gvsig.fmap.geom.Geometry;
52
import org.gvsig.fmap.geom.primitive.GeneralPathX;
53
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
54

    
55
import com.iver.utiles.XMLEntity;
56

    
57

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

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

    
78
        return points.size();
79
    }
80

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

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

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

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

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

    
123
        shape.transform(at);
124
    }
125

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

    
154
    /**
155
     * Set the point of cursor.
156
     *
157
     * @param p Point of cursor.
158
     */
159
    public abstract void pointPosition(Point2D p);
160

    
161
    /**
162
     * Obtains the shape of the Geometry.
163
     *
164
     * @return Geometry.
165
     */
166
    public abstract Geometry getGeometry(AffineTransform at);
167

    
168
    /**
169
     * Returns all the points of Geometry.
170
     *
171
     * @return Array of points.
172
     */
173
    public Point2D[] getPoints() {
174
        return (Point2D[]) points.toArray(new Point2D[0]);
175
    }
176

    
177
    /**
178
     * Draws a handler in each vertex of the Geometry.
179
     *
180
     * @param g Graphics
181
     * @param at AffineTransform.
182
     */
183
    public void drawVertex(Graphics2D g, AffineTransform at) {
184
        Point2D[] ps = getPoints();
185
        Point2D[] pointRes = new Point2D[ps.length];
186
        at.transform(ps, 0, pointRes, 0, ps.length);
187

    
188
        int d = 3;
189

    
190
        for (int i = 0; i < pointRes.length; i++) {
191
            g.fillRect((int) pointRes[i].getX() - d,
192
                (int) pointRes[i].getY() - d, d * 2, d * 2);
193
        }
194
    }
195

    
196
    /**
197
     * Modifies a point of the Geometry from an index by the one that is passed
198
     * like parameter.
199
     *
200
     * @param pos Index
201
     * @param point Point
202
     */
203
    public void changePoint(int pos, Point2D point) {
204
        this.points.set(pos, point);
205
    }
206

    
207
    /**
208
     * Add all the points of Geometry.
209
     *
210
     * @param points All points.
211
     */
212
    public void setPoints(Point2D[] points) {
213
        this.points.clear();
214

    
215
        for (int i = 0; i < points.length; i++) {
216
            this.points.add(points[i]);
217
        }
218
    }
219

    
220
    /**
221
     * DOCUMENT ME!
222
     *
223
     * @return DOCUMENT ME!
224
     */
225
    public XMLEntity getXMLEntity() {
226
        XMLEntity xml = new XMLEntity();
227
        double[] ps = new double[points.size() * 2];
228
        int j = 0;
229

    
230
        for (int i = 0; i < points.size(); i++) {
231
            ps[j] = (((Point2D) (points.get(i))).getX());
232
            ps[j + 1] = (((Point2D) (points.get(i))).getY());
233
            j = j + 2;
234
        }
235

    
236
        xml.putProperty("points", ps);
237
        xml.putProperty("className", this.getClass().getName());
238

    
239
        return xml;
240
    }
241

    
242
    /**
243
     * DOCUMENT ME!
244
     *
245
     * @param xml DOCUMENT ME!
246
     *
247
     * @return DOCUMENT ME!
248
     */
249
    public static GeometryAdapter createFromXML(XMLEntity xml) {
250
        GeometryAdapter geometry = null;
251

    
252
        try {
253
            Class clase = Class.forName(xml.getStringProperty("className"));
254
            geometry = (GeometryAdapter) clase.newInstance();
255
        } catch (Exception e) {
256
        }
257

    
258
        double[] ps = xml.getDoubleArrayProperty("points");
259
        Point2D[] pointsAux = new Point2D[ps.length / 2];
260
        int j = 0;
261

    
262
        for (int i = 0; i < ps.length; i = i + 2) {
263
            pointsAux[j] = new Point2D.Double(ps[i], ps[i + 1]);
264
            j++;
265
        }
266

    
267
        geometry.setPoints(pointsAux);
268
        geometry.end();
269

    
270
        return geometry;
271
    }
272

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