Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / geometryadapters / GeometryAdapter.java @ 37201

History | View | Annotate | Download (9.58 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 org.gvsig.app.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
import java.util.Collections;
49
import java.util.List;
50

    
51
import org.gvsig.compat.print.PrintAttributes;
52
import org.gvsig.fmap.geom.Geometry;
53
import org.gvsig.fmap.geom.GeometryLocator;
54
import org.gvsig.fmap.geom.GeometryManager;
55
import org.gvsig.fmap.geom.exception.CreateGeometryException;
56
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
57
import org.gvsig.tools.ToolsLocator;
58
import org.gvsig.tools.dynobject.DynStruct;
59
import org.gvsig.tools.lang.Cloneable;
60
import org.gvsig.tools.persistence.PersistenceManager;
61
import org.gvsig.tools.persistence.Persistent;
62
import org.gvsig.tools.persistence.PersistentState;
63
import org.gvsig.tools.persistence.exception.PersistenceException;
64

    
65
/**
66
 * Abstract adaptor to relate the geometries with the fframes and to be able to
67
 * integrate them in the Layout.
68
 * 
69
 * @author Vicente Caballero Navarro
70
 */
71
public abstract class GeometryAdapter implements Persistent, Cloneable {
72

    
73
    public static final String PERSISTENCE_DEFINITION_NAME = "GeometryAdapter";
74

    
75
    private static final String POINTS_FIELD = "points";
76
    
77
    protected static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
78

    
79
    private List<Point2D> points = new ArrayList<Point2D>();
80
    private Geometry geometry;
81

    
82
    public GeometryAdapter() {
83
        super();
84
    }
85

    
86
    /**
87
     * Add a point to de geometry.
88
     * 
89
     * @param point
90
     *            Point that is added.
91
     * 
92
     * @return Number of points that contains the geometry.
93
     */
94
    public int addPoint(Point2D point) {
95
        points.add(point);
96

    
97
        return points.size();
98
    }
99

    
100
    /**
101
     * End the creation of the geometry with the last point added.
102
     */
103
    public void end() {
104
        obtainShape((Point2D) points.get(points.size() - 1));
105
    }
106

    
107
    /**
108
     * Adds the GeneralPathX with all the points of the geometry.
109
     * 
110
     * @param gpx
111
     *            GeneralPathX
112
     */
113
    protected void setGeometry(Geometry geometry) {
114
        this.geometry = geometry;
115
    }
116

    
117
    /**
118
     * Obtains the geometry passing him as parameter the last point.
119
     * 
120
     * @param p
121
     *            Last Point.
122
     * @throws CreateGeometryException 
123
     */
124
    public abstract void obtainShape(Point2D p);
125

    
126
    /**
127
     * Applies the transformation to all the points of the geometry.
128
     * 
129
     * @param at
130
     *            AffineTransform
131
     */
132
    public void applyTransform(AffineTransform at) {
133
        for (int i = 0; i < points.size(); i++) {
134
            at.transform((Point2D) points.get(i), (Point2D) points.get(i));
135
        }
136

    
137
        geometry.transform(at);
138
    }
139

    
140
    /**
141
     * It draws the geometry on the Graphics that is passed like parameter.
142
     * 
143
     * @param g
144
     *            Graphics
145
     * @param at
146
     *            AffineTransform
147
     * @param symbol
148
     *            FSymbol
149
     */
150
    public void draw(Graphics2D g, AffineTransform at, ISymbol symbol){
151
        symbol.draw(g, at, geometry, null, null);
152
    }
153

    
154
    /**
155
     * It print the geometry on the Graphics that is passed like parameter.
156
     * 
157
     * @param g
158
     *            Graphics
159
     * @param at
160
     *            AffineTransform
161
     * @param symbol
162
     *            ISymbol
163
     * @param properties
164
     */
165
    public void print(Graphics2D g, AffineTransform at,
166
        ISymbol symbol, PrintAttributes properties){
167
        symbol.print(g, at, getGeometry(), properties);
168
    }
169

    
170
    /**
171
     * Paints the geometry on the Graphics adding him the last point if the
172
     * parameter andLastPoint is true.
173
     * 
174
     * @param g
175
     *            Graphics
176
     * @param at
177
     *            AffineTransform
178
     * @param andLastPoint
179
     *            If true add last point.
180
     */
181
    public abstract void paint(Graphics2D g, AffineTransform at,
182
        boolean andLastPoint);
183

    
184
    /**
185
     * Set the point of cursor.
186
     * 
187
     * @param p
188
     *            Point of cursor.
189
     */
190
    public abstract void pointPosition(Point2D p);
191

    
192
    /**
193
     * Obtains the shape of the Geometry.
194
     * 
195
     * @return Geometry.
196
     * @deprecated
197
     */
198
    public Geometry getGeometry(AffineTransform at){
199
        Geometry clonedGeom = geometry.cloneGeometry();
200
        clonedGeom.transform(at);
201
        return clonedGeom;
202
    }
203
    
204
    /**
205
     * Returns the geometry to draw
206
     * @return
207
     *      the geometry
208
     */
209
    public Geometry getGeometry(){
210
        return geometry;
211
    }
212

    
213
    /**
214
     * Returns all the points of Geometry.
215
     * 
216
     * @return Array of points.
217
     */
218
    public Point2D[] getPoints() {
219
        return (Point2D[]) points.toArray(new Point2D[0]);
220
    }
221

    
222
    /**
223
     * Draws a handler in each vertex of the Geometry.
224
     * 
225
     * @param g
226
     *            Graphics
227
     * @param at
228
     *            AffineTransform.
229
     */
230
    public void drawVertex(Graphics2D g, AffineTransform at) {
231
        Point2D[] ps = getPoints();
232
        Point2D[] pointRes = new Point2D[ps.length];
233
        at.transform(ps, 0, pointRes, 0, ps.length);
234

    
235
        int d = 3;
236

    
237
        for (int i = 0; i < pointRes.length; i++) {
238
            g.fillRect((int) pointRes[i].getX() - d, (int) pointRes[i].getY()
239
                - d, d * 2, d * 2);
240
        }
241
    }
242

    
243
    /**
244
     * Modifies a point of the Geometry from an index by the one that is passed
245
     * like parameter.
246
     * 
247
     * @param pos
248
     *            Index
249
     * @param point
250
     *            Point
251
     */
252
    public void changePoint(int pos, Point2D point) {
253
        this.points.set(pos, point);
254
    }
255

    
256
    /**
257
     * Add all the points of Geometry.
258
     * 
259
     * @param points
260
     *            All points.
261
     */
262
    public void setPoints(Point2D[] points) {
263
        this.points.clear();
264

    
265
        for (int i = 0; i < points.length; i++) {
266
            this.points.add(points[i]);
267
        }
268
    }
269

    
270
    /**
271
     * Remove last point of Geometry.
272
     */
273
    public void delLastPoint() {
274
        if (points.size() > 0) {
275
            points.remove(points.size() - 1);
276
        }
277
    }
278

    
279
    public Object clone() throws CloneNotSupportedException {
280
        GeometryAdapter cloneAdapter = (GeometryAdapter) super.clone();
281
        Collections.copy(cloneAdapter.points, this.points);
282
        cloneAdapter.geometry = (Geometry) this.geometry.cloneGeometry();
283
        return cloneAdapter;
284
    }
285

    
286
    public Rectangle2D getBounds2D() {
287
        Rectangle2D r = geometry.getBounds2D();
288
        double w = r.getWidth();
289
        double h = r.getHeight();
290
        double x = r.getX();
291
        double y = r.getY();
292
        boolean modified = false;
293
        if (r.getWidth() < 0.5) {
294
            modified = true;
295
            w = 0.5;
296
            x = x - 0.25;
297
        }
298
        if (r.getHeight() < 0.5) {
299
            modified = true;
300
            h = 0.5;
301
            y = y - 0.25;
302
        }
303
        if (modified) {
304
            return new Rectangle2D.Double(x, y, w, h);
305
        }
306
        return r;
307
    }
308

    
309
    public static void registerPersistent() {
310
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
311
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
312
            DynStruct definition =
313
                manager.addDefinition(GeometryAdapter.class,
314
                    PERSISTENCE_DEFINITION_NAME,
315
                    "GeometryAdapter persistence definition", null, null);
316

    
317
            definition.addDynFieldList(POINTS_FIELD)
318
                .setClassOfItems(Point2D.class).setMandatory(true);
319
        }
320

    
321
        CircleAdapter.registerPersistent();
322
        PointAdapter.registerPersistent();
323
        PolygonAdapter.registerPersistent();
324
        PolyLineAdapter.registerPersistent();
325
        RectangleAdapter.registerPersistent();
326
    }
327

    
328
    public void loadFromState(PersistentState state)
329
        throws PersistenceException {
330
        points = state.getList(POINTS_FIELD);
331
        obtainShape((Point2D) points.get(points.size() - 1));
332
    }
333

    
334
    public void saveToState(PersistentState state) throws PersistenceException {
335
        state.set(POINTS_FIELD, points);
336
    }
337
}