Monday, April 28, 2014

Re: menus with UIBinder

I would have done it like this:

@UiField MenuItem itm_Login;

private void init ()
{
 setCommand (itm_Login,"cmd_Login"); // one line per item
 ... 
}

private void setCommand (MenuItem itm,String cmd)
{
 final Method m = this.class.getMethod (cmd,null);

  itm.setScheduledCommand
  (
   new ScheduledCommand ()
   {
    public void execute ()
    {
     m.invoke ...
    }
   }
  );
}

Aren't there similar methods to do this?

You could use a reflection library but that will probably generate quite some code you don't need or you could annotate either your fields or your command methods with a custom annotation and write a GWT generator (or annotation processor) to generate the code for you. That would be similar to how @UiHandler binds event handlers to your @UiFields via naming conventions. However in both cases you loose compile time error checking.

Personally I would live with it until GWT understands Java8 lambda's and method references which would eliminate most anonymous classes in GWT sooner or later. To make the init() method look nicer I would think about writing it as:


void init() {
  itm_Login.setScheduledCommand(executeLoginCmd());
}

private ScheduledCommand executeLoginCmd() {
  return new ScheduledCommand() {
    public void execute() {
      // code here
    }
  }
}


Well ok personally I would never use MenuBar + MenuItem because I like to have real hyperlinks so I can open things in tabs. I would have used something like FlowPanel + PopupPanel to build the menu and Hyperlink + GWT Places to build the menu items.


-- J.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment