use of com.vaadin.data.util.GeneratedPropertyContainer in project cuba by cuba-platform.
the class WebDataGrid method initComponent.
protected void initComponent(CubaGrid component) {
setSelectionMode(SelectionMode.SINGLE);
component.setImmediate(true);
component.setColumnReorderingAllowed(true);
containerWrapper = new GeneratedPropertyContainer(component.getContainerDataSource());
component.setContainerDataSource(containerWrapper);
component.addSelectionListener(e -> {
if (datasource == null) {
return;
}
final Set<E> selected = getSelected();
if (selected.isEmpty()) {
Entity dsItem = datasource.getItemIfValid();
// noinspection unchecked
datasource.setItem(null);
if (dsItem == null) {
// in this case item change event will not be generated
refreshActionsState();
}
} else {
// reset selection and select new item
if (isMultiSelect()) {
// noinspection unchecked
datasource.setItem(null);
}
Entity newItem = selected.iterator().next();
Entity dsItem = datasource.getItemIfValid();
// noinspection unchecked
datasource.setItem(newItem);
if (Objects.equals(dsItem, newItem)) {
// in this case item change event will not be generated
refreshActionsState();
}
}
LookupSelectionChangeEvent selectionChangeEvent = new LookupSelectionChangeEvent(this);
getEventRouter().fireEvent(LookupSelectionChangeListener.class, LookupSelectionChangeListener::lookupValueChanged, selectionChangeEvent);
if (getEventRouter().hasListeners(SelectionListener.class)) {
List<E> addedItems = getItemsByIds(e.getAdded());
List<E> removedItems = getItemsByIds(e.getRemoved());
List<E> selectedItems = getItemsByIds(e.getSelected());
SelectionEvent<E> event = new SelectionEvent<>(WebDataGrid.this, addedItems, removedItems, selectedItems);
// noinspection unchecked
getEventRouter().fireEvent(SelectionListener.class, SelectionListener::selected, event);
}
});
component.addShortcutListener(new ShortcutListener("dataGridEnter", KeyCode.ENTER, null) {
@Override
public void handleAction(Object sender, Object target) {
if (target == WebDataGrid.this.component) {
if (WebDataGrid.this.isEditorEnabled()) {
// since it's the default shortcut to open editor
return;
}
if (enterPressAction != null) {
enterPressAction.actionPerform(WebDataGrid.this);
} else {
handleDoubleClickAction();
}
}
}
});
component.addItemClickListener(e -> {
if (e.isDoubleClick() && e.getItem() != null && !WebDataGrid.this.isEditorEnabled()) {
// note: for now Grid doesn't send double click if editor is enabled,
// but it's better to handle it manually
handleDoubleClickAction();
}
if (getEventRouter().hasListeners(ItemClickListener.class)) {
MouseEventDetails mouseEventDetails = WebWrapperUtils.toMouseEventDetails(e);
// noinspection unchecked
E item = (E) datasource.getItem(e.getItemId());
if (item == null) {
// datasource, so we don't want to send such event because it's useless
return;
}
Column column = getColumnByPropertyId(e.getPropertyId());
ItemClickEvent<E> event = new ItemClickEvent<>(WebDataGrid.this, mouseEventDetails, item, e.getItemId(), column != null ? column.getId() : null);
// noinspection unchecked
getEventRouter().fireEvent(ItemClickListener.class, ItemClickListener::onItemClick, event);
}
});
component.addColumnReorderListener(e -> {
if (e.isUserOriginated()) {
// Grid doesn't know about columns hidden by security permissions,
// so we need to return them back to they previous positions
columnsOrder = restoreColumnsOrder(getColumnsOrderInternal());
if (getEventRouter().hasListeners(ColumnReorderListener.class)) {
ColumnReorderEvent event = new ColumnReorderEvent(WebDataGrid.this);
getEventRouter().fireEvent(ColumnReorderListener.class, ColumnReorderListener::columnReordered, event);
}
}
});
componentComposition = new GridComposition();
componentComposition.setPrimaryStyleName("c-data-grid-composition");
componentComposition.setGrid(component);
componentComposition.addComponent(component);
componentComposition.setWidthUndefined();
component.setSizeUndefined();
component.setHeightMode(HeightMode.UNDEFINED);
component.setRowStyleGenerator(createRowStyleGenerator());
component.setCellStyleGenerator(createCellStyleGenerator());
}
use of com.vaadin.data.util.GeneratedPropertyContainer in project cuba by cuba-platform.
the class ComponentGridDecorator method initGpc.
/**
* Replaces the current grid container with a {@link GeneratedPropertyContainer}
* while preserving the {@link Grid.DetailsGenerator}.
*/
private void initGpc() {
gpc = new GeneratedPropertyContainer(grid.getContainerDataSource());
Grid.DetailsGenerator details = grid.getDetailsGenerator();
grid.setContainerDataSource(gpc);
grid.setDetailsGenerator(details);
}
use of com.vaadin.data.util.GeneratedPropertyContainer in project cuba by cuba-platform.
the class WebDataGrid method setDatasource.
@Override
public void setDatasource(CollectionDatasource datasource) {
checkNotNullArgument(datasource, "datasource is null");
if (!(datasource instanceof CollectionDatasource.Indexed)) {
throw new IllegalArgumentException("Datasource must implement " + "com.haulmont.cuba.gui.data.CollectionDatasource.Indexed");
}
if (this.datasource != null) {
if (!this.datasource.getMetaClass().equals(datasource.getMetaClass())) {
throw new IllegalArgumentException("The new datasource must correspond to the same MetaClass");
}
if (collectionDsListenersWrapper != null) {
collectionDsListenersWrapper.unbind(this.datasource);
if (containerDatasource != null) {
containerDatasource.unsubscribe();
containerDatasource = null;
}
}
}
addInitialColumns(datasource);
this.datasource = datasource;
List<Column> visibleColumnsOrder = getInitialVisibleColumns();
if (collectionDsListenersWrapper == null) {
collectionDsListenersWrapper = new CollectionDsListenersWrapper();
}
component.removeAllColumns();
containerDatasource = createContainerDatasource((CollectionDatasource.Indexed) datasource, getPropertyColumns(), collectionDsListenersWrapper);
containerWrapper = new GeneratedPropertyContainer(containerDatasource);
component.setContainerDataSource(containerWrapper);
createStubsForGeneratedColumns();
// mark columns hidden by security permissions as visible = false
// and remove Grid.Column to prevent its property changing
columnsOrder.stream().filter(column -> !visibleColumnsOrder.contains(column)).forEach(column -> {
ColumnImpl columnImpl = (ColumnImpl) column;
columnImpl.setVisible(false);
columnImpl.setGridColumn(null);
});
for (Column column : visibleColumnsOrder) {
Grid.Column gridColumn = component.getColumn(((ColumnImpl) column).getColumnPropertyId());
setupGridColumnProperties(gridColumn, column);
}
component.setColumnOrder(getColumnPropertyIds());
initShowInfoAction(datasource);
if (rowsCount != null) {
rowsCount.setDatasource(datasource);
}
collectionDsListenersWrapper.bind(datasource);
refreshActionsState();
if (!canBeSorted(datasource)) {
setSortable(false);
}
assignAutoDebugId();
component.setCollectionDatasource(datasource);
}
use of com.vaadin.data.util.GeneratedPropertyContainer in project VaadinUtils by rlsutton1.
the class GridHeadingPropertySet method wrapGridContainer.
private GeneratedPropertyContainer wrapGridContainer(final Grid grid) {
Indexed gridContainer = grid.getContainerDataSource();
if (!(gridContainer instanceof GeneratedPropertyContainer)) {
final GeneratedPropertyContainer gpc = new GeneratedPropertyContainer(gridContainer);
grid.setContainerDataSource(gpc);
gridContainer = gpc;
}
return (GeneratedPropertyContainer) gridContainer;
}
use of com.vaadin.data.util.GeneratedPropertyContainer in project VaadinUtils by rlsutton1.
the class GridHeadingPropertySet method applyToGrid.
/**
* @param grid
* @param uniqueId
* - an id for this layout/grid combination, it is used to
* identify stored column widths in a key value map
*/
public void applyToGrid(final Grid grid, final String uniqueId) {
this.grid = grid;
this.uniqueId = uniqueId;
try {
// Changing the bound container at this point can cause issues
// elsewhere, so we avoid it if possible
Indexed gridContainer = grid.getContainerDataSource();
for (GridHeadingToPropertyId column : getColumns()) {
if (column.isGenerated()) {
gridContainer = wrapGridContainer(grid);
break;
}
}
final List<String> colsToShow = new LinkedList<>();
for (GridHeadingToPropertyId column : getColumns()) {
final String propertyId = column.getPropertyId();
if (column.isGenerated()) {
final PropertyValueGenerator<?> columnGenerator = column.getColumnGenerator();
((GeneratedPropertyContainer) gridContainer).addGeneratedProperty(propertyId, columnGenerator);
final Column gridColumn = grid.getColumn(propertyId);
if (columnGenerator.getType() == String.class && gridColumn.getRenderer() instanceof TextRenderer) {
gridColumn.setRenderer(new HtmlRenderer(), null);
} else if (columnGenerator.getType() == Component.class) {
gridColumn.setRenderer(new ComponentRenderer());
}
} else {
Preconditions.checkArgument(grid.getContainerDataSource().getContainerPropertyIds().contains(propertyId), propertyId + " is not a valid property id, valid property ids are " + grid.getContainerDataSource().getContainerPropertyIds().toString());
}
colsToShow.add(propertyId);
final Column gridColumn = grid.getColumn(propertyId);
if (column.getRenderer() != null) {
gridColumn.setRenderer(column.getRenderer());
}
if (column.getConverter() != null) {
gridColumn.setConverter(column.getConverter());
}
gridColumn.setHeaderCaption(column.getHeader());
if (column.getWidth() != null) {
gridColumn.setWidth(column.getWidth());
} else {
gridColumn.setExpandRatio(1);
gridColumn.setMinimumWidth(1);
}
if (column.isLocked()) {
gridColumn.setHidable(false);
} else {
gridColumn.setHidable(true);
if (!column.isVisibleByDefault()) {
gridColumn.setHidden(true);
}
}
}
grid.setColumns(colsToShow.toArray());
if (eraseSavedConfig) {
eraseSavedConfig(uniqueId);
}
if (!deferLoadSettings) {
configureSaveColumnWidths(grid, uniqueId);
configureSaveColumnOrder(grid, uniqueId);
configureSaveColumnVisible(grid, uniqueId);
}
} catch (Exception e) {
logger.error(e, e);
}
}
Aggregations