Search in sources :

Example 16 with LegacyFrame

use of com.haulmont.cuba.gui.screen.compatibility.LegacyFrame in project cuba by cuba-platform.

the class FoldersBean method openFolder.

@Override
public void openFolder(AbstractSearchFolder folder) {
    if (StringUtils.isBlank(folder.getFilterComponentId())) {
        log.warn("Unable to open folder: componentId is blank");
        return;
    }
    String[] strings = ValuePathHelper.parse(folder.getFilterComponentId());
    String screenId = strings[0];
    WindowInfo windowInfo = windowConfig.getWindowInfo(screenId);
    Map<String, Object> params = new HashMap<>();
    WindowParams.DISABLE_AUTO_REFRESH.set(params, true);
    WindowParams.DISABLE_RESUME_SUSPENDED.set(params, true);
    if (!StringUtils.isBlank(folder.getTabName())) {
        WindowParams.DESCRIPTION.set(params, messages.getMainMessage(folder.getTabName()));
    } else {
        WindowParams.DESCRIPTION.set(params, messages.getMainMessage(folder.getName()));
    }
    WindowParams.FOLDER_ID.set(params, folder.getId());
    WindowManager wm = App.getInstance().getWindowManager();
    Window window = wm.openWindow(windowInfo, OpenType.NEW_TAB, params);
    Filter filterComponent = null;
    if (strings.length > 1) {
        String filterComponentId = StringUtils.join(Arrays.copyOfRange(strings, 1, strings.length), '.');
        filterComponent = (Filter) window.getComponentNN(filterComponentId);
        FilterEntity filterEntity = metadata.create(FilterEntity.class);
        filterEntity.setFolder(folder);
        filterEntity.setComponentId(folder.getFilterComponentId());
        filterEntity.setName(folder.getLocName());
        filterEntity.setXml(folder.getFilterXml());
        filterEntity.setApplyDefault(BooleanUtils.isNotFalse(folder.getApplyDefault()));
        if (folder instanceof SearchFolder) {
            filterEntity.setIsSet(((SearchFolder) folder).getIsSet());
        }
        filterComponent.setFilterEntity(filterEntity);
        filterComponent.switchFilterMode(FilterDelegate.FilterMode.GENERIC_MODE);
    }
    if (filterComponent != null && folder instanceof SearchFolder) {
        SearchFolder searchFolder = (SearchFolder) folder;
        if (searchFolder.getPresentation() != null) {
            ((HasPresentations) filterComponent.getApplyTo()).applyPresentation(searchFolder.getPresentation().getId());
        }
    }
    if (window.getFrameOwner() instanceof LegacyFrame) {
        DsContext dsContext = ((LegacyFrame) window.getFrameOwner()).getDsContext();
        if (dsContext != null) {
            ((DsContextImplementation) dsContext).resumeSuspended();
        }
    }
}
Also used : Window(com.haulmont.cuba.gui.components.Window) HasPresentations(com.haulmont.cuba.gui.components.HasPresentations) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) DsContext(com.haulmont.cuba.gui.data.DsContext) HashMap(java.util.HashMap) FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) AbstractSearchFolder(com.haulmont.cuba.core.entity.AbstractSearchFolder) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) WindowInfo(com.haulmont.cuba.gui.config.WindowInfo) WindowManager(com.haulmont.cuba.gui.WindowManager) Filter(com.haulmont.cuba.gui.components.Filter)

Example 17 with LegacyFrame

use of com.haulmont.cuba.gui.screen.compatibility.LegacyFrame in project cuba by cuba-platform.

the class CreateAction method internalOpenEditor.

@SuppressWarnings("unchecked")
protected void internalOpenEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs, Map<String, Object> params) {
    LegacyFrame frameOwner = (LegacyFrame) target.getFrame().getFrameOwner();
    AbstractEditor window = frameOwner.openEditor(getWindowId(), newItem, getOpenType(), params, parentDs);
    if (editorCloseListener == null) {
        window.addCloseListener(actionId -> {
            // move focus to owner
            if (target instanceof Component.Focusable) {
                ((Component.Focusable) target).focus();
            }
            if (Window.COMMIT_ACTION_ID.equals(actionId)) {
                Entity editedItem = window.getItem();
                if (editedItem != null) {
                    if (parentDs == null) {
                        editedItem = AppBeans.get(GuiActionSupport.class).reloadEntityIfNeeded(editedItem, datasource);
                        if (addFirst && datasource instanceof CollectionDatasource.Ordered)
                            ((CollectionDatasource.Ordered) datasource).includeItemFirst(editedItem);
                        else
                            datasource.includeItem(editedItem);
                    }
                    target.setSelected(editedItem);
                    afterCommit(editedItem);
                    if (afterCommitHandler != null) {
                        afterCommitHandler.handle(editedItem);
                    }
                }
            }
            afterWindowClosed(window);
            if (afterWindowClosedHandler != null) {
                afterWindowClosedHandler.handle(window, actionId);
            }
        });
    } else {
        window.addCloseListener(editorCloseListener);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame)

Example 18 with LegacyFrame

use of com.haulmont.cuba.gui.screen.compatibility.LegacyFrame in project cuba by cuba-platform.

the class UiControllerDependencyInjector method doInjection.

protected void doInjection(AnnotatedElement element, Class annotationClass, InjectionContext injectionContext) {
    Class<?> type;
    String name = null;
    if (annotationClass == Named.class) {
        name = element.getAnnotation(Named.class).value();
    } else if (annotationClass == Resource.class) {
        name = element.getAnnotation(Resource.class).name();
    } else if (annotationClass == WindowParam.class) {
        name = element.getAnnotation(WindowParam.class).name();
    }
    boolean required = true;
    if (element.isAnnotationPresent(WindowParam.class)) {
        required = element.getAnnotation(WindowParam.class).required();
    } else if (element.isAnnotationPresent(Autowired.class)) {
        required = element.getAnnotation(Autowired.class).required();
    }
    if (element instanceof Field) {
        type = ((Field) element).getType();
        if (StringUtils.isEmpty(name)) {
            name = ((Field) element).getName();
        }
    } else if (element instanceof Method) {
        Class<?>[] types = ((Method) element).getParameterTypes();
        if (types.length != 1) {
            throw new IllegalStateException("Can inject to methods with one parameter only");
        }
        type = types[0];
        if (StringUtils.isEmpty(name)) {
            if (((Method) element).getName().startsWith("set")) {
                name = StringUtils.uncapitalize(((Method) element).getName().substring(3));
            } else {
                name = ((Method) element).getName();
            }
        }
    } else {
        throw new IllegalStateException("Can inject to fields and setter methods only");
    }
    Object instance = getInjectedInstance(type, name, annotationClass, element, injectionContext);
    FrameOwner frameOwner = injectionContext.getFrameOwner();
    if (instance != null) {
        assignValue(element, instance, injectionContext);
    } else if (required) {
        Class<?> declaringClass = ((Member) element).getDeclaringClass();
        Class<? extends FrameOwner> frameClass = frameOwner.getClass();
        String msg;
        if (frameClass == declaringClass) {
            msg = String.format("Unable to find an instance of type '%s' named '%s' for instance of '%s'", type, name, frameClass.getCanonicalName());
        } else {
            msg = String.format("Unable to find an instance of type '%s' named '%s' declared in '%s' for instance of '%s'", type, name, declaringClass.getCanonicalName(), frameClass.getCanonicalName());
        }
        if (!(frameOwner instanceof LegacyFrame)) {
            throw new DevelopmentException(msg);
        } else {
            log.warn(msg);
        }
    } else {
        log.trace("Skip injection {} of {} as it is optional and instance not found", name, frameOwner.getClass());
    }
}
Also used : Resource(javax.annotation.Resource) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) AnnotatedMethod(com.haulmont.cuba.gui.sys.UiControllerReflectionInspector.AnnotatedMethod) DevelopmentException(com.haulmont.cuba.core.global.DevelopmentException) Field(java.lang.reflect.Field) Autowired(org.springframework.beans.factory.annotation.Autowired)

Example 19 with LegacyFrame

use of com.haulmont.cuba.gui.screen.compatibility.LegacyFrame 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 20 with LegacyFrame

use of com.haulmont.cuba.gui.screen.compatibility.LegacyFrame in project cuba by cuba-platform.

the class DsContextImpl method get.

@Override
public Datasource get(String id) {
    checkNotNullArgument(id, "Null datasource ID");
    id = aliasesMap.getOrDefault(id, id);
    Datasource ds = null;
    if (!id.contains(".")) {
        ds = datasourceMap.get(id);
        if (ds == null && parent != null) {
            ds = parent.get(id);
        }
    } else {
        if (windowContext != null) {
            String nestedFramePath = id.substring(0, id.indexOf("."));
            Component nestedFrame = getFrameContext().getFrame().getComponent(nestedFramePath);
            if (nestedFrame instanceof Frame) {
                String nestedDsId = id.substring(id.indexOf(".") + 1);
                FrameOwner frameOwner = ((Frame) nestedFrame).getFrameOwner();
                if (frameOwner instanceof LegacyFrame) {
                    ds = ((LegacyFrame) frameOwner).getDsContext().get(nestedDsId);
                }
            }
        }
    }
    return ds;
}
Also used : Frame(com.haulmont.cuba.gui.components.Frame) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) Component(com.haulmont.cuba.gui.components.Component)

Aggregations

LegacyFrame (com.haulmont.cuba.gui.screen.compatibility.LegacyFrame)23 Entity (com.haulmont.cuba.core.entity.Entity)5 WindowConfig (com.haulmont.cuba.gui.config.WindowConfig)5 Datasource (com.haulmont.cuba.gui.data.Datasource)5 DsContext (com.haulmont.cuba.gui.data.DsContext)5 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 DsContextImplementation (com.haulmont.cuba.gui.data.impl.DsContextImplementation)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)2 DevelopmentException (com.haulmont.cuba.core.global.DevelopmentException)2 Messages (com.haulmont.cuba.core.global.Messages)2 WindowManager (com.haulmont.cuba.gui.WindowManager)2 OpenType (com.haulmont.cuba.gui.WindowManager.OpenType)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 Component (com.haulmont.cuba.gui.components.Component)2 Filter (com.haulmont.cuba.gui.components.Filter)2 Frame (com.haulmont.cuba.gui.components.Frame)2 WindowInfo (com.haulmont.cuba.gui.config.WindowInfo)2 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)2