From 1c1506ef0447b1d979a6adb4d812ae9858f00b67 Mon Sep 17 00:00:00 2001
From: John Ahlroos
+ * Example usage: + * + *
+ * // Add a new column group row to the grid + * ColumnGroupRow row = grid.addColumnGroupRow(); + * + * // Group "Column1" and "Column2" together to form a header in the row + * ColumnGroup column12 = row.addGroup("Column1", "Column2"); + * + * // Set a common header for "Column1" and "Column2" + * column12.setHeader("Column 1&2"); + *+ * + * + * + * @return a column group instance you can use to add column groups + */ + public ColumnGroupRow addColumnGroupRow() { + ColumnGroupRowState state = new ColumnGroupRowState(); + ColumnGroupRow row = new ColumnGroupRow(this, state, columnKeys); + columnGroupRows.add(row); + getState().columnGroupRows.add(state); + return row; + } + + /** + * Adds a new column group to the grid at a specific index + * + * @param rowIndex + * the index of the row + * @return a column group instance you can use to add column groups + */ + public ColumnGroupRow addColumnGroupRow(int rowIndex) { + ColumnGroupRowState state = new ColumnGroupRowState(); + ColumnGroupRow row = new ColumnGroupRow(this, state, columnKeys); + columnGroupRows.add(rowIndex, row); + getState().columnGroupRows.add(rowIndex, state); + return row; + } + + /** + * Removes a column group. + * + * @param row + * the row to remove + */ + public void removeColumnGroupRow(ColumnGroupRow row) { + columnGroupRows.remove(row); + getState().columnGroupRows.remove(row.getState()); + } + + /** + * Gets the column group rows. + * + * @return an unmodifiable list of column group rows + */ + public List
null
if not found
+ * @return the column with the id or null
if not found
*/
GridColumn getColumnByColumnId(String columnId) {
- Object propertyId = columnKeys.get(columnId);
+ Object propertyId = getPropertyIdByColumnId(columnId);
return getColumn(propertyId);
}
+ /**
+ * Used internally by the {@link Grid} to get a property id by referencing
+ * the columns generated state id.
+ *
+ * @param columnId
+ * The state id of the column
+ * @return The column instance or null if not found
+ */
+ Object getPropertyIdByColumnId(String columnId) {
+ return columnKeys.get(columnId);
+ }
+
@Override
protected GridState getState() {
return (GridState) super.getState();
@@ -241,7 +337,7 @@ public class Grid extends AbstractComponent {
* @param datasourcePropertyId
* The property id of a property in the datasource
*/
- protected GridColumn appendColumn(Object datasourcePropertyId) {
+ private GridColumn appendColumn(Object datasourcePropertyId) {
if (datasourcePropertyId == null) {
throw new IllegalArgumentException("Property id cannot be null");
}
diff --git a/server/src/com/vaadin/ui/components/grid/GridColumn.java b/server/src/com/vaadin/ui/components/grid/GridColumn.java
index 505919b3cf..dde0669238 100644
--- a/server/src/com/vaadin/ui/components/grid/GridColumn.java
+++ b/server/src/com/vaadin/ui/components/grid/GridColumn.java
@@ -16,6 +16,8 @@
package com.vaadin.ui.components.grid;
+import java.io.Serializable;
+
import com.vaadin.shared.ui.grid.GridColumnState;
/**
@@ -25,10 +27,10 @@ import com.vaadin.shared.ui.grid.GridColumnState;
* @since 7.2
* @author Vaadin Ltd
*/
-public class GridColumn {
+public class GridColumn implements Serializable {
/**
- * The shared state of the column
+ * The state of the column shared to the client
*/
private final GridColumnState state;
@@ -138,9 +140,16 @@ public class GridColumn {
* the new pixel width of the column
* @throws IllegalStateException
* if the column is no longer attached to any grid
+ * @throws IllegalArgumentException
+ * thrown if pixel width is less than zero
*/
- public void setWidth(int pixelWidth) throws IllegalStateException {
+ public void setWidth(int pixelWidth) throws IllegalStateException,
+ IllegalArgumentException {
checkColumnIsAttached();
+ if (pixelWidth < 0) {
+ throw new IllegalArgumentException(
+ "Pixel width should be greated than 0");
+ }
state.width = pixelWidth;
grid.markAsDirty();
}
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 5989d537b4..85864160a8 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
@@ -32,6 +32,8 @@ import com.vaadin.data.util.IndexedContainer;
import com.vaadin.server.KeyMapper;
import com.vaadin.shared.ui.grid.GridColumnState;
import com.vaadin.shared.ui.grid.GridState;
+import com.vaadin.ui.components.grid.ColumnGroup;
+import com.vaadin.ui.components.grid.ColumnGroupRow;
import com.vaadin.ui.components.grid.Grid;
import com.vaadin.ui.components.grid.GridColumn;
@@ -110,9 +112,15 @@ public class GridColumns {
assertEquals(100, column.getWidth());
assertEquals(column.getWidth(), getColumnState("column1").width);
- column.setWidth(-1);
- assertEquals(-1, column.getWidth());
- assertEquals(-1, getColumnState("column1").width);
+ try {
+ column.setWidth(-1);
+ fail("Setting width to -1 should throw exception");
+ } catch (IllegalArgumentException iae) {
+
+ }
+
+ assertEquals(100, column.getWidth());
+ assertEquals(100, getColumnState("column1").width);
}
@Test
@@ -126,6 +134,7 @@ public class GridColumns {
try {
column.setHeaderCaption("asd");
+
fail("Succeeded in modifying a detached column");
} catch (IllegalStateException ise) {
// Detached state should throw exception
@@ -157,7 +166,7 @@ public class GridColumns {
}
@Test
- public void testAddingColumn() {
+ public void testAddingColumn() throws Exception {
grid.getContainerDatasource().addContainerProperty("columnX",
String.class, "");
GridColumn column = grid.getColumn("columnX");
@@ -165,33 +174,72 @@ public class GridColumns {
}
@Test
- public void testHeaderVisiblility() {
+ public void testHeaderVisiblility() throws Exception {
- assertTrue(grid.isHeaderVisible());
- assertTrue(state.headerVisible);
+ assertTrue(grid.isColumnHeadersVisible());
+ assertTrue(state.columnHeadersVisible);
- grid.setHeaderVisible(false);
- assertFalse(grid.isHeaderVisible());
- assertFalse(state.headerVisible);
+ grid.setColumnHeadersVisible(false);
+ assertFalse(grid.isColumnHeadersVisible());
+ assertFalse(state.columnHeadersVisible);
- grid.setHeaderVisible(true);
- assertTrue(grid.isHeaderVisible());
- assertTrue(state.headerVisible);
+ grid.setColumnHeadersVisible(true);
+ assertTrue(grid.isColumnHeadersVisible());
+ assertTrue(state.columnHeadersVisible);
}
@Test
- public void testFooterVisibility() {
+ public void testFooterVisibility() throws Exception {
+
+ assertFalse(grid.isColumnFootersVisible());
+ assertFalse(state.columnFootersVisible);
- assertTrue(grid.isFooterVisible());
- assertTrue(state.footerVisible);
+ grid.setColumnFootersVisible(false);
+ assertFalse(grid.isColumnFootersVisible());
+ assertFalse(state.columnFootersVisible);
- grid.setFooterVisible(false);
- assertFalse(grid.isFooterVisible());
- assertFalse(state.footerVisible);
+ grid.setColumnFootersVisible(true);
+ assertTrue(grid.isColumnFootersVisible());
+ assertTrue(state.columnFootersVisible);
+ }
- grid.setFooterVisible(true);
- assertTrue(grid.isFooterVisible());
- assertTrue(state.footerVisible);
+ @Test
+ public void testColumnGroups() throws Exception {
+
+ // Add a new row
+ ColumnGroupRow row = grid.addColumnGroupRow();
+ assertTrue(state.columnGroupRows.size() == 1);
+
+ // Add a group by property id
+ ColumnGroup columns12 = row.addGroup("column1", "column2");
+ assertTrue(state.columnGroupRows.get(0).groups.size() == 1);
+
+ // Set header of column
+ columns12.setHeaderCaption("Column12");
+ assertEquals("Column12",
+ state.columnGroupRows.get(0).groups.get(0).header);
+
+ // Set footer of column
+ columns12.setFooterCaption("Footer12");
+ assertEquals("Footer12",
+ state.columnGroupRows.get(0).groups.get(0).footer);
+
+ // Add another group by column instance
+ ColumnGroup columns34 = row.addGroup(grid.getColumn("column3"),
+ grid.getColumn("column4"));
+ assertTrue(state.columnGroupRows.get(0).groups.size() == 2);
+
+ // add another group row
+ ColumnGroupRow row2 = grid.addColumnGroupRow();
+ assertTrue(state.columnGroupRows.size() == 2);
+
+ // add a group by combining the two previous groups
+ ColumnGroup columns1234 = row2.addGroup(columns12, columns34);
+ assertTrue(columns1234.getColumns().size() == 4);
+
+ // Insert a group as the second group
+ ColumnGroupRow newRow2 = grid.addColumnGroupRow(1);
+ assertTrue(state.columnGroupRows.size() == 3);
}
private GridColumnState getColumnState(Object propertyId) {
diff --git a/shared/src/com/vaadin/shared/ui/grid/ColumnGroupRowState.java b/shared/src/com/vaadin/shared/ui/grid/ColumnGroupRowState.java
new file mode 100644
index 0000000000..a8e0f87457
--- /dev/null
+++ b/shared/src/com/vaadin/shared/ui/grid/ColumnGroupRowState.java
@@ -0,0 +1,46 @@
+/*
+ * 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.shared.ui.grid;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * The column group row data shared between the server and client
+ *
+ * @since 7.2
+ * @author Vaadin Ltd
+ */
+public class ColumnGroupRowState implements Serializable {
+
+ /**
+ * The groups that has been added to the row
+ */
+ public List
+ * All columns up to and including the given column will be frozen in place
+ * when the grid is scrolled sideways.
+ *
+ * @param lastFrozenColumn
+ * the rightmost column to freeze, or
+ * Note: Most usually, this method returns the very value set with
+ * {@link #setLastFrozenColumn(GridColumn)}. This value, however, can be
+ * reset to
+ * All columns up to and including the given column will be frozen in place
+ * when the grid is scrolled sideways.
+ *
+ * @param lastFrozenColumn
+ * the rightmost column to freeze, or
+ * All columns up to and including the indicated property will be frozen in
+ * place when the grid is scrolled sideways.
+ *
+ * Note: If the container used by this grid supports a propertyId
+ *
+ * Note: Most often, this method returns the very value set with
+ * {@link #setLastFrozenPropertyId(Object)}. This value, however, can be
+ * reset to null
to not
+ * have any columns frozen
+ * @throws IllegalArgumentException
+ * if {@code lastFrozenColumn} is not a column from this grid
+ */
+ public void setLastFrozenColumn(GridColumnnull
if the column is removed from this grid.
+ *
+ * @return the rightmost frozen column in the grid, or null
if
+ * no columns are frozen.
+ */
+ public GridColumnnull
to not
+ * have any columns frozen
+ * @throws IllegalArgumentException
+ * if {@code lastFrozenColumn} is not a column from this grid
+ */
+ void setLastFrozenColumn(GridColumn lastFrozenColumn) {
+ /*
+ * TODO: If and when Grid supports column reordering or insertion of
+ * columns before other columns, make sure to mention that adding
+ * columns before lastFrozenColumn will change the frozen column count
+ */
+
+ if (lastFrozenColumn == null) {
+ getState().lastFrozenColumnId = null;
+ } else if (columns.containsValue(lastFrozenColumn)) {
+ getState().lastFrozenColumnId = lastFrozenColumn.getState().id;
+ } else {
+ throw new IllegalArgumentException(
+ "The given column isn't attached to this grid");
+ }
+ }
+
+ /**
+ * Sets (or unsets) the rightmost frozen column in the grid.
+ * null
, it can never be defined as the last frozen column, as
+ * a null
parameter will always reset the frozen columns in
+ * Grid.
+ *
+ * @param propertyId
+ * the property id corresponding to the column that should be the
+ * last frozen column, or null
to not have any
+ * columns frozen.
+ * @throws IllegalArgumentException
+ * if {@code lastFrozenColumn} is not a column from this grid
+ */
+ public void setLastFrozenPropertyId(Object propertyId) {
+ final GridColumn column;
+ if (propertyId == null) {
+ column = null;
+ } else {
+ column = getColumn(propertyId);
+ if (column == null) {
+ throw new IllegalArgumentException(
+ "property id does not exist.");
+ }
+ }
+ setLastFrozenColumn(column);
+ }
+
+ /**
+ * Gets the rightmost frozen column in the grid.
+ * null
if the column is detached from this grid.
+ *
+ * @return the rightmost frozen column in the grid, or null
if
+ * no columns are frozen.
+ */
+ public Object getLastFrozenPropertyId() {
+ return columnKeys.get(getState().lastFrozenColumnId);
+ }
}
diff --git a/server/src/com/vaadin/ui/components/grid/GridColumn.java b/server/src/com/vaadin/ui/components/grid/GridColumn.java
index dde0669238..8dae9428e5 100644
--- a/server/src/com/vaadin/ui/components/grid/GridColumn.java
+++ b/server/src/com/vaadin/ui/components/grid/GridColumn.java
@@ -192,4 +192,16 @@ public class GridColumn implements Serializable {
throw new IllegalStateException("Column no longer exists.");
}
}
+
+ /**
+ * Sets this column as the last frozen column in its grid.
+ *
+ * @throws IllegalArgumentException
+ * if the column is no longer attached to any grid
+ * @see Grid#setLastFrozenColumn(GridColumn)
+ */
+ public void setLastFrozenColumn() {
+ checkColumnIsAttached();
+ grid.setLastFrozenColumn(this);
+ }
}
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 85864160a8..c129db0264 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
@@ -242,6 +242,20 @@ public class GridColumns {
assertTrue(state.columnGroupRows.size() == 3);
}
+ @Test
+ public void testFrozenColumnByPropertyId() {
+ assertNull("Grid should not start with a frozen column",
+ grid.getLastFrozenPropertyId());
+
+ Object propertyId = grid.getContainerDatasource()
+ .getContainerPropertyIds().iterator().next();
+ grid.setLastFrozenPropertyId(propertyId);
+ assertEquals(propertyId, grid.getLastFrozenPropertyId());
+
+ grid.getContainerDatasource().removeContainerProperty(propertyId);
+ assertNull(grid.getLastFrozenPropertyId());
+ }
+
private GridColumnState getColumnState(Object propertyId) {
String columnId = columnIdMapper.key(propertyId);
for (GridColumnState columnState : state.columns) {
diff --git a/shared/src/com/vaadin/shared/ui/grid/GridState.java b/shared/src/com/vaadin/shared/ui/grid/GridState.java
index d1167f3d4f..93e602a539 100644
--- a/shared/src/com/vaadin/shared/ui/grid/GridState.java
+++ b/shared/src/com/vaadin/shared/ui/grid/GridState.java
@@ -53,4 +53,12 @@ public class GridState extends AbstractComponentState {
* The column groups added to the grid
*/
public Listtrue
if the column is included in a group
*/
private boolean hasColumnBeenGrouped(Object propertyId) {
+ return getGroupForProperty(propertyId) != null;
+ }
+
+ private ColumnGroup getGroupForProperty(Object propertyId) {
for (ColumnGroup group : groups) {
if (group.isColumnInGroup(propertyId)) {
- return true;
+ return group;
}
}
- return false;
+ return null;
}
/**
diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/GridColumnGroups.java b/server/tests/src/com/vaadin/tests/server/component/grid/GridColumnGroups.java
index 11dc48f7d5..4350bf1a7b 100644
--- a/server/tests/src/com/vaadin/tests/server/component/grid/GridColumnGroups.java
+++ b/server/tests/src/com/vaadin/tests/server/component/grid/GridColumnGroups.java
@@ -233,4 +233,33 @@ public class GridColumnGroups {
}
}
+
+ @Test
+ public void testColumnGroupLimits() throws Exception {
+
+ ColumnGroupRow row = grid.addColumnGroupRow();
+ row.addGroup("column1", "column2");
+ row.addGroup("column3", "column4");
+
+ try {
+ row.addGroup("column2", "column3");
+ fail("Adding a group with already grouped properties should throw exception");
+ } catch (IllegalArgumentException iae) {
+
+ }
+
+ ColumnGroupRow row2 = grid.addColumnGroupRow();
+
+ try {
+ row2.addGroup("column2", "column3");
+ fail("Adding a group that breaks previous grouping boundaries should throw exception");
+ } catch (IllegalArgumentException iae) {
+
+ }
+
+ // This however should not throw an exception as it spans completely
+ // over the parent rows groups
+ row2.addGroup("column1", "column2", "column3", "column4");
+
+ }
}
--
cgit v1.2.3