use of com.haulmont.cuba.gui.components.data.ValueSource in project cuba by cuba-platform.
the class AbstractComponentGenerationStrategy method createEntityField.
@SuppressWarnings("unchecked")
protected Component createEntityField(ComponentGenerationContext context, MetaPropertyPath mpp) {
String linkAttribute = null;
Element xmlDescriptor = context.getXmlDescriptor();
if (xmlDescriptor != null) {
linkAttribute = xmlDescriptor.attributeValue("link");
}
if (!Boolean.parseBoolean(linkAttribute)) {
Options options = context.getOptions();
boolean useOptionsLoader = false;
if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
CategoryAttribute attribute = metaProperty.getAttribute();
CategoryAttributeConfiguration configuration = attribute.getConfiguration();
if (Boolean.TRUE.equals(attribute.getLookup()) && configuration.hasOptionsLoader()) {
useOptionsLoader = true;
}
}
PickerField pickerField;
if (options == null && !useOptionsLoader) {
pickerField = uiComponents.create(PickerField.class);
setValueSource(pickerField, context);
if (mpp.getMetaProperty().getType() == MetaProperty.Type.ASSOCIATION) {
guiActionSupport.createActionById(pickerField, PickerField.ActionType.LOOKUP.getId());
if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
DynamicAttributesMetaProperty dynamicAttributesMetaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
getDynamicAttributesGuiTools().initEntityPickerField(pickerField, dynamicAttributesMetaProperty.getAttribute());
}
boolean actionsByMetaAnnotations = guiActionSupport.createActionsByMetaAnnotations(pickerField);
if (!actionsByMetaAnnotations) {
guiActionSupport.createActionById(pickerField, PickerField.ActionType.CLEAR.getId());
}
} else {
guiActionSupport.createActionById(pickerField, PickerField.ActionType.OPEN.getId());
guiActionSupport.createActionById(pickerField, PickerField.ActionType.CLEAR.getId());
}
} else {
LookupPickerField lookupPickerField = uiComponents.create(LookupPickerField.class);
setValueSource(lookupPickerField, context);
if (useOptionsLoader) {
DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
CategoryAttribute attribute = metaProperty.getAttribute();
ValueSource valueSource = context.getValueSource();
if (valueSource instanceof ContainerValueSource) {
setOptionsLoader(attribute, lookupPickerField, (ContainerValueSource) valueSource);
}
} else {
lookupPickerField.setOptions(options);
}
pickerField = lookupPickerField;
guiActionSupport.createActionsByMetaAnnotations(pickerField);
}
if (xmlDescriptor != null) {
String captionProperty = xmlDescriptor.attributeValue("captionProperty");
if (StringUtils.isNotEmpty(captionProperty)) {
pickerField.setCaptionMode(CaptionMode.PROPERTY);
pickerField.setCaptionProperty(captionProperty);
}
}
setValidators(pickerField, context);
return pickerField;
} else {
EntityLinkField linkField = uiComponents.create(EntityLinkField.class);
setValueSource(linkField, context);
setLinkFieldAttributes(linkField, context);
return linkField;
}
}
use of com.haulmont.cuba.gui.components.data.ValueSource in project cuba by cuba-platform.
the class AbstractComponentGenerationStrategy method setOptionsLoader.
protected void setOptionsLoader(CategoryAttribute categoryAttribute, LookupField lookupField, ContainerValueSource valueSource) {
InstanceContainer<?> container = valueSource.getContainer();
Entity entity = container.getItemOrNull();
if (entity != null) {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) entity, categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
}
container.addItemChangeListener(e -> {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
});
List<CategoryAttribute> dependsOnAttributes = categoryAttribute.getConfiguration().getDependsOnAttributes();
if (dependsOnAttributes != null && !dependsOnAttributes.isEmpty()) {
List<String> dependsOnAttributesCodes = dependsOnAttributes.stream().map(a -> DynamicAttributesUtils.encodeAttributeCode(a.getCode())).collect(Collectors.toList());
container.addItemPropertyChangeListener(e -> {
if (dependsOnAttributesCodes.contains(e.getProperty())) {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
if (!options.contains(lookupField.getValue())) {
// noinspection unchecked
lookupField.setValue(null);
}
}
});
}
}
use of com.haulmont.cuba.gui.components.data.ValueSource in project cuba by cuba-platform.
the class GuiActionSupport method createActionsByMetaAnnotations.
/**
* Adds actions specified in {@link Lookup} annotation on entity attribute to the given PickerField.
*
* @param pickerField field
* @return true if actions have been added
*/
public boolean createActionsByMetaAnnotations(PickerField pickerField) {
ValueSource valueSource = pickerField.getValueSource();
if (!(valueSource instanceof EntityValueSource)) {
return false;
}
EntityValueSource entityValueSource = (EntityValueSource) pickerField.getValueSource();
MetaPropertyPath mpp = entityValueSource.getMetaPropertyPath();
if (mpp == null) {
return false;
}
String[] actionIds = (String[]) metadataTools.getMetaAnnotationAttributes(mpp.getMetaProperty().getAnnotations(), Lookup.class).get("actions");
if (actionIds != null && actionIds.length > 0) {
for (String actionId : actionIds) {
createActionById(pickerField, actionId);
}
return true;
}
return false;
}
use of com.haulmont.cuba.gui.components.data.ValueSource in project cuba by cuba-platform.
the class EditorBuilderProcessor method buildEditor.
@SuppressWarnings("unchecked")
public <E extends Entity, S extends Screen> S buildEditor(EditorBuilder<E> builder) {
FrameOwner origin = builder.getOrigin();
Screens screens = getScreenContext(origin).getScreens();
ListComponent<E> listComponent = builder.getListComponent();
CollectionContainer<E> container = builder.getContainer();
if (container == null && listComponent != null) {
DataUnit items = listComponent.getItems();
container = items instanceof ContainerDataUnit ? ((ContainerDataUnit) items).getContainer() : null;
}
E entity = initEntity(builder, container);
if (builder.getMode() == EditMode.EDIT && entity == null) {
throw new IllegalStateException(String.format("Editor of %s cannot be open with mode EDIT, entity is not set", builder.getEntityClass()));
}
Screen screen = createScreen(builder, screens, entity);
EditorScreen<E> editorScreen = (EditorScreen<E>) screen;
editorScreen.setEntityToEdit(entity);
DataContext parentDataContext = setupParentDataContext(origin, screen, container, builder.getParentDataContext());
if (container != null) {
CollectionContainer<E> ct = container;
screen.addAfterCloseListener(event -> {
CloseAction closeAction = event.getCloseAction();
if (isCommitCloseAction(closeAction)) {
E entityFromEditor = getCommittedEntity(editorScreen, parentDataContext);
E reloadedEntity = reloadIfNeeded(entityFromEditor, ct, builder);
E committedEntity = transform(reloadedEntity, builder);
E mergedEntity = merge(committedEntity, origin, parentDataContext);
if (builder.getMode() == EditMode.CREATE) {
boolean addsFirst;
if (!(ct instanceof Nested)) {
addsFirst = clientConfig.getCreateActionAddsFirst();
if (builder.getAddFirst() != null) {
addsFirst = builder.getAddFirst();
}
} else {
addsFirst = false;
}
if (ct instanceof Nested || !addsFirst) {
ct.getMutableItems().add(mergedEntity);
} else {
ct.getMutableItems().add(0, mergedEntity);
}
} else {
ct.replaceItem(mergedEntity);
}
}
if (listComponent instanceof com.haulmont.cuba.gui.components.Component.Focusable) {
((com.haulmont.cuba.gui.components.Component.Focusable) listComponent).focus();
}
});
}
HasValue<E> field = builder.getField();
if (field != null) {
if (parentDataContext == null && field instanceof HasValueSource) {
ValueSource fieldValueSource = ((HasValueSource) field).getValueSource();
if (fieldValueSource instanceof EntityValueSource) {
if (isCompositionProperty((EntityValueSource) fieldValueSource)) {
DataContext thisDataContext = UiControllerUtils.getScreenData(origin).getDataContext();
DataContext dataContext = UiControllerUtils.getScreenData(screen).getDataContext();
checkDataContext(screen, dataContext);
dataContext.setParent(thisDataContext);
}
}
}
screen.addAfterCloseListener(event -> {
CloseAction closeAction = event.getCloseAction();
if (isCommitCloseAction(closeAction)) {
E entityFromEditor = editorScreen.getEditedEntity();
E editedEntity = transform(entityFromEditor, builder);
if (field instanceof LookupPickerField) {
LookupPickerField lookupPickerField = ((LookupPickerField) field);
Options options = lookupPickerField.getOptions();
if (options instanceof EntityOptions) {
EntityOptions entityOptions = (EntityOptions) options;
if (entityOptions.containsItem(editedEntity)) {
entityOptions.updateItem(editedEntity);
}
}
}
if (field instanceof SupportsUserAction) {
((SupportsUserAction) field).setValueFromUser(editedEntity);
} else {
field.setValue(editedEntity);
}
}
if (field instanceof com.haulmont.cuba.gui.components.Component.Focusable) {
((com.haulmont.cuba.gui.components.Component.Focusable) field).focus();
}
});
}
if (builder instanceof EditorClassBuilder) {
@SuppressWarnings("unchecked") Consumer<AfterScreenCloseEvent> closeListener = ((EditorClassBuilder) builder).getCloseListener();
if (closeListener != null) {
screen.addAfterCloseListener(new AfterCloseListenerAdapter(closeListener));
}
}
return (S) screen;
}
use of com.haulmont.cuba.gui.components.data.ValueSource in project cuba by cuba-platform.
the class WebTokenList method getViewForField.
/**
* If the value for a component is selected from the lookup screen there may be cases when lookup screen contains a list of entities loaded with a
* view that doesn't contain all attributes, required by the token list.
* <p>
* The method evaluates the view that was is for loading entities in the token list
*
* @return a view or null if the view cannot be evaluated
*/
@Nullable
protected View getViewForField() {
ValueSource valueSource = getValueSource();
if (valueSource instanceof ContainerValueSource) {
ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
InstanceContainer container = containerValueSource.getContainer();
View view = container.getView();
if (view != null) {
MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
View curView = view;
if (metaPropertyPath != null) {
for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
ViewProperty viewProperty = curView.getProperty(metaProperty.getName());
if (viewProperty != null) {
curView = viewProperty.getView();
if (curView == null)
return null;
}
}
MetaProperty inverseMetaProperty = metaPropertyPath.getMetaProperty().getInverse();
if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
curView.addProperty(inverseMetaProperty.getName());
}
}
if (curView != view) {
return curView;
}
}
}
return null;
}
Aggregations