use of com.vaadin.data.provider.GridSortOrder in project jmix by jmix-framework.
the class AbstractDataGridSettingsBinder method applyColumnSettings.
protected void applyColumnSettings(DataGrid dataGrid, DataGridSettings settings, Collection<DataGrid.Column> oldColumns) {
Grid grid = getGrid(dataGrid);
List<DataGridSettings.ColumnSettings> columnsSettings = CollectionUtils.isEmpty(settings.getColumns()) ? Collections.emptyList() : settings.getColumns();
List<DataGrid.Column> newColumns = new ArrayList<>();
// add columns from saved settings
for (DataGridSettings.ColumnSettings columnSettings : columnsSettings) {
for (DataGrid.Column column : oldColumns) {
if (column.getId().equals(columnSettings.getId())) {
newColumns.add(column);
Double width = columnSettings.getWidth();
if (width != null) {
column.setWidth(width);
} else {
column.setWidthAuto();
}
Boolean collapsed = columnSettings.getCollapsed();
if (collapsed != null && grid.isColumnReorderingAllowed()) {
column.setCollapsed(collapsed);
}
break;
}
}
}
// add columns not saved in settings (perhaps new)
for (DataGrid.Column column : oldColumns) {
if (!newColumns.contains(column)) {
newColumns.add(column);
}
}
// if the data grid contains only one column, always show it
if (newColumns.size() == 1) {
newColumns.get(0).setCollapsed(false);
}
// We don't save settings for columns hidden by security permissions,
// so we need to return them back to they initial positions
DataGridSettingsUtils.restoreColumnsOrder(dataGrid, newColumns);
grid.setColumnOrder(newColumns.stream().map(DataGrid.Column::getId).toArray(String[]::new));
if (dataGrid.isSortable() && !isApplyDataLoadingSettings(dataGrid)) {
// apply sorting
grid.clearSortOrder();
String sortColumnId = settings.getSortColumnId();
if (StringUtils.isNotEmpty(sortColumnId)) {
Grid.Column column = grid.getColumn(sortColumnId);
if (column != null) {
DataGrid.SortDirection sortDirection = settings.getSortDirection();
if (sortDirection != null) {
List<GridSortOrder> sortOrders = Collections.singletonList(new GridSortOrder(column, WrapperUtils.convertToGridSortDirection(sortDirection)));
grid.setSortOrder(sortOrders);
}
}
}
}
}
use of com.vaadin.data.provider.GridSortOrder in project jmix by jmix-framework.
the class AbstractDataGridSettingsBinder method applyDataLoadingSettings.
@Override
public void applyDataLoadingSettings(DataGrid dataGrid, SettingsWrapper wrapper) {
DataGridSettings dataGridSettings = wrapper.getSettings();
if (dataGrid.isSortable() && isApplyDataLoadingSettings(dataGrid)) {
if (dataGridSettings.getColumns() == null) {
return;
}
String sortColumnId = dataGridSettings.getSortColumnId();
if (StringUtils.isNotEmpty(sortColumnId)) {
Grid grid = getGrid(dataGrid);
Grid.Column column = grid.getColumn(sortColumnId);
if (column != null) {
if (dataGrid.getItems() instanceof DataGridItems.Sortable) {
((DataGridItems.Sortable) dataGrid.getItems()).suppressSorting();
}
try {
grid.clearSortOrder();
DataGrid.SortDirection sortDirection = dataGridSettings.getSortDirection();
if (sortDirection != null) {
List<GridSortOrder> sortOrders = Collections.singletonList(new GridSortOrder<>(column, WrapperUtils.convertToGridSortDirection(sortDirection)));
grid.setSortOrder(sortOrders);
}
} finally {
if (dataGrid.getItems() instanceof DataGridItems.Sortable) {
((DataGridItems.Sortable) dataGrid.getItems()).enableSorting();
}
}
}
}
}
}
use of com.vaadin.data.provider.GridSortOrder in project cuba by cuba-platform.
the class WebAbstractDataGrid method onSort.
protected void onSort(com.vaadin.event.SortEvent<GridSortOrder<E>> e) {
if (component.getDataProvider() instanceof SortableDataProvider) {
// noinspection unchecked
SortableDataProvider<E> dataProvider = (SortableDataProvider<E>) component.getDataProvider();
List<GridSortOrder<E>> sortOrders = e.getSortOrder();
if (sortOrders.isEmpty()) {
dataProvider.resetSortOrder();
} else {
GridSortOrder<E> sortOrder = sortOrders.get(0);
Column<E> column = getColumnByGridColumn(sortOrder.getSorted());
if (column != null) {
MetaPropertyPath propertyPath = column.getPropertyPath();
boolean ascending = com.vaadin.shared.data.sort.SortDirection.ASCENDING.equals(sortOrder.getDirection());
dataProvider.sort(new Object[] { propertyPath }, new boolean[] { ascending });
}
}
}
List<SortOrder> sortOrders = convertToDataGridSortOrder(e.getSortOrder());
SortEvent event = new SortEvent(WebAbstractDataGrid.this, sortOrders, e.isUserOriginated());
publish(SortEvent.class, event);
}
use of com.vaadin.data.provider.GridSortOrder in project cuba by cuba-platform.
the class WebAbstractDataGrid method applyDataLoadingSettings.
public void applyDataLoadingSettings(Element element) {
if (!isSettingsEnabled()) {
return;
}
if (isSortable() && isApplyDataLoadingSettings()) {
Element columnsElem = element.element("columns");
if (columnsElem != null) {
String sortColumnId = columnsElem.attributeValue("sortColumnId");
if (!StringUtils.isEmpty(sortColumnId)) {
Grid.Column<E, ?> column = component.getColumn(sortColumnId);
if (column != null) {
if (getItems() instanceof DataGridItems.Sortable) {
((DataGridItems.Sortable<E>) getItems()).suppressSorting();
}
try {
component.clearSortOrder();
String sortDirection = columnsElem.attributeValue("sortDirection");
if (StringUtils.isNotEmpty(sortDirection)) {
List<GridSortOrder<E>> sortOrders = Collections.singletonList(new GridSortOrder<>(column, com.vaadin.shared.data.sort.SortDirection.valueOf(sortDirection)));
component.setSortOrder(sortOrders);
}
} finally {
if (getItems() instanceof DataGridItems.Sortable) {
((DataGridItems.Sortable<E>) getItems()).enableSorting();
}
}
}
}
}
}
}
use of com.vaadin.data.provider.GridSortOrder in project cuba by cuba-platform.
the class WebAbstractDataGrid method applyColumnSettings.
protected void applyColumnSettings(Element element, Collection<Column<E>> oldColumns) {
Element columnsElem = element.element("columns");
List<Column<E>> newColumns = new ArrayList<>();
// add columns from saved settings
for (Element colElem : columnsElem.elements("columns")) {
for (Column<E> column : oldColumns) {
if (column.getId().equals(colElem.attributeValue("id"))) {
newColumns.add(column);
String width = colElem.attributeValue("width");
if (width != null) {
column.setWidth(Double.parseDouble(width));
} else {
column.setWidthAuto();
}
String collapsed = colElem.attributeValue("collapsed");
if (collapsed != null && component.isColumnReorderingAllowed()) {
column.setCollapsed(Boolean.parseBoolean(collapsed));
}
break;
}
}
}
// add columns not saved in settings (perhaps new)
for (Column<E> column : oldColumns) {
if (!newColumns.contains(column)) {
newColumns.add(column);
}
}
// if the data grid contains only one column, always show it
if (newColumns.size() == 1) {
newColumns.get(0).setCollapsed(false);
}
// We don't save settings for columns hidden by security permissions,
// so we need to return them back to they initial positions
columnsOrder = restoreColumnsOrder(newColumns);
component.setColumnOrder(newColumns.stream().map(Column::getId).toArray(String[]::new));
if (isSortable() && !isApplyDataLoadingSettings()) {
// apply sorting
component.clearSortOrder();
String sortColumnId = columnsElem.attributeValue("sortColumnId");
if (!StringUtils.isEmpty(sortColumnId)) {
Grid.Column<E, ?> column = component.getColumn(sortColumnId);
if (column != null) {
String sortDirection = columnsElem.attributeValue("sortDirection");
if (StringUtils.isNotEmpty(sortDirection)) {
List<GridSortOrder<E>> sortOrders = Collections.singletonList(new GridSortOrder<>(column, com.vaadin.shared.data.sort.SortDirection.valueOf(sortDirection)));
component.setSortOrder(sortOrders);
}
}
}
}
}
Aggregations