Revision: 10309
Author:   jlaba...@google.com
Date:     Thu Jun  9 09:55:08 2011
Log: 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.

Review at http://gwt-code-reviews.appspot.com/1446817

Review by: rchan...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=10309

Modified:
 /trunk/user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java
 /trunk/user/src/com/google/gwt/user/cellview/client/Column.java
/trunk/user/test/com/google/gwt/user/cellview/client/AbstractCellTableTestBase.java

=======================================
--- /trunk/user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java Wed Jun 1 10:50:41 2011 +++ /trunk/user/src/com/google/gwt/user/cellview/client/AbstractCellTable.java Thu Jun 9 09:55:08 2011
@@ -1273,10 +1273,16 @@
         if (curColumn == columnCount - 1) {
           tdClasses += lastColumnStyle;
         }
+
+        // Add class names specific to the cell.
+        Context context = new Context(i, curColumn, getValueKey(value));
+        String cellStyles = column.getCellStyleNames(context, value);
+        if (cellStyles != null) {
+          tdClasses += " " + cellStyles;
+        }

         SafeHtmlBuilder cellBuilder = new SafeHtmlBuilder();
         if (value != null) {
-          Context context = new Context(i, curColumn, getValueKey(value));
           column.render(context, value, cellBuilder);
         }

=======================================
--- /trunk/user/src/com/google/gwt/user/cellview/client/Column.java Wed Jan 5 04:42:33 2011 +++ /trunk/user/src/com/google/gwt/user/cellview/client/Column.java Thu Jun 9 09:55:08 2011
@@ -41,6 +41,8 @@
    */
   private final Cell<C> cell;

+  private String cellStyleNames = null;
+
   /**
    * The {@link FieldUpdater} used for updating values in the column.
    */
@@ -64,9 +66,23 @@
    *
    * @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, or null if the row is empty + * @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.
@@ -74,10 +90,12 @@
    * @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);
   }

@@ -130,6 +149,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;
+  }

   /**
    * Set the {@link FieldUpdater} used for updating values in the column.
@@ -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;
   }
=======================================
--- /trunk/user/test/com/google/gwt/user/cellview/client/AbstractCellTableTestBase.java Wed Jun 1 10:50:41 2011 +++ /trunk/user/test/com/google/gwt/user/cellview/client/AbstractCellTableTestBase.java Thu Jun 9 09:55:08 2011
@@ -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;
@@ -163,6 +164,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();

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to