Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / labeling / placements / LinePlacementInTheMiddle.java @ 20768

History | View | Annotate | Download (3.49 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.symbology.fmap.labeling.placements;
42

    
43
import java.awt.geom.Point2D;
44

    
45
import org.apache.batik.ext.awt.geom.PathLength;
46

    
47
import com.iver.cit.gvsig.fmap.core.FShape;
48
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IPlacementConstraints;
49
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass;
50
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelLocationMetrics;
51
import com.iver.utiles.swing.threads.Cancellable;
52

    
53
/**
54
 * 
55
 * LinePlacementInTheMiddle.java
56
 *
57
 * 
58
 * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 17, 2007
59
 *
60
 */
61
public class LinePlacementInTheMiddle extends AbstractLinePlacement {
62
        
63
        public boolean isSuitableFor(IPlacementConstraints placementConstraints,
64
                        int shapeType) {
65
                if (shapeType == FShape.LINE) {
66
                        return placementConstraints != null && 
67
                           placementConstraints.isInTheMiddleOfLine() 
68
                           && !placementConstraints.isFollowingLine();
69
                }
70
                return false;
71
        }
72

    
73
        @Override
74
        LabelLocationMetrics initialLocation(LabelClass lc,
75
                        IPlacementConstraints pc, PathLength pathLen, Cancellable cancel) {
76
                if (cancel.isCanceled()) return null;
77
                
78
                float length = pathLen.lengthOfPath();
79
                float distance = (float) (length * 0.5);
80
                
81
                
82
                double theta = 0;
83
                if (pc.isParallel()) {
84
                        // get the line theta and apply it
85
                        theta = pathLen.angleAtLength(distance);
86

    
87
                } else if (pc.isPerpendicular()) {
88
                        // get the line theta with 90 degrees
89
                        theta = pathLen.angleAtLength(distance) + AbstractLinePlacement.HALF_PI;
90
                }
91
                
92
                Point2D p = pathLen.pointAtLength(distance);
93
                
94
                /* 
95
                 * Offset the point to a distance of the height of the
96
                 * label class's height to make the label appear to
97
                 * be on the line.
98
                 * 
99
                 */ 
100
                double x = p.getX();
101
                double y = p.getY();
102
                double halfHeight = lc.getBounds().getHeight()*.5;
103
                double halfWidth = lc.getBounds().getWidth()*.5;
104
                
105
                double sinTheta = Math.sin(theta);
106
                double cosTheta = Math.cos(theta);
107
                
108
                double xOffset = halfHeight * sinTheta;
109
                double yOffset = halfHeight * cosTheta;
110
                
111
                /*
112
                 * now, offset the anchor point as much as need to
113
                 * make the center of the label be the middle of the
114
                 * line 
115
                 */
116
                xOffset -= halfWidth * cosTheta;
117
                yOffset += halfWidth * sinTheta;
118
                
119
                p.setLocation(x + xOffset, y - yOffset);
120
                
121
                return new LabelLocationMetrics(
122
                                p,
123
                                theta,
124
                                true);
125
        }
126

    
127
}
128