if (w instanceof SeparatorToolItem) {
menu.add(new SeparatorMenuItem());
} else if (w instanceof SplitButton) {
final SplitButton sb = (SplitButton) w;
MenuItem item = new MenuItem(sb.getText(), sb.getIcon());
item.setEnabled(sb.isEnabled());
item.setItemId(sb.getItemId());
if (sb.getData("gxt-menutext") != null) {
item.setText(sb.getData("gxt-menutext").toString());
}
if (sb.getMenu() != null) {
item.setSubMenu(sb.getMenu());
}
item.addSelectionHandler(new SelectionHandler<Item>() {
@Override
public void onSelection(SelectionEvent<Item> event) {
sb.fireEvent(new SelectEvent());
}
});
menu.add(item);
} else if (w instanceof TextButton) {
final TextButton b = (TextButton) w;
MenuItem item = new MenuItem(b.getText(), b.getIcon());
item.setItemId(b.getItemId());
if (b.getData("gxt-menutext") != null) {
item.setText(b.getData("gxt-menutext").toString());
}
if (b.getMenu() != null) {
item.setHideOnClick(false);
item.setSubMenu(b.getMenu());
}
item.setEnabled(b.isEnabled());
item.addSelectionHandler(new SelectionHandler<Item>() {
@Override
public void onSelection(SelectionEvent<Item> event) {
b.fireEvent(new SelectEvent());
}
});
menu.add(item);
} else if (w instanceof ButtonGroup) {
ButtonGroup g = (ButtonGroup) w;
g.setItemId(g.getItemId());
menu.add(new SeparatorMenuItem());
String heading = g.getHeadingText();
if (heading != null && heading.length() > 0 && !heading.equals(" ")) {
menu.add(new HeaderMenuItem(heading));
}
Widget con = g.getWidget();
if (con != null && con instanceof HasWidgets) {
HasWidgets widgets = (HasWidgets) con;
Iterator<Widget> it = widgets.iterator();
while (it.hasNext()) {
addWidgetToMenu(menu, it.next());
}
}
menu.add(new SeparatorMenuItem());
} else if (w instanceof ToggleButton) {
final ToggleButton b = (ToggleButton) w;
final CheckMenuItem item = new CheckMenuItem(b.getText());
item.setItemId(b.getItemId());
item.setChecked(b.getValue());
if (b.getData("gxt-menutext") != null) {
item.setText(b.getData("gxt-menutext").toString());
}
item.setEnabled(b.isEnabled());
item.addCheckChangeHandler(new CheckChangeHandler<CheckMenuItem>() {
@Override
public void onCheckChange(CheckChangeEvent<CheckMenuItem> event) {
// must pass true to cause value change event to fire
b.setValue(event.getItem().isChecked(), true);