views:

931

answers:

1

Hello Everyone, I'm trying to make an Dynamic Menu in GWT, reading it from an XML file. The XML file must have the button name and the action (the composite associated that will be added to an Horizontal Panel).

To make the action, a need to do Reflection of the Class, wish is given me a lot of problems. I've tried 2 different solution, Client Side and Server Side. On Client Side I've tried "gwt-ent" and "gwt reflection" libraries, but I've got a lot of errors and its necessary to set what Classes will be reflected (which I don't want because I want a completely dynamic Menu, and not semi-dynamic). On Server Side, I've tried to return the menu but its not possible to deal with client side widgets on server side. So I tried to make the reflection and return the instance to the client, but on server side its no possible to get an client side Class.

Anyone knows other solution? I'm doing something wrong? How can I reflect the class to put the Composite on the Horizontal Panel?

Thansk for your help. Regards.

+1  A: 

One approach is to have your server side code create a "factory" instance that will create the appropriate Widget(s) on the client side. This "factory" is then serialized to the client (now its a DTO). Something like this:

public interface WidgetFactory {
    public Widget createWidget();
}

public class MenuOptionDTO implements Serializable {
    public String optionText;
    public WidgetFactory widgetFactory;
}

public class WidgetOnMenu extends Composite {
    ...
    public static class Factory implements WidgetFactory, Serializable {
        public Widget createWidget() {
            return new WidgetOnMenu();
        }
    }
    ...
}

You can use normal reflection on the server side to create instances of your WidgetFactory's.

<menu>
    <option text="Option1" factory="com.acme.WidgetOnMenu.Factory"/>
</menu>

This is the approach used by the GWT Portlets framework.

David Tinker