Statistics
| Revision:

svn-gvsig-desktop / 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 / PolygonAdapter.java @ 36648

History | View | Annotate | Download (4.49 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout.geometryadapters;
23

    
24
import java.awt.Graphics2D;
25
import java.awt.geom.AffineTransform;
26
import java.awt.geom.Point2D;
27

    
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30

    
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
33
import org.gvsig.fmap.geom.GeometryLocator;
34
import org.gvsig.fmap.geom.GeometryManager;
35
import org.gvsig.fmap.geom.exception.CreateGeometryException;
36
import org.gvsig.fmap.geom.primitive.GeneralPathX;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.dynobject.DynStruct;
40
import org.gvsig.tools.persistence.PersistenceManager;
41

    
42
/**
43
 * DOCUMENT ME!
44
 * 
45
 * @author Vicente Caballero Navarro
46
 */
47
public class PolygonAdapter extends PolyLineAdapter {
48

    
49
    public static final String PERSISTENCE_DEFINITION_NAME = "PolygonAdapter";
50
    private static final GeometryManager geomManager = GeometryLocator
51
        .getGeometryManager();
52
    private static final Logger logger = LoggerFactory
53
        .getLogger(PolygonAdapter.class);
54

    
55
    public PolygonAdapter() {
56
        super();
57
    }
58

    
59
    /**
60
     * DOCUMENT ME!
61
     * 
62
     * @param p
63
     *            DOCUMENT ME!
64
     */
65
    public void obtainShape(Point2D p) {
66
        Point2D[] points = getPoints();
67
        GeneralPathX elShape =
68
            new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, points.length);
69

    
70
        if (points.length > 0) {
71
            elShape.moveTo(((Point2D) points[0]).getX(),
72
                ((Point2D) points[0]).getY());
73
        }
74

    
75
        for (int i = 1; i < points.length; i++) {
76
            elShape.lineTo(((Point2D) points[i]).getX(),
77
                ((Point2D) points[i]).getY());
78
        }
79

    
80
        if (points.length > 0) {
81
            elShape.lineTo(p.getX(), p.getY());
82
            elShape.lineTo(((Point2D) points[0]).getX(),
83
                ((Point2D) points[0]).getY());
84
        }
85

    
86
        setGPX(elShape);
87
    }
88

    
89
    /**
90
     * DOCUMENT ME!
91
     * 
92
     * @return DOCUMENT ME!
93
     */
94
    public Geometry getGeometry() {
95
        try {
96
            return geomManager.createSurface(getGPX(), SUBTYPES.GEOM2D);
97
        } catch (CreateGeometryException e) {
98
            logger.error("Error creating a surface", e);
99
        }
100
        return null;
101
    }
102

    
103
    /**
104
     * DOCUMENT ME!
105
     * 
106
     * @param g
107
     *            DOCUMENT ME!
108
     * @param at
109
     *            DOCUMENT ME!
110
     * @param symbol
111
     *            DOCUMENT ME!
112
     */
113
    public void draw(Graphics2D g, AffineTransform at, ISymbol symbol) {
114
        GeneralPathX rectangle =
115
            new GeneralPathX(getGeometry().getPathIterator(null));
116
        rectangle.transform(at);
117

    
118
        Geometry shapeAux;
119
        try {
120
            shapeAux = geomManager.createSurface(rectangle, SUBTYPES.GEOM2D);
121
            symbol.draw(g, at, shapeAux, null, null);
122
        } catch (CreateGeometryException e) {
123
            logger.error("Error creating a surface", e);
124
        }
125
    }
126

    
127
    public static void registerPersistent() {
128
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
129
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
130
            DynStruct definition =
131
                manager.addDefinition(PolygonAdapter.class,
132
                    PERSISTENCE_DEFINITION_NAME,
133
                    "Polygon Adapter persistence definition", null, null);
134

    
135
            definition.extend(manager
136
                .getDefinition(GeometryAdapter.PERSISTENCE_DEFINITION_NAME));
137
        }
138
    }
139
}