comparison artifacts/src/main/java/org/dive4elements/river/jfree/CollisionFreeXYTextAnnotation.java @ 5838:5aa05a7a34b7

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

http://dive4elements.wald.intevation.org