Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/CollisionFreeXYTextAnnotation.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 0f7abd95c6e2 |
children | 6ed8ebd48d6e |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
1 package de.intevation.flys.jfree; | |
2 | |
3 import org.apache.log4j.Logger; | |
4 | |
5 import java.awt.Shape; | |
6 import java.awt.geom.Rectangle2D; | |
7 | |
8 import org.jfree.chart.entity.EntityCollection; | |
9 import org.jfree.chart.annotations.XYTextAnnotation; | |
10 import org.jfree.chart.axis.ValueAxis; | |
11 import org.jfree.chart.plot.PlotOrientation; | |
12 import org.jfree.chart.plot.XYPlot; | |
13 import org.jfree.chart.entity.XYAnnotationEntity; | |
14 import org.jfree.chart.plot.PlotRenderingInfo; | |
15 import org.jfree.chart.plot.Plot; | |
16 | |
17 import org.jfree.text.TextUtilities; | |
18 | |
19 import org.jfree.ui.RectangleEdge; | |
20 | |
21 /** | |
22 * Custom Annotations class that is drawn only if no collisions with other | |
23 * already drawn CustomAnnotations in current plot are found. | |
24 */ | |
25 public class CollisionFreeXYTextAnnotation extends XYTextAnnotation { | |
26 | |
27 /** Logger for this class. */ | |
28 private static Logger logger = | |
29 Logger.getLogger(CollisionFreeXYTextAnnotation.class); | |
30 | |
31 | |
32 public CollisionFreeXYTextAnnotation(String text, double x, double y) { | |
33 super(text, x, y); | |
34 } | |
35 | |
36 | |
37 /** | |
38 * Draw the Annotation only if it does not collide with other | |
39 * already drawn Annotations- texts. | |
40 * | |
41 * @param g2 the graphics device. | |
42 * @param plot the plot. | |
43 * @param dataArea the data area. | |
44 * @param domainAxis the domain axis. | |
45 * @param rangeAxis the range axis. | |
46 * @param rendererIndex the render index. | |
47 * @param info state information, escpecially collects info about | |
48 * already drawn shapes (and thus annotations), used | |
49 * for collision detection. | |
50 */ | |
51 @Override | |
52 public void draw( | |
53 java.awt.Graphics2D g2, | |
54 XYPlot plot, | |
55 java.awt.geom.Rectangle2D dataArea, | |
56 ValueAxis domainAxis, | |
57 ValueAxis rangeAxis, | |
58 int rendererIndex, | |
59 PlotRenderingInfo info | |
60 ) { | |
61 // From superclass, adjusted access only. | |
62 PlotOrientation orientation = plot.getOrientation(); | |
63 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( | |
64 plot.getDomainAxisLocation(), orientation); | |
65 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( | |
66 plot.getRangeAxisLocation(), orientation); | |
67 | |
68 float anchorX = (float) domainAxis.valueToJava2D( | |
69 this.getX(), dataArea, domainEdge); | |
70 float anchorY = (float) rangeAxis.valueToJava2D( | |
71 this.getY(), dataArea, rangeEdge); | |
72 | |
73 if (orientation == PlotOrientation.HORIZONTAL) { | |
74 float tempAnchor = anchorX; | |
75 anchorX = anchorY; | |
76 anchorY = tempAnchor; | |
77 } | |
78 | |
79 g2.setFont(getFont()); | |
80 Shape hotspot = TextUtilities.calculateRotatedStringBounds( | |
81 getText(), g2, anchorX, anchorY, getTextAnchor(), | |
82 getRotationAngle(), getRotationAnchor()); | |
83 | |
84 // Deviation from superclass: prevent collision. | |
85 Rectangle2D hotspotBox = hotspot.getBounds2D(); | |
86 | |
87 if (JFreeUtil.collides(hotspot, info.getOwner().getEntityCollection(), | |
88 XYAnnotationEntity.class)) { | |
89 return; | |
90 } | |
91 | |
92 if (this.getBackgroundPaint() != null) { | |
93 g2.setPaint(this.getBackgroundPaint()); | |
94 g2.fill(hotspot); | |
95 } | |
96 g2.setPaint(getPaint()); | |
97 TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY, | |
98 getTextAnchor(), getRotationAngle(), getRotationAnchor()); | |
99 if (this.isOutlineVisible()) { | |
100 g2.setStroke(this.getOutlineStroke()); | |
101 g2.setPaint(this.getOutlinePaint()); | |
102 g2.draw(hotspot); | |
103 } | |
104 | |
105 //String toolTip = getToolTipText(); | |
106 //String url = getURL(); | |
107 String toolTip = "CollisionFreeXYTextAnnotation"; | |
108 String url = toolTip; | |
109 | |
110 if (toolTip != null || url != null) { | |
111 addEntity(info, hotspot, rendererIndex, toolTip, url); | |
112 } | |
113 else { | |
114 addEntity(info, hotspot, rendererIndex, | |
115 "CollisionFreeXYTextAnnotation", | |
116 "CollisionFreeXYTextAnnotation"); | |
117 } | |
118 } | |
119 | |
120 /** | |
121 * A utility method for adding an {@link CollisionFreeXYAnnotationEntity} to | |
122 * a {@link PlotRenderingInfo} instance. | |
123 * | |
124 * @param info the plot rendering info (<code>null</code> permitted). | |
125 * @param hotspot the hotspot area. | |
126 * @param rendererIndex the renderer index. | |
127 * @param toolTipText the tool tip text. | |
128 * @param urlText the URL text. | |
129 */ | |
130 protected void addEntity(PlotRenderingInfo info, | |
131 Shape hotspot, int rendererIndex, | |
132 String toolTipText, String urlText) { | |
133 if (info == null) { | |
134 return; | |
135 } | |
136 EntityCollection entities = info.getOwner().getEntityCollection(); | |
137 if (entities == null) { | |
138 return; | |
139 } | |
140 CollisionFreeXYTextAnnotationEntity entity = | |
141 new CollisionFreeXYTextAnnotationEntity(hotspot, | |
142 rendererIndex, toolTipText, urlText); | |
143 entities.add(entity); | |
144 } | |
145 } | |
146 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |