use of com.haulmont.cuba.gui.components.data.DataUnit in project cuba by cuba-platform.
the class ScreenBuilders method lookup.
/**
* Creates a screen builder using list component.
* <p>
* Example of building a lookup screen for adding row to table / tree component:
* <pre>{@code
* SomeCustomerListScreen screen = screenBuilders.lookup(customersTable)
* .withScreen(SomeCustomerListScreen.class)
* .build();
* }</pre>
*
* @param listComponent {@link Table}, {@link DataGrid} or another component containing the list of entities
* @param <E> type of entity
* @see #lookup(Class, FrameOwner)
*/
public <E extends Entity> LookupBuilder<E> lookup(ListComponent<E> listComponent) {
checkNotNullArgument(listComponent);
checkNotNullArgument(listComponent.getFrame());
FrameOwner frameOwner = listComponent.getFrame().getFrameOwner();
Class<E> entityClass;
DataUnit items = listComponent.getItems();
if (items instanceof EntityDataUnit) {
entityClass = ((EntityDataUnit) items).getEntityMetaClass().getJavaClass();
} else {
throw new IllegalStateException(String.format("Component %s is not bound to data", listComponent));
}
LookupBuilder<E> builder = new LookupBuilder<>(frameOwner, entityClass, lookupBuilderProcessor::buildLookup);
builder.withListComponent(listComponent);
return builder;
}
use of com.haulmont.cuba.gui.components.data.DataUnit 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.DataUnit in project cuba by cuba-platform.
the class WebFilterHelper method initTableFtsTooltips.
@Override
public void initTableFtsTooltips(ListComponent listComponent, MetaClass metaClass, String searchTerm) {
FtsFilterHelper ftsFilterHelper;
if (beanLocator.containsBean(FtsFilterHelper.NAME)) {
ftsFilterHelper = beanLocator.get(FtsFilterHelper.class);
} else {
return;
}
if (listComponent instanceof Table) {
Map<Object, String> tooltipsCache = new HashMap<>();
Map<Object, String> metaClassesCache = new HashMap<>();
listComponent.withUnwrapped(com.vaadin.v7.ui.Table.class, vTable -> vTable.setItemDescriptionGenerator((source, itemId, propertyId) -> {
return tooltipsCache.computeIfAbsent(itemId, k -> {
if (k instanceof GroupInfo) {
return "";
}
return ftsFilterHelper.buildTableTooltip(metaClassesCache.computeIfAbsent(itemId, id -> {
DataUnit dataUnit = listComponent.getItems();
Entity<?> entity = null;
if (dataUnit instanceof DatasourceDataUnit) {
// legacy GUI
entity = ((DatasourceDataUnit) dataUnit).getDatasource().getItem(id);
} else if (dataUnit instanceof ContainerDataUnit) {
entity = ((ContainerDataUnit) dataUnit).getContainer().getItem(id);
}
if (entity != null)
return entity.getMetaClass().getName();
return metaClass.getName();
}), k, searchTerm);
});
}));
} else if (listComponent instanceof DataGrid) {
((DataGrid) listComponent).setRowDescriptionProvider(o -> {
if (o instanceof Entity) {
Entity entity = (Entity) o;
return ftsFilterHelper.buildTableTooltip(entity.getMetaClass().getName(), entity.getId(), searchTerm);
} else {
return null;
}
}, ContentMode.HTML);
}
}
use of com.haulmont.cuba.gui.components.data.DataUnit in project cuba by cuba-platform.
the class RemoveOperation method builder.
/**
* Creates a remove builder using list component, e.g. {@link Table} or {@link DataGrid}.
*
* @param listComponent list component
* @param <E> type of entity
*/
public <E extends Entity> RemoveBuilder<E> builder(ListComponent<E> listComponent) {
checkNotNullArgument(listComponent);
checkNotNullArgument(listComponent.getFrame());
FrameOwner frameOwner = listComponent.getFrame().getFrameOwner();
Class<E> entityClass;
DataUnit items = listComponent.getItems();
if (items instanceof ContainerDataUnit) {
entityClass = ((ContainerDataUnit) items).getEntityMetaClass().getJavaClass();
} else {
throw new IllegalStateException(String.format("Component %s is not bound to data", listComponent));
}
return builder(entityClass, frameOwner).withListComponent(listComponent);
}
use of com.haulmont.cuba.gui.components.data.DataUnit in project cuba by cuba-platform.
the class ScreenBuilders method editor.
/**
* Creates a screen builder using list component.
* <p>
* Example of building a screen for editing a currently selected entity:
* <pre>{@code
* SomeCustomerEditor screen = screenBuilders.editor(customersTable)
* .withScreen(SomeCustomerEditor.class)
* .build();
* }</pre>
* <p>
* Example of building a screen for creating a new entity instance:
* <pre>{@code
* SomeCustomerEditor screen = screenBuilders.editor(customersTable)
* .withScreen(SomeCustomerEditor.class)
* .newEntity()
* .build();
* }</pre>
*
* @param listComponent {@link Table}, {@link DataGrid} or another component containing the list of entities
* @see #editor(Class, FrameOwner)
*/
public <E extends Entity> EditorBuilder<E> editor(ListComponent<E> listComponent) {
checkNotNullArgument(listComponent);
checkNotNullArgument(listComponent.getFrame());
FrameOwner frameOwner = listComponent.getFrame().getFrameOwner();
Class<E> entityClass;
DataUnit items = listComponent.getItems();
if (items instanceof EntityDataUnit) {
entityClass = ((EntityDataUnit) items).getEntityMetaClass().getJavaClass();
} else {
throw new IllegalStateException(String.format("Component %s is not bound to data", listComponent));
}
EditorBuilder<E> builder = new EditorBuilder<>(frameOwner, entityClass, editorBuilderProcessor::buildEditor);
builder.withListComponent(listComponent);
builder.editEntity(listComponent.getSingleSelected());
return builder;
}
Aggregations