use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.
the class WebAbstractTable 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);
MetaPropertyPath targetCol = getDatasource().getMetaClass().getPropertyPath(columnId);
Object generatedColumnId = targetCol != null ? targetCol : columnId;
Column column = getColumn(columnId);
Column associatedRuntimeColumn = null;
if (column == null) {
Column newColumn = new Column(generatedColumnId);
columns.put(newColumn.getId(), newColumn);
columnsOrder.add(newColumn);
associatedRuntimeColumn = newColumn;
newColumn.setOwner(this);
}
// 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) {
@SuppressWarnings("unchecked")
@Override
public Object generateCell(com.vaadin.ui.Table source, Object itemId, Object columnId) {
Entity entity = getDatasource().getItem(itemId);
com.haulmont.cuba.gui.components.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(WebAbstractTable.this);
com.vaadin.ui.Component vComponent = component.unwrapComposition(Component.class);
// wrap field for show required asterisk
if ((vComponent instanceof com.vaadin.ui.Field) && (((com.vaadin.ui.Field) vComponent).isRequired())) {
VerticalLayout layout = new VerticalLayout();
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 com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.
the class WebAbstractTable method removeGeneratedColumn.
@Override
public void removeGeneratedColumn(String columnId) {
MetaPropertyPath targetCol = getDatasource().getMetaClass().getPropertyPath(columnId);
removeGeneratedColumn(targetCol == null ? columnId : targetCol);
}
use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.
the class WebAbstractTable method setDatasource.
@Override
public void setDatasource(final CollectionDatasource datasource) {
Preconditions.checkNotNullArgument(datasource, "datasource is null");
if (this.datasource != null) {
if (!this.datasource.getMetaClass().equals(datasource.getMetaClass())) {
throw new IllegalArgumentException("The new datasource must correspond to the same MetaClass");
}
if (fieldDatasources != null) {
fieldDatasources.clear();
}
if (collectionDsListenersWrapper != null) {
collectionDsListenersWrapper.unbind(this.datasource);
if (containerDatasource != null) {
containerDatasource.unsubscribe();
containerDatasource = null;
}
}
}
MessageTools messageTools = AppBeans.get(MessageTools.NAME);
final Collection<Object> columns;
if (this.columns.isEmpty()) {
Collection<MetaPropertyPath> paths = datasource.getView() != null ? // if a view is specified - use view properties
metadataTools.getViewPropertyPaths(datasource.getView(), datasource.getMetaClass()) : // otherwise use all properties from meta-class
metadataTools.getPropertyPaths(datasource.getMetaClass());
for (MetaPropertyPath metaPropertyPath : paths) {
MetaProperty property = metaPropertyPath.getMetaProperty();
if (!property.getRange().getCardinality().isMany() && !metadataTools.isSystem(property)) {
Table.Column column = new Table.Column(metaPropertyPath);
String propertyName = property.getName();
MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
column.setCaption(messageTools.getPropertyCaption(propertyMetaClass, propertyName));
column.setType(metaPropertyPath.getRangeJavaClass());
Element element = DocumentHelper.createElement("column");
column.setXmlDescriptor(element);
addColumn(column);
}
}
}
columns = this.columns.keySet();
this.datasource = datasource;
if (collectionDsListenersWrapper == null) {
collectionDsListenersWrapper = createCollectionDsListenersWrapper();
}
containerDatasource = createContainerDatasource(datasource, getPropertyColumns(), collectionDsListenersWrapper);
component.setContainerDataSource(containerDatasource);
List<MetaPropertyPath> editableColumns = null;
if (isEditable()) {
editableColumns = new LinkedList<>();
}
MetaClass metaClass = datasource.getMetaClass();
for (final Object columnId : columns) {
final Table.Column column = this.columns.get(columnId);
final String caption;
if (column != null) {
caption = getColumnCaption(columnId, column);
} else {
caption = StringUtils.capitalize(getColumnCaption(columnId));
}
setColumnHeader(columnId, caption);
if (column != null) {
if (editableColumns != null && column.isEditable() && (columnId instanceof MetaPropertyPath)) {
MetaPropertyPath propertyPath = ((MetaPropertyPath) columnId);
if (security.isEntityAttrUpdatePermitted(metaClass, propertyPath.toString())) {
editableColumns.add(propertyPath);
}
}
if (column.isCollapsed() && component.isColumnCollapsingAllowed()) {
if (!(columnId instanceof MetaPropertyPath) || security.isEntityAttrReadPermitted(metaClass, columnId.toString())) {
component.setColumnCollapsed(column.getId(), true);
}
}
if (column.getAggregation() != null && isAggregatable()) {
checkAggregation(column.getAggregation());
component.addContainerPropertyAggregation(column.getId(), WebComponentsHelper.convertAggregationType(column.getAggregation().getType()));
}
}
}
if (editableColumns != null && !editableColumns.isEmpty()) {
setEditableColumns(editableColumns);
}
createColumns(containerDatasource);
for (Table.Column column : this.columnsOrder) {
if (editable && column.getAggregation() != null && (BooleanUtils.isTrue(column.isEditable()) || BooleanUtils.isTrue(column.isCalculatable()))) {
addAggregationCell(column);
}
}
createStubsForGeneratedColumns();
setVisibleColumns(getInitialVisibleColumns());
if (security.isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION)) {
ShowInfoAction action = (ShowInfoAction) getAction(ShowInfoAction.ACTION_ID);
if (action == null) {
action = new ShowInfoAction();
addAction(action);
}
action.setDatasource(datasource);
}
if (rowsCount != null) {
rowsCount.setDatasource(datasource);
}
collectionDsListenersWrapper.bind(datasource);
for (Action action : getActions()) {
action.refreshState();
}
if (!canBeSorted(datasource))
setSortable(false);
assignAutoDebugId();
}
use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.
the class WebAbstractTable method setEditable.
@Override
public void setEditable(boolean editable) {
if (this.editable != editable) {
this.editable = editable;
component.disableContentBufferRefreshing();
if (datasource != null) {
com.vaadin.data.Container ds = component.getContainerDataSource();
@SuppressWarnings("unchecked") final Collection<MetaPropertyPath> propertyIds = (Collection<MetaPropertyPath>) ds.getContainerPropertyIds();
if (editable) {
MetaClass metaClass = datasource.getMetaClass();
final List<MetaPropertyPath> editableColumns = new ArrayList<>(propertyIds.size());
for (final MetaPropertyPath propertyId : propertyIds) {
if (!security.isEntityAttrUpdatePermitted(metaClass, propertyId.toString())) {
continue;
}
final Table.Column column = getColumn(propertyId.toString());
if (BooleanUtils.isTrue(column.isEditable())) {
com.vaadin.ui.Table.ColumnGenerator generator = component.getColumnGenerator(column.getId());
if (generator != null) {
if (generator instanceof SystemTableColumnGenerator) {
// remove default generator
component.removeGeneratedColumn(propertyId);
} else {
// do not edit generated columns
continue;
}
}
editableColumns.add(propertyId);
}
}
setEditableColumns(editableColumns);
} else {
setEditableColumns(Collections.emptyList());
Window window = ComponentsHelper.getWindowImplementation(this);
boolean isLookup = window instanceof Window.Lookup;
// restore generators for some type of attributes
for (MetaPropertyPath propertyId : propertyIds) {
final Table.Column column = columns.get(propertyId);
if (column != null) {
final String isLink = column.getXmlDescriptor() == null ? null : column.getXmlDescriptor().attributeValue("link");
if (component.getColumnGenerator(column.getId()) == null) {
if (propertyId.getRange().isClass()) {
if (!isLookup && StringUtils.isNotEmpty(isLink)) {
setClickListener(propertyId.toString(), new LinkCellClickListener());
}
} else if (propertyId.getRange().isDatatype()) {
if (!isLookup && !StringUtils.isEmpty(isLink)) {
setClickListener(propertyId.toString(), new LinkCellClickListener());
} else {
if (column.getMaxTextLength() != null) {
addGeneratedColumn(propertyId, new AbbreviatedColumnGenerator(column));
}
}
}
}
}
}
}
}
component.setEditable(editable);
component.enableContentBufferRefreshing(true);
}
}
Aggregations