final Object insertResult = InsertationUtil.insert(rawLeadSelection, report, o);
selectedElements[i] = insertResult;
if (insertResult instanceof Element)
{
final Element insertElement = (Element) insertResult;
final Section parent = insertElement.getParentSection();
if (parent == null)
{
throw new IllegalStateException("A newly inserted section must have a parent."); // NON-NLS
}
final int position = ModelUtility.findIndexOf(parent, insertElement);
if (position == -1)
{
if (insertElement instanceof SubReport && parent instanceof RootLevelBand)
{
final SubReport subReport = (SubReport) insertElement;
final RootLevelBand arb = (RootLevelBand) parent;
final int subreportPosition = ModelUtility.findSubreportIndexOf(arb, subReport);
if (subreportPosition == -1)
{
throw new IllegalStateException("A newly inserted section must have a position within its parent.");
}
else
{
undos.add(new BandedSubreportEditUndoEntry(parent.getObjectID(), arb.getSubReportCount(), null, subReport));
}
}
else
{
throw new IllegalStateException("A newly inserted section must have a position within its parent.");
}
}
else
{
undos.add(new ElementEditUndoEntry(parent.getObjectID(), position, null, insertElement));
}
}
else if (insertResult instanceof Expression)
{
final Expression insertExpression = (Expression) insertResult;