Search in sources :

Example 1 with Fragment

use of io.jmix.ui.component.Fragment 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 Fragment

use of io.jmix.ui.component.Fragment in project jmix by jmix-framework.

the class FragmentComponentLoader method createComponent.

@Override
public void createComponent() {
    WindowInfo windowInfo = createWindowInfo(element);
    String fragmentId;
    if (element.attributeValue("id") != null) {
        fragmentId = element.attributeValue("id");
    } else {
        fragmentId = windowInfo.getId();
    }
    Timer.Sample createSample = Timer.start(getMeterRegistry());
    Fragment fragment = createComponentInternal();
    FragmentHelper fragmentHelper = getFragmentHelper();
    ScreenFragment controller = fragmentHelper.createController(windowInfo, fragment);
    // setup screen and controller
    ComponentLoaderContext parentContext = (ComponentLoaderContext) getContext();
    FrameOwner hostController = parentContext.getFrame().getFrameOwner();
    setHostController(controller, hostController);
    setWindowId(controller, windowInfo.getId());
    setFrame(controller, fragment);
    setupScreenContext(controller, new ScreenContextImpl(windowInfo, parentContext.getOptions(), getScreenContext(hostController)));
    setScreenData(controller, applicationContext.getBean(ScreenData.class));
    FragmentImplementation fragmentImpl = (FragmentImplementation) fragment;
    fragmentImpl.setFrameOwner(controller);
    fragmentImpl.setId(fragmentId);
    FragmentContextImpl frameContext = new FragmentContextImpl(fragment, innerContext);
    ((FrameImplementation) fragment).setContext(frameContext);
    if (windowInfo.getTemplate() != null) {
        String frameId = fragmentId;
        if (parentContext.getFullFrameId() != null) {
            frameId = parentContext.getFullFrameId() + "." + frameId;
        }
        innerContext = createInnerContext();
        innerContext.setCurrentFrameId(fragmentId);
        innerContext.setFullFrameId(frameId);
        innerContext.setFrame(fragment);
        innerContext.setParent(parentContext);
        innerContext.setProperties(loadProperties(element));
        LayoutLoader layoutLoader = getLayoutLoader(innerContext);
        ScreenXmlLoader screenXmlLoader = applicationContext.getBean(ScreenXmlLoader.class);
        Element rootElement = screenXmlLoader.load(windowInfo.getTemplate(), windowInfo.getId(), getComponentContext().getParams());
        innerContext.setMessageGroup(fragmentHelper.findMessageGroup(rootElement, windowInfo.getTemplate()));
        loadAdditionalData(rootElement);
        this.fragmentLoader = layoutLoader.createFragmentContent(fragment, rootElement);
    }
    createSample.stop(createScreenTimer(getMeterRegistry(), ScreenLifeCycle.CREATE, windowInfo.getId()));
    this.resultComponent = fragment;
}
Also used : FragmentImplementation(io.jmix.ui.component.impl.FragmentImplementation) Element(org.dom4j.Element) FrameImplementation(io.jmix.ui.component.impl.FrameImplementation) ScreenFragment(io.jmix.ui.screen.ScreenFragment) Fragment(io.jmix.ui.component.Fragment) WindowInfo(io.jmix.ui.WindowInfo) Timer(io.micrometer.core.instrument.Timer) UiMonitoring.createScreenTimer(io.jmix.ui.monitoring.UiMonitoring.createScreenTimer) ScreenFragment(io.jmix.ui.screen.ScreenFragment) FrameOwner(io.jmix.ui.screen.FrameOwner) ScreenData(io.jmix.ui.model.ScreenData)

Example 3 with Fragment

use of io.jmix.ui.component.Fragment in project jmix by jmix-framework.

the class RuntimePropertiesFrameLoader method loadComponent.

@Override
public void loadComponent() {
    if (getContext().getFrame() != null) {
        resultComponent.setFrame(getContext().getFrame());
    }
    String src = element.attributeValue("src");
    if (src == null) {
        src = DEFAULT_DESCRIPTOR;
    }
    String runtimeDs = element.attributeValue("runtimeDs");
    if (StringUtils.isEmpty(runtimeDs)) {
        throw new GuiDevelopmentException("runtimePropsDatasource is not set for runtimeProperties component", getContext().getFullFrameId());
    }
    getContext().getParams().put("runtimeDs", runtimeDs);
    String categoriesDs = element.attributeValue("categoriesDs");
    if (StringUtils.isEmpty(categoriesDs)) {
        throw new GuiDevelopmentException("categoriesDs is not set for runtimeProperties component", getContext().getFullFrameId());
    }
    getContext().getParams().put("categoriesDs", categoriesDs);
    String rows = element.attributeValue("rows");
    getContext().getParams().put("rows", rows);
    String cols = element.attributeValue("cols");
    getContext().getParams().put("cols", cols);
    String fieldWidth = element.attributeValue("fieldWidth");
    getContext().getParams().put("fieldWidth", fieldWidth);
    String fieldCaptionWidth = element.attributeValue("fieldCaptionWidth");
    getContext().getParams().put("fieldCaptionWidth", fieldCaptionWidth);
    String screenPath = Objects.equals(src, DEFAULT_DESCRIPTOR) ? "runtimeProperties" : src;
    if (element.attributeValue("id") != null) {
        screenPath = element.attributeValue("id");
    }
    if (getContext().getFrame() != null) {
        String parentId = getContext().getFullFrameId();
        if (StringUtils.isNotEmpty(parentId)) {
            screenPath = parentId + "." + screenPath;
        }
    }
    Timer.Sample loadSample = 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);
    loadSample.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((Fragment) resultComponent, options, applicationContext));
    parentContext.addInitTask(new FragmentHelper.FragmentLoaderInitTask((Fragment) resultComponent, options, (ComponentLoaderContext) context, applicationContext));
}
Also used : Timer(io.micrometer.core.instrument.Timer) UiMonitoring.createScreenTimer(io.jmix.ui.monitoring.UiMonitoring.createScreenTimer) FragmentLoaderInjectTask(io.jmix.ui.sys.FragmentHelper.FragmentLoaderInjectTask) FragmentHelper(io.jmix.ui.sys.FragmentHelper) GuiDevelopmentException(io.jmix.ui.GuiDevelopmentException) ScreenOptions(io.jmix.ui.screen.ScreenOptions) ScreenFragment(io.jmix.ui.screen.ScreenFragment) Fragment(io.jmix.ui.component.Fragment)

Example 4 with Fragment

use of io.jmix.ui.component.Fragment in project jmix by jmix-framework.

the class RuntimePropertiesFrameLoader method createComponent.

@Override
public void createComponent() {
    String src = element.attributeValue("src");
    String screenId = element.attributeValue("id");
    if (src == null) {
        src = DEFAULT_DESCRIPTOR;
    }
    String fragmentId = screenId != null ? screenId : src;
    FragmentHelper fragmentHelper = getFragmentHelper();
    FrameHelper frameHelper = getFrameHelper();
    WindowInfo windowInfo = frameHelper.createFakeWindowInfo(src, fragmentId);
    UiComponents uiComponents = applicationContext.getBean(UiComponents.class);
    Fragment fragment = uiComponents.create(Fragment.NAME);
    ScreenFragment controller = fragmentHelper.createController(windowInfo, fragment);
    // setup screen and controller
    ComponentLoaderContext parentContext = (ComponentLoaderContext) getContext();
    FrameOwner hostController = parentContext.getFrame().getFrameOwner();
    // setup screen and controller
    setHostController(controller, hostController);
    setWindowId(controller, windowInfo.getId());
    setFrame(controller, fragment);
    setScreenContext(controller, new ScreenContextImpl(windowInfo, parentContext.getOptions(), getScreenContext(hostController)));
    setScreenData(controller, applicationContext.getBean(ScreenData.class));
    FragmentImplementation fragmentImpl = (FragmentImplementation) fragment;
    fragmentImpl.setFrameOwner(controller);
    fragmentImpl.setId(fragmentId);
    FragmentContextImpl frameContext = new FragmentContextImpl(fragment, innerContext);
    ((FrameImplementation) fragment).setContext(frameContext);
    if (windowInfo.getTemplate() != null) {
        String frameId = fragmentId;
        if (parentContext.getFullFrameId() != null) {
            frameId = parentContext.getFullFrameId() + "." + frameId;
        }
        innerContext = new ComponentLoaderContext(getContext().getOptions());
        innerContext.setMessageGroup(fragmentHelper.getMessageGroup(windowInfo.getTemplate()));
        innerContext.setCurrentFrameId(fragmentId);
        innerContext.setFullFrameId(frameId);
        innerContext.setFrame(fragment);
        innerContext.setParent(parentContext);
        LayoutLoader layoutLoader = getLayoutLoader(innerContext);
        ScreenXmlLoader screenXmlLoader = applicationContext.getBean(ScreenXmlLoader.class);
        Element rootElement = screenXmlLoader.load(windowInfo.getTemplate(), windowInfo.getId(), getContext().getParams());
        String messagesPack = rootElement.attributeValue("messagesPack");
        if (messagesPack != null) {
            innerContext.setMessageGroup(messagesPack);
        }
        this.fragmentLoader = layoutLoader.createFragmentContent(fragment, rootElement);
    }
    this.resultComponent = fragment;
}
Also used : LayoutLoader(io.jmix.ui.xml.layout.loader.LayoutLoader) FrameHelper(com.haulmont.cuba.gui.sys.FrameHelper) UiComponents(com.haulmont.cuba.gui.UiComponents) FragmentImplementation(io.jmix.ui.component.impl.FragmentImplementation) Element(org.dom4j.Element) FragmentContextImpl(io.jmix.ui.sys.FragmentContextImpl) FrameImplementation(io.jmix.ui.component.impl.FrameImplementation) ScreenFragment(io.jmix.ui.screen.ScreenFragment) Fragment(io.jmix.ui.component.Fragment) WindowInfo(io.jmix.ui.WindowInfo) ScreenFragment(io.jmix.ui.screen.ScreenFragment) FrameOwner(io.jmix.ui.screen.FrameOwner) FragmentHelper(io.jmix.ui.sys.FragmentHelper) ScreenContextImpl(io.jmix.ui.sys.ScreenContextImpl) ScreenXmlLoader(io.jmix.ui.sys.ScreenXmlLoader) ScreenData(io.jmix.ui.model.ScreenData)

Example 5 with Fragment

use of io.jmix.ui.component.Fragment in project jmix by jmix-framework.

the class CanvasUiComponentsFactory method createCanvasWidgetLayout.

@Override
public CanvasWidgetLayout createCanvasWidgetLayout(CanvasFragment canvasFragment, WidgetLayout widgetLayout) {
    Widget widget = widgetLayout.getWidget();
    Optional<WidgetTypeInfo> widgetTypeOpt = widgetRepository.getWidgetTypesInfo().stream().filter(widgetType -> StringUtils.equals(widget.getFragmentId(), widgetType.getFragmentId())).findFirst();
    if (!widgetTypeOpt.isPresent()) {
        CanvasWidgetLayout layout = components.create(CanvasWidgetLayout.class).init(widgetLayout);
        Label<String> label = components.create(Label.class);
        String message = messages.formatMessage(CanvasUiComponentsFactory.class, "widgetNotFound", widget.getCaption(), widget.getName());
        label.setValue(message);
        layout.addComponent(label);
        log.error(message);
        return layout;
    }
    widget.setDashboard(canvasFragment.getDashboardModel());
    String fragmentId = widgetTypeOpt.get().getFragmentId();
    Map<String, Object> params = new HashMap<>(ParamsMap.of(WIDGET, widget, DASHBOARD_MODEL, canvasFragment.getDashboardModel(), DASHBOARD, canvasFragment.getDashboard()));
    params.putAll(widgetRepository.getWidgetParams(widget));
    ScreenFragment screenFragment = AppUI.getCurrent().getFragments().create(canvasFragment, fragmentId, new MapScreenOptions(params)).init();
    Fragment fragment = screenFragment.getFragment();
    fragment.setSizeFull();
    Component widgetComponent = fragment;
    if (BooleanUtils.isTrue(widget.getShowWidgetCaption())) {
        VBoxLayout vBoxLayout = components.create(VBoxLayout.class);
        vBoxLayout.setSpacing(true);
        vBoxLayout.setMargin(true);
        vBoxLayout.setSizeFull();
        Label<String> label = components.create(Label.class);
        label.setValue(widget.getCaption());
        label.setStyleName("h2");
        vBoxLayout.add(label);
        vBoxLayout.add(fragment);
        vBoxLayout.expand(fragment);
        widgetComponent = vBoxLayout;
    } else {
        fragment.setMargin(true);
    }
    CanvasWidgetLayout layout = components.create(CanvasWidgetLayout.class).init(widgetLayout);
    layout.setUuid(UUID.randomUUID());
    layout.addComponent(widgetComponent);
    layout.setWidgetComponent(screenFragment);
    layout.setInnerLayout(widgetComponent);
    layout.setWidget(widget);
    layout.getDelegate().expand(widgetComponent);
    layout.setSizeFull();
    return layout;
}
Also used : ScreenFragment(io.jmix.ui.screen.ScreenFragment) Dashboard(io.jmix.dashboardsui.component.Dashboard) Fragment(io.jmix.ui.component.Fragment) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ParamsMap(io.jmix.core.common.util.ParamsMap) BooleanUtils(org.apache.commons.lang3.BooleanUtils) HashMap(java.util.HashMap) Widget(io.jmix.dashboards.model.Widget) StringUtils(org.apache.commons.lang3.StringUtils) Messages(io.jmix.core.Messages) WidgetRepository(io.jmix.dashboardsui.repository.WidgetRepository) Component(io.jmix.ui.component.Component) CanvasComponentsFactory(io.jmix.dashboardsui.dashboard.tools.factory.CanvasComponentsFactory) UiComponents(io.jmix.ui.UiComponents) VBoxLayout(io.jmix.ui.component.VBoxLayout) Map(java.util.Map) MapScreenOptions(io.jmix.ui.screen.MapScreenOptions) Logger(org.slf4j.Logger) UUID(java.util.UUID) io.jmix.dashboards.model.visualmodel(io.jmix.dashboards.model.visualmodel) CanvasLayout(io.jmix.dashboardsui.component.CanvasLayout) Label(io.jmix.ui.component.Label) AppUI(io.jmix.ui.AppUI) Optional(java.util.Optional) io.jmix.dashboardsui.component.impl(io.jmix.dashboardsui.component.impl) CanvasFragment(io.jmix.dashboardsui.screen.dashboard.editor.canvas.CanvasFragment) WidgetTypeInfo(io.jmix.dashboardsui.repository.WidgetTypeInfo) VBoxLayout(io.jmix.ui.component.VBoxLayout) HashMap(java.util.HashMap) Widget(io.jmix.dashboards.model.Widget) WidgetTypeInfo(io.jmix.dashboardsui.repository.WidgetTypeInfo) ScreenFragment(io.jmix.ui.screen.ScreenFragment) Fragment(io.jmix.ui.component.Fragment) CanvasFragment(io.jmix.dashboardsui.screen.dashboard.editor.canvas.CanvasFragment) ScreenFragment(io.jmix.ui.screen.ScreenFragment) MapScreenOptions(io.jmix.ui.screen.MapScreenOptions) Component(io.jmix.ui.component.Component)

Aggregations

Fragment (io.jmix.ui.component.Fragment)6 ScreenFragment (io.jmix.ui.screen.ScreenFragment)5 FragmentImplementation (io.jmix.ui.component.impl.FragmentImplementation)3 FrameImplementation (io.jmix.ui.component.impl.FrameImplementation)3 ScreenData (io.jmix.ui.model.ScreenData)3 UiMonitoring.createScreenTimer (io.jmix.ui.monitoring.UiMonitoring.createScreenTimer)3 WindowInfo (io.jmix.ui.WindowInfo)2 Component (io.jmix.ui.component.Component)2 FrameOwner (io.jmix.ui.screen.FrameOwner)2 FragmentHelper (io.jmix.ui.sys.FragmentHelper)2 FragmentLoaderInjectTask (io.jmix.ui.sys.FragmentHelper.FragmentLoaderInjectTask)2 Timer (io.micrometer.core.instrument.Timer)2 Element (org.dom4j.Element)2 UiComponents (com.haulmont.cuba.gui.UiComponents)1 FrameHelper (com.haulmont.cuba.gui.sys.FrameHelper)1 Messages (io.jmix.core.Messages)1 ParamsMap (io.jmix.core.common.util.ParamsMap)1 Widget (io.jmix.dashboards.model.Widget)1 io.jmix.dashboards.model.visualmodel (io.jmix.dashboards.model.visualmodel)1 CanvasLayout (io.jmix.dashboardsui.component.CanvasLayout)1