int gap = l.getGap();
int stateIconSize = 0;
int stateIconYPosition = 0;
String text = l.getText();
Style style = l.getStyle();
Font font = style.getFont();
int fontHeight = 0;
if (text == null) {
text = "";
}
if(text.length() > 0){
fontHeight = font.getHeight();
}
if (stateIcon != null) {
stateIconSize = stateIcon.getWidth(); //square image width == height
stateIconYPosition = l.getY() + l.getStyle().getPadding(Component.TOP) + (l.getHeight() - l.getStyle().getPadding(Component.TOP) - l.getStyle().getPadding(Component.BOTTOM)) / 2 - stateIconSize / 2;
preserveSpaceForState = stateIconSize + gap;
g.drawImage(stateIcon, l.getX() + l.getStyle().getPadding(Component.LEFT), stateIconYPosition);
}
//default for bottom left alignment
int x = l.getX() + l.getStyle().getPadding(Component.LEFT) + preserveSpaceForState;
int y = l.getY() + l.getStyle().getPadding(Component.TOP);
//set initial x,y position according to the alignment and textPosition
if (l.getAlignment() == Component.LEFT) {
switch (l.getTextPosition()) {
case Label.LEFT:
case Label.RIGHT:
y = y + (l.getHeight() - (l.getStyle().getPadding(Component.TOP) + l.getStyle().getPadding(Component.BOTTOM) + Math.max(((icon != null) ? icon.getHeight() : 0), l.getStyle().getFont().getHeight()))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
y = y + (l.getHeight() - (l.getStyle().getPadding(Component.TOP) + l.getStyle().getPadding(Component.BOTTOM) + ((icon != null) ? icon.getHeight() + gap : 0) + l.getStyle().getFont().getHeight())) / 2;
break;
}
} else if (l.getAlignment() == Component.CENTER) {
switch (l.getTextPosition()) {
case Label.LEFT:
case Label.RIGHT:
x = x + (l.getWidth() - (preserveSpaceForState +
style.getPadding(Component.LEFT) +
style.getPadding(Component.RIGHT) +
((icon != null) ? icon.getWidth() : 0) +
font.stringWidth(text))) / 2;
x = Math.max(x, l.getX() + style.getPadding(Component.LEFT) + preserveSpaceForState);
y = y + (l.getHeight() - (style.getPadding(Component.TOP) +
style.getPadding(Component.BOTTOM) +
Math.max(((icon != null) ? icon.getHeight() : 0),
fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
x = x + (l.getWidth() - (preserveSpaceForState + style.getPadding(Component.LEFT) +
style.getPadding(Component.RIGHT) +
Math.max(((icon != null) ? icon.getWidth() : 0),
font.stringWidth(text)))) / 2;
x = Math.max(x, l.getX() + style.getPadding(Component.LEFT) + preserveSpaceForState);
y = y + (l.getHeight() - (style.getPadding(Component.TOP) +
style.getPadding(Component.BOTTOM) +
((icon != null) ? icon.getHeight() + gap : 0) +
fontHeight)) / 2;
break;
}
} else if (l.getAlignment() == Component.RIGHT) {
switch (l.getTextPosition()) {
case Label.LEFT:
case Label.RIGHT:
x = l.getX() + l.getWidth() - style.getPadding(Component.RIGHT) -
(preserveSpaceForState + ((icon != null) ? (icon.getWidth() + gap) : 0) +
font.stringWidth(text));
x = Math.max(x, l.getX() + style.getPadding(Component.LEFT) + preserveSpaceForState);
y = y + (l.getHeight() - (style.getPadding(Component.TOP) +
style.getPadding(Component.BOTTOM) +
Math.max(((icon != null) ? icon.getHeight() : 0),
fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
x = l.getX() + l.getWidth() - style.getPadding(Component.RIGHT) -
preserveSpaceForState - (Math.max(((icon != null) ? (icon.getWidth()) : 0),
font.stringWidth(text)));
x = Math.max(x, l.getX() + style.getPadding(Component.LEFT) + preserveSpaceForState);
y = y + (l.getHeight() - (style.getPadding(Component.TOP) +
style.getPadding(Component.BOTTOM) +
((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
break;