use of io.jmix.ui.settings.component.DataGridSettings 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 io.jmix.ui.settings.component.DataGridSettings 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 io.jmix.ui.settings.component.DataGridSettings in project jmix by jmix-framework.
the class AbstractDataGridSettingsBinder method saveSettings.
@Override
public boolean saveSettings(DataGrid dataGrid, SettingsWrapper wrapper) {
DataGridSettings settings = wrapper.getSettings();
String sortColumnId = null;
DataGrid.SortDirection sortDirection = null;
if (settings.getColumns() != null) {
sortColumnId = settings.getSortColumnId();
sortDirection = settings.getSortDirection();
}
boolean commonSettingsChanged = isCommonDataGridSettingsChanged(dataGrid, settings);
boolean sortChanged = isSortPropertySettingsChanged(dataGrid, sortColumnId, sortDirection);
boolean settingsChanged = commonSettingsChanged || sortChanged;
if (settingsChanged) {
settings.setColumns(getColumnsSettings(dataGrid));
List<GridSortOrder> sortOrders = getGrid(dataGrid).getSortOrder();
// so we don't save null sorting
if (!sortOrders.isEmpty()) {
GridSortOrder sortOrder = sortOrders.get(0);
settings.setSortColumnId(sortOrder.getSorted().getId());
settings.setSortDirection(WrapperUtils.convertToDataGridSortDirection(sortOrder.getDirection()));
}
}
return settingsChanged;
}
use of io.jmix.ui.settings.component.DataGridSettings in project jmix by jmix-framework.
the class AbstractDataGridSettingsBinder method applySettings.
@Override
public void applySettings(DataGrid dataGrid, SettingsWrapper wrapper) {
DataGridSettings settings = wrapper.getSettings();
List<DataGridSettings.ColumnSettings> columns = settings.getColumns();
if (columns != null) {
List<DataGrid.Column> modelColumns = dataGrid.getVisibleColumns();
List<String> modelIds = modelColumns.stream().map(String::valueOf).collect(Collectors.toList());
List<String> loadedIds = columns.stream().map(DataGridSettings.ColumnSettings::getId).collect(Collectors.toList());
if (CollectionUtils.isEqualCollection(modelIds, loadedIds)) {
applyColumnSettings(dataGrid, settings, modelColumns);
}
}
}
use of io.jmix.ui.settings.component.DataGridSettings in project jmix by jmix-framework.
the class AbstractDataGridSettingsBinder method getSettings.
@Override
public DataGridSettings getSettings(DataGrid dataGrid) {
DataGridSettings settings = createSettings();
settings.setId(dataGrid.getId());
settings.setColumns(getColumnsSettings(dataGrid));
List<GridSortOrder> sortOrders = getGrid(dataGrid).getSortOrder();
if (!sortOrders.isEmpty()) {
GridSortOrder sortOrder = sortOrders.get(0);
settings.setSortColumnId(sortOrder.getSorted().getId());
settings.setSortDirection(WrapperUtils.convertToDataGridSortDirection(sortOrder.getDirection()));
}
return settings;
}
Aggregations