ColumnPicker extends UberfireColumnPicker<T> { public void adjustColumnWidths() { super.adjustColumnWidths(); List<GridColumnPreference> preferences = getColumnsState(); if (preferences.isEmpty()) { return; } if (preferences.size() == 1) { columnZeroWidth = Optional.of(preferences.get(0).getWidth()); dataGrid.setColumnWidth(dataGrid.getColumn(0), 100, Style.Unit.PCT); dataGridMinWidth = defaultColumnWidthSize; return; } columnZeroWidth.ifPresent(w -> { preferences.get(0).setWidth(w); columnZeroWidth = Optional.empty(); }); int fixedColumnsWidth = 0; Map<String, String> fixedWidths = new HashMap<String, String>(); List<String> columnsToCalculate = new ArrayList<String>(); for (GridColumnPreference preference : preferences) { if (preference.getWidth() != null && preference.getWidth().endsWith(Style.Unit.PX.getType())) { String width = preference.getWidth().substring(0, preference.getWidth().indexOf(Style.Unit.PX.getType())); if (width.contains(".")) { width = width.substring(0, width.indexOf(".")); } fixedWidths.put(preference.getName(), width + Style.Unit.PX.getType()); fixedColumnsWidth += Integer.decode(width); } else { columnsToCalculate.add(preference.getName()); } } if (columnsToCalculate.size() > 0) { double columnPCT = (100 / columnsToCalculate.size()); if (dataGrid.getOffsetWidth() != 0) { int availableColumnSpace = dataGrid.getOffsetWidth() - fixedColumnsWidth; double availablePCT = availableColumnSpace * 100 / dataGrid.getOffsetWidth(); columnPCT = columnPCT * availablePCT / 100; } for (ColumnMeta<T> cm : columnMetaList) { if (cm.isVisible()) { if (columnsToCalculate.contains(getColumnStoreName(cm))) { dataGrid.setColumnWidth(cm.getColumn(), columnPCT, Style.Unit.PCT); } else { dataGrid.setColumnWidth(cm.getColumn(), fixedWidths.get(getColumnStoreName(cm))); } } } } dataGridMinWidth = fixedColumnsWidth + columnsToCalculate.size() * defaultColumnWidthSize; } ColumnPicker(DataGrid<T> dataGrid,
                        GridPreferencesStore gridPreferences);  ColumnPicker(DataGrid<T> dataGrid); void setGridPreferencesStore(GridPreferencesStore gridPreferences); List<GridColumnPreference> getColumnsState(); void adjustColumnWidths(); int getDataGridMinWidth(); void setDefaultColumnWidthSize(int defaultColumnSize); static int DETAULT_COLUMN_WIDTH; }
@Test public void testAdjustColumnWidths() { final Column column1 = createColumn("col1", "col1"); final ColumnMeta meta1 = new ColumnMeta(column1, "caption1", true, 1); meta1.setHeader(new TextHeader("header1")); final Column column2 = createColumn("col2", "col2"); final ColumnMeta meta2 = new ColumnMeta(column2, "caption2", true, 0); meta2.setHeader(new TextHeader("header2")); final Column column3 = createColumn("col3", "col3"); final ColumnMeta meta3 = new ColumnMeta(column3, "caption3", true, 0); meta3.setHeader(new TextHeader("header3")); when(dataGrid.getColumnWidth(column1)).thenReturn("35px"); when(dataGrid.getColumnWidth(column2)).thenReturn(null); when(dataGrid.getColumnWidth(column3)).thenReturn(null); List<ColumnMeta> columnMetasList = new ArrayList<ColumnMeta>(); columnMetasList.add(meta1); columnMetasList.add(meta2); columnMetasList.add(meta3); columnPicker.addColumns(columnMetasList); verify(dataGrid).setColumnWidth(column1, "35px"); verify(dataGrid).setColumnWidth(column2, 50, Style.Unit.PCT); verify(dataGrid).setColumnWidth(column3, 50, Style.Unit.PCT); }