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 @ 40911
History | View | Annotate | Download (3.98 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.labeling.placements; |
42 |
|
43 |
import java.awt.geom.Point2D; |
44 |
|
45 |
import org.apache.batik.ext.awt.geom.PathLength; |
46 |
import org.gvsig.fmap.geom.Geometry; |
47 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
48 |
import org.gvsig.fmap.geom.GeometryLocator; |
49 |
import org.gvsig.fmap.geom.type.GeometryType; |
50 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass; |
51 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints; |
52 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.LabelLocationMetrics; |
53 |
import org.gvsig.tools.task.Cancellable; |
54 |
import org.slf4j.Logger; |
55 |
import org.slf4j.LoggerFactory; |
56 |
|
57 |
/**
|
58 |
*
|
59 |
* LinePlacementAtExtremities.java
|
60 |
*
|
61 |
*
|
62 |
* @author jaume dominguez faus - jaume.dominguez@iver.es Dec 17, 2007
|
63 |
*
|
64 |
*/
|
65 |
public class LinePlacementAtExtremities extends AbstractLinePlacement { |
66 |
|
67 |
private static Logger logger = |
68 |
LoggerFactory.getLogger(LinePlacementAtExtremities.class); |
69 |
|
70 |
public LabelLocationMetrics initialLocation(
|
71 |
ILabelClass lc, |
72 |
IPlacementConstraints pc, PathLength pathLen, |
73 |
Cancellable cancel) { |
74 |
|
75 |
if (cancel.isCanceled()) return null; |
76 |
|
77 |
float distance = 0; |
78 |
if (pc.isAtTheEndOfLine()) {
|
79 |
distance = (float) (pathLen.lengthOfPath()) ;
|
80 |
} |
81 |
|
82 |
Point2D p = pathLen.pointAtLength(distance);
|
83 |
|
84 |
double theta = 0; |
85 |
if (pc.isParallel()) {
|
86 |
// get the line theta and apply it
|
87 |
theta = pathLen.angleAtLength(distance); |
88 |
|
89 |
} else if (pc.isPerpendicular()) { |
90 |
// get the line theta with 90 degrees
|
91 |
theta = pathLen.angleAtLength(distance) + AbstractLinePlacement.HALF_PI; |
92 |
} |
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 |
|
104 |
double sinTheta = Math.sin(theta); |
105 |
double cosTheta = Math.cos(theta); |
106 |
double xOffset = halfHeight * sinTheta;
|
107 |
double yOffset = halfHeight * cosTheta;
|
108 |
|
109 |
if (pc.isAtTheEndOfLine()) {
|
110 |
double width = lc.getBounds().getWidth();
|
111 |
xOffset -= width * cosTheta; |
112 |
yOffset += width * sinTheta; |
113 |
} |
114 |
|
115 |
p.setLocation(x + xOffset, y - yOffset); |
116 |
|
117 |
return new LabelLocationMetrics( |
118 |
p, |
119 |
theta, |
120 |
true);
|
121 |
} |
122 |
|
123 |
public boolean isSuitableFor(IPlacementConstraints placementConstraints, |
124 |
int shapeType) {
|
125 |
|
126 |
GeometryType gt = null;
|
127 |
|
128 |
try {
|
129 |
gt = GeometryLocator.getGeometryManager().getGeometryType( |
130 |
shapeType, Geometry.SUBTYPES.GEOM2D); |
131 |
} catch (Exception e) { |
132 |
logger.error("While getting aux geo type.", e);
|
133 |
} |
134 |
|
135 |
if (gt.isTypeOf(TYPES.CURVE)
|
136 |
|| shapeType == TYPES.MULTICURVE) { |
137 |
|
138 |
return placementConstraints != null |
139 |
/* && !placementConstraints.isFollowingLine()*/
|
140 |
&& (placementConstraints.isAtTheBeginingOfLine() |
141 |
|| placementConstraints.isAtTheEndOfLine()); |
142 |
} else {
|
143 |
return false; |
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
} |