Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / placements / LinePlacementInTheMiddle.java @ 40911

History | View | Annotate | Download (4 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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.labeling.placements;
42

    
43
import java.awt.geom.Point2D;
44

    
45
import org.apache.batik.ext.awt.geom.PathLength;
46
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.fmap.geom.Geometry.TYPES;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.type.GeometryType;
50
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
51
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
52
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.LabelLocationMetrics;
53
import org.gvsig.tools.task.Cancellable;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

    
57
/**
58
 *
59
 * LinePlacementInTheMiddle.java
60
 *
61
 *
62
 * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 17, 2007
63
 *
64
 */
65
public class LinePlacementInTheMiddle extends AbstractLinePlacement {
66

    
67
        private static Logger logger =
68
                        LoggerFactory.getLogger(LinePlacementInTheMiddle.class);
69
                                        
70
        public boolean isSuitableFor(
71
                        IPlacementConstraints placementConstraints,
72
                        int shapeType) {
73
                
74
                GeometryType gt = null;
75
                
76
                try {
77
                        gt = GeometryLocator.getGeometryManager().getGeometryType(
78
                                        shapeType, Geometry.SUBTYPES.GEOM2D);
79
                } catch (Exception e) {
80
                        logger.error("While getting aux geo type.", e);
81
                }
82
                
83
                if (gt.isTypeOf(TYPES.CURVE)
84
                                || shapeType == TYPES.MULTICURVE) {
85
                        
86
                        return placementConstraints != null &&
87
                           placementConstraints.isInTheMiddleOfLine()
88
                          /* && !placementConstraints.isFollowingLine()*/;
89
                } else {
90
                        return false;
91
                }
92
                
93
        }
94

    
95
        public LabelLocationMetrics initialLocation(ILabelClass lc,
96
                        IPlacementConstraints pc, PathLength pathLen, Cancellable cancel) {
97
                
98
                if (cancel.isCanceled()) return null;
99

    
100
                float length = pathLen.lengthOfPath();
101
                float distance = (float) (length * 0.5);
102

    
103

    
104
                double theta = 0;
105
                if (pc.isParallel()) {
106
                        // get the line theta and apply it
107
                        theta = pathLen.angleAtLength(distance);
108

    
109
                } else if (pc.isPerpendicular()) {
110
                        // get the line theta with 90 degrees
111
                        theta = pathLen.angleAtLength(distance) + AbstractLinePlacement.HALF_PI;
112
                }
113

    
114
                Point2D p = pathLen.pointAtLength(distance);
115

    
116
                /*
117
                 * Offset the point to a distance of the height of the
118
                 * label class's height to make the label appear to
119
                 * be on the line.
120
                 *
121
                 */
122
                double x = p.getX();
123
                double y = p.getY();
124
                double halfHeight = lc.getBounds().getHeight()*.5;
125
                double halfWidth = lc.getBounds().getWidth()*.5;
126

    
127
                double sinTheta = Math.sin(theta);
128
                double cosTheta = Math.cos(theta);
129

    
130
                double xOffset = halfHeight * sinTheta;
131
                double yOffset = halfHeight * cosTheta;
132

    
133
                /*
134
                 * now, offset the anchor point as much as need to
135
                 * make the center of the label be the middle of the
136
                 * line
137
                 */
138
                xOffset -= halfWidth * cosTheta;
139
                yOffset += halfWidth * sinTheta;
140

    
141
                p.setLocation(x + xOffset, y - yOffset);
142

    
143
                return new LabelLocationMetrics(
144
                                p,
145
                                theta,
146
                                true);
147
        }
148

    
149
}
150