Search in sources :

Example 1 with ClientConfiguration

use of com.haulmont.cuba.client.ClientConfiguration in project cuba by cuba-platform.

the class ControllerDependencyInjector method getInjectedInstance.

protected Object getInjectedInstance(Class<?> type, String name, Class annotationClass, AnnotatedElement element) {
    if (annotationClass == WindowParam.class) {
        // Injecting a parameter
        return params.get(name);
    } else if (Component.class.isAssignableFrom(type)) {
        // Injecting a UI component
        return frame.getComponent(name);
    } else if (Datasource.class.isAssignableFrom(type)) {
        // Injecting a datasource
        return frame.getDsContext().get(name);
    } else if (DsContext.class.isAssignableFrom(type)) {
        // Injecting the DsContext
        return frame.getDsContext();
    } else if (DataSupplier.class.isAssignableFrom(type)) {
        // Injecting the DataSupplier
        return frame.getDsContext().getDataSupplier();
    } else if (FrameContext.class.isAssignableFrom(type)) {
        // Injecting the FrameContext
        return frame.getContext();
    } else if (Action.class.isAssignableFrom(type)) {
        // Injecting an action
        return ComponentsHelper.findAction(name, frame);
    } else if (ExportDisplay.class.isAssignableFrom(type)) {
        // Injecting an ExportDisplay
        return AppConfig.createExportDisplay(frame);
    } else if (ThemeConstants.class.isAssignableFrom(type)) {
        // Injecting a Theme
        ThemeConstantsManager themeManager = (ThemeConstantsManager) applicationContext.getBean(ThemeConstantsManager.NAME);
        return themeManager.getConstants();
    } else if (Config.class.isAssignableFrom(type)) {
        ClientConfiguration configuration = (ClientConfiguration) applicationContext.getBean(Configuration.NAME);
        // noinspection unchecked
        return configuration.getConfigCached((Class<? extends Config>) type);
    } else if (Logger.class == type && element instanceof Field) {
        return LoggerFactory.getLogger(((Field) element).getDeclaringClass());
    } else {
        Object instance;
        // Try to find a Spring bean
        Map<String, ?> beans = applicationContext.getBeansOfType(type, true, true);
        if (!beans.isEmpty()) {
            instance = beans.get(name);
            // If a bean with required name found, return it. Otherwise return first found.
            if (instance != null) {
                return instance;
            } else {
                return beans.values().iterator().next();
            }
        }
        // There are no Spring beans of required type - the last option is Companion
        if (frame instanceof AbstractFrame) {
            instance = ((AbstractFrame) frame).getCompanion();
            if (instance != null && type.isAssignableFrom(instance.getClass())) {
                return instance;
            }
        }
    }
    return null;
}
Also used : DsContext(com.haulmont.cuba.gui.data.DsContext) ThemeConstantsManager(com.haulmont.cuba.gui.theme.ThemeConstantsManager) Config(com.haulmont.cuba.core.config.Config) Logger(org.slf4j.Logger) Field(java.lang.reflect.Field) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) ClientConfiguration(com.haulmont.cuba.client.ClientConfiguration)

Aggregations

ClientConfiguration (com.haulmont.cuba.client.ClientConfiguration)1 Config (com.haulmont.cuba.core.config.Config)1 DsContext (com.haulmont.cuba.gui.data.DsContext)1 ExportDisplay (com.haulmont.cuba.gui.export.ExportDisplay)1 ThemeConstantsManager (com.haulmont.cuba.gui.theme.ThemeConstantsManager)1 Field (java.lang.reflect.Field)1 Logger (org.slf4j.Logger)1