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]);
}
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());
}
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);
}
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);
}
}
}
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());
}
Aggregations