Added overrides of insertRow and removeRow to the Grid class. Previously, Grid did not override or expose these protected methods in HTMLTable, so calling them would lead to an invalid state where Grid's numRows variable did not coorespond to the actual number of rows. This could be a breaking change because we now expose these as public methods, so subclasses of Grid that override insert/removeRow but keep them protected will result in a compiler error (reducing visibility of a method).
Patch by: jlabanca
Review by: jgw
Issue: 1384
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@3510 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/Grid.java b/user/src/com/google/gwt/user/client/ui/Grid.java
index 756788d..a535673 100644
--- a/user/src/com/google/gwt/user/client/ui/Grid.java
+++ b/user/src/com/google/gwt/user/client/ui/Grid.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2007 Google Inc.
+ * Copyright 2008 Google Inc.
*
* 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
@@ -127,6 +127,35 @@
}
/**
+ * Inserts a new row into the table. If you want to add multiple rows at once,
+ * use {@link #resize(int, int)} or {@link #resizeRows(int)} as they are more
+ * efficient.
+ *
+ * @param beforeRow the index before which the new row will be inserted
+ * @return the index of the newly-created row
+ * @throws IndexOutOfBoundsException
+ */
+ @Override
+ public int insertRow(int beforeRow) {
+ // Physically insert the row
+ int index = super.insertRow(beforeRow);
+ numRows++;
+
+ // Add the columns to the new row
+ for (int i = 0; i < numColumns; i++) {
+ insertCell(index, i);
+ }
+
+ return index;
+ }
+
+ @Override
+ public void removeRow(int row) {
+ super.removeRow(row);
+ numRows--;
+ }
+
+ /**
* Resizes the grid.
*
* @param rows the number of rows
@@ -191,7 +220,7 @@
} else {
while (numRows > rows) {
// Fewer rows. Remove extraneous ones.
- removeRow(--numRows);
+ removeRow(numRows - 1);
}
}
}
diff --git a/user/test/com/google/gwt/user/client/ui/GridTest.java b/user/test/com/google/gwt/user/client/ui/GridTest.java
index 44ed1f1..7274436 100644
--- a/user/test/com/google/gwt/user/client/ui/GridTest.java
+++ b/user/test/com/google/gwt/user/client/ui/GridTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2007 Google Inc.
+ * Copyright 2008 Google Inc.
*
* 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
@@ -19,10 +19,11 @@
import com.google.gwt.user.client.Element;
/**
- * TODO: document me.
+ * Tests for {@link Grid}.
*/
public class GridTest extends HTMLTableTestBase {
+ @Override
public HTMLTable getTable(int row, int column) {
return new Grid(row, column);
}
@@ -105,18 +106,48 @@
}
/**
+ * Verify that rows can be inserted.
+ */
+ public void testInsertion() {
+ Grid r = new Grid(4, 3);
+ assertEquals(4, r.getRowCount());
+ assertEquals(3, r.getColumnCount());
+
+ int index = r.insertRow(2);
+ assertEquals(index, 2);
+ assertEquals(5, r.getRowCount());
+ assertEquals(3, r.getColumnCount());
+ assertEquals(5, r.getDOMRowCount());
+ assertEquals(3, r.getDOMCellCount(2));
+ }
+
+ /**
* Ensures row and column counts stay in sync during resizing.
*/
public void testResizing() {
- Grid r = new Grid(4, 1);
- assertEquals(4, r.getRowCount());
- assertEquals(1, r.getColumnCount());
- r.resizeRows(0);
- assertEquals(0, r.getRowCount());
- r.resizeColumns(0);
- assertEquals(0, r.getColumnCount());
- r.resize(3, 2);
- assertEquals(3, r.getRowCount());
- assertEquals(2, r.getColumnCount());
+ {
+ // Resize using resize/resizeRows/resizeColumns
+ Grid r = new Grid(4, 1);
+ assertEquals(4, r.getRowCount());
+ assertEquals(1, r.getColumnCount());
+ r.resizeRows(0);
+ assertEquals(0, r.getRowCount());
+ r.resizeColumns(0);
+ assertEquals(0, r.getColumnCount());
+ r.resize(3, 2);
+ assertEquals(3, r.getRowCount());
+ assertEquals(2, r.getColumnCount());
+ }
+
+ {
+ // Resize using removeRow
+ Grid r = new Grid(4, 1);
+ assertEquals(4, r.getRowCount());
+ assertEquals(1, r.getColumnCount());
+ r.removeRow(2);
+ assertEquals(3, r.getRowCount());
+ assertEquals(1, r.getColumnCount());
+ assertEquals(3, r.getDOMRowCount());
+ }
}
}