Reviewers: rchandia,
Description:
Adding Column#set/getCellStyleNames() to specify style names to apply to
individual cells of a CellTable/DataGrid. Users can set a general style
name to apply to the column, or override getCellStyleNames() to provide
style names based on the row/cell value.
I added the methods to Column instead of creating a CellStyles class
(similar to RowStyles) because users will most likely want to choose
styles based on the Cell value. If we provide one CellStyles for a
CellTable, then it won't be parameterized to the Column type, making it
difficult to get the cell value out of the row value.
Please review this at http://gwt-code-reviews.appspot.com/1446817/
Affected files:
M user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java
M user/src/com/google/gwt/user/cellview/client/Column.java
M
user/test/com/google/gwt/user/cellview/client/AbstractCellTableTestBase.java
Index: user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java
===================================================================
--- user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java
(revision 10304)
+++ user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java
(working copy)
@@ -1277,6 +1277,14 @@
SafeHtmlBuilder cellBuilder = new SafeHtmlBuilder();
if (value != null) {
Context context = new Context(i, curColumn, getValueKey(value));
+
+ // Add class names specific to the cell.
+ String cellStyles = column.getCellStyleNames(context, value);
+ if (cellStyles != null) {
+ tdClasses += " " + cellStyles;
+ }
+
+ // Render the cell.
column.render(context, value, cellBuilder);
}
Index: user/src/com/google/gwt/user/cellview/client/Column.java
===================================================================
--- user/src/com/google/gwt/user/cellview/client/Column.java (revision
10304)
+++ user/src/com/google/gwt/user/cellview/client/Column.java (working copy)
@@ -41,6 +41,8 @@
*/
private final Cell<C> cell;
+ private String cellStyleNames = null;
+
/**
* The {@link FieldUpdater} used for updating values in the column.
*/
@@ -64,20 +66,36 @@
*
* @return a Cell
*/
+ @Override
public Cell<C> getCell() {
return cell;
}
/**
+ * Get extra style names that should be applied to a cell in this column.
+ *
+ * @param context the cell context
+ * @param object the base object to be updated
+ * @return the extra styles of the given row in a space-separated list,
or
+ * {@code null} if there are no extra styles for the cells in
this
+ * column
+ */
+ public String getCellStyleNames(Context context, T object) {
+ return cellStyleNames;
+ }
+
+ /**
* Returns the {@link FieldUpdater} used for updating values in the
column.
*
* @return an instance of FieldUpdater<T, C>
* @see #setFieldUpdater(FieldUpdater)
*/
+ @Override
public FieldUpdater<T, C> getFieldUpdater() {
return fieldUpdater;
}
+ @Override
public HorizontalAlignmentConstant getHorizontalAlignment() {
return hAlign;
}
@@ -85,8 +103,10 @@
/**
* Returns the column value from within the underlying data object.
*/
+ @Override
public abstract C getValue(T object);
+ @Override
public VerticalAlignmentConstant getVerticalAlignment() {
return vAlign;
}
@@ -108,15 +128,14 @@
* @param object the base object to be updated
* @param event the native browser event
*/
- public void onBrowserEvent(Context context, Element elem, final T object,
- NativeEvent event) {
+ public void onBrowserEvent(Context context, Element elem, final T
object, NativeEvent event) {
final int index = context.getIndex();
- ValueUpdater<C> valueUpdater = (fieldUpdater == null) ? null
- : new ValueUpdater<C>() {
- public void update(C value) {
- fieldUpdater.update(index, object, value);
- }
- };
+ ValueUpdater<C> valueUpdater = (fieldUpdater == null) ? null : new
ValueUpdater<C>() {
+ @Override
+ public void update(C value) {
+ fieldUpdater.update(index, object, value);
+ }
+ };
cell.onBrowserEvent(context, elem, getValue(object), event,
valueUpdater);
}
@@ -129,6 +148,21 @@
*/
public void render(Context context, T object, SafeHtmlBuilder sb) {
cell.render(context, getValue(object), sb);
+ }
+
+ /**
+ * Set extra style names that should be applied to every cell.
+ *
+ * <p>
+ * If you want to apply style names based on the row or cell value,
override
+ * {@link #getCellStyleNames(Context, Object)} directly.
+ * </p>
+ *
+ * @param styleNames the extra style names to applyin a space-separated
list,
+ * or {@code null} if there are no extra styles for this cell
+ */
+ public void setCellStyleNames(String styleNames) {
+ this.cellStyleNames = styleNames;
}
/**
@@ -149,6 +183,7 @@
* rendered.
* </p>
*/
+ @Override
public void setHorizontalAlignment(HorizontalAlignmentConstant align) {
this.hAlign = align;
}
@@ -170,6 +205,7 @@
* The new vertical alignment will apply the next time the table is
rendered.
* </p>
*/
+ @Override
public void setVerticalAlignment(VerticalAlignmentConstant align) {
this.vAlign = align;
}
Index:
user/test/com/google/gwt/user/cellview/client/AbstractCellTableTestBase.java
===================================================================
---
user/test/com/google/gwt/user/cellview/client/AbstractCellTableTestBase.java
(revision 10304)
+++
user/test/com/google/gwt/user/cellview/client/AbstractCellTableTestBase.java
(working copy)
@@ -18,6 +18,7 @@
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.TextCell;
+import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.TableCellElement;
@@ -164,6 +165,43 @@
RootPanel.get().remove(table);
}
+ public void testCellStyles() {
+ T table = createAbstractHasData();
+
+ // A column that return a static cell style.
+ TextColumn<String> col0 = new TextColumn<String>() {
+ @Override
+ public String getValue(String object) {
+ return object;
+ }
+ };
+ col0.setCellStyleNames("col0");
+ table.addColumn(col0);
+
+ // A column that returns dynamic cell styles.
+ TextColumn<String> col1 = new TextColumn<String>() {
+ @Override
+ public String getCellStyleNames(Context context, String object) {
+ return object.replace(" ", "_");
+ }
+
+ @Override
+ public String getValue(String object) {
+ return object;
+ }
+ };
+ table.addColumn(col1);
+
+ // Populate the table.
+ table.setRowData(createData(0, 10));
+ table.flush();
+
+ assertTrue(getBodyElement(table, 1, 0).getClassName().contains("
col0"));
+ assertFalse(getBodyElement(table, 1, 0).getClassName().contains("
test_1"));
+ assertFalse(getBodyElement(table, 1, 1).getClassName().contains("
col0"));
+ assertTrue(getBodyElement(table, 1, 1).getClassName().contains("
test_1"));
+ }
+
public void testGetColumnIndex() {
T table = createAbstractHasData();
Column<String, String> col0 = new IdentityColumn<String>(new
TextCell());
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors