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