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 @ 43510
History | View | Annotate | Download (3.55 KB)
1 | 43510 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40911 | jldominguez | *
|
4 | 43510 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40911 | jldominguez | *
|
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 | 43510 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40911 | jldominguez | * 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 | 43510 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40911 | jldominguez | *
|
21 | 43510 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40911 | jldominguez | */
|
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 | 43510 | jjdelcerro | import org.gvsig.fmap.geom.GeometryManager; |
32 | 40911 | jldominguez | 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 LinePlacementInTheMiddle extends AbstractLinePlacement { |
||
40 | |||
41 | private static Logger logger = |
||
42 | LoggerFactory.getLogger(LinePlacementInTheMiddle.class); |
||
43 | |||
44 | 43510 | jjdelcerro | @Override
|
45 | 40911 | jldominguez | public boolean isSuitableFor( |
46 | IPlacementConstraints placementConstraints, |
||
47 | int shapeType) {
|
||
48 | |||
49 | 43510 | jjdelcerro | GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
50 | if( geomManager.isSubtype(TYPES.CURVE, shapeType) ||
|
||
51 | geomManager.isSubtype(TYPES.MULTICURVE, shapeType) ) { |
||
52 | 40911 | jldominguez | return placementConstraints != null && |
53 | placementConstraints.isInTheMiddleOfLine() |
||
54 | /* && !placementConstraints.isFollowingLine()*/;
|
||
55 | } else {
|
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | } |
||
60 | |||
61 | 43510 | jjdelcerro | @Override
|
62 | 40911 | jldominguez | public LabelLocationMetrics initialLocation(ILabelClass lc,
|
63 | IPlacementConstraints pc, PathLength pathLen, Cancellable cancel) { |
||
64 | |||
65 | 43510 | jjdelcerro | if (cancel.isCanceled()) {
|
66 | return null; |
||
67 | } |
||
68 | 40911 | jldominguez | |
69 | float length = pathLen.lengthOfPath();
|
||
70 | float distance = (float) (length * 0.5); |
||
71 | |||
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 | Point2D p = pathLen.pointAtLength(distance);
|
||
84 | |||
85 | /*
|
||
86 | * Offset the point to a distance of the height of the
|
||
87 | * label class's height to make the label appear to
|
||
88 | * be on the line.
|
||
89 | *
|
||
90 | */
|
||
91 | double x = p.getX();
|
||
92 | double y = p.getY();
|
||
93 | double halfHeight = lc.getBounds().getHeight()*.5; |
||
94 | double halfWidth = lc.getBounds().getWidth()*.5; |
||
95 | |||
96 | double sinTheta = Math.sin(theta); |
||
97 | double cosTheta = Math.cos(theta); |
||
98 | |||
99 | double xOffset = halfHeight * sinTheta;
|
||
100 | double yOffset = halfHeight * cosTheta;
|
||
101 | |||
102 | /*
|
||
103 | * now, offset the anchor point as much as need to
|
||
104 | * make the center of the label be the middle of the
|
||
105 | * line
|
||
106 | */
|
||
107 | xOffset -= halfWidth * cosTheta; |
||
108 | yOffset += halfWidth * sinTheta; |
||
109 | |||
110 | p.setLocation(x + xOffset, y - yOffset); |
||
111 | |||
112 | return new LabelLocationMetrics( |
||
113 | p, |
||
114 | theta, |
||
115 | true);
|
||
116 | } |
||
117 | |||
118 | } |