use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.
the class AbstractTable method addGeneratedColumn.
@Override
public void addGeneratedColumn(String columnId, ColumnGenerator<? super E> generator) {
checkNotNullArgument(columnId, "columnId is null");
checkNotNullArgument(generator, "generator is null for column id '%s'", columnId);
EntityTableItems<E> entityTableSource = (EntityTableItems<E>) getItems();
MetaPropertyPath targetCol = entityTableSource != null ? entityTableSource.getEntityMetaClass().getPropertyPath(columnId) : null;
Object generatedColumnId = targetCol != null ? targetCol : columnId;
Column column = getColumn(columnId);
Column associatedRuntimeColumn = null;
if (column == null) {
Column<E> newColumn = createColumn(generatedColumnId, this);
columns.put(newColumn.getId(), newColumn);
columnsOrder.add(newColumn);
associatedRuntimeColumn = newColumn;
}
// save column order
Object[] visibleColumns = component.getVisibleColumns();
boolean removeOldGeneratedColumn = component.getColumnGenerator(generatedColumnId) != null;
// replace generator for column if exist
if (removeOldGeneratedColumn) {
component.removeGeneratedColumn(generatedColumnId);
}
component.addGeneratedColumn(generatedColumnId, new CustomColumnGenerator(generator, associatedRuntimeColumn) {
@Nullable
@Override
public Object generateCell(com.vaadin.v7.ui.Table source, Object itemId, Object columnId) {
EntityTableItems<E> entityTableSource = (EntityTableItems<E>) getItems();
if (entityTableSource == null) {
return null;
}
E entity = entityTableSource.getItem(itemId);
io.jmix.ui.component.Component component = getColumnGenerator().generateCell(entity);
if (component == null) {
return null;
}
if (component instanceof PlainTextCell) {
return ((PlainTextCell) component).getText();
}
if (component instanceof BelongToFrame) {
BelongToFrame belongToFrame = (BelongToFrame) component;
if (belongToFrame.getFrame() == null) {
belongToFrame.setFrame(getFrame());
}
}
component.setParent(AbstractTable.this);
AbstractComponent vComponent = component.unwrapComposition(AbstractComponent.class);
if (component instanceof HasValueSource) {
HasValueSource<?> hasValueSource = (HasValueSource) component;
vComponent.setData(hasValueSource);
}
// wrap field for show required asterisk
if ((vComponent instanceof com.vaadin.v7.ui.Field) && (((com.vaadin.v7.ui.Field) vComponent).isRequired())) {
VerticalLayout layout = new VerticalLayout();
layout.setMargin(false);
layout.setSpacing(false);
layout.addComponent(vComponent);
if (vComponent.getWidth() < 0) {
layout.setWidthUndefined();
}
layout.addComponent(vComponent);
vComponent = layout;
}
return vComponent;
}
});
if (removeOldGeneratedColumn) {
// restore column order
component.setVisibleColumns(visibleColumns);
}
}
use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.
the class AbstractTableSettingsBinder method applyColumnSettings.
protected void applyColumnSettings(TableSettings tableSettings, Table table) {
com.vaadin.v7.ui.Table vTable = getVTable(table);
List<TableSettings.ColumnSettings> settingsColumns = CollectionUtils.isEmpty(tableSettings.getColumns()) ? Collections.emptyList() : tableSettings.getColumns();
Object[] oldColumns = vTable.getVisibleColumns();
List<Object> newColumns = new ArrayList<>();
// add columns from saved settings
for (TableSettings.ColumnSettings columnSetting : settingsColumns) {
for (Object column : oldColumns) {
if (column.toString().equals(columnSetting.getId())) {
newColumns.add(column);
Integer width = columnSetting.getWidth();
vTable.setColumnWidth(column, width == null ? -1 : width);
Boolean visible = columnSetting.getVisible();
if (visible != null) {
if (vTable.isColumnCollapsingAllowed()) {
// throws exception if not
vTable.setColumnCollapsed(column, !visible);
}
}
break;
}
}
}
// add columns not saved in settings (perhaps new)
for (Object column : oldColumns) {
if (!newColumns.contains(column)) {
newColumns.add(column);
}
}
// if the table contains only one column, always show it
if (newColumns.size() == 1) {
if (vTable.isColumnCollapsingAllowed()) {
// throws exception if not
vTable.setColumnCollapsed(newColumns.get(0), false);
}
}
vTable.setVisibleColumns(newColumns.toArray());
EntityTableItems entityTableSource = (EntityTableItems) table.getItems();
if (table.isSortable() && !isApplyDataLoadingSettings(table) && entityTableSource != null) {
String sortProp = tableSettings.getSortProperty();
if (!StringUtils.isEmpty(sortProp)) {
MetaPropertyPath sortProperty = entityTableSource.getEntityMetaClass().getPropertyPath(sortProp);
if (newColumns.contains(sortProperty)) {
vTable.setSortContainerPropertyId(null);
vTable.setSortAscending(Boolean.TRUE.equals(tableSettings.getSortAscending()));
vTable.setSortContainerPropertyId(sortProperty);
}
} else {
vTable.setSortContainerPropertyId(null);
}
}
}
use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.
the class AbstractTableSettingsBinder method applyDataLoadingSettings.
@Override
public void applyDataLoadingSettings(Table table, SettingsWrapper wrapper) {
EntityTableItems entityTableSource = (EntityTableItems) table.getItems();
if (table.isSortable() && isApplyDataLoadingSettings(table) && entityTableSource != null) {
TableSettings tableSettings = wrapper.getSettings();
List<TableSettings.ColumnSettings> columns = tableSettings.getColumns();
if (columns != null) {
String sortProp = tableSettings.getSortProperty();
if (sortProp != null) {
List visibleColumns = Arrays.asList(getVTable(table).getVisibleColumns());
MetaPropertyPath sortProperty = entityTableSource.getEntityMetaClass().getPropertyPath(sortProp);
if (visibleColumns.contains(sortProperty)) {
if (table.getItems() instanceof TableItems.Sortable) {
((TableItems.Sortable) table.getItems()).suppressSorting();
}
try {
com.vaadin.v7.ui.Table vTable = getVTable(table);
vTable.setSortContainerPropertyId(null);
vTable.setSortAscending(Boolean.TRUE.equals(tableSettings.getSortAscending()));
vTable.setSortContainerPropertyId(sortProperty);
} finally {
if (table.getItems() instanceof TableItems.Sortable) {
((TableItems.Sortable) table.getItems()).enableSorting();
}
}
}
} else if (table.getSortInfo() != null) {
TableItems tableItems = table.getItems();
if (tableItems instanceof TableItems.Sortable) {
((TableItems.Sortable) tableItems).resetSortOrder();
}
}
}
}
}
use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.
the class AbstractTable method removeGeneratedColumn.
@Override
public void removeGeneratedColumn(String columnId) {
EntityTableItems<E> entityTableSource = (EntityTableItems<E>) getItems();
MetaPropertyPath targetCol = entityTableSource != null ? entityTableSource.getEntityMetaClass().getPropertyPath(columnId) : null;
removeGeneratedColumnInternal(targetCol == null ? columnId : targetCol);
}
use of io.jmix.ui.component.data.meta.EntityTableItems in project jmix by jmix-framework.
the class AbstractTable method getInstanceContainer.
@SuppressWarnings("unchecked")
@Override
public InstanceContainer<E> getInstanceContainer(E item) {
if (fieldDatasources == null) {
fieldDatasources = new WeakHashMap<>();
}
Object fieldDatasource = fieldDatasources.get(item);
if (fieldDatasource instanceof InstanceContainer) {
return (InstanceContainer<E>) fieldDatasource;
}
EntityTableItems containerTableItems = (EntityTableItems) getItems();
if (containerTableItems == null) {
throw new IllegalStateException("Table is not bound to items");
}
InstanceContainer<E> instanceContainer;
MetaClass metaClass = containerTableItems.getEntityMetaClass();
if (metaClass instanceof KeyValueMetaClass) {
instanceContainer = (InstanceContainer<E>) dataComponents.createKeyValueContainer(metaClass);
} else {
instanceContainer = dataComponents.createInstanceContainer(metaClass.getJavaClass());
}
FetchPlan view = viewRepository.getFetchPlan(metaClass, FetchPlan.LOCAL);
instanceContainer.setFetchPlan(view);
instanceContainer.setItem(item);
fieldDatasources.put(item, instanceContainer);
return instanceContainer;
}
Aggregations