Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    MenuItemStringFunction

    Hello,

    I have a menu and the following works GREAT, but only IF I use it at the top level. If I make a submenu then the MenuItemStringFunction does not fire. I was hoping to change the title on the submenus, why does this not work?

    Thanks,
    Evan

    startsSearchItem.addClickHandler(startsSearchClickHandler);
    startsSearchItem.setIcon("text_align_left.png");
    final String strTitleStartsSearch = "Prefix (starts with)";
    startsSearchItem.setTitle(strTitleStartsSearch);
    MenuItemStringFunction startsMenuItemStringFunction = new MenuItemStringFunction() {
    @Override
    public String execute(Canvas target, Menu menu, MenuItem item) {
    return getMenuItemOperatorTitle(OperatorId.ISTARTS_WITH, strTitleStartsSearch);
    }
    };
    startsSearchItem.setDynamicTitleFunction(startsMenuItemStringFunction);

    #2
    Can you confirm which SmartGWT build you're using (branch and date).
    Also the complete code that defines the menu, and applies it as a submenu would be useful. Something we can just drop into an EntryPoint class on our end and run so we can be sure there's nothing in your application that is impacting this.

    Thanks
    Isomorphic Software

    Comment


      #3
      ok

      I am using smart gwt 3.1p and gwt 2.5.1

      SmartClient Version: v8.3p_2013-04-19/PowerEdition Deployment (built 2013-04-19)

      Here is the code that puts it on the top level. When I add submenus the * does not follow. So I have left everything at the top.

      In my class I define the following instance variables
      Code:
      	// View Mode
      	final MenuItem listViewModeItem = new MenuItem();
      	final MenuItem treeViewModeItem = new MenuItem();
      And I have the following menu code
      Code:
      	private ToolStripMenuButton getToolStripMenuButton() {
      		final Menu menu = new Menu();
      		menu.setShowShadow(true);
      		menu.setShadowDepth(3);
      
      		// --------------------------------------------------------------------
      		// View Mode
      
      		listViewModeItem.setIcon("list-view.png");
      		final String strTitleListMode = "List Search Results";
      		listViewModeItem.setTitle(strTitleListMode);
      		MenuItemStringFunction listModeMenuItemStringFunction = new MenuItemStringFunction() {
      			@Override
      			public String execute(Canvas target, Menu menu, MenuItem item) {
      				return getMenuItemViewModeTitle(SearchViewModeEnumType.ENUM_LIST, strTitleListMode);
      			}
      		};
      		listViewModeItem.setDynamicTitleFunction(listModeMenuItemStringFunction);
      
      		com.smartgwt.client.widgets.menu.events.ClickHandler clickHandlerListMode = new com.smartgwt.client.widgets.menu.events.ClickHandler() {
      			@Override
      			public void onClick(MenuItemClickEvent event) {
      				viewModeSearch = SearchViewModeEnumType.ENUM_LIST;
      				myRun();
      			}
      		};
      		listViewModeItem.addClickHandler(clickHandlerListMode);
      
      		treeViewModeItem.setIcon("tree-view.png");
      		final String strTitleTreeMode = "Tree Search Results";
      		treeViewModeItem.setTitle(strTitleTreeMode);
      		MenuItemStringFunction treeModeMenuItemStringFunction = new MenuItemStringFunction() {
      			@Override
      			public String execute(Canvas target, Menu menu, MenuItem item) {
      				return getMenuItemViewModeTitle(SearchViewModeEnumType.ENUM_TREE, strTitleTreeMode);
      			}
      		};
      		treeViewModeItem.setDynamicTitleFunction(treeModeMenuItemStringFunction);
      		com.smartgwt.client.widgets.menu.events.ClickHandler clickHandlerTreeMode = new com.smartgwt.client.widgets.menu.events.ClickHandler() {
      			@Override
      			public void onClick(MenuItemClickEvent event) {
      				viewModeSearch = SearchViewModeEnumType.ENUM_TREE;
      				myRun();
      			}
      		};
      		treeViewModeItem.addClickHandler(clickHandlerTreeMode);
      
      		// we only have "data" for trees for disease and drugs, all others disable options for trees
      		MenuItemIfFunction menuItemIfFunction = new MenuItemIfFunction() {
      			@Override
      			public boolean execute(Canvas target, Menu menu, MenuItem item) {
      				return updateViewSearchMode();
      			}
      		};
      		// see what results returned before doing this ...
      		listViewModeItem.setEnableIfCondition(menuItemIfFunction);
      		treeViewModeItem.setEnableIfCondition(menuItemIfFunction);
      
      		// ------------------------------------------------------------------
      		// Search method
      		// TODO: try soundex ...
      
      		MenuItemSeparator separator = new MenuItemSeparator();
      
      		MenuItem searchByItem = new MenuItem();
      		Menu submenuSearchBy = new Menu();
      		final MenuItem containsSearchItem = new MenuItem();
      		final MenuItem startsSearchItem = new MenuItem();
      		submenuSearchBy.setItems(startsSearchItem, containsSearchItem);
      		searchByItem.setSubmenu(submenuSearchBy);
      		searchByItem.setTitle("Search by ...");
      
      		com.smartgwt.client.widgets.menu.events.ClickHandler startsSearchClickHandler = new com.smartgwt.client.widgets.menu.events.ClickHandler() {
      			@Override
      			public void onClick(MenuItemClickEvent event) {
      				handleMenuItemOperatorClick(OperatorId.ISTARTS_WITH);
      			}
      		};
      		startsSearchItem.addClickHandler(startsSearchClickHandler);
      		startsSearchItem.setIcon("text_align_left.png");
      		final String strTitleStartsSearch = "Search Prefix (starts with)";
      		startsSearchItem.setTitle(strTitleStartsSearch);
      		MenuItemStringFunction startsMenuItemStringFunction = new MenuItemStringFunction() {
      			@Override
      			public String execute(Canvas target, Menu menu, MenuItem item) {
      				return getMenuItemOperatorTitle(OperatorId.ISTARTS_WITH, strTitleStartsSearch);
      			}
      		};
      		startsSearchItem.setDynamicTitleFunction(startsMenuItemStringFunction);
      
      		com.smartgwt.client.widgets.menu.events.ClickHandler containsSearchClickHandler = new com.smartgwt.client.widgets.menu.events.ClickHandler() {
      			@Override
      			public void onClick(MenuItemClickEvent event) {
      				handleMenuItemOperatorClick(OperatorId.ICONTAINS);
      			}
      		};
      		containsSearchItem.addClickHandler(containsSearchClickHandler);
      		containsSearchItem.setIcon("text_align_center.png");
      		final String strTitleContainsSearch = "Search Contains (substring)";
      		containsSearchItem.setTitle(strTitleContainsSearch);
      		MenuItemStringFunction containsMenuItemStringFunction = new MenuItemStringFunction() {
      			@Override
      			public String execute(Canvas target, Menu menu, MenuItem item) {
      				return getMenuItemOperatorTitle(OperatorId.ICONTAINS, strTitleContainsSearch);
      			}
      		};
      		containsSearchItem.setDynamicTitleFunction(containsMenuItemStringFunction);
      
      		// -----------------------------------------------------------------------------
      		// Create menu
      		// SubMenus' don't support changing title so put at top level
      		menu.setItems(startsSearchItem, containsSearchItem, separator, listViewModeItem, treeViewModeItem);
      
      		String strSettingsTitle = AppUtils.getLinkHTML("Settings");
      		ToolStripMenuButton menuButton = new ToolStripMenuButton(strSettingsTitle, menu);
      		menuButton.setWidth(50);
      		return menuButton;
      	}
      
      
      
      	private String getMenuItemViewModeTitle(SearchViewModeEnumType eType, String strTitle) {
      		if (eType == viewModeSearch)
      			return strTitle + " *";
      		else
      			return strTitle;
      	}
      
      	private String getMenuItemOperatorTitle(OperatorId op, String strTitle) {
      		if (currentOperatorId == op)
      			return strTitle + " *";
      		else
      			return strTitle;
      	}
      
      	private void handleMenuItemOperatorClick(OperatorId op) {
      		if (op != currentOperatorId) {
      			currentOperatorId = op;
      			myRun();
      		}
      	}
      Last edited by Isomorphic; 6 May 2013, 08:40. Reason: Formatting code blocks

      Comment


        #4
        Thanks for the update.
        Unfortunately this doesn't give us enough to reproduce the problem. The code you've shown us here isn't runnable as is. We modified it to get rid of stuff that was unrunnable locally (references to classes we don't have etc) and tweaked it to put the dynamic menu items into a submenu, and things appear to work correctly. Here's the modified code. As you can see we've made the dynamic title functions simple string-concatinations, and if you run this test code they appear to be being executed fine. So whatever is going wrong for you is due to some unrelated code in your app (which may or may not indicate a framework bug).

        The best way for you to proceed if you're still seeing this would be to build a complete entry point class we can actually run on our end that demonstrates the problem. Try to make it as compact as possible.

        Thanks
        Isomorphic Software
        Code:
        import com.google.gwt.core.client.EntryPoint;
        import com.smartgwt.client.widgets.Canvas;
        import com.smartgwt.client.widgets.menu.Menu;
        import com.smartgwt.client.widgets.menu.MenuItem;
        import com.smartgwt.client.widgets.menu.MenuItemSeparator;
        import com.smartgwt.client.widgets.menu.MenuItemStringFunction;
        import com.smartgwt.client.widgets.toolbar.ToolStripMenuButton;
        
        public class SubMenuIssue implements EntryPoint {
        
        	@Override
        	public void onModuleLoad() {
        		getToolStripMenuButton().draw();
        		
        	}
        	
        	// View Mode
        	final MenuItem listViewModeItem = new MenuItem();
        	final MenuItem treeViewModeItem = new MenuItem();	
        	
        	
        	private ToolStripMenuButton getToolStripMenuButton() {
        		final Menu menu = new Menu();
        		menu.setShowShadow(true);
        		menu.setShadowDepth(3);
        
        		// --------------------------------------------------------------------
        		// View Mode
        
        		listViewModeItem.setIcon("list-view.png");
        		final String strTitleListMode = "List Search Results";
        		listViewModeItem.setTitle(strTitleListMode);
        		MenuItemStringFunction listModeMenuItemStringFunction = new MenuItemStringFunction() {
        			@Override
        			public String execute(Canvas target, Menu menu, MenuItem item) {
        				// Replacing stock code with simple dynamic function.
        				return "Dynamic Title " + item.getTitle();
        //				return getMenuItemViewModeTitle(SearchViewModeEnumType.ENUM_LIST, strTitleListMode);
        			}
        		};
        		listViewModeItem.setDynamicTitleFunction(listModeMenuItemStringFunction);
        
        		treeViewModeItem.setIcon("tree-view.png");
        		final String strTitleTreeMode = "Tree Search Results";
        		treeViewModeItem.setTitle(strTitleTreeMode);
        		MenuItemStringFunction treeModeMenuItemStringFunction = new MenuItemStringFunction() {
        			@Override
        			public String execute(Canvas target, Menu menu, MenuItem item) {
        				return "Custom title 2:" + item.getTitle();
        //				return getMenuItemViewModeTitle(SearchViewModeEnumType.ENUM_TREE, strTitleTreeMode);
        			}
        		};
        		treeViewModeItem.setDynamicTitleFunction(treeModeMenuItemStringFunction);
        
        
        		MenuItemSeparator separator = new MenuItemSeparator();
        
        		MenuItem searchByItem = new MenuItem();
        		Menu submenuSearchBy = new Menu();
        		final MenuItem containsSearchItem = new MenuItem();
        		final MenuItem startsSearchItem = new MenuItem();
        		submenuSearchBy.setItems(startsSearchItem, containsSearchItem);
        		searchByItem.setSubmenu(submenuSearchBy);
        		searchByItem.setTitle("Search by ...");
        
        		startsSearchItem.setIcon("text_align_left.png");
        		final String strTitleStartsSearch = "Search Prefix (starts with)";
        		startsSearchItem.setTitle(strTitleStartsSearch);
        		MenuItemStringFunction startsMenuItemStringFunction = new MenuItemStringFunction() {
        			@Override
        			public String execute(Canvas target, Menu menu, MenuItem item) {
        				return "Custom title 3:" + item.getTitle();
        //				return getMenuItemOperatorTitle(OperatorId.ISTARTS_WITH, strTitleStartsSearch);
        			}
        		};
        		startsSearchItem.setDynamicTitleFunction(startsMenuItemStringFunction);
        
        		containsSearchItem.setIcon("text_align_center.png");
        		final String strTitleContainsSearch = "Search Contains (substring)";
        		containsSearchItem.setTitle(strTitleContainsSearch);
        		MenuItemStringFunction containsMenuItemStringFunction = new MenuItemStringFunction() {
        			@Override
        			public String execute(Canvas target, Menu menu, MenuItem item) {
        				return "Custom title 4:" + item.getTitle();
        //				return getMenuItemOperatorTitle(OperatorId.ICONTAINS, strTitleContainsSearch);
        			}
        		};
        		containsSearchItem.setDynamicTitleFunction(containsMenuItemStringFunction);
        
        		// -----------------------------------------------------------------------------
        		// Create menu
        		// SubMenus' don't support changing title so put at top level
        		menu.setItems(startsSearchItem, containsSearchItem, separator, listViewModeItem, treeViewModeItem);
        		
        		// xxx Altered to put intems into a submenu
        		MenuItem parentItem = new MenuItem("Show Submenu");
        		parentItem.setSubmenu(menu);
        		Menu parentMenu = new Menu();
        		parentMenu.setItems(parentItem);
        
        		String strSettingsTitle = "Settings";//AppUtils.getLinkHTML("Settings");
        		
        		
        		
        //		ToolStripMenuButton menuButton = new ToolStripMenuButton(strSettingsTitle, menu);
        		ToolStripMenuButton menuButton = new ToolStripMenuButton(strSettingsTitle, parentMenu);
        		
        		menuButton.setWidth(50);
        		return menuButton;
        	}
        
        
        }

        Comment

        Working...
        X