All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.gwt.test.uibinder.DefaultUiWidgetTagFactory Maven / Gradle / Ivy

There is a newer version: 0.63
Show newest version
package com.googlecode.gwt.test.uibinder;

import com.googlecode.gwt.test.internal.GwtConfig;
import com.googlecode.gwt.test.uibinder.widget.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Default UiWidgetTagFactory, which try to delegate {@link UiObjectTag} instanciation to
 * UiWidgetTagFactories added by users before using those implemented in gwt-test-utils.
 *
 * @author Gael Lazzari
 */
class DefaultUiWidgetTagFactory implements UiObjectTagFactory {

    private static final DefaultUiWidgetTagFactory INSTANCE = new DefaultUiWidgetTagFactory();

    public static DefaultUiWidgetTagFactory get() {
        return INSTANCE;
    }

    private final List> gwtTestUtilsFactories;

    private DefaultUiWidgetTagFactory() {
        gwtTestUtilsFactories = new ArrayList>();

        gwtTestUtilsFactories.add(new UiHTMLPanelTagFactory());
        gwtTestUtilsFactories.add(new UiCellPanelTagFactory());
        gwtTestUtilsFactories.add(new UiGridTagFactory());
        gwtTestUtilsFactories.add(new UiListBoxTagFactory());
        gwtTestUtilsFactories.add(new UiDateLabelTagFactory());
        gwtTestUtilsFactories.add(new UiDockLayoutPanelTagFactory());
        gwtTestUtilsFactories.add(new UiImageTagFactory());
        gwtTestUtilsFactories.add(new UiLayoutPanelTagFactory());
        gwtTestUtilsFactories.add(new UiMenuBarTagFactory());
        gwtTestUtilsFactories.add(new UiMenuItemTagFactory());
        gwtTestUtilsFactories.add(new UiStackLayoutPanelTagFactory());
        gwtTestUtilsFactories.add(new UiTabLayoutPanelTagFactory());
        gwtTestUtilsFactories.add(new UiDisclosurePanelTagFactory());
    }

    /*
     * (non-Javadoc)
     *
     * @see com.googlecode.gwt.test.uibinder.UiObjectTagFactory#createUiObjectTag(java .lang .Class,
     * java.util.Map)
     */
    public UiObjectTag createUiObjectTag(Class clazz, Map attributes) {

        // try with user's custom UiObjectTagFactories
        UiObjectTag result = tryInstanciateUiObjectTag(clazz, attributes,
                GwtConfig.get().getUiObjectTagFactories());

        if (result != null) {
            return result;
        }

        // try with gwt-test-utils custom UiObjectTagFactories
        result = tryInstanciateUiObjectTag(clazz, attributes, gwtTestUtilsFactories);
        if (result != null) {
            return result;
        }

        // default
        return new UiObjectTag() {

            @Override
            protected void finalizeObject(Object widget) {
                // nothing to do
            }

            @Override
            protected void initializeObject(Object wrapped, Map attributes,
                                            Object owner) {
                // nothing to do

            }

        };
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    private UiObjectTag tryInstanciateUiObjectTag(Class clazz,
                                                          Map attributes, List> uiObjectTagFactories) {
        for (UiObjectTagFactory factory : uiObjectTagFactories) {

            UiObjectTag result = factory.createUiObjectTag(clazz, attributes);
            if (result != null) {
                return result;
            }
        }

        return null;
    }

}