use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class LookupFieldDsTest method testUnsubscribeSubscribeComponentListener.
@Test
public void testUnsubscribeSubscribeComponentListener() {
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);
// setup
boolean[] valueWasChanged = { false };
Component.ValueChangeListener listener = e -> valueWasChanged[0] = true;
// datasource before listener
lookupField.setDatasource(userDs, "group");
lookupField.addValueChangeListener(listener);
user.setGroup(metadata.create(Group.class));
assertEquals(true, valueWasChanged[0]);
// reset state
valueWasChanged[0] = false;
lookupField.setDatasource(null, null);
lookupField.removeValueChangeListener(listener);
lookupField.setValue(null);
// listener before datasource
lookupField.addValueChangeListener(listener);
lookupField.setDatasource(userDs, "group");
assertEquals(true, valueWasChanged[0]);
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class LookupPickerFieldDsTest method testUnsubscribeSubscribeOptions.
@Test
public void testUnsubscribeSubscribeOptions() {
LookupPickerField component = (LookupPickerField) factory.createComponent(LookupPickerField.NAME);
CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
component.setOptionsDatasource(groupsDs);
List<Group> groups = new ArrayList<>(groupsDs.getItems());
Datasource<User> userDs = getTestUserDatasource();
User user = userDs.getItem();
user.setGroup(groups.get(0));
component.setDatasource(userDs, "group");
// unbind
component.setOptionsDatasource(null);
Datasource.ItemChangeListener<Group> listener = e -> {
throw new RuntimeException("Value was changed externally");
};
groupsDs.addItemChangeListener(listener);
component.setValue(groups.get(1));
// bind
component.setOptionsDatasource(groupsDs);
groupsDs.removeItemChangeListener(listener);
boolean[] valueWasChanged = { false };
listener = e -> valueWasChanged[0] = true;
groupsDs.addItemChangeListener(listener);
component.setValue(groups.get(2));
assertEquals(true, valueWasChanged[0]);
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class LookupPickerFieldDsTest method testOptionsDsUnsubscribe.
@Test
public void testOptionsDsUnsubscribe() {
LookupPickerField component = (LookupPickerField) factory.createComponent(LookupPickerField.NAME);
CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
component.setOptionsDatasource(groupsDs);
List<Group> groups = new ArrayList<>(groupsDs.getItems());
Datasource<User> userDs = getTestUserDatasource();
User user = userDs.getItem();
user.setGroup(groups.get(0));
component.setDatasource(userDs, "group");
// unbind
component.setOptionsDatasource(null);
Datasource.ItemChangeListener<Group> listener = e -> {
throw new RuntimeException("Value was changed externally");
};
groupsDs.addItemChangeListener(listener);
component.setValue(groups.get(1));
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class SearchFieldDsTest method testValueChangeListener.
@Test
public void testValueChangeListener() {
SearchField searchField = factory.createComponent(SearchField.class);
CollectionDatasource<Group, UUID> groupsDs = getTestCollectionDatasource();
searchField.setOptionsDatasource(groupsDs);
List<Group> groups = new ArrayList<>(groupsDs.getItems());
Datasource<User> userDs = getTestUserDatasource();
User user = userDs.getItem();
user.setGroup(groups.get(0));
boolean[] valueWasChanged = { false };
Component.ValueChangeListener listener = e -> valueWasChanged[0] = true;
searchField.addValueChangeListener(listener);
searchField.setDatasource(userDs, "group");
assertEquals(true, valueWasChanged[0]);
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class DynamicAttributesGuiTools method createOptionsDatasourceForLookup.
/**
* Creates the collection datasource that is used for selecting the dynamic attribute value. If the
* CategoryAttribute has "where" or "join" clauses then only items that satisfy these clauses will be presented in
* the options datasource
*/
public CollectionDatasource createOptionsDatasourceForLookup(MetaClass metaClass, String joinClause, String whereClause) {
CollectionDatasource optionsDatasource = DsBuilder.create().setMetaClass(metaClass).setViewName(View.MINIMAL).buildCollectionDatasource();
String query = "select e from " + metaClass.getName() + " e";
if (!Strings.isNullOrEmpty(joinClause)) {
query += " " + joinClause;
}
if (!Strings.isNullOrEmpty(whereClause)) {
query += " where " + whereClause.replaceAll("\\{E\\}", "e");
}
optionsDatasource.setQuery(query);
optionsDatasource.refresh();
return optionsDatasource;
}
Aggregations