use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class DesktopTokenList method getMasterEntity.
@Nullable
protected Entity getMasterEntity(CollectionDatasource datasource) {
if (datasource instanceof NestedDatasource) {
Datasource masterDs = ((NestedDatasource) datasource).getMaster();
com.google.common.base.Preconditions.checkState(masterDs != null);
return masterDs.getItem();
}
return null;
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class WebTokenList method getMasterEntity.
@Nullable
protected Entity getMasterEntity(CollectionDatasource datasource) {
if (datasource instanceof NestedDatasource) {
Datasource masterDs = ((NestedDatasource) datasource).getMaster();
com.google.common.base.Preconditions.checkState(masterDs != null);
return masterDs.getItem();
}
return null;
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class WebTokenList method setLookup.
@Override
public void setLookup(boolean lookup) {
if (this.lookup != lookup) {
if (lookup) {
lookupAction = new PickerField.LookupAction(lookupPickerField) {
@Nonnull
@Override
protected Map<String, Object> prepareScreenParams() {
Map<String, Object> screenParams = super.prepareScreenParams();
if (isMultiSelect()) {
screenParams = new HashMap<>(screenParams);
WindowParams.MULTI_SELECT.set(screenParams, true);
// for backward compatibility
screenParams.put("multiSelect", "true");
}
return screenParams;
}
@SuppressWarnings("unchecked")
@Override
protected void handleLookupWindowSelection(Collection items) {
if (items.isEmpty()) {
return;
}
@SuppressWarnings("unchecked") Collection<Entity> selected = items;
CollectionDatasource optionsDatasource = lookupPickerField.getOptionsDatasource();
if (optionsDatasource != null && lookupPickerField.isRefreshOptionsOnLookupClose()) {
optionsDatasource.refresh();
if (datasource != null) {
for (Object obj : getDatasource().getItems()) {
Entity entity = (Entity) obj;
if (getOptionsDatasource().containsItem(entity.getId())) {
datasource.updateItem(getOptionsDatasource().getItem(entity.getId()));
}
}
}
}
// add all selected items to tokens
if (itemChangeHandler != null) {
for (Entity newItem : selected) {
itemChangeHandler.addItem(newItem);
}
} else if (datasource != null) {
// get master entity and inverse attribute in case of nested datasource
Entity masterEntity = getMasterEntity(datasource);
MetaProperty inverseProp = getInverseProperty(datasource);
for (Entity newItem : selected) {
if (!datasource.containsItem(newItem.getId())) {
// Initialize reference to master entity
if (inverseProp != null && isInitializeMasterReference(inverseProp)) {
newItem.setValue(inverseProp.getName(), masterEntity);
}
datasource.addItem(newItem);
}
}
}
afterSelect(items);
if (afterLookupSelectionHandler != null) {
afterLookupSelectionHandler.onSelect(items);
}
}
};
lookupPickerField.addAction(lookupAction);
if (getLookupScreen() != null) {
lookupAction.setLookupScreen(getLookupScreen());
}
lookupAction.setLookupScreenOpenType(lookupOpenMode);
lookupAction.setLookupScreenParams(lookupScreenParams);
lookupAction.setLookupScreenDialogParams(lookupScreenDialogParams);
} else {
lookupPickerField.removeAction(lookupAction);
}
lookupAction.setAfterLookupCloseHandler((window, actionId) -> {
if (afterLookupCloseHandler != null) {
afterLookupCloseHandler.onClose(window, actionId);
}
});
lookupAction.setAfterLookupSelectionHandler(items -> {
if (afterLookupSelectionHandler != null) {
afterLookupSelectionHandler.onSelect(items);
}
});
}
this.lookup = lookup;
component.refreshComponent();
}
use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.
the class LookupPickerFieldTest method testValueLoadFromOptions.
@Test
public void testValueLoadFromOptions() {
LookupPickerField component = factory.createComponent(LookupPickerField.class);
// noinspection unchecked
Datasource<User> testDs = new DsBuilder().setId("testDs").setJavaClass(User.class).setView(viewRepository.getView(User.class, View.LOCAL)).buildDatasource();
testDs.setItem(new User());
((DatasourceImpl) testDs).valid();
assertNull(component.getValue());
Group g = new Group();
testDs.getItem().setGroup(g);
// noinspection unchecked
CollectionDatasource<Group, UUID> groupsDs = new DsBuilder().setId("testDs").setJavaClass(Group.class).setView(viewRepository.getView(Group.class, View.LOCAL)).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setAllowCommit(false).buildCollectionDatasource();
groupsDs.includeItem(g);
Group g1 = new Group();
g1.setId(g.getId());
groupsDs.includeItem(g1);
Group g2 = new Group();
groupsDs.includeItem(g2);
component.setOptionsDatasource(groupsDs);
component.setDatasource(testDs, "group");
assertTrue("Value should be from options ds", g1 == component.getValue());
component.setValue(g2);
Component.ValueChangeListener listener1 = e -> {
assertEquals(g2, e.getPrevValue());
assertEquals(g1, e.getValue());
};
component.addValueChangeListener(listener1);
component.setValue(g);
}
use of com.haulmont.cuba.gui.data.CollectionDatasource 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]);
}
Aggregations