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 / PolyLineAdapter.java @ 36648

History | View | Annotate | Download (6.02 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.Color;
25
import java.awt.Graphics2D;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.Point2D;
28

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

    
32
import org.gvsig.compat.print.PrintAttributes;
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
37
import org.gvsig.fmap.geom.exception.CreateGeometryException;
38
import org.gvsig.fmap.geom.primitive.GeneralPathX;
39
import org.gvsig.fmap.mapcontext.MapContextLocator;
40
import org.gvsig.fmap.mapcontext.MapContextManager;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DynStruct;
45
import org.gvsig.tools.persistence.PersistenceManager;
46

    
47
/**
48
 * DOCUMENT ME!
49
 * 
50
 * @author Vicente Caballero Navarro
51
 */
52
public class PolyLineAdapter extends GeometryAdapter {
53

    
54
    public static final String PERSISTENCE_DEFINITION_NAME = "PolyLineAdapter";
55
    private static final GeometryManager geomManager = GeometryLocator
56
        .getGeometryManager();
57
    private static final Logger logger = LoggerFactory
58
        .getLogger(PolyLineAdapter.class);
59
    protected Point2D pointPosition = new Point2D.Double();
60
    private AffineTransform identity = new AffineTransform();
61

    
62
    private MapContextManager mapContextManager = MapContextLocator
63
        .getMapContextManager();
64

    
65
    public PolyLineAdapter() {
66
        super();
67
    }
68

    
69
    /**
70
     * DOCUMENT ME!
71
     * 
72
     * @param p
73
     *            DOCUMENT ME!
74
     */
75
    public void obtainShape(Point2D p) {
76
        Point2D[] points = getPoints();
77
        GeneralPathX elShape =
78
            new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, points.length);
79

    
80
        if (points.length > 0) {
81
            elShape.moveTo((points[0]).getX(), (points[0]).getY());
82
        }
83

    
84
        for (int i = 0; i < points.length; i++) {
85
            elShape.lineTo((points[i]).getX(), (points[i]).getY());
86
        }
87

    
88
        if (points.length > 0) {
89
            elShape.lineTo(p.getX(), p.getY());
90
        }
91

    
92
        setGPX(elShape);
93
    }
94

    
95
    /**
96
     * DOCUMENT ME!
97
     * 
98
     * @return DOCUMENT ME!
99
     */
100
    public Geometry getGeometry(AffineTransform at) {
101
        GeneralPathX polyLine;
102
        try {
103
            polyLine =
104
                new GeneralPathX(geomManager.createCurve(getGPX(),
105
                    SUBTYPES.GEOM2D).getPathIterator(null));
106
            polyLine.transform(at);
107

    
108
            return geomManager.createCurve(polyLine, SUBTYPES.GEOM2D);
109
        } catch (CreateGeometryException e) {
110
            logger.error("Error creating a curve", e);
111
        }
112
        return null;
113
    }
114

    
115
    /**
116
     * DOCUMENT ME!
117
     * 
118
     * @param g
119
     *            DOCUMENT ME!
120
     * @param at
121
     *            DOCUMENT ME!
122
     * @param symbol
123
     *            DOCUMENT ME!
124
     */
125
    public void draw(Graphics2D g, AffineTransform at, ISymbol symbol) {
126
        Geometry shapeAux = getGeometry(at);
127
        symbol.draw(g, at, shapeAux, null, null);
128
        // FGraphicUtilities.DrawShape(g, at, shapeAux, symbol);
129
    }
130

    
131
    public void print(Graphics2D g, AffineTransform at, ISymbol symbol,
132
        PrintAttributes properties) {
133
        Geometry shapeAux = getGeometry(at);
134
        symbol.print(g, at, shapeAux, properties);
135
    }
136

    
137
    /**
138
     * DOCUMENT ME!
139
     * 
140
     * @param g
141
     *            DOCUMENT ME!
142
     * @param at
143
     *            DOCUMENT ME!
144
     * @param andLastPoint
145
     *            DOCUMENT ME!
146
     */
147
    public void paint(Graphics2D g, AffineTransform at, boolean andLastPoint) {
148
        if (andLastPoint) {
149
            obtainShape(pointPosition);
150
        }
151

    
152
        Geometry shapeAux = getGeometry(at);
153
        // ISymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE, Color.red);
154
        ILineSymbol symbol =
155
            (ILineSymbol) mapContextManager.getSymbolManager().createSymbol(
156
                ILineSymbol.SYMBOL_NAME);
157
        symbol.setLineColor(Color.RED);
158
        symbol.draw(g, identity, shapeAux, null, null);
159
        // FGraphicUtilities.DrawShape(g, identity, shapeAux,
160
        // new FSymbol(FConstant.SYMBOL_TYPE_LINE, Color.red));
161
    }
162

    
163
    /**
164
     * DOCUMENT ME!
165
     * 
166
     * @param p
167
     *            DOCUMENT ME!
168
     */
169
    public void pointPosition(Point2D p) {
170
        pointPosition = p;
171
    }
172

    
173
    public static void registerPersistent() {
174
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
175
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
176
            DynStruct definition =
177
                manager.addDefinition(PolyLineAdapter.class,
178
                    PERSISTENCE_DEFINITION_NAME,
179
                    "PolyLine Adapter persistence definition", null, null);
180

    
181
            definition.extend(manager
182
                .getDefinition(GeometryAdapter.PERSISTENCE_DEFINITION_NAME));
183
        }
184
    }
185
}