diff options
author | Leif Åstrand <leif@vaadin.com> | 2014-12-09 21:05:22 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-12-09 19:25:47 +0000 |
commit | d8c47250a2a8a47fd75e9f48f21aac1ff6aa2170 (patch) | |
tree | c9cae6bb22e5cb7cf556dd31cb59befab3531173 /server/src/com | |
parent | 4157a770fd0df18f6f57b9b9cb017985251f4d0b (diff) | |
download | vaadin-framework-d8c47250a2a8a47fd75e9f48f21aac1ff6aa2170.tar.gz vaadin-framework-d8c47250a2a8a47fd75e9f48f21aac1ff6aa2170.zip |
Add Grid.addRow (#13334)
Change-Id: I2c317b920d29ca8a74658ef3b980c6bc2a7622ac
Diffstat (limited to 'server/src/com')
-rw-r--r-- | server/src/com/vaadin/data/util/IndexedContainer.java | 27 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/Grid.java | 77 |
2 files changed, 92 insertions, 12 deletions
diff --git a/server/src/com/vaadin/data/util/IndexedContainer.java b/server/src/com/vaadin/data/util/IndexedContainer.java index f9cc4c482a..b851baf674 100644 --- a/server/src/com/vaadin/data/util/IndexedContainer.java +++ b/server/src/com/vaadin/data/util/IndexedContainer.java @@ -242,10 +242,11 @@ public class IndexedContainer extends return true; } - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container#addItem() + /** + * {@inheritDoc} + * <p> + * The item ID is generated from a sequence of Integers. The id of the first + * added item is 1. */ @Override public Object addItem() { @@ -363,10 +364,11 @@ public class IndexedContainer extends new IndexedContainerItem(newItemId), true); } - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object) + /** + * {@inheritDoc} + * <p> + * The item ID is generated from a sequence of Integers. The id of the first + * added item is 1. */ @Override public Object addItemAfter(Object previousItemId) { @@ -392,10 +394,11 @@ public class IndexedContainer extends newItemId), true); } - /* - * (non-Javadoc) - * - * @see com.vaadin.data.Container.Indexed#addItemAt(int) + /** + * {@inheritDoc} + * <p> + * The item ID is generated from a sequence of Integers. The id of the first + * added item is 1. */ @Override public Object addItemAt(int index) { diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 1fad4d2304..5f3bea98ca 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -31,6 +31,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; import com.google.gwt.thirdparty.guava.common.collect.Sets; import com.google.gwt.thirdparty.guava.common.collect.Sets.SetView; @@ -3592,4 +3594,79 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, public CellStyleGenerator getCellStyleGenerator() { return cellStyleGenerator; } + + /** + * Adds a row to the underlying container. The order of the parameters + * should match the current visible column order. + * <p> + * Please note that it's generally only safe to use this method during + * initialization. After Grid has been initialized and the visible column + * order might have been changed, it's better to instead add items directly + * to the underlying container and use {@link Item#getItemProperty(Object)} + * to make sure each value is assigned to the intended property. + * + * @param values + * the cell values of the new row, in the same order as the + * visible column order, not <code>null</code>. + * @return the item id of the new row + * @throws IllegalArgumentException + * if values is null + * @throws IllegalArgumentException + * if its length does not match the number of visible columns + * @throws IllegalArgumentException + * if a parameter value is not an instance of the corresponding + * property type + * @throws UnsupportedOperationException + * if the container does not support adding new items + */ + public Object addRow(Object... values) { + if (values == null) { + throw new IllegalArgumentException("Values cannot be null"); + } + + Indexed dataSource = getContainerDataSource(); + List<String> columnOrder = getState(false).columnOrder; + + if (values.length != columnOrder.size()) { + throw new IllegalArgumentException("There are " + + columnOrder.size() + " visible columns, but " + + values.length + " cell values were provided."); + } + + // First verify all parameter types + for (int i = 0; i < columnOrder.size(); i++) { + Object propertyId = getPropertyIdByColumnId(columnOrder.get(i)); + + Class<?> propertyType = dataSource.getType(propertyId); + if (values[i] != null && !propertyType.isInstance(values[i])) { + throw new IllegalArgumentException("Parameter " + i + "(" + + values[i] + ") is not an instance of " + + propertyType.getCanonicalName()); + } + } + + Object itemId = dataSource.addItem(); + try { + Item item = dataSource.getItem(itemId); + for (int i = 0; i < columnOrder.size(); i++) { + Object propertyId = getPropertyIdByColumnId(columnOrder.get(i)); + Property<Object> property = item.getItemProperty(propertyId); + property.setValue(values[i]); + } + } catch (RuntimeException e) { + try { + dataSource.removeItem(itemId); + } catch (Exception e2) { + getLogger().log(Level.SEVERE, + "Error recovering from exception in addRow", e); + } + throw e; + } + + return itemId; + } + + private static Logger getLogger() { + return Logger.getLogger(Grid.class.getName()); + } } |