use of com.haulmont.cuba.gui.components.LookupField in project cuba by cuba-platform.
the class LookupFieldDsTest method testUnsubscribeSubscribeOptions.
@Test
public void testUnsubscribeSubscribeOptions() {
LookupField lookupField = factory.createComponent(LookupField.class);
CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
lookupField.setOptionsDatasource(groupsDs);
List<Group> groups = new ArrayList<>(groupsDs.getItems());
Datasource<User> userDs = getTestUserDatasource();
userDs.getItem().setGroup(groups.get(0));
lookupField.setDatasource(userDs, "group");
// unbind
lookupField.setOptionsDatasource(null);
Datasource.ItemChangeListener<Group> listener = e -> {
throw new RuntimeException("Value was changed externally");
};
groupsDs.addItemChangeListener(listener);
lookupField.setValue(groups.get(1));
// setup
groupsDs.removeItemChangeListener(listener);
boolean[] valueWasChanged = { false };
listener = e -> valueWasChanged[0] = true;
groupsDs.addItemChangeListener(listener);
lookupField.setOptionsDatasource(groupsDs);
lookupField.setValue(groups.get(2));
assertEquals(true, valueWasChanged[0]);
}
use of com.haulmont.cuba.gui.components.LookupField in project cuba by cuba-platform.
the class LookupFieldDsTest method testUnsubscribeComponentListener.
@Test
public void testUnsubscribeComponentListener() {
LookupField lookupField = factory.createComponent(LookupField.class);
CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
lookupField.setOptionsDatasource(groupsDs);
Datasource<User> userDs = getTestUserDatasource();
Group group = groupsDs.getItems().iterator().next();
User user = userDs.getItem();
user.setGroup(group);
lookupField.setDatasource(userDs, "group");
// unbind
lookupField.setDatasource(null, null);
Component.ValueChangeListener listener = e -> {
throw new RuntimeException("Value was changed externally");
};
lookupField.addValueChangeListener(listener);
user.setGroup(metadata.create(Group.class));
}
use of com.haulmont.cuba.gui.components.LookupField in project cuba by cuba-platform.
the class LookupFieldDsTest method testUnsubscribeSubscribeDsListener.
@Test
public void testUnsubscribeSubscribeDsListener() {
LookupField lookupField = factory.createComponent(LookupField.class);
CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
lookupField.setOptionsDatasource(groupsDs);
Datasource<User> userDs = getTestUserDatasource();
Group group = groupsDs.getItems().iterator().next();
userDs.getItem().setGroup(group);
lookupField.setDatasource(userDs, "group");
// unbind
lookupField.setDatasource(null, null);
// setup
boolean[] valueWasChanged = { false };
Datasource.ItemPropertyChangeListener<User> listener = e -> valueWasChanged[0] = true;
userDs.addItemPropertyChangeListener(listener);
lookupField.setDatasource(userDs, "group");
lookupField.setValue(null);
assertEquals(true, valueWasChanged[0]);
}
use of com.haulmont.cuba.gui.components.LookupField in project cuba by cuba-platform.
the class LookupFieldLoader method loadDatasource.
@Override
protected void loadDatasource(DatasourceComponent component, Element element) {
super.loadDatasource(component, element);
String datasource = element.attributeValue("optionsDatasource");
if (!StringUtils.isEmpty(datasource)) {
Datasource ds = context.getDsContext().get(datasource);
((LookupField) component).setOptionsDatasource((CollectionDatasource) ds);
}
}
Aggregations