Statistics
| Revision:

svn-gvsig-desktop / trunk / extSymbology / src / org / gvsig / symbology / fmap / labeling / placements / LinePlacementInTheMiddle.java @ 18755

History | View | Annotate | Download (3.44 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.isInTheMiddleOfLine() && !placementConstraints.isFollowingLine();
67
                }
68
                return false;
69
        }
70

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

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

    
125
}
126