diff options
7 files changed, 114 insertions, 29 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/GridConnector.java b/client/src/com/vaadin/client/ui/grid/GridConnector.java index a2563488d3..67f4dc4045 100644 --- a/client/src/com/vaadin/client/ui/grid/GridConnector.java +++ b/client/src/com/vaadin/client/ui/grid/GridConnector.java @@ -36,6 +36,7 @@ import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.data.DataSource.RowHandle; import com.vaadin.client.data.RpcDataSourceConnector.RpcDataSource; import com.vaadin.client.ui.AbstractComponentConnector; +import com.vaadin.client.ui.grid.GridStaticSection.StaticRow; import com.vaadin.client.ui.grid.renderers.AbstractRendererConnector; import com.vaadin.client.ui.grid.selection.AbstractRowHandleSelectionModel; import com.vaadin.client.ui.grid.selection.SelectionChangeEvent; @@ -54,6 +55,9 @@ import com.vaadin.shared.ui.grid.GridColumnState; import com.vaadin.shared.ui.grid.GridServerRpc; import com.vaadin.shared.ui.grid.GridState; import com.vaadin.shared.ui.grid.GridState.SharedSelectionMode; +import com.vaadin.shared.ui.grid.GridStaticSectionState; +import com.vaadin.shared.ui.grid.GridStaticSectionState.CellState; +import com.vaadin.shared.ui.grid.GridStaticSectionState.RowState; import com.vaadin.shared.ui.grid.ScrollDestination; import com.vaadin.shared.ui.grid.SortDirection; @@ -262,9 +266,6 @@ public class GridConnector extends AbstractComponentConnector { getWidget().addSelectionChangeHandler(internalSelectionChangeHandler); - // TODO: Remove this workaround once we have header/footer communication - getWidget().getFooter().appendRow(); - getWidget().addSortHandler(new SortEventHandler<JSONObject>() { @Override public void sort(SortEvent<JSONObject> event) { @@ -321,14 +322,12 @@ public class GridConnector extends AbstractComponentConnector { } } - // Header - if (stateChangeEvent.hasPropertyChanged("columnHeadersVisible")) { - getWidget().getHeader().setVisible(getState().columnHeadersVisible); + if (stateChangeEvent.hasPropertyChanged("header")) { + updateSectionFromState(getWidget().getHeader(), getState().header); } - // Footer - if (stateChangeEvent.hasPropertyChanged("columnFootersVisible")) { - getWidget().getFooter().setVisible(getState().columnFootersVisible); + if (stateChangeEvent.hasPropertyChanged("footer")) { + updateSectionFromState(getWidget().getFooter(), getState().footer); } // Column row groups @@ -353,6 +352,29 @@ public class GridConnector extends AbstractComponentConnector { } } + private void updateSectionFromState(GridStaticSection<?> section, + GridStaticSectionState state) { + + while (section.getRowCount() != 0) { + section.removeRow(0); + } + + for (RowState rowState : state.rows) { + StaticRow<?> row = section.appendRow(); + + assert rowState.cells.size() == getWidget().getColumnCount(); + + int i = 0; + for (CellState cellState : rowState.cells) { + row.getCell(i++).setText(cellState.text); + } + } + + section.setVisible(state.visible); + + section.refreshGrid(); + } + /** * Updates a column from a state change event. * diff --git a/server/src/com/vaadin/ui/components/grid/Grid.java b/server/src/com/vaadin/ui/components/grid/Grid.java index c91924f5a8..e4e6dcf4be 100644 --- a/server/src/com/vaadin/ui/components/grid/Grid.java +++ b/server/src/com/vaadin/ui/components/grid/Grid.java @@ -46,6 +46,8 @@ import com.vaadin.shared.ui.grid.GridColumnState; import com.vaadin.shared.ui.grid.GridServerRpc; import com.vaadin.shared.ui.grid.GridState; import com.vaadin.shared.ui.grid.GridState.SharedSelectionMode; +import com.vaadin.shared.ui.grid.GridStaticSectionState.CellState; +import com.vaadin.shared.ui.grid.GridStaticSectionState.RowState; import com.vaadin.shared.ui.grid.HeightMode; import com.vaadin.shared.ui.grid.ScrollDestination; import com.vaadin.shared.ui.grid.SortDirection; @@ -222,6 +224,11 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier { * the data source for the grid */ public Grid(final Container.Indexed datasource) { + + getState().header.rows.add(new RowState()); + getState().footer.rows.add(new RowState()); + setColumnFootersVisible(false); + setContainerDataSource(datasource); setSelectionMode(SelectionMode.MULTI); addSelectionChangeListener(new SelectionChangeListener() { @@ -465,7 +472,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier { * <code>true</code> if the header rows should be visible */ public void setColumnHeadersVisible(boolean visible) { - getState().columnHeadersVisible = visible; + getState().header.visible = visible; } /** @@ -474,7 +481,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier { * @return <code>true</code> if the headers of the columns are visible */ public boolean isColumnHeadersVisible() { - return getState(false).columnHeadersVisible; + return getState(false).header.visible; } /** @@ -484,7 +491,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier { * <code>true</code> if the footer rows should be visible */ public void setColumnFootersVisible(boolean visible) { - getState().columnFootersVisible = visible; + getState().footer.visible = visible; } /** @@ -493,7 +500,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier { * @return <code>true</code> if the footer rows should be visible */ public boolean isColumnFootersVisible() { - return getState(false).columnFootersVisible; + return getState(false).footer.visible; } /** @@ -623,6 +630,12 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier { GridColumnState columnState = new GridColumnState(); columnState.id = columnKeys.key(datasourcePropertyId); getState().columns.add(columnState); + for (RowState row : getState().header.rows) { + row.cells.add(new CellState()); + } + for (RowState row : getState().footer.rows) { + row.cells.add(new CellState()); + } GridColumn column = new GridColumn(this, columnState); columns.put(datasourcePropertyId, column); diff --git a/server/src/com/vaadin/ui/components/grid/GridColumn.java b/server/src/com/vaadin/ui/components/grid/GridColumn.java index 43b2003e35..667b4f86db 100644 --- a/server/src/com/vaadin/ui/components/grid/GridColumn.java +++ b/server/src/com/vaadin/ui/components/grid/GridColumn.java @@ -22,6 +22,7 @@ import com.vaadin.data.util.converter.Converter; import com.vaadin.data.util.converter.ConverterUtil; import com.vaadin.server.VaadinSession; import com.vaadin.shared.ui.grid.GridColumnState; +import com.vaadin.shared.ui.grid.GridStaticSectionState.CellState; import com.vaadin.ui.UI; import com.vaadin.ui.components.grid.renderers.TextRenderer; @@ -88,7 +89,7 @@ public class GridColumn implements Serializable { */ public String getHeaderCaption() throws IllegalStateException { checkColumnIsAttached(); - return state.header; + return getHeaderCellState().text; } /** @@ -102,6 +103,7 @@ public class GridColumn implements Serializable { */ public void setHeaderCaption(String caption) throws IllegalStateException { checkColumnIsAttached(); + getHeaderCellState().text = caption; state.header = caption; grid.markAsDirty(); } @@ -116,7 +118,7 @@ public class GridColumn implements Serializable { */ public String getFooterCaption() throws IllegalStateException { checkColumnIsAttached(); - return state.footer; + return getFooterCellState().text; } /** @@ -130,10 +132,21 @@ public class GridColumn implements Serializable { */ public void setFooterCaption(String caption) throws IllegalStateException { checkColumnIsAttached(); + getFooterCellState().text = caption; state.footer = caption; grid.markAsDirty(); } + private CellState getHeaderCellState() { + int index = grid.getState().columns.indexOf(state); + return grid.getState().header.rows.get(0).cells.get(index); + } + + private CellState getFooterCellState() { + int index = grid.getState().columns.indexOf(state); + return grid.getState().footer.rows.get(0).cells.get(index); + } + /** * Returns the width (in pixels). By default a column is 100px wide. * 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 381135d7ab..9d71d21d59 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 @@ -175,30 +175,30 @@ public class GridColumns { public void testHeaderVisiblility() throws Exception { assertTrue(grid.isColumnHeadersVisible()); - assertTrue(state.columnHeadersVisible); + assertTrue(state.header.visible); grid.setColumnHeadersVisible(false); assertFalse(grid.isColumnHeadersVisible()); - assertFalse(state.columnHeadersVisible); + assertFalse(state.header.visible); grid.setColumnHeadersVisible(true); assertTrue(grid.isColumnHeadersVisible()); - assertTrue(state.columnHeadersVisible); + assertTrue(state.header.visible); } @Test public void testFooterVisibility() throws Exception { assertFalse(grid.isColumnFootersVisible()); - assertFalse(state.columnFootersVisible); + assertFalse(state.footer.visible); grid.setColumnFootersVisible(false); assertFalse(grid.isColumnFootersVisible()); - assertFalse(state.columnFootersVisible); + assertFalse(state.footer.visible); grid.setColumnFootersVisible(true); assertTrue(grid.isColumnFootersVisible()); - assertTrue(state.columnFootersVisible); + assertTrue(state.footer.visible); } @Test diff --git a/shared/src/com/vaadin/shared/ui/grid/GridColumnState.java b/shared/src/com/vaadin/shared/ui/grid/GridColumnState.java index b9bae35db6..5c0d04d968 100644 --- a/shared/src/com/vaadin/shared/ui/grid/GridColumnState.java +++ b/shared/src/com/vaadin/shared/ui/grid/GridColumnState.java @@ -37,11 +37,13 @@ public class GridColumnState implements Serializable { /** * Header caption for the column */ + @Deprecated public String header; /** * Footer caption for the column */ + @Deprecated public String footer; /** diff --git a/shared/src/com/vaadin/shared/ui/grid/GridState.java b/shared/src/com/vaadin/shared/ui/grid/GridState.java index ef1b9a806a..68ee64dfe4 100644 --- a/shared/src/com/vaadin/shared/ui/grid/GridState.java +++ b/shared/src/com/vaadin/shared/ui/grid/GridState.java @@ -98,15 +98,9 @@ public class GridState extends AbstractComponentState { */ public List<GridColumnState> columns = new ArrayList<GridColumnState>(); - /** - * Is the column header row visible - */ - public boolean columnHeadersVisible = true; + public GridStaticSectionState header = new GridStaticSectionState(); - /** - * Is the column footer row visible - */ - public boolean columnFootersVisible = false; + public GridStaticSectionState footer = new GridStaticSectionState(); /** * The column groups added to the grid diff --git a/shared/src/com/vaadin/shared/ui/grid/GridStaticSectionState.java b/shared/src/com/vaadin/shared/ui/grid/GridStaticSectionState.java new file mode 100644 index 0000000000..358c06d089 --- /dev/null +++ b/shared/src/com/vaadin/shared/ui/grid/GridStaticSectionState.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.shared.ui.grid; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +/** + * Shared state for Grid headers and footers. + * + * @since + * @author Vaadin Ltd + */ +public class GridStaticSectionState implements Serializable { + + public static class CellState implements Serializable { + public String text = ""; + } + + public static class RowState implements Serializable { + public List<CellState> cells = new ArrayList<CellState>(); + } + + public List<RowState> rows = new ArrayList<RowState>(); + + public boolean visible = true; +} |