Search in sources :

Example 1 with Component

use of io.jmix.ui.component.Component in project jmix-docs by Haulmont.

the class ValuePickerScreen method onInit.

// end::inject-valuePickerClearAction[]
// tag::onInit-start[]
@Subscribe
protected void onInit(InitEvent event) {
    // end::onInit-start[]
    // tag::base-action-value-picker[]
    ValuePicker valueField = uiComponents.create(ValuePicker.NAME);
    valueField.addAction(new BaseAction("hello") {

        @Override
        public String getCaption() {
            return null;
        }

        @Override
        public String getDescription() {
            return messageBundle.getMessage("helloDescription");
        }

        @Override
        public String getIcon() {
            return JmixIcon.HANDSHAKE_O.source();
        }

        @Override
        public void actionPerform(Component component) {
            notifications.create().withCaption("Hello!").withType(Notifications.NotificationType.TRAY).show();
        }
    });
    valueField.addAction(new BaseAction("goodbye").withCaption(null).withDescription(messageBundle.getMessage("goodbyeDescription")).withIcon(JmixIcon.HAND_PAPER_O.source()).withHandler(e -> notifications.create().withCaption("Goodbye!").withType(Notifications.NotificationType.TRAY).show()));
    vBox.add(valueField);
    // end::base-action-value-picker[]
    loginValuePicker.addAction(new BaseAction("showValue").withHandler(actionPerformedEvent -> {
        String value = loginValuePicker.getValue();
        notifications.create().withCaption(value != null ? value : "No value").show();
    }).withDescription("Show Value").withIcon(JmixIcon.EYE.source()));
    // tag::add-clear-action1[]
    loginValuePicker.addAction(actions.create(ValueClearAction.ID));
// end::add-clear-action1[]
// tag::onInit-end[]
}
Also used : ValuePicker(io.jmix.ui.component.ValuePicker) Dialogs(io.jmix.ui.Dialogs) Autowired(org.springframework.beans.factory.annotation.Autowired) ValuePicker(io.jmix.ui.component.ValuePicker) Notifications(io.jmix.ui.Notifications) JmixIcon(io.jmix.ui.icon.JmixIcon) Strings(com.google.common.base.Strings) Component(io.jmix.ui.component.Component) UiComponents(io.jmix.ui.UiComponents) ValueClearAction(io.jmix.ui.action.valuepicker.ValueClearAction) io.jmix.ui.screen(io.jmix.ui.screen) VBoxLayout(io.jmix.ui.component.VBoxLayout) Action(io.jmix.ui.action.Action) BaseAction(io.jmix.ui.action.BaseAction) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Named(javax.inject.Named) DialogAction(io.jmix.ui.action.DialogAction) Actions(io.jmix.ui.Actions) BaseAction(io.jmix.ui.action.BaseAction) Component(io.jmix.ui.component.Component)

Example 2 with Component

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

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(io.jmix.ui.component.Frame) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) FrameOwner(io.jmix.ui.screen.FrameOwner) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) Component(io.jmix.ui.component.Component)

Example 3 with Component

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

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(io.jmix.ui.screen.FrameOwner) Method(java.lang.reflect.Method) LegacyFragmentAdapter(com.haulmont.cuba.gui.components.compatibility.LegacyFragmentAdapter) Component(io.jmix.ui.component.Component)

Example 4 with Component

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

the class FieldGroupTest method getComposition.

protected Object getComposition(FieldGroup.FieldConfig fc) {
    Method getCompositionMethod = MethodUtils.getAccessibleMethod(fc.getClass(), "getComponent");
    Object composition;
    try {
        composition = getCompositionMethod.invoke(fc);
    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException("Invoke error", e);
    }
    return ((Component) composition).unwrap(Object.class);
}
Also used : Method(java.lang.reflect.Method) Component(io.jmix.ui.component.Component) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with Component

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

the class LookupFieldTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    LookupField component = create(LookupField.class);
    final AtomicInteger counter = new AtomicInteger(0);
    // noinspection unchecked
    Datasource<User> testDs = new DsBuilder().setId("testDs").setJavaClass(User.class).setView(viewRepository.getFetchPlan(User.class, FetchPlan.LOCAL)).buildDatasource();
    testDs.setItem(new User());
    ((DatasourceImpl) testDs).valid();
    assertNull(component.getValue());
    final Group g = new Group();
    testDs.getItem().setGroup(g);
    // noinspection unchecked
    CollectionDatasource<Group, UUID> groupsDs = new DsBuilder().setId("testDs").setJavaClass(Group.class).setView(viewRepository.getFetchPlan(Group.class, FetchPlan.LOCAL)).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setAllowCommit(false).buildCollectionDatasource();
    groupsDs.includeItem(g);
    Group g1 = new Group();
    groupsDs.includeItem(g1);
    Group g2 = new Group();
    groupsDs.includeItem(g2);
    component.setOptionsDatasource(groupsDs);
    Consumer<HasValue.ValueChangeEvent> listener1 = e -> {
        assertNull(e.getPrevValue());
        assertEquals(g2, e.getValue());
        counter.addAndGet(1);
    };
    Subscription subscription = component.addValueChangeListener(listener1);
    component.setValue(g2);
    subscription.remove();
    assertEquals(1, counter.get());
    Consumer<HasValue.ValueChangeEvent> listener2 = e -> {
        assertEquals(g2, e.getPrevValue());
        assertEquals(g, e.getValue());
        counter.addAndGet(1);
    };
    subscription = component.addValueChangeListener(listener2);
    component.setDatasource(testDs, "group");
    assertEquals(g, component.getValue());
    assertEquals(2, counter.get());
    subscription.remove();
    component.setValue(g1);
    assertEquals(g1, testDs.getItem().getGroup());
    assertEquals(2, counter.get());
    Consumer<HasValue.ValueChangeEvent> listener3 = e -> {
        assertEquals(g1, e.getPrevValue());
        assertEquals(g2, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(listener3);
    testDs.getItem().setGroup(g2);
    assertEquals(g2, component.getValue());
    assertEquals(3, counter.get());
}
Also used : Arrays(java.util.Arrays) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) FetchPlan(io.jmix.core.FetchPlan) UUID(java.util.UUID) Disabled(org.junit.jupiter.api.Disabled) Group(com.haulmont.cuba.core.model.common.Group) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Component(io.jmix.ui.component.Component) HasValue(io.jmix.ui.component.HasValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) Assertions(org.junit.jupiter.api.Assertions) LookupField(com.haulmont.cuba.gui.components.LookupField) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) User(com.haulmont.cuba.core.model.common.User) Group(com.haulmont.cuba.core.model.common.Group) User(com.haulmont.cuba.core.model.common.User) LookupField(com.haulmont.cuba.gui.components.LookupField) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) Subscription(io.jmix.core.common.event.Subscription) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

Component (io.jmix.ui.component.Component)81 GuiDevelopmentException (io.jmix.ui.GuiDevelopmentException)16 Datasource (com.haulmont.cuba.gui.data.Datasource)12 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)11 Test (org.junit.jupiter.api.Test)11 Element (org.dom4j.Element)10 Consumer (java.util.function.Consumer)9 ListComponent (com.haulmont.cuba.gui.components.ListComponent)8 MetaClass (io.jmix.core.metamodel.model.MetaClass)7 Action (io.jmix.ui.action.Action)7 HasValue (io.jmix.ui.component.HasValue)7 FrameOwner (io.jmix.ui.screen.FrameOwner)7 ArrayList (java.util.ArrayList)7 DatasourceImpl (com.haulmont.cuba.gui.data.impl.DatasourceImpl)6 FetchPlan (io.jmix.core.FetchPlan)6 UUID (java.util.UUID)6 Nullable (javax.annotation.Nullable)6 User (com.haulmont.cuba.core.model.common.User)5 DsBuilder (com.haulmont.cuba.gui.data.DsBuilder)5 Entity (io.jmix.core.Entity)5