@Override
public void buttonPressed(Button button) {
if (listViewPopup.isOpen()) {
listViewPopup.close();
} else {
ListButton listButton = (ListButton)button;
if (listButton.getListData().getLength() > 0) {
// Determine the popup's location and preferred size, relative
// to the button
Display display = listButton.getDisplay();
if (display != null) {
int width = getWidth();
int height = getHeight();
Component content = listViewPopup.getContent();
// Ensure that the popup remains within the bounds of the display
Point buttonLocation = listButton.mapPointToAncestor(display, 0, 0);
Dimensions displaySize = display.getSize();
Dimensions popupSize = content.getPreferredSize();
int x = buttonLocation.x;
if (popupSize.width > width
&& x + popupSize.width > displaySize.width) {
x = buttonLocation.x + width - popupSize.width;
}
int y = buttonLocation.y + height - 1;
if (y + popupSize.height > displaySize.height) {
if (buttonLocation.y - popupSize.height > 0) {
y = buttonLocation.y - popupSize.height + 1;
} else {
popupSize.height = displaySize.height - y;
}
} else {
popupSize.height = -1;
}
listViewPopup.setLocation(x, y);
listViewPopup.setPreferredSize(popupSize);
listViewPopup.open(listButton.getWindow());
if (listView.getFirstSelectedIndex() == -1
&& listView.getListData().getLength() > 0) {
listView.setSelectedIndex(0);
}