final ElementRenderer rendererRoot = getElementRenderer();
final ReportDocumentContext renderContext = getRenderContext();
if (clearSelectionOnDrag)
{
final DocumentContextSelectionModel selectionModel = renderContext.getSelectionModel();
selectionModel.clearSelection();
clearSelectionOnDrag = false;
}
selectionRectangleTarget = e.getPoint();
// Calculate the bounding box for the selection
final double y1 = Math.min(normalizedSelectionRectangleOrigin.getY(), normalizedSelectionRectangleTarget.getY());
final double x1 = Math.min(normalizedSelectionRectangleOrigin.getX(), normalizedSelectionRectangleTarget.getX());
final double y2 = Math.max(normalizedSelectionRectangleOrigin.getY(), normalizedSelectionRectangleTarget.getY());
final double x2 = Math.max(normalizedSelectionRectangleOrigin.getX(), normalizedSelectionRectangleTarget.getX());
final Element[] allNodes = rendererRoot.getElementsAt(x1, y1, x2 - x1, y2 - y1);
final DocumentContextSelectionModel selectionModel = renderContext.getSelectionModel();
// Convert between points to micro-points (1 point X 100K is a micro-point)
final StrictBounds rect1 = StrictGeomUtility.createBounds(x1, y1, x2 - x1, y2 - y1);
final StrictBounds rect2 = new StrictBounds();
for (int i = allNodes.length - 1; i >= 0; i -= 1)
{
final Element element = allNodes[i];
if (element instanceof RootLevelBand)
{
continue;
}
final CachedLayoutData data = ModelUtility.getCachedLayoutData(element);
rect2.setRect(data.getX(), data.getY(), data.getWidth(), data.getHeight());
// Checking if the bounding box intersects an element
if (StrictBounds.intersects(rect1, rect2))
{
if (selectionModel.add(element))
{
newlySelectedElements.add(element);
}
}
}
// second step, check which previously added elements are no longer selected by the rectangle.
for (Iterator<Element> visualReportElementIterator = newlySelectedElements.iterator(); visualReportElementIterator.hasNext(); )
{
final Element element = visualReportElementIterator.next();
final CachedLayoutData data = ModelUtility.getCachedLayoutData(element);
rect2.setRect(data.getX(), data.getY(), data.getWidth(), data.getHeight());
if (StrictBounds.intersects(rect1, rect2) == false)
{
selectionModel.remove(element);
visualReportElementIterator.remove();
}
}