if (event.getEntity() instanceof XYItemEntity) {
// The trigger point should correspond to the XYItemEntity's
// position.
lastPointClicked = event.getTrigger().getPoint();
XYItemEntity entity = (XYItemEntity) event.getEntity();
lastDatasetSelected = entity.getDataset();
int series = entity.getSeriesIndex();
int item = entity.getItem();
lastObSelected = obsModel.getValidObservation(series, item);
if (event.getTrigger().getClickCount() == 2) {
new ObservationDetailsDialog(lastObSelected);
}
} else {
// ...else if not XYItemEntity as subject of the event, select a
// valid observation by asking: which XYItemEntity is closest to the
// cross hairs?
// Where are the cross hairs pointing?
lastPointClicked = chartPanel.getAnchor();
EntityCollection entities = chartPanel.getChartRenderingInfo()
.getEntityCollection();
double closestDist = Double.MAX_VALUE;
// Note: This operation is linear in the number of visible
// observations!
// Unfortunately, the list of XYItemEntities must always be searched
// exhaustively since we don't know which XYItemEntity will turn out
// to be closest to the mouse selection. Actually, this may not be
// the case if we can assume an ordering of XYItemEntities by domain
// (X). If so, once itemBounds.getCenterX() is greater than
// lastPointClicked.getX(), we could terminate the loop. But I don't
// know if we can make that assumption.
@SuppressWarnings("unchecked")
Iterator it = entities.iterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof XYItemEntity) {
XYItemEntity entity = (XYItemEntity) o;
Rectangle2D itemBounds = entity.getArea().getBounds2D();
Point2D centerPt = new Point2D.Double(itemBounds
.getCenterX(), itemBounds.getCenterY());
double dist = centerPt.distance(lastPointClicked);
if (dist < closestDist) {
closestDist = dist;
lastDatasetSelected = entity.getDataset();
lastObSelected = obsModel.getValidObservation(entity
.getSeriesIndex(), entity.getItem());
}
// Note: The approach below definitely does not work.
// if (item.getArea().contains(lastPointClicked)) {
// lastObSelected = obsModel.getValidObservation(item