Search in sources :

Example 16 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class TextFieldDsTest method testValueChangeListener.

@Test
public void testValueChangeListener() {
    TextField textField = uiComponents.create(TextField.NAME);
    Datasource<User> userDs = createTestDatasource(User.class);
    userDs.getItem().setName("testName");
    // listener before datasource
    boolean[] valueWasChanged = { false };
    Consumer<HasValue.ValueChangeEvent> listener = e -> valueWasChanged[0] = true;
    Subscription subscription = textField.addValueChangeListener(listener);
    textField.setDatasource(userDs, "name");
    assertTrue(valueWasChanged[0]);
    // reset state
    valueWasChanged[0] = false;
    subscription.remove();
    textField.setDatasource(null, null);
    // datasource before listener
    textField.setDatasource(userDs, "name");
    textField.addValueChangeListener(listener);
    userDs.getItem().setName("anotherName");
    assertTrue(valueWasChanged[0]);
}
Also used : Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) HasValue(io.jmix.ui.component.HasValue) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TextField(com.haulmont.cuba.gui.components.TextField) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.core.model.common.User) User(com.haulmont.cuba.core.model.common.User) TextField(com.haulmont.cuba.gui.components.TextField) Subscription(io.jmix.core.common.event.Subscription) Test(org.junit.jupiter.api.Test)

Example 17 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class DateFieldTest method testValueChangeListener.

@Test
public void testValueChangeListener() throws ParseException {
    DateField component = create(DateField.class);
    AtomicInteger counter = new AtomicInteger(0);
    Date value1 = new SimpleDateFormat("dd.MM.yyyy").parse("12.12.2000");
    Consumer<HasValue.ValueChangeEvent> okListener = e -> {
        assertNull(e.getPrevValue());
        assertEquals(value1, e.getValue());
        counter.addAndGet(1);
    };
    Subscription subscription = component.addValueChangeListener(okListener);
    component.setValue(value1);
    assertEquals(1, counter.get());
    subscription.remove();
    Date value2 = new SimpleDateFormat("dd.MM.yyyy").parse("10.10.2000");
    component.setValue(value2);
    assertEquals(1, counter.get());
    // 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();
    Consumer<HasValue.ValueChangeEvent> dsLoadListener = e -> {
        assertEquals(value2, e.getPrevValue());
        assertNull(e.getValue());
        counter.addAndGet(1);
    };
    subscription = component.addValueChangeListener(dsLoadListener);
    component.setDatasource(testDs, "createTs");
    assertEquals(2, counter.get());
    subscription.remove();
    Date value3 = new SimpleDateFormat("dd.MM.yyyy").parse("01.01.2000");
    Consumer<HasValue.ValueChangeEvent> dsListener = e -> {
        assertNull(e.getPrevValue());
        assertEquals(value3, e.getValue());
        counter.addAndGet(1);
    };
    component.addValueChangeListener(dsListener);
    testDs.getItem().setCreateTs(value3);
    assertEquals(3, counter.get());
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Date(java.util.Date) FetchPlan(io.jmix.core.FetchPlan) SimpleDateFormat(java.text.SimpleDateFormat) Disabled(org.junit.jupiter.api.Disabled) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Component(io.jmix.ui.component.Component) HasValue(io.jmix.ui.component.HasValue) DateField(com.haulmont.cuba.gui.components.DateField) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) Assertions(org.junit.jupiter.api.Assertions) ParseException(java.text.ParseException) User(com.haulmont.cuba.core.model.common.User) User(com.haulmont.cuba.core.model.common.User) DatasourceImpl(com.haulmont.cuba.gui.data.impl.DatasourceImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DateField(com.haulmont.cuba.gui.components.DateField) Subscription(io.jmix.core.common.event.Subscription) SimpleDateFormat(java.text.SimpleDateFormat) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 18 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class FilterDelegateImpl method addConditionListener.

protected void addConditionListener(AbstractCondition condition, Subscription current) {
    if (conditionListeners == null) {
        conditionListeners = new HashMap<>();
    }
    AbstractCondition.Listener listener = new AbstractCondition.Listener() {

        protected Subscription previous = current;

        @Override
        public void captionChanged() {
        // do nothing
        }

        @Override
        public void paramChanged(Param oldParam, Param newParam) {
            previous.remove();
            paramValueChangeSubscriptions.remove(previous);
            Subscription newSubscription = newParam.addParamValueChangeListener(event -> applyWithImmediateMode());
            paramValueChangeSubscriptions.add(newSubscription);
            previous = newSubscription;
            applyWithImmediateMode();
        }
    };
    condition.addListener(listener);
    conditionListeners.put(condition, listener);
}
Also used : Subscription(io.jmix.core.common.event.Subscription) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition)

Example 19 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class FilterDelegateImpl method subscribeToParamValueChangeEventRecursively.

protected void subscribeToParamValueChangeEventRecursively(List<Node<AbstractCondition>> conditions) {
    if (paramValueChangeSubscriptions == null) {
        paramValueChangeSubscriptions = new ArrayList<>();
    }
    for (Node<AbstractCondition> node : conditions) {
        AbstractCondition condition = node.getData();
        if (condition.isGroup()) {
            subscribeToParamValueChangeEventRecursively(node.getChildren());
        } else {
            Subscription subscription = condition.getParam().addParamValueChangeListener(event -> applyWithImmediateMode());
            paramValueChangeSubscriptions.add(subscription);
            addConditionListener(condition, subscription);
        }
    }
}
Also used : Subscription(io.jmix.core.common.event.Subscription) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition)

Example 20 with Subscription

use of io.jmix.core.common.event.Subscription in project jmix by jmix-framework.

the class CheckBoxDsTest method testUnsubscribeSubscribeComponentListener.

@Test
public void testUnsubscribeSubscribeComponentListener() {
    CheckBox checkBox = uiComponents.create(CheckBox.NAME);
    Datasource<User> userDs = createTestDatasource(User.class);
    User user = userDs.getItem();
    user.setActive(true);
    checkBox.setDatasource(userDs, "active");
    // unbind
    checkBox.setDatasource(null, null);
    // datasource before listener
    checkBox.setDatasource(userDs, "active");
    assertEquals(true, checkBox.getValue());
    boolean[] valueWasChanged = { false };
    Consumer listener = e -> valueWasChanged[0] = true;
    Subscription valueChangeListenerSub = checkBox.addValueChangeListener(listener);
    user.setActive(false);
    assertTrue(valueWasChanged[0]);
    assertEquals(false, checkBox.getValue());
    // reset state
    valueChangeListenerSub.remove();
    checkBox.setDatasource(null, null);
    valueWasChanged[0] = false;
    checkBox.setValue(true);
    // listener before datasource
    checkBox.addValueChangeListener(listener);
    checkBox.setDatasource(userDs, "active");
    assertTrue(valueWasChanged[0]);
    assertEquals(false, checkBox.getValue());
}
Also used : Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Datasource(com.haulmont.cuba.gui.data.Datasource) Subscription(io.jmix.core.common.event.Subscription) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Disabled(org.junit.jupiter.api.Disabled) User(com.haulmont.cuba.core.model.common.User) User(com.haulmont.cuba.core.model.common.User) Consumer(java.util.function.Consumer) CheckBox(com.haulmont.cuba.gui.components.CheckBox) Subscription(io.jmix.core.common.event.Subscription) Test(org.junit.jupiter.api.Test)

Aggregations

Subscription (io.jmix.core.common.event.Subscription)26 Datasource (com.haulmont.cuba.gui.data.Datasource)24 Consumer (java.util.function.Consumer)24 Disabled (org.junit.jupiter.api.Disabled)24 Test (org.junit.jupiter.api.Test)24 HasValue (io.jmix.ui.component.HasValue)22 User (com.haulmont.cuba.core.model.common.User)20 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)18 Assertions (org.junit.jupiter.api.Assertions)10 Group (com.haulmont.cuba.core.model.common.Group)9 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)7 UUID (java.util.UUID)7 ArrayList (java.util.ArrayList)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)6 UiComponents (com.haulmont.cuba.gui.UiComponents)5 Date (java.util.Date)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 Role (com.haulmont.cuba.core.model.common.Role)4