Menu columnsMenu = new Menu(true);
for (int i = 0; i < columns.length; i++) {
final int index = i;
Column column = columns[i];
if (column.supports(Column.Feature.HIDABLE)) {
final CheckMenuItem item = new CheckMenuItem(column.getName(), column.isShownByDefault());
item.setCommand(new Command() {
public void execute() {
setColumnVisible(index, item.isSelected());
popup.hide();
}
});
columnsMenu.addItem(item);
}
}
popup = new MenuPopup(true);
popup.getMenu().addItem("sortAsc", "Sort Asc", new Command() {
public void execute() {
int columnIndex = menuPopupContext.getColumnIndex();
sortByColumn(columnIndex, true);
}
}).setIconStyleName("Sort-menu-icon-asc");
popup.getMenu().addItem("sortDesc", "Sort Desc", new Command() {
public void execute() {
int columnIndex = menuPopupContext.getColumnIndex();
sortByColumn(columnIndex, false);
}
}).setIconStyleName("Sort-menu-icon-desc");
popup.addSubMenu("Columns", columnsMenu).setIconStyleName("Columns-menu-icon");
popup.setSubMenuPopupPositionCallback(new Menu.DefaultSubMenuPositionCallback() {
@Override
public void setPosition(Popup popup, int popupWidth, int popupHeight, Menu menubar, MenuItemBase item) {
int right = popupWidth + menubar.getAbsoluteLeft() + menubar.getOffsetWidth() - 1;
if (right < Window.getClientWidth()) {
super.setPosition(popup, popupWidth, popupHeight, menubar, item);
} else {
int left = menubar.getAbsoluteLeft() - popupWidth + 1;
int top = item.getAbsoluteTop();
popup.setPopupPosition(left, top);
}
}
});