use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.
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 FetchPlan getViewForField() {
ValueSource valueSource = getValueSource();
if (valueSource instanceof ContainerValueSource) {
ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
InstanceContainer container = containerValueSource.getContainer();
FetchPlan view = container.getFetchPlan();
if (view != null) {
MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
FetchPlan curView = view;
if (metaPropertyPath != null) {
for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
FetchPlanProperty viewProperty = curView.getProperty(metaProperty.getName());
if (viewProperty != null) {
curView = viewProperty.getFetchPlan();
if (curView == null)
return null;
}
}
MetaProperty inverseMetaProperty = metaPropertyPath.getMetaProperty().getInverse();
if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
curView = fetchPlans.builder(curView).add(inverseMetaProperty.getName()).build();
}
}
if (curView != view) {
return curView;
}
}
}
return null;
}
use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.
the class EditorBuilderProcessor method initEntity.
@Nullable
protected <E> E initEntity(EditorBuilder<E> builder, @Nullable CollectionContainer<E> container) {
E entity;
boolean oneToOneComposition = false;
EntityValueSource entityValueSource = null;
HasValue<E> field = builder.getField();
if (field instanceof HasValueSource) {
ValueSource valueSource = ((HasValueSource) field).getValueSource();
if (valueSource instanceof EntityValueSource) {
entityValueSource = (EntityValueSource) valueSource;
oneToOneComposition = isCompositionProperty(entityValueSource);
}
}
if (builder.getMode() == EditMode.CREATE || (oneToOneComposition && field.getValue() == null)) {
if (builder.getNewEntity() == null) {
entity = metadata.create(builder.getEntityClass());
} else {
entity = builder.getNewEntity();
}
if (container instanceof Nested) {
initializeNestedEntity(entity, (Nested) container);
}
if (oneToOneComposition) {
Object ownerEntity = entityValueSource.getItem();
MetaProperty inverseProp = entityValueSource.getMetaPropertyPath().getMetaProperty().getInverse();
if (inverseProp != null) {
EntityValues.setValue(entity, inverseProp.getName(), ownerEntity);
}
}
if (builder.getInitializer() != null) {
builder.getInitializer().accept(entity);
}
} else {
entity = builder.getEditedEntity();
}
return entity;
}
use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.
the class LookupBuilderProcessor method getFetchPlanForField.
/**
* The method evaluates the fetch plan that is used for the entity in the given {@code field}
* <p>
* If the value for a component (e.g. {@link EntityPicker}) is selected from lookup screen then there may be cases
* when in entities in lookup screen some attributes required in the editor are not loaded.
*
* @return a view or {@code null} if the fetch plan cannot be evaluated
*/
@Nullable
protected FetchPlan getFetchPlanForField(HasValue field) {
if (field instanceof HasValueSource) {
ValueSource valueSource = ((HasValueSource) field).getValueSource();
if (valueSource instanceof ContainerValueSource) {
ContainerValueSource containerValueSource = (ContainerValueSource) valueSource;
InstanceContainer container = containerValueSource.getContainer();
FetchPlan fetchPlan = container.getFetchPlan();
if (fetchPlan != null) {
MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
FetchPlan curFetchPlan = fetchPlan;
for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
FetchPlanProperty viewProperty = curFetchPlan.getProperty(metaProperty.getName());
if (viewProperty != null) {
curFetchPlan = viewProperty.getFetchPlan();
}
if (curFetchPlan == null)
break;
}
if (curFetchPlan != fetchPlan) {
return curFetchPlan;
}
}
}
}
return null;
}
use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.
the class EntityFieldCreationSupport method addDefaultActions.
public boolean addDefaultActions(EntityPicker entityPicker) {
ValueSource valueSource = entityPicker.getValueSource();
if (!(valueSource instanceof EntityValueSource)) {
return false;
}
EntityValueSource entityValueSource = (EntityValueSource) entityPicker.getValueSource();
MetaPropertyPath mpp = entityValueSource.getMetaPropertyPath();
MetaClass metaClass = mpp.getMetaProperty().getRange().asClass();
List<String> actionIds = componentProperties.getEntityFieldActions().get(metaClass.getName());
if (actionIds == null || actionIds.isEmpty()) {
return false;
}
addActions(entityPicker, actionIds);
return true;
}
use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.
the class DateFieldImpl method valueBindingConnected.
@Override
protected void valueBindingConnected(ValueSource<V> valueSource) {
super.valueBindingConnected(valueSource);
if (valueSource instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) valueSource;
DataAwareComponentsTools dataAwareComponentsTools = applicationContext.getBean(DataAwareComponentsTools.class);
dataAwareComponentsTools.setupDateFormat(this, entityValueSource);
dataAwareComponentsTools.setupZoneId(this, entityValueSource);
if (valueSourceStateChangeSubscription != null) {
valueSourceStateChangeSubscription.remove();
}
// setup dateRange after valueSource is activated and value is set because
// Vaadin dateField rejects value if it is not in range
valueSourceStateChangeSubscription = valueSource.addStateChangeListener(event -> {
if (event.getState() == BindingState.ACTIVE) {
dataAwareComponentsTools.setupDateRange(this, entityValueSource);
}
});
}
}
Aggregations