Search in sources :

Example 1 with LegacyFragmentAdapter

use of com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter in project cuba by cuba-platform.

the class FragmentHelper method createController.

@SuppressWarnings("unchecked")
public ScreenFragment createController(WindowInfo windowInfo, Fragment fragment) {
    Class screenClass = windowInfo.getControllerClass();
    if (AbstractWindow.class.isAssignableFrom(screenClass)) {
        AbstractWindow legacyScreen;
        try {
            legacyScreen = (AbstractWindow) invokeConstructor(screenClass);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
            throw new DevelopmentException("Unable to create " + screenClass);
        }
        LegacyFragmentAdapter adapter = new LegacyFragmentAdapter(legacyScreen);
        legacyScreen.setFrame(fragment);
        adapter.setWrappedFrame(fragment);
        log.warn("Fragment class '{}' should not be inherited from AbstractWindow. " + "It may cause problems with controller life cycle. " + "Fragment controllers should inherit ScreenFragment.", screenClass.getSimpleName());
        return adapter;
    }
    // new screens cannot be opened in fragments
    if (!ScreenFragment.class.isAssignableFrom(screenClass)) {
        throw new IllegalStateException(String.format("Fragment controllers should inherit ScreenFragment." + " UI controller is not ScreenFragment - %s %s", windowInfo.toString(), screenClass.getSimpleName()));
    }
    ScreenFragment controller;
    try {
        controller = (ScreenFragment) invokeConstructor(screenClass);
    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException("Unable to create instance of screen class " + screenClass);
    }
    return controller;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) DevelopmentException(com.haulmont.cuba.core.global.DevelopmentException) AbstractWindow(com.haulmont.cuba.gui.components.AbstractWindow) ScreenFragment(com.haulmont.cuba.gui.screen.ScreenFragment) LegacyFragmentAdapter(com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter)

Example 2 with LegacyFragmentAdapter

use of com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter in project cuba by cuba-platform.

the class DeclarativeAction method actionPerform.

@Override
public void actionPerform(Component component) {
    if (StringUtils.isEmpty(methodName)) {
        return;
    }
    FrameOwner controller = frame.getFrameOwner();
    if (controller instanceof LegacyFragmentAdapter) {
        controller = ((LegacyFragmentAdapter) controller).getRealScreen();
    }
    Method method;
    try {
        method = controller.getClass().getMethod(methodName, Component.class);
    } catch (NoSuchMethodException e) {
        try {
            method = controller.getClass().getMethod(methodName);
        } catch (NoSuchMethodException e1) {
            throw new IllegalStateException(String.format("No suitable methods named %s for action %s", methodName, id));
        }
    }
    try {
        if (method.getParameterCount() == 1) {
            method.invoke(controller, component);
        } else {
            method.invoke(controller);
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception on action handling", e);
    }
}
Also used : FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) Method(java.lang.reflect.Method) LegacyFragmentAdapter(com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter) Component(com.haulmont.cuba.gui.components.Component)

Example 3 with LegacyFragmentAdapter

use of com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter in project cuba by cuba-platform.

the class DeclarativeFieldGenerator method generateField.

@Override
public Component generateField(Datasource datasource, String propertyId) {
    Frame frame = fieldGroup.getFrame();
    if (frame == null) {
        throw new IllegalStateException("Table should be attached to frame");
    }
    FrameOwner controller = frame.getFrameOwner();
    if (controller instanceof LegacyFragmentAdapter) {
        controller = ((LegacyFragmentAdapter) controller).getRealScreen();
    }
    Class<? extends FrameOwner> cCls = controller.getClass();
    Method exactMethod = getAccessibleMethod(cCls, methodName, Datasource.class, String.class);
    if (exactMethod != null) {
        checkGeneratorMethodResultType(exactMethod, frame);
        try {
            return (Component) exactMethod.invoke(controller, datasource, propertyId);
        } catch (Exception e) {
            throw new RuntimeException("Exception in declarative FieldGroup Field generator " + methodName, e);
        }
    }
    Method dsMethod = getAccessibleMethod(cCls, methodName, Datasource.class);
    if (dsMethod != null) {
        checkGeneratorMethodResultType(dsMethod, frame);
        try {
            return (Component) dsMethod.invoke(controller, datasource);
        } catch (Exception e) {
            throw new RuntimeException("Exception in declarative FieldGroup Field generator " + methodName, e);
        }
    }
    Method parameterLessMethod = getAccessibleMethod(cCls, methodName);
    if (parameterLessMethod != null) {
        checkGeneratorMethodResultType(parameterLessMethod, frame);
        try {
            return (Component) parameterLessMethod.invoke(controller);
        } catch (Exception e) {
            throw new RuntimeException("Exception in declarative FieldGroup Field generator " + methodName, e);
        }
    }
    String fieldGroupId = fieldGroup.getId() == null ? "" : fieldGroup.getId();
    throw new IllegalStateException(String.format("No suitable method named %s for column generator of table %s", methodName, fieldGroupId));
}
Also used : Frame(com.haulmont.cuba.gui.components.Frame) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) MethodUtils.getAccessibleMethod(org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod) Method(java.lang.reflect.Method) LegacyFragmentAdapter(com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter) Component(com.haulmont.cuba.gui.components.Component) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 4 with LegacyFragmentAdapter

use of com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter in project cuba by cuba-platform.

the class UiControllerDependencyInjector method getInjectedInstance.

@Nullable
protected Object getInjectedInstance(Class<?> type, String name, Class annotationClass, AnnotatedElement element, InjectionContext injectionContext) {
    FrameOwner frameOwner = injectionContext.getFrameOwner();
    ScreenOptions options = injectionContext.getScreenOptions();
    Frame frame = UiControllerUtils.getFrame(frameOwner);
    if (annotationClass == WindowParam.class) {
        if (options instanceof MapScreenOptions) {
            return ((MapScreenOptions) options).getParams().get(name);
        }
        // Injecting a parameter
        return null;
    } else if (ScreenFragment.class.isAssignableFrom(type)) {
        // Injecting inner fragment controller
        Component fragment = frame.getComponent(name);
        if (fragment == null) {
            return null;
        }
        return ((Fragment) fragment).getFrameOwner();
    } else if (AbstractWindow.class.isAssignableFrom(type)) {
        // Injecting inner legacy screen controller
        Component fragment = frame.getComponent(name);
        if (fragment == null) {
            return null;
        }
        ScreenFragment frameOwner1 = ((Fragment) fragment).getFrameOwner();
        if (frameOwner1 instanceof LegacyFragmentAdapter) {
            return ((LegacyFragmentAdapter) frameOwner1).getRealScreen();
        }
        return frameOwner1;
    } else if (Component.class.isAssignableFrom(type)) {
        // / if legacy frame - inject controller
        Component component = frame.getComponent(name);
        if (component instanceof Fragment) {
            ScreenFragment frameOwner1 = ((Fragment) component).getFrameOwner();
            if (type.isAssignableFrom(frameOwner1.getClass())) {
                return frameOwner1;
            }
        }
        // for legacy screens only
        if (component == null && frameOwner instanceof LegacyFrame) {
            // try to find using slow iteration
            component = ComponentsHelper.getComponent(frame, name);
        }
        // Injecting a UI component
        return component;
    } else if (InstanceContainer.class.isAssignableFrom(type)) {
        // Injecting a container
        ScreenData data = getScreenData(frameOwner);
        return data.getContainer(name);
    } else if (DataLoader.class.isAssignableFrom(type)) {
        // Injecting a loader
        ScreenData data = getScreenData(frameOwner);
        return data.getLoader(name);
    } else if (DataContext.class.isAssignableFrom(type)) {
        // Injecting the data context
        ScreenData data = getScreenData(frameOwner);
        return data.getDataContext();
    } else if (Datasource.class.isAssignableFrom(type)) {
        // Injecting a datasource
        return ((LegacyFrame) frameOwner).getDsContext().get(name);
    } else if (DsContext.class.isAssignableFrom(type)) {
        if (frameOwner instanceof LegacyFrame) {
            // Injecting the DsContext
            return ((LegacyFrame) frameOwner).getDsContext();
        } else {
            throw new DevelopmentException("DsContext can be injected only into LegacyFrame inheritors");
        }
    } else if (DataSupplier.class.isAssignableFrom(type)) {
        if (frameOwner instanceof LegacyFrame) {
            // Injecting the DataSupplier
            return ((LegacyFrame) frameOwner).getDsContext().getDataSupplier();
        } else {
            throw new DevelopmentException("DataSupplier can be injected only into LegacyFrame inheritors");
        }
    } 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 (Facet.class.isAssignableFrom(type)) {
        // Injecting non-visual component
        String[] elements = ValuePathHelper.parse(name);
        if (elements.length == 1) {
            return frame.getFacet(name);
        }
        String prefix = pathPrefix(elements);
        Component component = frame.getComponent(prefix);
        if (component == null) {
            return null;
        }
        if (!(component instanceof Fragment)) {
            throw new UnsupportedOperationException(String.format("Unable to inject facet with id %s and type %s. Component %s is not a fragment", name, type, prefix));
        }
        String facetId = elements[elements.length - 1];
        return ((Fragment) component).getFacet(facetId);
    } else if (ExportDisplay.class.isAssignableFrom(type)) {
        // Injecting an ExportDisplay
        ExportDisplay exportDisplay = beanLocator.get(ExportDisplay.NAME);
        exportDisplay.setFrame(frame);
        return exportDisplay;
    } else if (Config.class.isAssignableFrom(type)) {
        ClientConfiguration configuration = beanLocator.get(Configuration.NAME);
        // noinspection unchecked
        return configuration.getConfigCached((Class<? extends Config>) type);
    } else if (Logger.class == type && element instanceof Field) {
        // injecting logger
        return LoggerFactory.getLogger(((Field) element).getDeclaringClass());
    } else if (Screens.class.isAssignableFrom(type)) {
        // injecting screens
        return getScreenContext(frameOwner).getScreens();
    } else if (Dialogs.class.isAssignableFrom(type)) {
        // injecting dialogs
        return getScreenContext(frameOwner).getDialogs();
    } else if (Notifications.class.isAssignableFrom(type)) {
        // injecting notifications
        return getScreenContext(frameOwner).getNotifications();
    } else if (Fragments.class.isAssignableFrom(type)) {
        // injecting fragments
        return getScreenContext(frameOwner).getFragments();
    } else if (UrlRouting.class.isAssignableFrom(type)) {
        // injecting urlRouting
        return getScreenContext(frameOwner).getUrlRouting();
    } else if (MessageBundle.class == type) {
        return createMessageBundle(element, frameOwner, frame);
    } else if (ThemeConstants.class == type) {
        // Injecting a Theme
        ThemeConstantsManager themeManager = beanLocator.get(ThemeConstantsManager.NAME);
        return themeManager.getConstants();
    } else if (WebBrowserTools.class.isAssignableFrom(type)) {
        // Injecting WebBrowserTools
        return getScreenContext(frameOwner).getWebBrowserTools();
    } else {
        Object instance;
        // Try to find a Spring bean
        Map<String, ?> beans = beanLocator.getAll(type);
        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 (frameOwner instanceof LegacyFrame) {
            instance = ((LegacyFrame) frameOwner).getCompanion();
            if (instance != null && type.isAssignableFrom(instance.getClass())) {
                return instance;
            }
        }
    }
    return null;
}
Also used : LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) ThemeConstantsManager(com.haulmont.cuba.gui.theme.ThemeConstantsManager) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) Field(java.lang.reflect.Field) DataLoader(com.haulmont.cuba.gui.model.DataLoader) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) Datasource(com.haulmont.cuba.gui.data.Datasource) DevelopmentException(com.haulmont.cuba.core.global.DevelopmentException) DataSupplier(com.haulmont.cuba.gui.data.DataSupplier) LegacyFragmentAdapter(com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter) Map(java.util.Map) ClientConfiguration(com.haulmont.cuba.client.ClientConfiguration) UiControllerUtils.getScreenData(com.haulmont.cuba.gui.screen.UiControllerUtils.getScreenData) ScreenData(com.haulmont.cuba.gui.model.ScreenData) Nullable(javax.annotation.Nullable)

Example 5 with LegacyFragmentAdapter

use of com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter in project cuba by cuba-platform.

the class DeclarativeColumnGenerator method generateCell.

@Override
public Component generateCell(Entity entity) {
    if (unableToFindMethod) {
        return null;
    }
    Frame frame = table.getFrame();
    if (frame == null) {
        throw new IllegalStateException("Table should be attached to frame");
    }
    FrameOwner controller = frame.getFrameOwner();
    if (controller instanceof LegacyFragmentAdapter) {
        controller = ((LegacyFragmentAdapter) controller).getRealScreen();
    }
    if (method == null) {
        method = findGeneratorMethod(controller.getClass(), methodName);
        if (method == null) {
            this.unableToFindMethod = true;
            String tableId = table.getId() == null ? "" : table.getId();
            throw new IllegalStateException(String.format("No suitable method named %s for column generator of table %s", methodName, tableId));
        }
    }
    try {
        return (Component) method.invoke(controller, entity);
    } catch (Exception e) {
        throw new RuntimeException("Exception in declarative Table column generator " + methodName, e);
    }
}
Also used : Frame(com.haulmont.cuba.gui.components.Frame) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) LegacyFragmentAdapter(com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter) Component(com.haulmont.cuba.gui.components.Component)

Aggregations

LegacyFragmentAdapter (com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter)9 FrameOwner (com.haulmont.cuba.gui.screen.FrameOwner)6 Method (java.lang.reflect.Method)5 Component (com.haulmont.cuba.gui.components.Component)4 DevelopmentException (com.haulmont.cuba.core.global.DevelopmentException)2 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)2 Frame (com.haulmont.cuba.gui.components.Frame)2 ClientConfiguration (com.haulmont.cuba.client.ClientConfiguration)1 com.haulmont.cuba.gui (com.haulmont.cuba.gui)1 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)1 AbstractWindow (com.haulmont.cuba.gui.components.AbstractWindow)1 Disposable (com.haulmont.cuba.gui.components.Component.Disposable)1 Timer (com.haulmont.cuba.gui.components.Timer)1 DataSupplier (com.haulmont.cuba.gui.data.DataSupplier)1 Datasource (com.haulmont.cuba.gui.data.Datasource)1 ExportDisplay (com.haulmont.cuba.gui.export.ExportDisplay)1 DataLoader (com.haulmont.cuba.gui.model.DataLoader)1 ScreenData (com.haulmont.cuba.gui.model.ScreenData)1 ScreenFragment (com.haulmont.cuba.gui.screen.ScreenFragment)1 UiControllerUtils.getScreenData (com.haulmont.cuba.gui.screen.UiControllerUtils.getScreenData)1