});
}
@Override
public void onResize() {
LayoutPanel parent = (LayoutPanel) getParent();
// Compute width and height available for inside area
int parentWidth = parent.getOffsetWidth();
int parentHeight = parent.getOffsetHeight();
int availableWidth = parentWidth - 2 * border;
int availableHeight = parentHeight - 2 * border;
if (availableWidth > 0 && availableHeight > 0) {
float parentRatio = (float) availableWidth / (float) availableHeight;
if (parentRatio < aspectRatio) {
// availableWidth/availableHeight < desiredWidth/desiredHeight
// Set desiredWidth = availableWidth ==> availableHeight > desiredHeight (good!)
// Then desiredHeight = availableWidth / aspectRatio
int desiredHeight = (int) (availableWidth / aspectRatio);
parent.setWidgetLeftRight(this, 0, Unit.PX, 0, Unit.PX);
parent.setWidgetTopHeight(this, (parentHeight - desiredHeight) / 2 - border, Unit.PX,
desiredHeight + 2 * border, Unit.PX);
} else {
// availableWidth/availableHeight > desiredWidth/desiredHeight
// Set desiredHeight = availableHeight ==> availableWidth > desiredWidth (good!)
// Then desiredWidth = availableHeight * aspectRatio
int desiredWidth = (int) (availableHeight * aspectRatio);
parent.setWidgetTopBottom(this, 0, Unit.PX, 0, Unit.PX);
parent.setWidgetLeftWidth(this, (parentWidth - desiredWidth) / 2 - border, Unit.PX,
desiredWidth + 2 * border, Unit.PX);
}
} else {
// Not enough room for inside area, border takes up everything.
if (parentWidth < parentHeight) {
// parentWidth is smaller, allocate parentWidth x parentWidth
parent.setWidgetLeftRight(this, 0, Unit.PX, 0, Unit.PX);
parent.setWidgetTopHeight(this, (parentHeight - parentWidth) / 2,
Unit.PX, parentWidth, Unit.PX);
} else {
// parentHeight is smaller, allocate parentHeight x parentHeight
parent.setWidgetTopBottom(this, 0, Unit.PX, 0, Unit.PX);
parent.setWidgetLeftWidth(this, (parentWidth - parentHeight) / 2,
Unit.PX, parentHeight, Unit.PX);
}
}
super.onResize();
}