cancel
Showing results for 
Search instead for 
Did you mean: 

Adding submenus dynamically

Former Member
0 Kudos

Dear all,

Can I bind RFC for menu? In my project, menu is having six levels of sub menus.Can I add submenus dynamically, like reading any property file?

Thanks

Rajani

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Following is the sample code to create menu and menu items dynamically


 IWDMenu menu = (IWDMenu)view.createElement(IWDMenu.class);
	  IWDMenuItem menuItem = (IWDMenu)view.createElement(IWDMenuItem.class);
	  menu.addItem(menuItem);
	  IWDMenuActionItem menuActionItem = (IWDMenuActionItem) view.createElement(IWDMenuActionItem.class);
	  menu.addItem(menuActionItem);

Regards

Ayyapparaj

Answers (1)

Answers (1)

Former Member
0 Kudos

Which release are you using and where do you want to attach the menu?

Armin

Former Member
0 Kudos

Thanks Ayyapparaj and Armin

I am using 7.0.14 release.

Like we are having "All Programs" in Start. Upon clicking the All Programs, it shows one level of options.Again in that Games and Accessories are submenus right.

Thanks

Rajani

Former Member
0 Kudos

Still unclear. Where are these menus, in a MenuBar? When exactly do you want to change the menu content, after executing some action?

If you want to add a submenu after an action has been triggered, you can do this as follows:

Add a boolean context attribute "updateMenu" to the view context. Set this to true in the action handler.

In method wdDoModifyView(), write something like


wdDoModifyView(...)
{
  if ( wdContext.currentContextElement().getUpdateMenu() )
  {
    /* reset flag */
    wdContext.currentContextElement().setUpdateMenu(false);
 
    /* access parent menu and submenu, create if not yet existing */
    IWDMenu parentMenu = (IWDMenu) view.getElement("ID_of_parent_menu");
    IWDMenu menu = (IWDMenu) view.getElement("ID_of_submenu");
    if (menu == null)
    {
      menu = (IWDMenu) view.createElement(IWDMenu.class, "ID_of_submenu");
      menu.setTitle("Title of Submenu");
      parentMenu.addItem(menu /*, index*/);  
    }
    else
    {
      /* here you can clear the previous menu items if needed: */
      menu.destroyAllItems();
    }
    /* Just an example, real data could come from model or wherever */
    String[][] itemDefs = { {"Command1", "Text1"}, {"Command2", "Text2"}  };
    for (int i = 0; i < itemDefs.length; ++i)
    {
      String[] itemDef = itemDefs<i>;
      IWDMenuActionItem item = (IWDMenuActionItem) view.createElement(IWDMenuActionItem.class, null);
      menu.addItem(item);
      item.setText(itemDef[1]);
      item.setOnAction( wdThis.wdGet<CommonActionForAllItems>Action() );
      /* use a constant event parameter to determine in the action handler which menu item was selected */
      item.mappingOfOnAction().setString("command", itemDef[0]);
  }
}

/** Action handler for common action. Add parameter "command" to action! */
void onAction<CommonActionForAllItems>(..., String command)
{
  if ("Command1".equals(command))
  {
    /* menu item corresponding to command1 has been selected */
  }
  /* etc. */
}

Armin