Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / labeling / placements / LinePlacementAtExtremities.java @ 29131

History | View | Annotate | Download (3.43 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
 * LinePlacementAtExtremities.java
56
 *
57
 *
58
 * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 17, 2007
59
 *
60
 */
61
public class LinePlacementAtExtremities extends AbstractLinePlacement {
62

    
63
        public LabelLocationMetrics initialLocation(LabelClass lc, IPlacementConstraints pc, PathLength pathLen, Cancellable cancel) {
64
                if (cancel.isCanceled()) return null;
65

    
66
                float distance = 0;
67
                if (pc.isAtTheEndOfLine()) {
68
                        distance = (float) (pathLen.lengthOfPath()) ;
69
                }
70

    
71
                Point2D p = pathLen.pointAtLength(distance);
72

    
73
                double theta = 0;
74
                if (pc.isParallel()) {
75
                        // get the line theta and apply it
76
                        theta = pathLen.angleAtLength(distance);
77

    
78
                } else if (pc.isPerpendicular()) {
79
                        // get the line theta with 90 degrees
80
                        theta = pathLen.angleAtLength(distance) + AbstractLinePlacement.HALF_PI;
81
                }
82

    
83
                /*
84
                 * Offset the point to a distance of the height of the
85
                 * label class's height to make the label appear to
86
                 * be on the line.
87
                 *
88
                 */
89
                double x = p.getX();
90
                double y = p.getY();
91
                double halfHeight = lc.getBounds().getHeight()*.5;
92

    
93
                double sinTheta = Math.sin(theta);
94
                double cosTheta = Math.cos(theta);
95
                double xOffset = halfHeight * sinTheta;
96
                double yOffset = halfHeight * cosTheta;
97

    
98
                if (pc.isAtTheEndOfLine()) {
99
                        double width = lc.getBounds().getWidth();
100
                        xOffset -= width * cosTheta;
101
                        yOffset += width * sinTheta;
102
                }
103

    
104
                p.setLocation(x + xOffset, y - yOffset);
105

    
106
                return new LabelLocationMetrics(
107
                                p,
108
                                theta,
109
                                true);
110
        }
111

    
112
        public boolean isSuitableFor(IPlacementConstraints placementConstraints,
113
                        int shapeType) {
114
                if ((shapeType%FShape.Z) == FShape.LINE) {
115
                        return placementConstraints != null/* && !placementConstraints.isFollowingLine()*/
116
                                        && (placementConstraints.isAtTheBeginingOfLine() || placementConstraints
117
                                                        .isAtTheEndOfLine());
118
                }
119
                return false;
120
        }
121

    
122
}