Search in sources :

Example 1 with FragmentLoaderInitTask

use of io.jmix.ui.sys.FragmentHelper.FragmentLoaderInitTask in project jmix by jmix-framework.

the class FragmentsImpl method createFragment.

protected <T extends ScreenFragment> T createFragment(FrameOwner parent, WindowInfo windowInfo, ScreenOptions options) {
    if (windowInfo.getType() != WindowInfo.Type.FRAGMENT) {
        throw new IllegalArgumentException(String.format("Unable to create fragment %s it is a screen: %s", windowInfo.getId(), windowInfo.getControllerClass()));
    }
    Timer.Sample createSample = Timer.start(meterRegistry);
    Fragment fragment = createFragmentInternal();
    ScreenFragment controller = fragmentHelper.createController(windowInfo, fragment);
    // setup screen and controller
    setHostController(controller, parent);
    setWindowId(controller, windowInfo.getId());
    setFrame(controller, fragment);
    setScreenContext(controller, new ScreenContextImpl(windowInfo, options, getScreenContext(parent)));
    setScreenData(controller, applicationContext.getBean(ScreenData.class));
    FragmentImplementation fragmentImpl = (FragmentImplementation) fragment;
    fragmentImpl.setFrameOwner(controller);
    fragmentImpl.setId(controller.getId());
    createSample.stop(createScreenTimer(meterRegistry, ScreenLifeCycle.CREATE, windowInfo.getId()));
    Timer.Sample loadSample = Timer.start(meterRegistry);
    Frame parentFrame = getFrame(parent);
    // fake parent loader context
    ComponentLoaderContext loaderContext = createComponentLoaderContext(options);
    FragmentContextImpl frameContext = new FragmentContextImpl(fragment, loaderContext);
    frameContext.setManualInitRequired(true);
    ((FrameImplementation) fragment).setContext(frameContext);
    loaderContext.setCurrentFrameId(windowInfo.getId());
    loaderContext.setFullFrameId(windowInfo.getId());
    loaderContext.setFrame(fragment);
    loaderContext.setParent(null);
    loaderContext.setScreenData(UiControllerUtils.getScreenData(parent));
    // load XML if needed
    if (windowInfo.getTemplate() != null) {
        ComponentLoaderContext innerContext = createComponentLoaderContext(options);
        innerContext.setCurrentFrameId(windowInfo.getId());
        innerContext.setFullFrameId(windowInfo.getId());
        innerContext.setFrame(fragment);
        innerContext.setParent(loaderContext);
        LayoutLoader layoutLoader = applicationContext.getBean(LayoutLoader.class, innerContext);
        Element rootElement = screenXmlLoader.load(windowInfo.getTemplate(), windowInfo.getId(), emptyMap());
        innerContext.setMessageGroup(fragmentHelper.findMessageGroup(rootElement, windowInfo.getTemplate()));
        loadAdditionalData(rootElement, innerContext);
        ComponentLoader<Fragment> fragmentLoader = layoutLoader.createFragmentContent(fragment, rootElement);
        fragmentLoader.loadComponent();
        loaderContext.getInjectTasks().addAll(innerContext.getInjectTasks());
        loaderContext.getInitTasks().addAll(innerContext.getInitTasks());
        loaderContext.getPostInitTasks().addAll(innerContext.getPostInitTasks());
    }
    loaderContext.addInjectTask(new FragmentLoaderInjectTask(fragment, options, applicationContext));
    loaderContext.addInitTask(new FragmentLoaderInitTask(fragment, options, loaderContext, applicationContext));
    loadSample.stop(createScreenTimer(meterRegistry, ScreenLifeCycle.LOAD, windowInfo.getId()));
    loaderContext.executeInjectTasks();
    fragmentImpl.setFrame(parentFrame);
    // noinspection unchecked
    return (T) controller;
}
Also used : LayoutLoader(io.jmix.ui.xml.layout.loader.LayoutLoader) Frame(io.jmix.ui.component.Frame) FragmentImplementation(io.jmix.ui.component.impl.FragmentImplementation) Element(org.dom4j.Element) FragmentLoaderInjectTask(io.jmix.ui.sys.FragmentHelper.FragmentLoaderInjectTask) FrameImplementation(io.jmix.ui.component.impl.FrameImplementation) Fragment(io.jmix.ui.component.Fragment) ComponentLoaderContext(io.jmix.ui.xml.layout.loader.ComponentLoaderContext) Timer(io.micrometer.core.instrument.Timer) UiMonitoring.createScreenTimer(io.jmix.ui.monitoring.UiMonitoring.createScreenTimer) FragmentLoaderInitTask(io.jmix.ui.sys.FragmentHelper.FragmentLoaderInitTask) ScreenData(io.jmix.ui.model.ScreenData)

Example 2 with FragmentLoaderInitTask

use of io.jmix.ui.sys.FragmentHelper.FragmentLoaderInitTask in project jmix by jmix-framework.

the class FragmentComponentLoader method loadComponent.

@Override
public void loadComponent() {
    loadAliases();
    if (getComponentContext().getFrame() != null) {
        resultComponent.setFrame(getComponentContext().getFrame());
    }
    String src = element.attributeValue("src");
    String screenId = element.attributeValue("screen");
    String screenPath = StringUtils.isEmpty(screenId) ? src : screenId;
    if (element.attributeValue("id") != null) {
        screenPath = element.attributeValue("id");
    }
    if (getComponentContext().getFrame() != null) {
        String parentId = getComponentContext().getFullFrameId();
        if (StringUtils.isNotEmpty(parentId)) {
            screenPath = parentId + "." + screenPath;
        }
    }
    Timer.Sample sample = Timer.start(getMeterRegistry());
    if (fragmentLoader != null) {
        fragmentLoader.loadComponent();
    }
    // load properties after inner context, they must override values defined inside of fragment
    assignXmlDescriptor(resultComponent, element);
    loadVisible(resultComponent, element);
    loadEnable(resultComponent, element);
    loadStyleName(resultComponent, element);
    loadResponsive(resultComponent, element);
    loadCss(resultComponent, element);
    loadAlign(resultComponent, element);
    loadHeight(resultComponent, element);
    loadWidth(resultComponent, element);
    loadIcon(resultComponent, element);
    loadCaption(resultComponent, element);
    loadDescription(resultComponent, element);
    sample.stop(createScreenTimer(getMeterRegistry(), ScreenLifeCycle.LOAD, screenPath));
    // propagate init phases
    ComponentLoaderContext parentContext = (ComponentLoaderContext) getContext();
    if (innerContext != null) {
        parentContext.getInjectTasks().addAll(innerContext.getInjectTasks());
        parentContext.getInitTasks().addAll(innerContext.getInitTasks());
        parentContext.getPostInitTasks().addAll(innerContext.getPostInitTasks());
    }
    ScreenOptions options = parentContext.getOptions();
    parentContext.addInjectTask(new FragmentLoaderInjectTask(resultComponent, options, applicationContext));
    parentContext.addInitTask(new FragmentLoaderInitTask(resultComponent, options, innerContext, applicationContext));
}
Also used : Timer(io.micrometer.core.instrument.Timer) UiMonitoring.createScreenTimer(io.jmix.ui.monitoring.UiMonitoring.createScreenTimer) FragmentLoaderInjectTask(io.jmix.ui.sys.FragmentHelper.FragmentLoaderInjectTask) FragmentLoaderInitTask(io.jmix.ui.sys.FragmentHelper.FragmentLoaderInitTask) ScreenOptions(io.jmix.ui.screen.ScreenOptions)

Aggregations

UiMonitoring.createScreenTimer (io.jmix.ui.monitoring.UiMonitoring.createScreenTimer)2 FragmentLoaderInitTask (io.jmix.ui.sys.FragmentHelper.FragmentLoaderInitTask)2 FragmentLoaderInjectTask (io.jmix.ui.sys.FragmentHelper.FragmentLoaderInjectTask)2 Timer (io.micrometer.core.instrument.Timer)2 Fragment (io.jmix.ui.component.Fragment)1 Frame (io.jmix.ui.component.Frame)1 FragmentImplementation (io.jmix.ui.component.impl.FragmentImplementation)1 FrameImplementation (io.jmix.ui.component.impl.FrameImplementation)1 ScreenData (io.jmix.ui.model.ScreenData)1 ScreenOptions (io.jmix.ui.screen.ScreenOptions)1 ComponentLoaderContext (io.jmix.ui.xml.layout.loader.ComponentLoaderContext)1 LayoutLoader (io.jmix.ui.xml.layout.loader.LayoutLoader)1 Element (org.dom4j.Element)1