summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Ahlroos <john@vaadin.com>2013-12-12 16:49:49 +0200
committerHenrik Paul <henrik@vaadin.com>2013-12-16 13:08:20 +0000
commit887eead6bfa56de25f5b6514b2eafe698b3e9947 (patch)
treeb7b67e57e17e35fc76399c0f217dadbfcf6221a1
parent5733fbda77f1dfdfdde57e596a79260d1e9eb549 (diff)
downloadvaadin-framework-887eead6bfa56de25f5b6514b2eafe698b3e9947.tar.gz
vaadin-framework-887eead6bfa56de25f5b6514b2eafe698b3e9947.zip
Support custom renderers for header, footer and column group cells #12993
Change-Id: I95f126ad83b9b7245dd4e123ab35c4f147e2910d
-rw-r--r--client/src/com/vaadin/client/ui/grid/ColumnGroup.java71
-rw-r--r--client/src/com/vaadin/client/ui/grid/Grid.java137
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java33
3 files changed, 234 insertions, 7 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/ColumnGroup.java b/client/src/com/vaadin/client/ui/grid/ColumnGroup.java
index 0b9cbee13c..e48656bc6b 100644
--- a/client/src/com/vaadin/client/ui/grid/ColumnGroup.java
+++ b/client/src/com/vaadin/client/ui/grid/ColumnGroup.java
@@ -21,11 +21,16 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import com.vaadin.client.ui.grid.renderers.TextRenderer;
+
/**
* Column groups are used to group columns together for adding common auxiliary
* headers and footers. Columns groups are added to {@link ColumnGroupRow
* ColumnGroupRows}.
*
+ * @param <T>
+ * The row type of the grid. The row type is the POJO type from where
+ * the data is retrieved into the column cells.
* @since 7.2
* @author Vaadin Ltd
*/
@@ -42,6 +47,16 @@ public class ColumnGroup<T> {
private String footer;
/**
+ * Renders the header cells for the column group
+ */
+ private Renderer<String> headerRenderer = new TextRenderer();
+
+ /**
+ * Renders the footer cells for the column group
+ */
+ private Renderer<String> footerRenderer = new TextRenderer();
+
+ /**
* The columns included in the group when also accounting for subgroup
* columns
*/
@@ -50,12 +65,12 @@ public class ColumnGroup<T> {
/**
* The grid associated with the column group
*/
- private final Grid grid;
+ private final Grid<T> grid;
/**
* Constructs a new column group
*/
- ColumnGroup(Grid grid, Collection<GridColumn<?, T>> columns) {
+ ColumnGroup(Grid<T> grid, Collection<GridColumn<?, T>> columns) {
if (columns == null) {
throw new IllegalArgumentException(
"columns cannot be null. Pass an empty list instead.");
@@ -114,4 +129,56 @@ public class ColumnGroup<T> {
public List<GridColumn<?, T>> getColumns() {
return columns;
}
+
+ /**
+ * Returns the renderer used for rendering the header cells
+ *
+ * @return a renderer that renders header cells
+ */
+ public Renderer<String> getHeaderRenderer() {
+ return headerRenderer;
+ }
+
+ /**
+ * Sets the renderer that renders header cells.
+ *
+ * @param renderer
+ * The renderer to use for rendering header cells. Must not be
+ * null.
+ * @throws IllegalArgumentException
+ * thrown when renderer is null
+ */
+ public void setHeaderRenderer(Renderer<String> renderer) {
+ if (renderer == null) {
+ throw new IllegalArgumentException("Renderer cannot be null.");
+ }
+ this.headerRenderer = renderer;
+ grid.refreshHeader();
+ }
+
+ /**
+ * Returns the renderer used for rendering the footer cells
+ *
+ * @return a renderer that renders footer cells
+ */
+ public Renderer<String> getFooterRenderer() {
+ return footerRenderer;
+ }
+
+ /**
+ * Sets the renderer that renders footer cells.
+ *
+ * @param renderer
+ * The renderer to use for rendering footer cells. Must not be
+ * null.
+ * @throws IllegalArgumentException
+ * thrown when renderer is null
+ */
+ public void setFooterRenderer(Renderer<String> renderer) {
+ if (renderer == null) {
+ throw new IllegalArgumentException("Renderer cannot be null.");
+ }
+ this.footerRenderer = renderer;
+ grid.refreshFooter();
+ }
}
diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java
index 7cb9db9782..2dbb0275cd 100644
--- a/client/src/com/vaadin/client/ui/grid/Grid.java
+++ b/client/src/com/vaadin/client/ui/grid/Grid.java
@@ -26,6 +26,7 @@ import com.google.gwt.user.client.ui.HasVisibility;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.data.DataChangeHandler;
import com.vaadin.client.data.DataSource;
+import com.vaadin.client.ui.grid.renderers.TextRenderer;
import com.vaadin.shared.util.SharedUtil;
/**
@@ -54,6 +55,9 @@ import com.vaadin.shared.util.SharedUtil;
* TODO Explain about what a data source is and how it should be implemented.
* </p>
*
+ * @param <T>
+ * The row type of the grid. The row type is the POJO type from where
+ * the data is retrieved into the column cells.
* @since 7.2
* @author Vaadin Ltd
*/
@@ -132,7 +136,7 @@ public class Grid<T> extends Composite {
/**
* Renderer for rendering a value into the cell
*/
- private Renderer<C> renderer = new Renderer<C>() {
+ private Renderer<C> bodyRenderer = new Renderer<C>() {
@Override
public void renderCell(Cell cell, C value) {
@@ -148,6 +152,16 @@ public class Grid<T> extends Composite {
};
/**
+ * Renderer for rendering the header cell value into the cell
+ */
+ private Renderer<String> headerRenderer = new TextRenderer();
+
+ /**
+ * Renderer for rendering the footer cell value into the cell
+ */
+ private Renderer<String> footerRenderer = new TextRenderer();
+
+ /**
* Constructs a new column.
*/
public AbstractGridColumn() {
@@ -164,7 +178,29 @@ public class Grid<T> extends Composite {
if (renderer == null) {
throw new IllegalArgumentException("Renderer cannot be null.");
}
- this.renderer = renderer;
+ this.bodyRenderer = renderer;
+ }
+
+ /**
+ * Constructs a new column with custom renderers for rows, header and
+ * footer cells.
+ *
+ * @param bodyRenderer
+ * The renderer to use for rendering body cells
+ * @param headerRenderer
+ * The renderer to use for rendering header cells
+ * @param footerRenderer
+ * The renderer to use for rendering footer cells
+ */
+ public AbstractGridColumn(Renderer<C> bodyRenderer,
+ Renderer<String> headerRenderer, Renderer<String> footerRenderer) {
+ this(bodyRenderer);
+ if (headerRenderer == null || footerRenderer == null) {
+ throw new IllegalArgumentException("Renderer cannot be null.");
+ }
+
+ this.headerRenderer = headerRenderer;
+ this.footerRenderer = footerRenderer;
}
/**
@@ -198,6 +234,56 @@ public class Grid<T> extends Composite {
}
/**
+ * Returns the renderer used for rendering the header cells
+ *
+ * @return a renderer that renders header cells
+ */
+ public Renderer<String> getHeaderRenderer() {
+ return headerRenderer;
+ }
+
+ /**
+ * Sets the renderer that renders header cells. Should not be null.
+ *
+ * @param renderer
+ * The renderer to use for rendering header cells.
+ */
+ public void setHeaderRenderer(Renderer<String> renderer) {
+ if (renderer == null) {
+ throw new IllegalArgumentException("Renderer cannot be null.");
+ }
+ headerRenderer = renderer;
+ if (grid != null) {
+ grid.refreshHeader();
+ }
+ }
+
+ /**
+ * Returns the renderer used for rendering the footer cells
+ *
+ * @return a renderer that renders footer cells
+ */
+ public Renderer<String> getFooterRenderer() {
+ return footerRenderer;
+ }
+
+ /**
+ * Sets the renderer that renders footer cells. Should not be null.
+ *
+ * @param renderer
+ * The renderer to use for rendering footer cells.
+ */
+ public void setFooterRenderer(Renderer<String> renderer) {
+ if (renderer == null) {
+ throw new IllegalArgumentException("Renderer cannot be null.");
+ }
+ footerRenderer = renderer;
+ if (grid != null) {
+ grid.refreshFooter();
+ }
+ }
+
+ /**
* Sets the text in the header of the column.
*
* @param caption
@@ -306,7 +392,7 @@ public class Grid<T> extends Composite {
* @return The renderer to render the cell content with
*/
public Renderer<C> getRenderer() {
- return renderer;
+ return bodyRenderer;
}
/**
@@ -417,6 +503,25 @@ public class Grid<T> extends Composite {
*/
public abstract boolean firstRowIsVisible();
+ /**
+ * The renderer that renders the cell
+ *
+ * @param column
+ * The column for which the cell should be rendered
+ *
+ * @return renderer used for rendering
+ */
+ public abstract Renderer<String> getRenderer(GridColumn<?, T> column);
+
+ /**
+ * The renderer that renders the cell for column groups
+ *
+ * @param group
+ * The group that should be rendered
+ * @return renderer used for rendering
+ */
+ public abstract Renderer<String> getGroupRenderer(ColumnGroup<T> group);
+
@Override
public void updateCells(Row row, Iterable<Cell> cellsToUpdate) {
@@ -433,7 +538,8 @@ public class Grid<T> extends Composite {
GridColumn<?, T> column = getColumnFromVisibleIndex(cell
.getColumn());
if (column != null) {
- cell.getElement().setInnerText(getColumnValue(column));
+ getRenderer(column).renderCell(cell,
+ getColumnValue(column));
}
}
@@ -465,7 +571,8 @@ public class Grid<T> extends Composite {
Element cellElement = cell.getElement();
if (group != null) {
- cellElement.setInnerText(getGroupValue(group));
+ getGroupRenderer(group).renderCell(cell,
+ getGroupValue(group));
cell.setColSpan(group.getColumns().size());
} else {
// Cells are reused
@@ -542,6 +649,16 @@ public class Grid<T> extends Composite {
public boolean firstRowIsVisible() {
return isColumnHeadersVisible();
}
+
+ @Override
+ public Renderer<String> getRenderer(GridColumn<?, T> column) {
+ return column.getHeaderRenderer();
+ }
+
+ @Override
+ public Renderer<String> getGroupRenderer(ColumnGroup<T> group) {
+ return group.getHeaderRenderer();
+ }
};
}
@@ -608,6 +725,16 @@ public class Grid<T> extends Composite {
public boolean firstRowIsVisible() {
return isColumnFootersVisible();
}
+
+ @Override
+ public Renderer<String> getRenderer(GridColumn<?, T> column) {
+ return column.getFooterRenderer();
+ }
+
+ @Override
+ public Renderer<String> getGroupRenderer(ColumnGroup<T> group) {
+ return group.getFooterRenderer();
+ }
};
}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java
new file mode 100644
index 0000000000..1f06a555c3
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2000-2013 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.client.ui.grid.renderers;
+
+import com.vaadin.client.ui.grid.Cell;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * Renderer that renders text into a cell.
+ *
+ * @since 7.2
+ * @author Vaadin Ltd
+ */
+public class TextRenderer implements Renderer<String> {
+
+ @Override
+ public void renderCell(Cell cell, String text) {
+ cell.getElement().setInnerText(text);
+ }
+}