use of com.haulmont.cuba.gui.components.LookupPickerField in project cuba by cuba-platform.
the class LookupBuilderProcessor method handleSelectionWithField.
@SuppressWarnings("unchecked")
protected <E extends Entity> void handleSelectionWithField(@SuppressWarnings("unused") LookupBuilder<E> builder, HasValue<E> field, Collection<E> itemsFromLookup) {
if (itemsFromLookup.isEmpty()) {
return;
}
Collection<E> selectedItems = transform(itemsFromLookup, builder);
Entity newValue = selectedItems.iterator().next();
View viewForField = clientConfig.getReloadUnfetchedAttributesFromLookupScreens() && metadataTools.isPersistent(newValue.getClass()) ? getViewForField(field) : null;
if (viewForField != null && !entityStates.isLoadedWithView(newValue, viewForField)) {
newValue = dataManager.reload(newValue, viewForField);
}
if (field instanceof LookupPickerField) {
LookupPickerField lookupPickerField = (LookupPickerField) field;
Options options = lookupPickerField.getOptions();
if (options instanceof EntityOptions) {
EntityOptions entityOptions = (EntityOptions) options;
if (entityOptions.containsItem(newValue)) {
entityOptions.updateItem(newValue);
}
if (lookupPickerField.isRefreshOptionsOnLookupClose()) {
entityOptions.refresh();
}
}
}
// In case of PickerField set the value as if the user had set it
if (field instanceof SupportsUserAction) {
((SupportsUserAction<E>) field).setValueFromUser((E) newValue);
} else {
field.setValue((E) newValue);
}
}
use of com.haulmont.cuba.gui.components.LookupPickerField in project cuba by cuba-platform.
the class LookupPickerFieldLoader method loadComponent.
@Override
public void loadComponent() {
super.loadComponent();
LookupPickerField lookupPickerField = (LookupPickerField) resultComponent;
String metaClass = element.attributeValue("metaClass");
if (!StringUtils.isEmpty(metaClass)) {
lookupPickerField.setMetaClass(getMetadata().getClass(metaClass));
}
loadActions(lookupPickerField, element);
if (lookupPickerField.getActions().isEmpty()) {
GuiActionSupport guiActionSupport = getGuiActionSupport();
boolean actionsByMetaAnnotations = guiActionSupport.createActionsByMetaAnnotations(lookupPickerField);
if (!actionsByMetaAnnotations) {
if (isLegacyFrame()) {
lookupPickerField.addLookupAction();
lookupPickerField.addClearAction();
} else {
Actions actions = getActions();
lookupPickerField.addAction(actions.create(LookupAction.ID));
lookupPickerField.addAction(actions.create(ClearAction.ID));
}
}
}
String refreshOptionsOnLookupClose = element.attributeValue("refreshOptionsOnLookupClose");
if (refreshOptionsOnLookupClose != null) {
lookupPickerField.setRefreshOptionsOnLookupClose(Boolean.parseBoolean(refreshOptionsOnLookupClose));
}
}
use of com.haulmont.cuba.gui.components.LookupPickerField in project cuba by cuba-platform.
the class LookupPickerFieldDsTest method testUnsubscribeSubscribeOptions.
@Test
public void testUnsubscribeSubscribeOptions() {
LookupPickerField component = uiComponents.create(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.components.LookupPickerField in project cuba by cuba-platform.
the class LookupPickerFieldDsTest method testOptionsDsUnsubscribe.
@Test
public void testOptionsDsUnsubscribe() {
LookupPickerField component = uiComponents.create(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.components.LookupPickerField in project cuba by cuba-platform.
the class LookupPickerFieldDsTest method testDatasourceRepeatableAssign.
@Test
public void testDatasourceRepeatableAssign() {
LookupPickerField component = uiComponents.create(LookupPickerField.NAME);
component.setDatasource(null, null);
component.setDatasource(null, null);
Datasource<User> userDs1 = getTestUserDatasource();
boolean exceptionWasThrown = false;
try {
component.setDatasource(userDs1, null);
} catch (Exception e) {
exceptionWasThrown = true;
}
assertTrue(exceptionWasThrown);
exceptionWasThrown = false;
try {
component.setDatasource(null, "group");
} catch (Exception e) {
exceptionWasThrown = true;
}
assertTrue(exceptionWasThrown);
component.setDatasource(userDs1, "group");
component.setDatasource(userDs1, "group");
userDs1.getItem().setGroup(metadata.create(Group.class));
component.setDatasource(userDs1, "group");
Datasource<User> userDs2 = getTestUserDatasource();
component.setDatasource(userDs2, "group");
component.setValue(null);
assertNotNull(userDs1.getItem().getGroup());
}
Aggregations