Announcement

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

    MenuItem setCheckIfCondition

    I'm wondering about the setCheckIfCondition() method of the MenuItem class, I just created a menu, and want it's item to automatically select one item based on some external condition (the callback)

    here's the code (this method is from a class extending Menu) :

    Code:
    public void addMenuItem(String title, final Tab tab) {
       MenuItem menuItem = new MenuItem(title);
       menuItem.setCheckIfCondition(new MenuItemIfFunction() {
          @Override
          public boolean execute(Canvas target, Menu menu, MenuItem item) {
             Window.alert(item.getTitle());
             return GridTabs.getInstance().getTabSet().getSelectedTab().equals(tab);
          }
       });
    
       menuItem.addClickHandler(new ClickHandler() {
          @Override
          public void onClick(MenuItemClickEvent event) {
             GridTabs.getInstance().getTabSet().selectTab(tab);
             Application.getInstance().doSessionCheck();
          }
       });
            
       addItem(menuItem);
    }
    the problem is that the callback never gets called. Perhaps I'm missing something here?

    #2
    the problem has not yet been resolved. I'm I doing something wrong?

    Comment


      #3
      Not following..which callback never gets called?

      Comment


        #4
        The method setCheckIfCondition is never called.

        Comment


          #5
          Well whenever you open the menu so that the item is visible it should be called. Do you get anything in the developer console? Nothing shows up if you log something inside the function? Set it to return true explicitly and see if the menu item is checked, that will tell you it is getting called and something with your code instead.

          Comment


            #6
            Do I need some kind of special option when compiling SmartGWT? I don't have the file %MODULENAME%/sc/system/helpers/Log.html

            Comment


              #7
              Please read the FAQ. You need to inherit com.smartgwt.tools.SmartGwtTools

              Comment


                #8
                Alright, so I enabled the Debug console, recompiled everything, but the function never gets called; there's no error, no feedback, nothing.

                Code:
                        MenuItem menuItem = new MenuItem(title);
                        menuItem.setCheckIfCondition(new MenuItemIfFunction() {
                        	@Override
                			public boolean execute(Canvas target, Menu menu, MenuItem item) {
                				SC.logEcho(target, "checkIf executed");
                				SC.say("checkIf executed");
                				return true;
                			}
                        });
                
                	menu.addItem(menuItem);
                When the menu opens, the submenu appears, with the menuItem inside like expected, but unchecked and no "alert". I click on it, still no alert, still not checked.

                ...how do I debug that? I mean, I don't do anything special here... I simply want the menu to be auto-checked on some condition.
                Last edited by yanick; 26 Feb 2010, 16:01.

                Comment


                  #9
                  Does this method actually work? Does anyone use it? It doesn't seem to be ever called, and I can't find any evidence of example using it.

                  Comment


                    #10
                    I use it and it works fine in my applications. Here is a standalone test for you, try going from it back to your application, it does work.

                    Note b is just a private boolean belonging to my class, just for the examples case.

                    Code:
                    public void onModuleLoad() {
                    		SC.showConsole();
                    		
                    		mainLayout = new VLayout();
                    		
                    		Menu menu = new Menu();
                    		menu.setShowShadow(true);
                    		menu.setShadowDepth(10);
                    		
                    		MenuItem subMenu = new MenuItem("SubMenu");
                    		
                    		Menu mySubMenu = new Menu();
                    		MenuItem item1 = new MenuItem("Item 1");
                    		item1.addClickHandler(new ClickHandler() {
                    			@Override
                    			public void onClick(MenuItemClickEvent event) {
                    				b = !b;
                    			}
                    		});
                    		MenuItemIfFunction ifFunc = new MenuItemIfFunction() {
                    			@Override
                    			public boolean execute(Canvas target, Menu menu, MenuItem item) {
                    				if (b)
                    					return true;
                    				return false;
                    			}
                        	};
                        	item1.setCheckIfCondition(ifFunc);
                        	mySubMenu.addItem(item1);
                    		
                        	subMenu.setSubmenu(mySubMenu);
                        	menu.addItem(subMenu);
                        	
                        	MenuButton mb = new MenuButton("Menu", menu);
                        	
                        	mainLayout.addMember(mb);
                    		mainLayout.draw();
                    	}

                    Comment


                      #11
                      I tried your example, and indeed, it works. But for some reasons, the same thing don't work with my code. Here's my method :

                      Code:
                        /**
                         * Add a new menu item to the list
                         * @param title String
                         * @param tab Tab
                         */
                        public void addMenuItem(String title, final Tab tab) {
                          MenuItem menuItem = new MenuItem(title);
                          //menuItem.setChecked(true);
                          SC.logWarn("AddMenuItem " + title);
                          menuItem.setCheckIfCondition(new MenuItemIfFunction() {
                            @Override
                            public boolean execute(Canvas target, Menu menu, MenuItem item) {
                              SC.logWarn("check if for " + item.getTitle());
                              return true; //GridTabs.getInstance().getTabSet().getSelectedTab().equals(tab);
                            }
                          });
                          menuItem.addClickHandler(new ClickHandler() {
                            @Override
                            public void onClick(MenuItemClickEvent event) {
                              GridTabs.getInstance().getTabSet().selectTab(tab);
                              Application.getInstance().doSessionCheck();
                            }
                          });
                              
                          itemTabs.put(tab, menuItem);
                              
                          addItem(menuItem);
                        }
                      This is a method from a class extending Menu. I did not remove any line so you can see the integral thing. The method get's called whenever the user creates a new tab; this method creates a new menu item and associate it with the tab via an HashMap for later reference (when the tab is closed, etc.)

                      The log shows "AddMenuItem ..." but never shows "check if for ..." at all. I also tried your example using the check if function directly on the subMenu instance, but your example still works, so mine should too! Is it because I add menu items AFTER the menu has been added to the layout? Is there some kind of method to call for the Menu to "revalidate" items or something?

                      Comment


                        #12
                        To your example, here's what I did to show you how it does not work

                        Code:
                        		SC.showConsole();
                        		
                        		VLayout mainLayout = new VLayout();
                        		
                        		final Menu menu = new Menu();
                        		menu.setShowShadow(true);
                        		menu.setShadowDepth(10);
                        		
                        		MenuItem subMenu = new MenuItem("SubMenu");
                        		
                        //		Menu mySubMenu = new Menu();
                        //		MenuItem item1 = new MenuItem("Item 1");
                        //		item1.addClickHandler(new ClickHandler() {
                        		subMenu.addClickHandler(new ClickHandler() {
                        			@Override
                        			public void onClick(MenuItemClickEvent event) {
                        				b = !b;
                        			}
                        		});
                        		MenuItemIfFunction ifFunc = new MenuItemIfFunction() {
                        			@Override
                        			public boolean execute(Canvas target, Menu menu, MenuItem item) {
                        				if (b)
                        					return true;
                        				return false;
                        			}
                            	};
                            	//item1.setCheckIfCondition(ifFunc);
                            	//mySubMenu.addItem(item1);
                            	subMenu.setCheckIfCondition(ifFunc);
                        		
                            	//subMenu.setSubmenu(mySubMenu);
                            	menu.addItem(subMenu);
                            	
                            	MenuButton mb = new MenuButton("Menu", menu);
                            	
                            	mainLayout.addMember(mb);
                        		mainLayout.draw();
                        		
                        		
                        		new Timer() {
                        			@Override
                        			public void run() {
                        				MenuItem subMenu = new MenuItem("SubMenu");
                        				subMenu.addClickHandler(new ClickHandler() {
                        					@Override
                        					public void onClick(MenuItemClickEvent event) {
                        						b = !b;
                        					}
                        				});
                        		    	subMenu.setCheckIfCondition(new MenuItemIfFunction() {
                        					@Override
                        					public boolean execute(Canvas target, Menu menu, MenuItem item) {
                        						SC.logWarn("New item check if");
                        						if (b)
                        							return false;
                        						return true;
                        					}
                        		    	});
                        
                        		    	menu.addItem(subMenu);
                        			}
                        		}.schedule(3000);
                        Note that I placed the checkIfFunction in the subMenu.

                        The b boolean value is still a private class member. The click handler of the newly delayed menu item works, but not the checkIfFunction.

                        The Timer simulates a new MenuItem created by user interaction later on in the application life cycle (since that menu needs to be dynamic)

                        Comment


                          #13
                          So helps to mention things like your creating a derived class, that can make a difference plus the fact your doing this on the fly. Let me try to reproduce your situation, it should still work.

                          Comment


                            #14
                            Just a heads up that we are aware of an issue with this API and are looking into it. However feel free to create a simplifies testcase and create an issue in tracker. Extra credit if you can troubleshoot the issue and provide a patch! The underlying API in SmartClient is tested and works.

                            Comment


                              #15
                              A test case if provided three posts back on this thread (http://forums.smartclient.com/member.php?u=34348) I will attempt to debug it, but you can take that test case and you can see for yourselves that the checkIfFunction is never get called on the menu item added by the Timer. The fact that I'm using a derived class from Menu does not change a thing in this situation as I do not override any parent method.

                              As I said, it does work for menu items added BEFORE adding the Menu to the layout, but as soon as the menu is drawn, the method setChecked(boolean) and the checkIfFunction does not work.

                              Comment

                              Working...
                              X