private static final int PADDING = 10;
@Override
public Location determinePlacement(final Workspace workspace, final View relativeToView, final View newView) {
if (relativeToView == null) {
return new Location();
}
final Size workspaceSize = workspace.getSize();
final View rootView = rootView(workspace, relativeToView);
final Location rootViewLocation = rootView.getLocation();
final Size rootViewSize = rootView.getSize();
final Location newLocation = new Location(rootViewLocation);
final Size requiredSize = newView.getView().getRequiredSize(Size.createMax());
if (rootViewLocation.getX() + rootViewSize.getWidth() + PADDING + requiredSize.getWidth() < workspaceSize.getWidth()) {
newLocation.add(rootViewSize.getWidth() + PADDING, 0);
} else if (rootViewLocation.getY() + rootViewSize.getHeight() + PADDING + requiredSize.getHeight() < workspaceSize.getHeight()) {
newLocation.add(0, rootViewSize.getHeight() + PADDING);
} else if (requiredSize.getWidth() + PADDING < rootViewLocation.getX()) {
newLocation.subtract(requiredSize.getWidth() + PADDING, 0);
} else if (requiredSize.getHeight() + PADDING < rootViewLocation.getY()) {
newLocation.subtract(0, requiredSize.getHeight() + PADDING);
} else {
newLocation.add(PADDING * 6, PADDING * 6);
}
final int maxSpaceToLeft = workspaceSize.getWidth() - requiredSize.getWidth();
final int maxSpaceAbove = workspaceSize.getHeight() - requiredSize.getHeight();
ensureWidth(newLocation, maxSpaceToLeft);
ensureHeight(newLocation, maxSpaceAbove);
final Location firstAttempt = new Location(newLocation);
while (workspace.subviewFor(newLocation) != null && workspace.subviewFor(newLocation).getLocation().equals(newLocation)) {
newLocation.add(PADDING * 4, PADDING * 4);
ensureWidth(newLocation, maxSpaceToLeft);
ensureHeight(newLocation, maxSpaceAbove);