diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2014-11-27 14:27:19 +0200 |
---|---|---|
committer | Henrik Paul <henrik@vaadin.com> | 2014-12-01 09:16:21 +0000 |
commit | 1edeb7c4a8f3a31915e895e2314d26d0f7534189 (patch) | |
tree | aba422c260750a1d2c06133347f501da7e22ea77 | |
parent | 553866297f3224e5e1bc852e41ac1fed2d51abce (diff) | |
download | vaadin-framework-1edeb7c4a8f3a31915e895e2314d26d0f7534189.tar.gz vaadin-framework-1edeb7c4a8f3a31915e895e2314d26d0f7534189.zip |
Make Grid.Column API setters fluid (#13334)
Change-Id: I3b160a265fe8bd16a97f895a05dc5e3a78314e07
7 files changed, 74 insertions, 49 deletions
diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java index e1c2925056..ffef7e5b9e 100644 --- a/server/src/com/vaadin/data/RpcDataProviderExtension.java +++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java @@ -47,7 +47,7 @@ import com.vaadin.shared.data.DataRequestRpc; import com.vaadin.shared.ui.grid.GridState; import com.vaadin.shared.ui.grid.Range; import com.vaadin.ui.Grid; -import com.vaadin.ui.Grid.GridColumn; +import com.vaadin.ui.Grid.Column; import com.vaadin.ui.components.grid.Renderer; import elemental.json.Json; @@ -720,7 +720,7 @@ public class RpcDataProviderExtension extends AbstractExtension { int i = 0; for (Object propertyId : propertyIds) { - GridColumn column = grid.getColumn(propertyId); + Column column = grid.getColumn(propertyId); Object propertyValue = item.getItemProperty(propertyId).getValue(); JsonValue encodedValue = encodeValue(propertyValue, diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 1b2ed6cd5f..715190e293 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -888,7 +888,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * A column in the grid. Can be obtained by calling * {@link Grid#getColumn(Object propertyId)}. */ - public static class GridColumn implements Serializable { + public static class Column implements Serializable { /** * The state of the column shared to the client @@ -917,7 +917,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * @param state * the shared state of this column */ - GridColumn(Grid grid, GridColumnState state) { + Column(Grid grid, GridColumnState state) { this.grid = grid; this.state = state; internalSetRenderer(new TextRenderer()); @@ -957,11 +957,12 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * * @param caption * the text to show in the caption + * @return the column itself * * @throws IllegalStateException * if the column is no longer attached to any grid */ - public void setHeaderCaption(String caption) + public Column setHeaderCaption(String caption) throws IllegalStateException { checkColumnIsAttached(); HeaderRow row = grid.getHeader().getDefaultRow(); @@ -969,6 +970,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, row.getCell(grid.getPropertyIdByColumnId(state.id)).setText( caption); } + return this; } /** @@ -988,12 +990,14 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * * @param pixelWidth * the new pixel width of the column + * @return the column itself + * * @throws IllegalStateException * if the column is no longer attached to any grid * @throws IllegalArgumentException * thrown if pixel width is less than zero */ - public void setWidth(int pixelWidth) throws IllegalStateException, + public Column setWidth(int pixelWidth) throws IllegalStateException, IllegalArgumentException { checkColumnIsAttached(); if (pixelWidth < 0) { @@ -1003,17 +1007,21 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, } state.width = pixelWidth; grid.markAsDirty(); + return this; } /** * Marks the column width as undefined meaning that the grid is free to * resize the column based on the cell contents and available space in * the grid. + * + * @return the column itself */ - public void setWidthUndefined() { + public Column setWidthUndefined() { checkColumnIsAttached(); state.width = -1; grid.markAsDirty(); + return this; } /** @@ -1034,13 +1042,16 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * * @param visible * is the column visible + * @return the column itself + * * @throws IllegalStateException * if the column is no longer attached to any grid */ - public void setVisible(boolean visible) throws IllegalStateException { + public Column setVisible(boolean visible) throws IllegalStateException { checkColumnIsAttached(); state.visible = visible; grid.markAsDirty(); + return this; } /** @@ -1059,13 +1070,16 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, /** * Sets this column as the last frozen column in its grid. * + * @return the column itself + * * @throws IllegalArgumentException * if the column is no longer attached to any grid - * @see Grid#setLastFrozenColumn(GridColumn) + * @see Grid#setLastFrozenColumn(Column) */ - public void setLastFrozenColumn() { + public Column setLastFrozenColumn() { checkColumnIsAttached(); grid.setLastFrozenColumn(this); + return this; } /** @@ -1076,6 +1090,8 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * * @param renderer * the renderer to use + * @return the column itself + * * @throws IllegalArgumentException * if no compatible converter could be found * @@ -1083,7 +1099,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * @see ConverterUtil#getConverter(Class, Class, VaadinSession) * @see #setConverter(Converter) */ - public void setRenderer(Renderer<?> renderer) { + public Column setRenderer(Renderer<?> renderer) { if (!internalSetRenderer(renderer)) { throw new IllegalArgumentException( "Could not find a converter for converting from the model type " @@ -1092,6 +1108,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, + renderer.getPresentationType() + " (in " + toString() + ")"); } + return this; } /** @@ -1102,11 +1119,12 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * the renderer to use, cannot be null * @param converter * the converter to use + * @return the column itself * * @throws IllegalArgumentException * if the renderer is already associated with a grid column */ - public <T> void setRenderer(Renderer<T> renderer, + public <T> Column setRenderer(Renderer<T> renderer, Converter<? extends T, ?> converter) { if (renderer.getParent() != null) { throw new IllegalArgumentException( @@ -1121,6 +1139,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, grid.addRenderer(renderer); state.rendererConnector = renderer; setConverter(converter); + return this; } /** @@ -1130,10 +1149,12 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * @param converter * the converter to use, or {@code null} to not use any * converters + * @return the column itself + * * @throws IllegalArgumentException * if the types are not compatible */ - public void setConverter(Converter<?, ?> converter) + public Column setConverter(Converter<?, ?> converter) throws IllegalArgumentException { Class<?> modelType = getModelType(); if (converter != null) { @@ -1189,6 +1210,8 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, @SuppressWarnings("unchecked") Converter<?, Object> castConverter = (Converter<?, Object>) converter; this.converter = castConverter; + + return this; } /** @@ -1252,11 +1275,13 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * @param sortable * <code>true</code> if the sorting controls should be * visible. + * @return the column itself */ - public void setSortable(boolean sortable) { + public Column setSortable(boolean sortable) { checkColumnIsAttached(); state.sortable = sortable; grid.markAsDirty(); + return this; } /** @@ -1788,7 +1813,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, /** * Property id to column instance mapping */ - private final Map<Object, GridColumn> columns = new HashMap<Object, GridColumn>(); + private final Map<Object, Column> columns = new HashMap<Object, Column>(); /** * Key generator for column server-to-client communication @@ -1842,7 +1867,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, if (event.getContainer() instanceof Sortable) { Collection<?> sortableProperties = ((Sortable) event .getContainer()).getSortableContainerPropertyIds(); - for (Entry<Object, GridColumn> columnEntry : columns.entrySet()) { + for (Entry<Object, Column> columnEntry : columns.entrySet()) { columnEntry.getValue().setSortable( sortableProperties.contains(columnEntry.getKey())); } @@ -2198,7 +2223,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, // Add columns HeaderRow row = getHeader().getDefaultRow(); for (Object propertyId : datasource.getContainerPropertyIds()) { - GridColumn column = appendColumn(propertyId); + Column column = appendColumn(propertyId); // Initial sorting is defined by container if (datasource instanceof Sortable) { @@ -2224,21 +2249,21 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * the property id of the column * @return the column or <code>null</code> if not found */ - public GridColumn getColumn(Object propertyId) { + public Column getColumn(Object propertyId) { return columns.get(propertyId); } /** - * Used internally by the {@link Grid} to get a {@link GridColumn} by - * referencing its generated state id. Also used by {@link GridColumn} to - * verify if it has been detached from the {@link Grid}. + * Used internally by the {@link Grid} to get a {@link Column} by + * referencing its generated state id. Also used by {@link Column} to verify + * if it has been detached from the {@link Grid}. * * @param columnId * the client id generated for the column when the column is * added to the grid * @return the column with the id or <code>null</code> if not found */ - GridColumn getColumnByColumnId(String columnId) { + Column getColumnByColumnId(String columnId) { Object propertyId = getPropertyIdByColumnId(columnId); return getColumn(propertyId); } @@ -2272,7 +2297,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * @param datasourcePropertyId * The property id of a property in the datasource */ - private GridColumn appendColumn(Object datasourcePropertyId) { + private Column appendColumn(Object datasourcePropertyId) { if (datasourcePropertyId == null) { throw new IllegalArgumentException("Property id cannot be null"); } @@ -2282,7 +2307,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, GridColumnState columnState = new GridColumnState(); columnState.id = columnKeys.key(datasourcePropertyId); - GridColumn column = new GridColumn(this, columnState); + Column column = new Column(this, columnState); columns.put(datasourcePropertyId, column); getState().columns.add(columnState); @@ -2304,7 +2329,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, private void removeColumn(Object propertyId) { header.removeColumn(propertyId); footer.removeColumn(propertyId); - GridColumn column = columns.remove(propertyId); + Column column = columns.remove(propertyId); getState().columnOrder.remove(columnKeys.key(propertyId)); getState().columns.remove(column.getState()); columnKeys.remove(propertyId); @@ -2355,7 +2380,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * @throws IllegalArgumentException * if {@code lastFrozenColumn} is not a column from this grid */ - void setLastFrozenColumn(GridColumn lastFrozenColumn) { + void setLastFrozenColumn(Column lastFrozenColumn) { /* * TODO: If and when Grid supports column reordering or insertion of * columns before other columns, make sure to mention that adding @@ -2391,7 +2416,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, * if {@code lastFrozenColumn} is not a column from this grid */ public void setLastFrozenPropertyId(Object propertyId) { - final GridColumn column; + final Column column; if (propertyId == null) { column = null; } else { diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java b/server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java index ab572cdd96..a7857bb4ff 100644 --- a/server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java +++ b/server/tests/src/com/vaadin/tests/server/component/grid/GridColumns.java @@ -36,7 +36,7 @@ import com.vaadin.server.KeyMapper; import com.vaadin.shared.ui.grid.GridColumnState; import com.vaadin.shared.ui.grid.GridState; import com.vaadin.ui.Grid; -import com.vaadin.ui.Grid.GridColumn; +import com.vaadin.ui.Grid.Column; public class GridColumns { @@ -76,7 +76,7 @@ public class GridColumns { .getContainerPropertyIds()) { // All property ids should get a column - GridColumn column = grid.getColumn(propertyId); + Column column = grid.getColumn(propertyId); assertNotNull(column); // Property id should be the column header by default @@ -93,7 +93,7 @@ public class GridColumns { // Change old columns so they wouldn't pass the check. for (Object propertyId : grid.getContainerDatasource() .getContainerPropertyIds()) { - GridColumn column = grid.getColumn(propertyId); + Column column = grid.getColumn(propertyId); column.setHeaderCaption("Old " + column.getHeaderCaption()); } @@ -110,7 +110,7 @@ public class GridColumns { .getContainerPropertyIds()) { // All property ids should get a column - GridColumn column = grid.getColumn(propertyId); + Column column = grid.getColumn(propertyId); assertNotNull(column); // Property id should be the column header by default @@ -123,7 +123,7 @@ public class GridColumns { public void testModifyingColumnProperties() throws Exception { // Modify first column - GridColumn column = grid.getColumn("column1"); + Column column = grid.getColumn("column1"); assertNotNull(column); column.setHeaderCaption("CustomHeader"); @@ -157,7 +157,7 @@ public class GridColumns { @Test public void testRemovingColumn() throws Exception { - GridColumn column = grid.getColumn("column1"); + Column column = grid.getColumn("column1"); assertNotNull(column); // Remove column @@ -193,7 +193,7 @@ public class GridColumns { public void testAddingColumn() throws Exception { grid.getContainerDatasource().addContainerProperty("columnX", String.class, ""); - GridColumn column = grid.getColumn("columnX"); + Column column = grid.getColumn("columnX"); assertNotNull(column); } diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/RendererTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/RendererTest.java index c798d4b1d6..608559a059 100644 --- a/server/tests/src/com/vaadin/tests/server/component/grid/RendererTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/grid/RendererTest.java @@ -35,7 +35,7 @@ import com.vaadin.server.VaadinSession; import com.vaadin.tests.util.AlwaysLockedVaadinSession; import com.vaadin.ui.ConnectorTracker; import com.vaadin.ui.Grid; -import com.vaadin.ui.Grid.GridColumn; +import com.vaadin.ui.Grid.Column; import com.vaadin.ui.UI; import com.vaadin.ui.components.grid.renderers.TextRenderer; @@ -92,10 +92,10 @@ public class RendererTest { private Grid grid; - private GridColumn foo; - private GridColumn bar; - private GridColumn baz; - private GridColumn bah; + private Column foo; + private Column bar; + private Column baz; + private Column bah; @Before public void setUp() { @@ -203,7 +203,7 @@ public class RendererTest { return new TestRenderer(); } - private JsonValue render(GridColumn column, Object value) { + private JsonValue render(Column column, Object value) { return RpcDataProviderExtension.encodeValue(value, column.getRenderer(), column.getConverter(), grid.getLocale()); } diff --git a/uitest/src/com/vaadin/tests/components/grid/GridColspans.java b/uitest/src/com/vaadin/tests/components/grid/GridColspans.java index c7ea61874e..602f17a15f 100644 --- a/uitest/src/com/vaadin/tests/components/grid/GridColspans.java +++ b/uitest/src/com/vaadin/tests/components/grid/GridColspans.java @@ -24,7 +24,7 @@ import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.FooterRow; -import com.vaadin.ui.Grid.GridColumn; +import com.vaadin.ui.Grid.Column; import com.vaadin.ui.Grid.HeaderRow; import com.vaadin.ui.components.grid.renderers.NumberRenderer; @@ -70,7 +70,7 @@ public class GridColspans extends AbstractTestUI { @Override public void buttonClick(ClickEvent event) { - GridColumn column = grid.getColumn("firstName"); + Column column = grid.getColumn("firstName"); column.setVisible(!column.isVisible()); } })); diff --git a/uitest/src/com/vaadin/tests/components/grid/GridSingleColumn.java b/uitest/src/com/vaadin/tests/components/grid/GridSingleColumn.java index ece60dc263..e9987db1a8 100644 --- a/uitest/src/com/vaadin/tests/components/grid/GridSingleColumn.java +++ b/uitest/src/com/vaadin/tests/components/grid/GridSingleColumn.java @@ -20,7 +20,7 @@ import com.vaadin.data.util.IndexedContainer; import com.vaadin.server.VaadinRequest; import com.vaadin.tests.components.AbstractTestUI; import com.vaadin.ui.Grid; -import com.vaadin.ui.Grid.GridColumn; +import com.vaadin.ui.Grid.Column; import com.vaadin.ui.Grid.SelectionMode; public class GridSingleColumn extends AbstractTestUI { @@ -39,7 +39,7 @@ public class GridSingleColumn extends AbstractTestUI { Grid grid = new Grid(indexedContainer); grid.setSelectionMode(SelectionMode.NONE); - GridColumn column = grid.getColumn("column1"); + Column column = grid.getColumn("column1"); column.setHeaderCaption("Header"); diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java index 980df5da85..b40dcc395d 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java @@ -39,7 +39,7 @@ import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.FooterCell; -import com.vaadin.ui.Grid.GridColumn; +import com.vaadin.ui.Grid.Column; import com.vaadin.ui.Grid.HeaderCell; import com.vaadin.ui.Grid.HeaderRow; import com.vaadin.ui.Grid.SelectionMode; @@ -420,7 +420,7 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> { public void execute(Grid grid, Boolean value, Object columnIndex) { Object propertyId = getColumnProperty((Integer) columnIndex); - GridColumn column = grid.getColumn(propertyId); + Column column = grid.getColumn(propertyId); column.setVisible(!column.isVisible()); } }, c); @@ -453,7 +453,7 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> { public void execute(Grid grid, Boolean value, Object columnIndex) { Object propertyId = getColumnProperty((Integer) columnIndex); - GridColumn column = grid.getColumn(propertyId); + Column column = grid.getColumn(propertyId); column.setSortable(value); } }, c); @@ -467,7 +467,7 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> { public void execute(Grid grid, Integer value, Object columnIndex) { Object propertyId = getColumnProperty((Integer) columnIndex); - GridColumn column = grid.getColumn(propertyId); + Column column = grid.getColumn(propertyId); column.setWidthUndefined(); } }, -1, c); @@ -480,7 +480,7 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> { public void execute(Grid grid, Integer value, Object columnIndex) { Object propertyId = getColumnProperty((Integer) columnIndex); - GridColumn column = grid.getColumn(propertyId); + Column column = grid.getColumn(propertyId); column.setWidth(value); } }, w, c); |