public void setBoundsInternal(Envelope newBounds, boolean forceContainBBoxZoom) {
Envelope finalBounds = newBounds;
if (getDefaultPreferredScaleDenominators() != getPreferredScaleDenominators()
&& validState()) {
IMapDisplay mapDisplay = getRenderManagerInternal().getMapDisplay();
ReferencedEnvelope referenced;
if (newBounds instanceof ReferencedEnvelope) {
referenced = (ReferencedEnvelope) newBounds;
} else {
referenced = new ReferencedEnvelope(newBounds, getCRS());
}
double scale = ScaleUtils.calculateScaleDenominator(referenced,
mapDisplay.getDisplaySize(), mapDisplay.getDPI());
scale = ScaleUtils.calculateClosestScale(getPreferredScaleDenominators(), scale,
ScaleUtils.zoomClosenessPreference());
finalBounds = ScaleUtils.calculateBoundsFromScale(scale, mapDisplay.getDisplaySize(),
mapDisplay.getDPI(), referenced);
if (forceContainBBoxZoom && !finalBounds.contains(newBounds)) {
Iterator<Double> tail = getPreferredScaleDenominators().tailSet(scale).iterator();
// the tail will include scale because scale is one of the elements in the set. So drop that
tail.next();
Double nextLargest = tail.next();
if (nextLargest != null) {
finalBounds = ScaleUtils.calculateBoundsFromScale(nextLargest,
mapDisplay.getDisplaySize(), mapDisplay.getDPI(), referenced);
}
}
}
Envelope oldBounds = bounds == null ? new Envelope() : bounds;