felix@2157: package de.intevation.flys.jfree;
felix@2157: 
felix@2157: import java.awt.Shape;
felix@2157: import java.awt.geom.Rectangle2D;
felix@2157: 
felix@2318: import org.jfree.chart.entity.EntityCollection;
felix@2157: import org.jfree.chart.annotations.XYTextAnnotation;
felix@2157: import org.jfree.chart.axis.ValueAxis;
felix@2157: import org.jfree.chart.plot.PlotOrientation;
felix@2157: import org.jfree.chart.plot.XYPlot;
felix@2157: import org.jfree.chart.entity.XYAnnotationEntity;
felix@2157: import org.jfree.chart.plot.PlotRenderingInfo;
felix@2157: import org.jfree.chart.plot.Plot;
felix@2157: 
felix@2157: import org.jfree.text.TextUtilities;
felix@2157: 
felix@2157: import org.jfree.ui.RectangleEdge;
felix@2157: 
felix@2157: /**
felix@2157:  * Custom Annotations class that is drawn only if no collisions with other
felix@2157:  * already drawn CustomAnnotations in current plot are found.
felix@2157:  */
felix@2157: public class CollisionFreeXYTextAnnotation extends XYTextAnnotation {
felix@2157: 
felix@2161:     public CollisionFreeXYTextAnnotation(String text, double x, double y) {
felix@2157:         super(text, x, y);
felix@2157:     }
felix@2157: 
felix@2157: 
felix@2157:     /**
felix@2157:      * Draw the Annotation only if it does not collide with other
felix@2157:      * already drawn Annotations- texts.
felix@2157:      *
felix@2157:      * @param g2            the graphics device.
felix@2157:      * @param plot          the plot.
felix@2157:      * @param dataArea      the data area.
felix@2157:      * @param domainAxis    the domain axis.
felix@2157:      * @param rangeAxis     the range axis.
felix@2157:      * @param rendererIndex the render index.
felix@2157:      * @param info          state information, escpecially collects info about
felix@2157:      *                      already drawn shapes (and thus annotations), used
felix@2157:      *                      for collision detection.
felix@2157:      */
felix@2157:     @Override
felix@2157:     public void draw(
felix@2157:         java.awt.Graphics2D g2,
felix@2157:         XYPlot plot,
felix@2157:         java.awt.geom.Rectangle2D dataArea,
felix@2157:         ValueAxis domainAxis,
felix@2157:         ValueAxis rangeAxis,
felix@2157:         int rendererIndex,
felix@2157:         PlotRenderingInfo info
felix@2157:     ) {
felix@2157:         // From superclass, adjusted access only.
felix@2157:         PlotOrientation orientation = plot.getOrientation();
felix@2157:         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
felix@2157:                 plot.getDomainAxisLocation(), orientation);
felix@2157:         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
felix@2157:                 plot.getRangeAxisLocation(), orientation);
felix@2157: 
felix@2157:         float anchorX = (float) domainAxis.valueToJava2D(
felix@2157:                 this.getX(), dataArea, domainEdge);
felix@2157:         float anchorY = (float) rangeAxis.valueToJava2D(
felix@2157:                 this.getY(), dataArea, rangeEdge);
felix@2157: 
felix@2157:         if (orientation == PlotOrientation.HORIZONTAL) {
felix@2157:             float tempAnchor = anchorX;
felix@2157:             anchorX = anchorY;
felix@2157:             anchorY = tempAnchor;
felix@2157:         }
felix@2157: 
felix@2157:         g2.setFont(getFont());
felix@2157:         Shape hotspot = TextUtilities.calculateRotatedStringBounds(
felix@2157:                 getText(), g2, anchorX, anchorY, getTextAnchor(),
felix@2157:                 getRotationAngle(), getRotationAnchor());
felix@2157: 
felix@2157:         // Deviation from superclass: prevent collision.
felix@3023:         if (JFreeUtil.collides(hotspot, info.getOwner().getEntityCollection(),
felix@3023:             XYAnnotationEntity.class)) {
felix@3023:             return;
felix@2157:         }
felix@2157: 
felix@2157:         if (this.getBackgroundPaint() != null) {
felix@2157:             g2.setPaint(this.getBackgroundPaint());
felix@2157:             g2.fill(hotspot);
felix@2157:         }
felix@2157:         g2.setPaint(getPaint());
felix@2157:         TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY,
felix@2157:                 getTextAnchor(), getRotationAngle(), getRotationAnchor());
felix@2157:         if (this.isOutlineVisible()) {
felix@2157:             g2.setStroke(this.getOutlineStroke());
felix@2157:             g2.setPaint(this.getOutlinePaint());
felix@2157:             g2.draw(hotspot);
felix@2157:         }
felix@2157: 
felix@2318:         //String toolTip = getToolTipText();
felix@2318:         //String url = getURL();
felix@2318:         String toolTip = "CollisionFreeXYTextAnnotation";
felix@2318:         String url     = toolTip;
felix@2318: 
felix@2157:         if (toolTip != null || url != null) {
felix@2157:             addEntity(info, hotspot, rendererIndex, toolTip, url);
felix@2157:         }
teichmann@4048:         // XXX: DEAD CODE (as long as a hard value is assigned to toolTip
teichmann@4048:         /*
felix@2157:         else {
felix@2157:             addEntity(info, hotspot, rendererIndex,
felix@2157:                 "CollisionFreeXYTextAnnotation",
felix@2157:                 "CollisionFreeXYTextAnnotation");
felix@2157:         }
teichmann@4048:         */
felix@2157:     }
felix@2318: 
felix@2318:     /**
felix@2318:      * A utility method for adding an {@link CollisionFreeXYAnnotationEntity} to
felix@2318:      * a {@link PlotRenderingInfo} instance.
felix@2318:      *
felix@2318:      * @param info  the plot rendering info (<code>null</code> permitted).
felix@2318:      * @param hotspot  the hotspot area.
felix@2318:      * @param rendererIndex  the renderer index.
felix@2318:      * @param toolTipText  the tool tip text.
felix@2318:      * @param urlText  the URL text.
felix@2318:      */
felix@2318:     protected void addEntity(PlotRenderingInfo info,
felix@2318:                              Shape hotspot, int rendererIndex,
felix@2318:                              String toolTipText, String urlText) {
felix@2318:         if (info == null) {
felix@2318:             return;
felix@2318:         }
felix@2318:         EntityCollection entities = info.getOwner().getEntityCollection();
felix@2318:         if (entities == null) {
felix@2318:             return;
felix@2318:         }
felix@2318:         CollisionFreeXYTextAnnotationEntity entity =
felix@2318:             new CollisionFreeXYTextAnnotationEntity(hotspot,
felix@2318:                 rendererIndex, toolTipText, urlText);
felix@2318:         entities.add(entity);
sascha@3076:     }
felix@2157: }
felix@2157: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :