felix@3018: package de.intevation.flys.jfree;
felix@3018: 
felix@3018: import java.awt.Shape;
felix@3018: import java.awt.geom.Rectangle2D;
felix@3018: 
felix@3018: import java.util.Iterator;
felix@3018: 
felix@3018: import org.jfree.chart.entity.ChartEntity;
felix@3018: import org.jfree.chart.entity.EntityCollection;
felix@3018: 
felix@3018: public class JFreeUtil {
felix@3018:     
felix@3018:     /** Do not instantiate. */
felix@3018:     private JFreeUtil() {
felix@3018:     }
felix@3018: 
felix@3018: 
felix@3018:     /**
felix@3018:      * True if \param hotspot collides with a Entity in \param entities.
felix@3018:      * @param hotspot Shape to compare against other shapes (bounds only).
felix@3018:      * @param entities entities against which to compare shape.
felix@3023:      * @param exclusiveEntityClass If not null, consider only entities of
felix@3023:      *        given class.
felix@3023:      * @return true if a collision (non-zero intersection) exists between
felix@3018:      *        shapes.
felix@3018:      */
felix@3023:     public static boolean collides(Shape hotspot, EntityCollection entities,
felix@3023:         Class exclusiveEntityClass) {
felix@3018:         if (entities == null) return false;
felix@3018: 
felix@3018:         Rectangle2D hotspotBox = hotspot.getBounds2D();
felix@3018:         
felix@3018:         for (Iterator i = entities.iterator(); i.hasNext(); ) {
felix@3018:             Object next = i.next();
felix@3018:             ChartEntity entity = (ChartEntity) next;
felix@3023:             if (exclusiveEntityClass == null
felix@3023:                 || exclusiveEntityClass.isInstance(entity))
felix@3023:                 {
felix@3023:                 if (entity.getArea().intersects(hotspotBox)) {
felix@3023:                     // Found collision, early stop.
felix@3023:                     return true;
felix@3023:                 }
felix@3018:             }
felix@3018:         }
felix@3018: 
felix@3018:         return false;
felix@3018:     }
felix@3018: }