Dynamic GWT menu from XML file. How to reflect a composite class?

I am trying to create a dynamic menu in GWT by reading it from an XML file. The XML file must have a button name and an action (associated with the composite to be added to the horizontal panel).

To make an action, I need to make a reflection of the class, the desire gave me many problems. I tried 2 different solutions, client and server. On the client side, I tried the "gwt-ent" and "gwt reflection" libraries, but I got a lot of errors and I needed to establish which classes would be displayed (which I don't want because I want a fully dynamic menu, not a semi-dynamic one). On the server side, I tried to return the menu, but on the server side it is not possible to deal with widgets on the client side. Therefore, I tried to make a reflection and return the instance to the client, but on the server side it cannot be obtained on the client side.

Does anyone know a different solution? Am I doing something wrong? How can I reflect a class for placing a composite on a horizontal panel?

Thansk for your help. Regards.

+3
source share
1 answer

One approach is for your server-side code to create an instance of a "factory" that will create the appropriate client-side widgets (s). This "factory" is then serialized to the client (now its DTO). Something like that:

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 server-side reflection to instantiate your WidgetFactory.

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

This is the approach used by the GWT Portlets framework .

+1
source

Source: https://habr.com/ru/post/1709702/


All Articles