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 / LinePlacementAtExtremities.java @ 43510
History | View | Annotate | Download (3.48 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.labeling.placements; |
25 |
|
26 |
import java.awt.geom.Point2D; |
27 |
|
28 |
import org.apache.batik.ext.awt.geom.PathLength; |
29 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
30 |
import org.gvsig.fmap.geom.GeometryLocator; |
31 |
import org.gvsig.fmap.geom.GeometryManager; |
32 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass; |
33 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints; |
34 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.LabelLocationMetrics; |
35 |
import org.gvsig.tools.task.Cancellable; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
public class LinePlacementAtExtremities extends AbstractLinePlacement { |
40 |
|
41 |
private static Logger logger = |
42 |
LoggerFactory.getLogger(LinePlacementAtExtremities.class); |
43 |
|
44 |
public LabelLocationMetrics initialLocation(
|
45 |
ILabelClass lc, |
46 |
IPlacementConstraints pc, PathLength pathLen, |
47 |
Cancellable cancel) { |
48 |
|
49 |
if (cancel.isCanceled()) return null; |
50 |
|
51 |
float distance = 0; |
52 |
if (pc.isAtTheEndOfLine()) {
|
53 |
distance = (float) (pathLen.lengthOfPath()) ;
|
54 |
} |
55 |
|
56 |
Point2D p = pathLen.pointAtLength(distance);
|
57 |
|
58 |
double theta = 0; |
59 |
if (pc.isParallel()) {
|
60 |
// get the line theta and apply it
|
61 |
theta = pathLen.angleAtLength(distance); |
62 |
|
63 |
} else if (pc.isPerpendicular()) { |
64 |
// get the line theta with 90 degrees
|
65 |
theta = pathLen.angleAtLength(distance) + AbstractLinePlacement.HALF_PI; |
66 |
} |
67 |
|
68 |
/*
|
69 |
* Offset the point to a distance of the height of the
|
70 |
* label class's height to make the label appear to
|
71 |
* be on the line.
|
72 |
*
|
73 |
*/
|
74 |
double x = p.getX();
|
75 |
double y = p.getY();
|
76 |
double halfHeight = lc.getBounds().getHeight()*.5; |
77 |
|
78 |
double sinTheta = Math.sin(theta); |
79 |
double cosTheta = Math.cos(theta); |
80 |
double xOffset = halfHeight * sinTheta;
|
81 |
double yOffset = halfHeight * cosTheta;
|
82 |
|
83 |
if (pc.isAtTheEndOfLine()) {
|
84 |
double width = lc.getBounds().getWidth();
|
85 |
xOffset -= width * cosTheta; |
86 |
yOffset += width * sinTheta; |
87 |
} |
88 |
|
89 |
p.setLocation(x + xOffset, y - yOffset); |
90 |
|
91 |
return new LabelLocationMetrics( |
92 |
p, |
93 |
theta, |
94 |
true);
|
95 |
} |
96 |
|
97 |
@Override
|
98 |
public boolean isSuitableFor(IPlacementConstraints placementConstraints, |
99 |
int shapeType) {
|
100 |
|
101 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
102 |
if( geomManager.isSubtype(TYPES.CURVE, shapeType) ||
|
103 |
geomManager.isSubtype(TYPES.MULTICURVE, shapeType) ) { |
104 |
return placementConstraints != null |
105 |
/* && !placementConstraints.isFollowingLine()*/
|
106 |
&& (placementConstraints.isAtTheBeginingOfLine() |
107 |
|| placementConstraints.isAtTheEndOfLine()); |
108 |
} else {
|
109 |
return false; |
110 |
} |
111 |
|
112 |
} |
113 |
|
114 |
} |