}
};
column.setRenderer(getRendererConnector().getRenderer());
- getParent().addColumn(column, getState().id);
+ getParent().addColumn(column, getState().internalId);
}
@SuppressWarnings("unchecked")
import com.vaadin.shared.ui.grid.GridStaticCellType;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.shared.ui.grid.SectionState;
-import com.vaadin.shared.util.SharedUtil;
import com.vaadin.ui.Grid.FooterRow;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.components.grid.AbstractSelectionModel;
/**
* Generates the sort orders when rows are sorted by a column.
- *
+ *
* @see Column#setSortOrderProvider
*
* @since 8.0
protected AbstractGridExtensionState getState(boolean markAsDirty) {
return (AbstractGridExtensionState) super.getState(markAsDirty);
}
+
+ protected String getInternalIdForColumn(Column<T, ?> column) {
+ return getParent().getInternalIdForColumn(column);
+ }
}
private final class GridServerRpcImpl implements GridServerRpc {
@Override
- public void sort(String[] columnIds, SortDirection[] directions,
+ public void sort(String[] columnInternalIds, SortDirection[] directions,
boolean isUserOriginated) {
- assert columnIds.length == directions.length : "Column and sort direction counts don't match.";
+
+ assert columnInternalIds.length == directions.length : "Column and sort direction counts don't match.";
List<SortOrder<Column<T, ?>>> list = new ArrayList<>(
directions.length);
- for (int i = 0; i < columnIds.length; ++i) {
- Column<T, ?> column = columnKeys.get(columnIds[i]);
+ for (int i = 0; i < columnInternalIds.length; ++i) {
+ Column<T, ?> column = columnKeys.get(columnInternalIds[i]);
list.add(new SortOrder<>(column, directions[i]));
}
setSortOrder(list, isUserOriginated);
}
@Override
- public void itemClick(String rowKey, String columnId,
+ public void itemClick(String rowKey, String columnInternalId,
MouseEventDetails details) {
- Column<T, ?> column = columnKeys.containsKey(columnId)
- ? columnKeys.get(columnId) : null;
+ Column<T, ?> column = getColumnByInternalId(columnInternalId);
T item = getDataCommunicator().getKeyMapper().get(rowKey);
fireEvent(new ItemClick<>(Grid.this, column, item, details));
}
@Override
- public void contextClick(int rowIndex, String rowKey, String columnId,
- Section section, MouseEventDetails details) {
+ public void contextClick(int rowIndex, String rowKey,
+ String columnInternalId, Section section,
+ MouseEventDetails details) {
T item = null;
if (rowKey != null) {
item = getDataCommunicator().getKeyMapper().get(rowKey);
}
fireEvent(new GridContextClickEvent<>(Grid.this, details, section,
- rowIndex, item, getColumn(columnId)));
+ rowIndex, item, getColumnByInternalId(columnInternalId)));
}
@Override
}
@Override
- public void columnVisibilityChanged(String id, boolean hidden) {
- Column<T, ?> column = getColumn(id);
+ public void columnVisibilityChanged(String internalId, boolean hidden) {
+ Column<T, ?> column = getColumnByInternalId(internalId);
ColumnState columnState = column.getState(false);
if (columnState.hidden != hidden) {
columnState.hidden = hidden;
}
@Override
- public void columnResized(String id, double pixels) {
- final Column<T, ?> column = getColumn(id);
+ public void columnResized(String internalId, double pixels) {
+ final Column<T, ?> column = getColumnByInternalId(internalId);
if (column != null && column.isResizable()) {
column.getState().width = pixels;
fireColumnResizeEvent(column, true);
private EditorComponentGenerator<T> componentGenerator;
+ private String userId;
+
/**
* Constructs a new Column configuration with given header caption,
* renderer and value provider.
*
- * @param caption
- * the header caption
* @param valueProvider
* the function to get values from items
* @param renderer
protected Column(String caption,
ValueProvider<T, ? extends V> valueProvider,
Renderer<V> renderer) {
- Objects.requireNonNull(caption, "Header caption can't be null");
Objects.requireNonNull(valueProvider,
"Value provider can't be null");
Objects.requireNonNull(renderer, "Renderer can't be null");
this.valueProvider = valueProvider;
state.renderer = renderer;
- state.caption = caption;
+ state.caption = "";
sortOrderProvider = d -> Stream.of();
// Add the renderer as a child extension of this extension, thus
*
* @return the identifier string
*/
- public String getId() {
- return getState(false).id;
+ private String getInternalId() {
+ return getState(false).internalId;
}
/**
* @param id
* the identifier string
*/
- private void setId(String id) {
+ private void setInternalId(String id) {
Objects.requireNonNull(id, "Communication ID can't be null");
- getState().id = id;
+ getState().internalId = id;
+ }
+
+ /**
+ * Returns the user-defined identifier for this column.
+ *
+ * @return the identifier string
+ */
+ public String getId() {
+ return userId;
+ }
+
+ /**
+ * Sets the user-defined identifier to map this column. The identifier
+ * can be used for example in {@link Grid#getColumn(String)}.
+ *
+ * @param id
+ * the identifier string
+ */
+ public Column<T, V> setId(String id) {
+ Objects.requireNonNull(id, "Column identifier cannot be null");
+ if (this.userId != null) {
+ throw new IllegalStateException(
+ "Column identifier cannot be changed");
+ }
+ this.userId = id;
+ getParent().setColumnId(id, this);
+ return this;
}
/**
HeaderRow row = getParent().getDefaultHeaderRow();
if (row != null) {
- row.getCell(getId()).setText(caption);
+ row.getCell(this).setText(caption);
}
return this;
ColumnState defaultState = new ColumnState();
+ if (getId() == null) {
+ setId("column" + getParent().getColumns().indexOf(this));
+ }
+
DesignAttributeHandler.writeAttribute("column-id", attributes,
getId(), null, String.class, designContext);
* @throws IllegalArgumentException
* if there is no such column in the grid
*/
- public default HeaderCell getCell(Column<?, ?> column) {
- return getCell(column.getId());
- }
+ public HeaderCell getCell(Column<?, ?> column);
/**
* Merges column cells in the row. Original cells are hidden, and new
* @throws IllegalArgumentException
* if there is no such column in the grid
*/
- public default FooterCell getCell(Column<?, ?> column) {
- return getCell(column.getId());
- }
+ public FooterCell getCell(Column<?, ?> column);
/**
* Merges column cells in the row. Original cells are hidden, and new
}
@Override
- protected Collection<Column<T, ?>> getColumns() {
- return Grid.this.getColumns();
+ protected Column<?, ?> getColumnByInternalId(String internalId) {
+ return getGrid().getColumnByInternalId(internalId);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected String getInternalIdForColumn(Column<?, ?> column) {
+ return getGrid().getInternalIdForColumn((Column<T, ?>) column);
}
};
}
@Override
- protected Collection<Column<T, ?>> getColumns() {
- return Grid.this.getColumns();
+ protected Column<?, ?> getColumnByInternalId(String internalId) {
+ return getGrid().getColumnByInternalId(internalId);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected String getInternalIdForColumn(Column<?, ?> column) {
+ return getGrid().getInternalIdForColumn((Column<T, ?>) column);
}
};
private final Set<Column<T, ?>> columnSet = new LinkedHashSet<>();
private final Map<String, Column<T, ?>> columnKeys = new HashMap<>();
+ private final Map<String, Column<T, ?>> columnIds = new HashMap<>();
private final List<SortOrder<Column<T, ?>>> sortOrder = new ArrayList<>();
private final DetailsManager<T> detailsManager;
userOriginated));
}
- /**
- * Adds a new column to this {@link Grid} with given identifier, typed
- * renderer and value provider.
- *
- * @param identifier
- * the identifier in camel case for the new column
- * @param valueProvider
- * the value provider
- * @param renderer
- * the column value class
- * @param <V>
- * the column value type
- *
- * @return the new column
- * @throws IllegalArgumentException
- * if the same identifier is used for multiple columns
- *
- * @see AbstractRenderer
- */
- public <V> Column<T, V> addColumn(String identifier,
- ValueProvider<T, ? extends V> valueProvider,
- AbstractRenderer<? super T, V> renderer)
- throws IllegalArgumentException {
- if (columnKeys.containsKey(identifier)) {
- throw new IllegalArgumentException(
- "Multiple columns with the same identifier: " + identifier);
- }
-
- final Column<T, V> column = new Column<>(
- SharedUtil.camelCaseToHumanFriendly(identifier), valueProvider,
- renderer);
- addColumn(identifier, column);
- return column;
- }
-
- /**
- * Adds a new text column to this {@link Grid} with given identifier and
- * string value provider. The column will use a {@link TextRenderer}.
- *
- * @param identifier
- * the header caption
- * @param valueProvider
- * the value provider
- *
- * @return the new column
- * @throws IllegalArgumentException
- * if the same identifier is used for multiple columns
- */
- public Column<T, String> addColumn(String identifier,
- ValueProvider<T, String> valueProvider) {
- return addColumn(identifier, valueProvider, new TextRenderer());
- }
-
/**
* Adds a new text column to this {@link Grid} with a value provider. The
* column will use a {@link TextRenderer}. The value is converted to a
* String using {@link Object#toString()}. Sorting in memory is executed by
- * comparing the String values. Identifier for the column is generated
- * automatically.
+ * comparing the String values.
*
* @param valueProvider
* the value provider
* @return the new column
*/
public Column<T, String> addColumn(ValueProvider<T, String> valueProvider) {
- return addColumn(getGeneratedIdentifier(),
- t -> String.valueOf(valueProvider.apply(t)),
+ return addColumn(t -> String.valueOf(valueProvider.apply(t)),
new TextRenderer());
}
/**
* Adds a new column to this {@link Grid} with typed renderer and value
- * provider. Identifier for the column is generated automatically.
+ * provider.
*
* @param valueProvider
* the value provider
public <V> Column<T, V> addColumn(
ValueProvider<T, ? extends V> valueProvider,
AbstractRenderer<? super T, V> renderer) {
- return addColumn(getGeneratedIdentifier(), valueProvider, renderer);
+ String generatedIdentifier = getGeneratedIdentifier();
+ Column<T, V> column = new Column<>("Column " + generatedIdentifier,
+ valueProvider, renderer);
+ addColumn(generatedIdentifier, column);
+ return column;
}
private void addColumn(String identifier, Column<T, ?> column) {
column.extend(this);
columnSet.add(column);
columnKeys.put(identifier, column);
- column.setId(identifier);
+ column.setInternalId(identifier);
addDataGenerator(column);
getState().columnOrder.add(identifier);
getHeader().addColumn(identifier);
if (getDefaultHeaderRow() != null) {
- getDefaultHeaderRow().getCell(identifier)
- .setText(column.getCaption());
+ getDefaultHeaderRow().getCell(column).setText(column.getCaption());
}
}
*/
public void removeColumn(Column<T, ?> column) {
if (columnSet.remove(column)) {
- String columnId = column.getId();
+ String columnId = column.getInternalId();
columnKeys.remove(columnId);
+ columnIds.remove(column.getId());
column.remove();
getHeader().removeColumn(columnId);
getFooter().removeColumn(columnId);
*/
public List<Column<T, ?>> getColumns() {
return Collections.unmodifiableList(getState(false).columnOrder.stream()
- .map(this::getColumn).collect(Collectors.toList()));
+ .map(columnKeys::get).collect(Collectors.toList()));
}
/**
*
* @param columnId
* the identifier of the column to get
- * @return the column corresponding to the given column id
+ * @return the column corresponding to the given column identifier
*/
public Column<T, ?> getColumn(String columnId) {
- return columnKeys.get(columnId);
+ return columnIds.get(columnId);
}
@Override
}
private String getGeneratedIdentifier() {
- String columnId = "generatedColumn" + counter;
+ String columnId = "" + counter;
counter++;
return columnId;
}
List<String> columnOrder = new ArrayList<>();
for (Column<T, ?> column : columns) {
if (columnSet.contains(column)) {
- columnOrder.add(column.getId());
+ columnOrder.add(column.getInternalId());
} else {
throw new IllegalArgumentException(
"setColumnOrder should not be called "
for (Element col : colgroups.get(0).getElementsByTag("col")) {
String id = DesignAttributeHandler.readAttribute("column-id",
col.attributes(), null, String.class);
- Column<T, String> column;
DeclarativeValueProvider<T> provider = new DeclarativeValueProvider<>();
+ Column<T, String> column = new Column<>("", provider,
+ new HtmlRenderer());
+ addColumn(getGeneratedIdentifier(), column);
if (id != null) {
- column = addColumn(id, provider, new HtmlRenderer());
- } else {
- column = addColumn(provider, new HtmlRenderer());
+ column.setId(id);
}
providers.add(provider);
column.readDesign(col, context);
return mode;
}
+ /**
+ * Sets a user-defined identifier for given column.
+ *
+ * @param column
+ * the column
+ * @param id
+ * the user-defined identifier
+ */
+ protected void setColumnId(String id, Column<T, ?> column) {
+ if (columnIds.containsKey(id)) {
+ throw new IllegalArgumentException("Duplicate ID for columns");
+ }
+ columnIds.put(id, column);
+ }
+
@Override
protected Collection<String> getCustomAttributes() {
Collection<String> result = super.getCustomAttributes();
return result;
}
+ /**
+ * Returns a column identified by its internal id. This id should not be
+ * confused with the user-defined identifier.
+ *
+ * @param columnId
+ * the internal id of column
+ * @return column identified by internal id
+ */
+ protected Column<T, ?> getColumnByInternalId(String columnId) {
+ return columnKeys.get(columnId);
+ }
+
+ /**
+ * Returns the internal id for given column. This id should not be confused
+ * with the user-defined identifier.
+ *
+ * @param column
+ * the column
+ * @return internal id of given column
+ */
+ protected String getInternalIdForColumn(Column<T, ?> column) {
+ return column.getInternalId();
+ }
+
private void setSortOrder(List<SortOrder<Column<T, ?>>> order,
boolean userOriginated) {
Objects.requireNonNull(order, "Sort order list cannot be null");
String message = errorGenerator.apply(fieldToColumn, status);
List<String> columnIds = fieldToColumn.values().stream()
- .map(Column::getId).collect(Collectors.toList());
+ .map(column -> getInternalIdForColumn(column))
+ .collect(Collectors.toList());
rpc.setErrorMessage(message, columnIds);
}
.apply(edited);
addComponentToGrid(component);
columnFields.put(c, component);
- getState().columnFields.put(c.getId(),
+ getState().columnFields.put(getInternalIdForColumn(c),
component.getConnectorId());
});
}
/*
* Copyright 2000-2016 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
import java.io.Serializable;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
protected abstract String getCellTagName();
/**
- * Adds a cell to this section, corresponding to the given column id.
+ * Adds a cell to this section, corresponding to the given user-defined
+ * column id.
*
* @param columnId
* the id of the column for which to add a cell
*/
protected void addCell(String columnId) {
+ Column<?, ?> column = section.getGrid().getColumn(columnId);
+ Objects.requireNonNull(column,
+ "No column matching given identifier");
+ addCell(column);
+ }
+
+ /**
+ * Adds a cell to this section for given column.
+ *
+ * @param column
+ * the column for which to add a cell
+ */
+ protected void addCell(Column<?, ?> column) {
+ if (!section.getGrid().getColumns().contains(column)) {
+ throw new IllegalArgumentException(
+ "Given column does not exist in this Grid");
+ }
+ internalAddCell(section.getInternalIdForColumn(column));
+ }
+
+ /**
+ * Adds a cell to this section, corresponding to the given internal
+ * column id.
+ *
+ * @param internalId
+ * the internal id of the column for which to add a cell
+ */
+ protected void internalAddCell(String internalId) {
CELL cell = createCell();
- cell.setColumnId(columnId);
- cells.put(columnId, cell);
- rowState.cells.put(columnId, cell.getCellState());
+ cell.setColumnId(internalId);
+ cells.put(internalId, cell);
+ rowState.cells.put(internalId, cell.getCellState());
}
/**
* if no cell was found for the column id
*/
public CELL getCell(String columnId) {
- CELL cell = cells.get(columnId);
+ Column<?, ?> column = section.getGrid().getColumn(columnId);
+ Objects.requireNonNull(column,
+ "No column matching given identifier");
+ return getCell(column);
+ }
+
+ /**
+ * Returns the cell in this section that corresponds to the given
+ * column.
+ *
+ * @param column
+ * the column
+ * @return the cell for the given column
+ *
+ * @throws IllegalArgumentException
+ * if no cell was found for the column
+ */
+ public CELL getCell(Column<?, ?> column) {
+ return internalGetCell(section.getInternalIdForColumn(column));
+ }
+
+ /**
+ * Returns the cell in this section that corresponds to the given
+ * internal column id.
+ *
+ * @param internalId
+ * the internal id of the column
+ * @return the cell for the given column
+ *
+ * @throws IllegalArgumentException
+ * if no cell was found for the column id
+ */
+ protected CELL internalGetCell(String internalId) {
+ CELL cell = cells.get(internalId);
if (cell == null) {
throw new IllegalArgumentException(
- "No cell found for column id " + columnId);
+ "No cell found for column id " + internalId);
}
return cell;
}
.getValue().contains(entry.getKey()))
.findFirst();
Stream<String> columnIds = Stream.of(entry.getKey());
+
if (groupCell.isPresent()) {
Set<String> orderedSet = new LinkedHashSet<>(
cells.keySet());
entry.getValue().getCellState());
}
cellElement.attr("column-ids",
- columnIds.collect(Collectors.joining(",")));
+ columnIds.map(section::getColumnByInternalId)
+ .map(Column::getId)
+ .collect(Collectors.joining(",")));
}
}
/**
- *
+ *
* Writes declarative design for the cell using its {@code state} to the
* given table cell element.
* <p>
* sometimes there is no a reference to the cell which should be written
* (merged cell) but only its state is available (the cell is virtual
* and is not stored).
- *
+ *
* @param cellElement
* Element to write design to
* @param context
protected abstract Grid<?> getGrid();
- protected abstract Collection<? extends Column<?, ?>> getColumns();
+ protected abstract Column<?, ?> getColumnByInternalId(String internalId);
+
+ protected abstract String getInternalIdForColumn(Column<?, ?> column);
/**
* Marks the state of this section as modified.
rows.add(index, row);
getState(true).rows.add(index, row.getRowState());
- getColumns().stream().forEach(column -> row.addCell(column.getId()));
+ getGrid().getColumns().stream().forEach(row::addCell);
return row;
}
*/
public void addColumn(String columnId) {
for (ROW row : rows) {
- row.addCell(columnId);
+ row.internalAddCell(columnId);
}
}
* indicating the source grid of possible events emitted by this renderer,
* such as {@link RendererClickEvent}s.
*
- * @return the grid this renderer is attached to or null if unattached
+ * @return the grid this renderer is attached to or {@code null} if
+ * unattached
*/
@SuppressWarnings("unchecked")
protected Grid<T> getParentGrid() {
return (Grid<T>) super.getParent().getParent();
}
+ @Override
+ public Column<T, V> getParent() {
+ return (Column<T, V>) super.getParent();
+ }
+
@Override
protected AbstractRendererState getState() {
return (AbstractRendererState) super.getState();
public static class RendererClickEvent<T> extends ClickEvent {
private final T item;
- private final Column column;
+ private final Column<T, ?> column;
- protected RendererClickEvent(Grid<T> source, T item, Column column,
- MouseEventDetails mouseEventDetails) {
+ protected RendererClickEvent(Grid<T> source, T item,
+ Column<T, ?> column, MouseEventDetails mouseEventDetails) {
super(source, mouseEventDetails);
this.item = item;
this.column = column;
*
* @return the column of the click event
*/
- public Column getColumn() {
+ public Column<T, ?> getColumn() {
return column;
}
}
MouseEventDetails mouseDetails) -> {
Grid<T> grid = getParentGrid();
T item = grid.getDataCommunicator().getKeyMapper().get(rowKey);
- Column column = grid.getColumn(columnId);
+ Column<T, V> column = getParent();
fireEvent(
new RendererClickEvent<>(grid, item, column, mouseDetails));
HeightMode heightMode = HeightMode.ROW;
double heightByRows = 13.7d;
- grid.addColumn(Person::getFirstName);
- grid.addColumn("id", Person::getLastName);
+ grid.addColumn(Person::getFirstName).setCaption("First Name");
+ grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
grid.setFrozenColumnCount(frozenColumns);
grid.setSelectionMode(SelectionMode.MULTI);
String design = String.format(
"<%s height-mode='%s' frozen-columns='%d' rows='%s' selection-mode='%s'><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable>"
+ + "<col column-id='column0' sortable>"
+ "<col column-id='id' sortable>" + "</colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>Generated Column0</th>"
+ + "<tr default><th plain-text column-ids='column0'>First Name</th>"
+ "<th plain-text column-ids='id'>Id</th></tr>"
+ "</thead></table></%s>",
getComponentTag(),
public void mergedHeaderCells() {
Grid<Person> grid = new Grid<>();
- Column<Person, String> column1 = grid.addColumn(Person::getFirstName);
- Column<Person, String> column2 = grid.addColumn("id",
- Person::getLastName);
- Column<Person, String> column3 = grid.addColumn("mail",
- Person::getEmail);
+ Column<Person, String> column1 = grid.addColumn(Person::getFirstName)
+ .setCaption("First Name");
+ Column<Person, String> column2 = grid.addColumn(Person::getLastName)
+ .setId("id").setCaption("Id");
+ Column<Person, String> column3 = grid.addColumn(Person::getEmail)
+ .setId("mail").setCaption("Mail");
HeaderRow header = grid.addHeaderRowAt(1);
String headerRowText1 = "foo";
String headerRowText3 = "foobar";
join.setText(headerRowText3);
- String design = String.format("<%s><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable>"
- + "<col column-id='id' sortable>"
- + "<col column-id='mail' sortable>" + "</colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>Generated Column0</th>"
- + "<th plain-text column-ids='id'>Id</th>"
- + "<th plain-text column-ids='mail'>Mail</th></tr>"
- + "<tr><th plain-text column-ids='generatedColumn0'>%s</th>"
- + "<th colspan='2' plain-text column-ids='id,mail'>foobar</th></tr>"
- + "</thead></table></%s>", getComponentTag(), headerRowText1,
- headerRowText3, getComponentTag());
+ String design = String.format(
+ "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
+ + "<col column-id='id' sortable>"
+ + "<col column-id='mail' sortable>"
+ + "</colgroup><thead>"
+ + "<tr default><th plain-text column-ids='column0'>First Name</th>"
+ + "<th plain-text column-ids='id'>Id</th>"
+ + "<th plain-text column-ids='mail'>Mail</th></tr>"
+ + "<tr><th plain-text column-ids='column0'>%s</th>"
+ + "<th colspan='2' plain-text column-ids='id,mail'>foobar</th></tr>"
+ + "</thead></table></%s>",
+ getComponentTag(), headerRowText1, headerRowText3,
+ getComponentTag());
testRead(design, grid);
testWrite(design, grid);
public void mergedFooterCells() {
Grid<Person> grid = new Grid<>();
- Column<Person, String> column1 = grid.addColumn(Person::getFirstName);
- Column<Person, String> column2 = grid.addColumn("id",
- Person::getLastName);
- Column<Person, String> column3 = grid.addColumn("mail",
- Person::getEmail);
+ Column<Person, String> column1 = grid.addColumn(Person::getFirstName)
+ .setCaption("First Name");
+ Column<Person, String> column2 = grid.addColumn(Person::getLastName)
+ .setId("id").setCaption("Id");
+ Column<Person, String> column3 = grid.addColumn(Person::getEmail)
+ .setId("mail").setCaption("Mail");
FooterRow footer = grid.addFooterRowAt(0);
String footerRowText2 = "foobar";
footer.join(cell2, cell3).setHtml(footerRowText2);
- String design = String.format("<%s><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable>"
- + "<col column-id='id' sortable>"
- + "<col column-id='mail' sortable>" + "</colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>Generated Column0</th>"
- + "<th plain-text column-ids='id'>Id</th>"
- + "<th plain-text column-ids='mail'>Mail</th></tr></thead>"
- + "<tfoot><tr><td plain-text column-ids='generatedColumn0'>%s</td>"
- + "<td colspan='2' column-ids='id,mail'>%s</td></tr></tfoot>"
- + "</table></%s>", getComponentTag(), footerRowText1,
- footerRowText2, getComponentTag());
+ String design = String.format(
+ "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
+ + "<col column-id='id' sortable>"
+ + "<col column-id='mail' sortable>"
+ + "</colgroup><thead>"
+ + "<tr default><th plain-text column-ids='column0'>First Name</th>"
+ + "<th plain-text column-ids='id'>Id</th>"
+ + "<th plain-text column-ids='mail'>Mail</th></tr></thead>"
+ + "<tfoot><tr><td plain-text column-ids='column0'>%s</td>"
+ + "<td colspan='2' column-ids='id,mail'>%s</td></tr></tfoot>"
+ + "</table></%s>",
+ getComponentTag(), footerRowText1, footerRowText2,
+ getComponentTag());
testRead(design, grid);
testWrite(design, grid);
Grid<Person> grid = new Grid<>();
String secondColumnId = "id";
- Column<Person, String> column1 = grid.addColumn(Person::getFirstName);
- Column<Person, String> column2 = grid.addColumn(secondColumnId,
- Person::getLastName);
+ Column<Person, String> column1 = grid.addColumn(Person::getFirstName)
+ .setCaption("First Name");
+ Column<Person, String> column2 = grid.addColumn(Person::getLastName)
+ .setId(secondColumnId).setCaption("Id");
String caption = "test-caption";
column1.setCaption(caption);
String design = String.format(
"<%s><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable='%s' editable resizable='%s' hidable hidden>"
+ + "<col column-id='column0' sortable='%s' editable resizable='%s' hidable hidden>"
+ "<col column-id='id' sortable hiding-toggle-caption='%s' width='%s' min-width='%s' max-width='%s' expand='%s'>"
+ "</colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>%s</th>"
+ + "<tr default><th plain-text column-ids='column0'>%s</th>"
+ "<th plain-text column-ids='id'>%s</th>"
+ "</tr></thead>" + "</table></%s>",
getComponentTag(), sortable, resizable, hidingToggleCaption,
public void headerFooterSerialization() {
Grid<Person> grid = new Grid<>();
- Column<Person, String> column1 = grid.addColumn(Person::getFirstName);
- Column<Person, String> column2 = grid.addColumn("id",
- Person::getLastName);
+ Column<Person, String> column1 = grid.addColumn(Person::getFirstName)
+ .setCaption("First Name");
+ Column<Person, String> column2 = grid.addColumn(Person::getLastName)
+ .setId("id").setCaption("Id");
FooterRow footerRow = grid.addFooterRowAt(0);
footerRow.getCell(column1).setText("x");
footerRow.getCell(column2).setHtml("y");
- String design = String.format("<%s><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable>"
- + "<col column-id='id' sortable></colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>Generated Column0</th>"
- + "<th plain-text column-ids='id'>Id</th></tr>"
- + "</thead><tbody></tbody>"
- + "<tfoot><tr><td plain-text column-ids='generatedColumn0'>x</td>"
- + "<td column-ids='id'>y</td></tr></tfoot>" + "</table></%s>",
+ String design = String.format(
+ "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
+ + "<col column-id='id' sortable></colgroup><thead>"
+ + "<tr default><th plain-text column-ids='column0'>First Name</th>"
+ + "<th plain-text column-ids='id'>Id</th></tr>"
+ + "</thead><tbody></tbody>"
+ + "<tfoot><tr><td plain-text column-ids='column0'>x</td>"
+ + "<td column-ids='id'>y</td></tr></tfoot>"
+ + "</table></%s>",
getComponentTag(), getComponentTag());
testRead(design, grid);
Person person2 = createPerson("name", "last-name");
grid.setItems(person1, person2);
- grid.addColumn(Person::getFirstName);
- grid.addColumn("id", Person::getLastName);
+ grid.addColumn(Person::getFirstName).setCaption("First Name");
+ grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
String design = String.format(
- "<%s><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable>"
+ "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
+ "<col column-id='id' sortable></colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>Generated Column0</th>"
+ + "<tr default><th plain-text column-ids='column0'>First Name</th>"
+ "<th plain-text column-ids='id'>Id</th></tr>"
+ "</thead><tbody>"
+ "<tr item='%s'><td>%s</td><td>%s</td></tr>"
Person person3 = createPerson("foo", "last-name");
grid.setItems(person1, person2, person3);
- grid.addColumn(Person::getFirstName);
- grid.addColumn("id", Person::getLastName);
+ grid.addColumn(Person::getFirstName).setCaption("First Name");
+ grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
Multi<Person> model = (Multi<Person>) grid
.setSelectionMode(SelectionMode.MULTI);
String design = String.format(
"<%s selection-mode='multi'><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable>"
+ + "<col column-id='column0' sortable>"
+ "<col column-id='id' sortable></colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>Generated Column0</th>"
+ + "<tr default><th plain-text column-ids='column0'>First Name</th>"
+ "<th plain-text column-ids='id'>Id</th></tr>"
+ "</thead><tbody>"
+ "<tr item='%s' selected><td>%s</td><td>%s</td></tr>"
Person person2 = createPerson("name", "last-name");
grid.setItems(person1, person2);
- grid.addColumn(Person::getFirstName);
- grid.addColumn("id", Person::getLastName);
+ grid.addColumn(Person::getFirstName).setCaption("First Name");
+ grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
Single<Person> model = (Single<Person>) grid
.setSelectionMode(SelectionMode.SINGLE);
model.select(person2);
String design = String.format(
- "<%s><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable>"
+ "<%s><table><colgroup>" + "<col column-id='column0' sortable>"
+ "<col column-id='id' sortable></colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>Generated Column0</th>"
+ + "<tr default><th plain-text column-ids='column0'>First Name</th>"
+ "<th plain-text column-ids='id'>Id</th></tr>"
+ "</thead><tbody>"
+ "<tr item='%s'><td>%s</td><td>%s</td></tr>"
Person person2 = createPerson("name", "last-name");
grid.setItems(person1, person2);
- grid.addColumn(Person::getFirstName);
- grid.addColumn("id", Person::getLastName);
+ grid.addColumn(Person::getFirstName).setCaption("First Name");
+ grid.addColumn(Person::getLastName).setId("id").setCaption("Id");
grid.setSelectionMode(SelectionMode.MULTI);
grid.asMultiSelect().setReadOnly(true);
String formatString = "<%s %s selection-allowed><table><colgroup>"
- + "<col column-id='generatedColumn0' sortable>"
+ + "<col column-id='column0' sortable>"
+ "<col column-id='id' sortable>" + "</colgroup><thead>"
- + "<tr default><th plain-text column-ids='generatedColumn0'>Generated Column0</th>"
+ + "<tr default><th plain-text column-ids='column0'>First Name</th>"
+ "<th plain-text column-ids='id'>Id</th></tr>"
+ "</thead><tbody>"
+ "<tr item='%s'><td>%s</td><td>%s</td></tr>"
@Test
public void testComponentInGridHeader() {
Grid<Person> grid = new Grid<>();
- Column<Person, String> column = grid.addColumn(Person::getFirstName);
+ Column<Person, String> column = grid.addColumn(Person::getFirstName)
+ .setCaption("First Name");
String html = "<b>Foo</b>";
Label component = new Label(html);
//@formatter:off
String design = String.format( "<%s><table>"
+ "<colgroup>"
- + " <col sortable column-id='generatedColumn0'>"
+ + " <col sortable column-id='column0'>"
+ "</colgroup>"
+ "<thead>"
- + "<tr default><th column-ids='generatedColumn0'><vaadin-label>%s</vaadin-label></th></tr>"
+ + "<tr default><th column-ids='column0'><vaadin-label>%s</vaadin-label></th></tr>"
+ "</thead>"
+ "</table></%s>", getComponentTag(), html, getComponentTag());
//@formatter:on
- grid.getDefaultHeaderRow().getCell(column.getId())
- .setComponent(component);
+ grid.getDefaultHeaderRow().getCell(column).setComponent(component);
testRead(design, grid, true);
testWrite(design, grid);
@Test
public void testComponentInGridFooter() {
Grid<Person> grid = new Grid<>();
- Column<Person, String> column = grid.addColumn(Person::getFirstName);
+ Column<Person, String> column = grid.addColumn(Person::getFirstName)
+ .setCaption("First Name");
String html = "<b>Foo</b>";
Label component = new Label(html);
//@formatter:off
String design = String.format( "<%s><table>"
+ "<colgroup>"
- + " <col sortable column-id='generatedColumn0'>"
+ + " <col sortable column-id='column0'>"
+ "</colgroup>"
+ "<thead>"
+"<tfoot>"
- + "<tr><td column-ids='generatedColumn0'><vaadin-label>%s</vaadin-label></td></tr>"
+ + "<tr><td column-ids='column0'><vaadin-label>%s</vaadin-label></td></tr>"
+ "</tfoot>"
+ "</table>"
+ "</%s>", getComponentTag(), html, getComponentTag());
//@formatter:off
String design = "<vaadin-grid><table>"
+ "<colgroup>"
- + " <col sortable column-id='generatedColumn0'>"
+ + " <col sortable column-id='column0'>"
+ "</colgroup>"
+ "<thead />"
+ "</table>"
+ "</vaadin-grid>";
//@formatter:on
Grid<Person> grid = new Grid<>();
- grid.addColumn(Person::getFirstName);
+ grid.addColumn(Person::getFirstName).setCaption("First Name");
grid.removeHeaderRow(grid.getDefaultHeaderRow());
testWrite(design, grid);
+ "<tr><td %s column-ids='%s'>> Test</td></tr>"
+ "</tfoot>"
+ "<tbody />"
- + "</table></%s>",
+ + "</table></%s>",
getComponentTag() , id, plainText, id, plainText, id, getComponentTag());
//@formatter:on
Assert.assertEquals(expected, actualFooter);
grid = new Grid<>();
- Column<Person, String> column = grid.addColumn(id,
- Person::getFirstName);
+ Column<Person, String> column = grid.addColumn(Person::getFirstName)
+ .setId(id);
HeaderRow header = grid.addHeaderRowAt(0);
FooterRow footer = grid.addFooterRowAt(0);
grid.removeHeaderRow(grid.getDefaultHeaderRow());
/*
* Copyright 2000-2016 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
public void setUp() {
grid = new Grid<>();
- column1 = grid.addColumn("First", ValueProvider.identity());
- column2 = grid.addColumn("Second", ValueProvider.identity());
+ column1 = grid.addColumn(ValueProvider.identity()).setId("First")
+ .setCaption("First");
+ column2 = grid.addColumn(ValueProvider.identity()).setId("Second")
+ .setCaption("Second");
}
@Test
@Test
public void addColumn_headerCellAdded() {
- Column<?, ?> column = grid.addColumn("Col",
- ValueProvider.identity());
+
+ Column<?, ?> column = grid.addColumn(ValueProvider.identity())
+ .setId("Col");
assertNotNull(grid.getHeaderRow(0).getCell(column));
}
@Test(expected = IllegalArgumentException.class)
public void removeColumn_headerCellRemoved() {
- Column<String, ?> column = grid.addColumn("Col",
- ValueProvider.identity());
+
+ Column<String, ?> column = grid.addColumn(ValueProvider.identity())
+ .setId("Col");
grid.removeColumn(column);
grid.getHeaderRow(0).getCell(column);
@Before
public void setUp() {
grid = new Grid<>();
- grid.addColumn("foo", ValueProvider.identity());
+
+ grid.addColumn(ValueProvider.identity()).setId("foo");
grid.addColumn(String::length, new NumberRenderer());
- grid.addColumn("randomColumnId", ValueProvider.identity());
+ grid.addColumn(ValueProvider.identity()).setId("randomColumnId");
}
@Test
grid.getHeaderRow(0).getCell("foo").getText());
}
- @Test
- public void testGridColumnGeneratedIdentifier() {
- assertEquals("Unexpected caption on a generated Column",
- "Generated Column0",
- grid.getColumn("generatedColumn0").getCaption());
- }
-
- @Test
- public void testGridColumnCaptionFromIdentifier() {
- assertEquals("Unexpected caption on a generated Column",
- "Random Column Id",
- grid.getColumn("randomColumnId").getCaption());
- }
-
@Test(expected = IllegalArgumentException.class)
public void testGridMultipleColumnsWithSameIdentifier() {
- grid.addColumn("foo", t -> t);
+ grid.addColumn(t -> t).setId("foo");
}
@Test
public class ColumnState extends AbstractGridExtensionState {
public String caption;
- public String id;
+ public String internalId;
public boolean sortable;
public boolean editable = false;
import com.vaadin.shared.ui.grid.GridConstants.Section;
/**
- * Client-to-server RPC interface for the Grid component
+ * Client-to-server RPC interface for the Grid component.
*
* @since 7.4
* @author Vaadin Ltd
*
* @param rowKey
* a key identifying the clicked item
- * @param columnId
- * column id identifying the clicked property
+ * @param columnInternalId
+ * column internal id identifying the clicked property
* @param details
* mouse event details
*/
- void itemClick(String rowKey, String columnId, MouseEventDetails details);
+ void itemClick(String rowKey, String columnInternalId,
+ MouseEventDetails details);
/**
* Informs the server that a context click has happened inside of Grid.
* index of clicked row in Grid section
* @param rowKey
* a key identifying the clicked item
- * @param columnId
- * column id identifying the clicked property
+ * @param columnInternalId
+ * column internal id identifying the clicked property
* @param section
* grid section (header, footer, body)
* @param details
* mouse event details
*/
- void contextClick(int rowIndex, String rowKey, String columnId,
+ void contextClick(int rowIndex, String rowKey, String columnInternalId,
Section section, MouseEventDetails details);
/**
*
* @since 7.5.0
* @param newColumnOrder
- * a list of column ids in the new order
+ * a list of column internal ids in the new order
* @param oldColumnOrder
- * a list of column ids in order before the change
+ * a list of column internal ids in order before the change
*/
void columnsReordered(List<String> newColumnOrder,
List<String> oldColumnOrder);
* Informs the server that a column's visibility has been changed.
*
* @since 7.5.0
- * @param id
- * the id of the column
+ * @param columnInternalId
+ * the internal id of the column
* @param hidden
* <code>true</code> if hidden, <code>false</code> if unhidden
*/
- void columnVisibilityChanged(String id, boolean hidden);
+ void columnVisibilityChanged(String columnInternalId, boolean hidden);
/**
* Informs the server that a column has been resized by the user.
*
* @since 7.6
- * @param id
- * the id of the column
+ * @param columnInternalId
+ * the internal id of the column
* @param pixels
* the new width of the column in pixels
*/
- void columnResized(String id, double pixels);
+ void columnResized(String columnInternalId, double pixels);
}
protected void setup(VaadinRequest request) {
Grid<String> grid = new Grid<>();
- grid.addColumn("Name", ValueProvider.identity());
+ grid.addColumn(ValueProvider.identity()).setId("Name")
+ .setCaption("Name");
List<String> data = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
Label checkBoxValueLabel = new Label("checkbox click label");
Grid<TestPOJO> grid = new Grid<>();
- grid.addColumn("images", pojo -> new ExternalResource(pojo.imageUrl),
- new ImageRenderer<>());
- grid.addColumn("buttons", pojo -> pojo.buttonText,
+ grid.addColumn(pojo -> new ExternalResource(pojo.imageUrl),
+ new ImageRenderer<>()).setId("images").setCaption("Images");
+ grid.addColumn(pojo -> pojo.buttonText,
new ButtonRenderer<>(event -> valueDisplayLabel
- .setValue(event.getItem().testText + " clicked")));
+ .setValue(event.getItem().testText + " clicked")))
+ .setId("buttons").setCaption("Buttons");
CheckBoxRenderer<TestPOJO> checkBoxRenderer = new CheckBoxRenderer<>(
pojo -> pojo.truthValue,
(pojo, newTruthValue) -> pojo.truthValue = newTruthValue);
checkBoxRenderer.addClickListener(click -> checkBoxValueLabel.setValue(
click.getItem().testText + " " + click.getItem().truthValue));
- grid.addColumn("checkboxes", pojo -> pojo.truthValue, checkBoxRenderer);
+ grid.addColumn(pojo -> pojo.truthValue, checkBoxRenderer)
+ .setId("checkboxes").setCaption("Checkboxes");
grid.setItems(new TestPOJO("first row", "", "button 1 text", true),
new TestPOJO("second row", "", "button 2 text", false));
@Override
protected void setup(VaadinRequest request) {
Grid<Person> grid = new Grid<>();
- Column<Person, String> nameColumn = grid
- .addColumn("Name", Person::getFirstName).setHidable(true);
+ Column<Person, String> nameColumn = grid.addColumn(Person::getFirstName)
+ .setHidable(true).setCaption("Name");
Column<Person, Number> ageColumn = grid
- .addColumn("Age", Person::getAge, new NumberRenderer())
+ .addColumn(Person::getAge, new NumberRenderer())
.setHidable(true)
- .setHidingToggleCaption("custom age column caption");
- Column<Person, String> emailColumn = grid.addColumn("Email",
- Person::getEmail);
+ .setHidingToggleCaption("custom age column caption")
+ .setCaption("Age");
+ Column<Person, String> emailColumn = grid.addColumn(Person::getEmail)
+ .setCaption("Email");
Button toggleNameColumn = new Button("server side toggle name column");
Button toggleAgeColumn = new Button("server side toggle age column");
TextField input = new TextField();
Label isResizedLabel = new Label("not resized");
Grid<Person> grid = new Grid<>();
- Column<Person, String> nameColumn = grid.addColumn("Name",
- Person::getFirstName);
- Column<Person, Number> ageColumn = grid.addColumn("Age", Person::getAge,
- new NumberRenderer());
+ Column<Person, String> nameColumn = grid.addColumn(Person::getFirstName)
+ .setCaption("Name");
+ Column<Person, Number> ageColumn = grid
+ .addColumn(Person::getAge, new NumberRenderer())
+ .setCaption("Age");
grid.addColumnResizeListener(event -> {
if (event.isUserOriginated()) {
isResizedLabel.setValue("client resized");
Grid<Person> grid = new Grid<>();
grid.setSizeFull();
grid.setItems(people);
- grid.addColumn("firstName", Person::getFirstName);
- grid.addColumn("lastNAme", Person::getLastName);
+ grid.addColumn(Person::getFirstName).setCaption("First Name");
+ grid.addColumn(Person::getLastName).setCaption("Last Name");
grid.setEnabled(false);
Grid<ItemBean> grid = new Grid<>();
- grid.addColumn("id", item -> item.getId().toString());
- grid.addColumn("bean", ItemBean::getBean, new MyBeanJSRenderer());
- grid.addColumn("string", ItemBean::getString,
- new JavaScriptStringRenderer());
+ grid.addColumn(item -> item.getId().toString()).setCaption("Id");
+ grid.addColumn(ItemBean::getBean, new MyBeanJSRenderer())
+ .setCaption("Bean");
+ grid.addColumn(ItemBean::getString, new JavaScriptStringRenderer())
+ .setCaption("String");
grid.setItems(items);
grid.setDataProvider(dataProvider);
grid.setDataProvider(dataProvider);
- grid.addColumn("Coordinates", DataObject::getCoordinates);
+ grid.addColumn(DataObject::getCoordinates).setCaption("Coordinates")
+ .setId("Coordinates");
addComponent(grid);
Button update = new Button("Update data",
protected Grid<Person> createTestComponent() {
Grid<Person> grid = new Grid<>();
grid.setItems(PersonContainer.createTestData());
- grid.addColumn("Address",
- person -> String.valueOf(person.getAddress()));
- grid.addColumn("Email", person -> String.valueOf(person.getEmail()));
- grid.addColumn("First Name",
- person -> String.valueOf(person.getFirstName()));
- grid.addColumn("Last Name",
- person -> String.valueOf(person.getLastName()));
- grid.addColumn("Phone Number",
- person -> String.valueOf(person.getPhoneNumber()));
- grid.addColumn("Street Address", person -> String
- .valueOf(person.getAddress().getStreetAddress()));
- grid.addColumn("City",
- person -> String.valueOf(person.getAddress().getCity()));
+ grid.addColumn(person -> String.valueOf(person.getAddress()))
+ .setCaption("Address");
+ grid.addColumn(person -> String.valueOf(person.getEmail()))
+ .setCaption("Email");
+ grid.addColumn(person -> String.valueOf(person.getFirstName()))
+ .setCaption("First Name");
+ grid.addColumn(person -> String.valueOf(person.getLastName()))
+ .setCaption("Last Name");
+ grid.addColumn(person -> String.valueOf(person.getPhoneNumber()))
+ .setCaption("Phone Number");
+ grid.addColumn(person -> String
+ .valueOf(person.getAddress().getStreetAddress()))
+ .setCaption("Street Address");
+ grid.addColumn(person -> String.valueOf(person.getAddress().getCity()))
+ .setCaption("City");
// grid.setFooterVisible(true);
// grid.appendFooterRow();
@Override
protected Grid<Person> createComponent() {
Grid<Person> grid = new Grid<>();
- grid.addColumn("First Name", Person::getFirstName);
- grid.addColumn("Last Name", Person::getLastName);
- grid.addColumn("Street",
- person -> Optional.ofNullable(person.getAddress())
- .map(Address::getStreetAddress).orElse(null));
- grid.addColumn("Zip", person -> Optional.ofNullable(person.getAddress())
- .map(Address::getPostalCode).map(Object::toString).orElse(""));
- grid.addColumn("City",
- person -> Optional.ofNullable(person.getAddress())
- .map(Address::getCity).orElse(null));
+ grid.addColumn(Person::getFirstName).setCaption("First Name");
+ grid.addColumn(Person::getLastName).setCaption("Last Name");
+ grid.addColumn(person -> Optional.ofNullable(person.getAddress())
+ .map(Address::getStreetAddress).orElse(null))
+ .setCaption("Street");
+ grid.addColumn(person -> Optional.ofNullable(person.getAddress())
+ .map(Address::getPostalCode).map(Object::toString).orElse(""))
+ .setCaption("Zip");
+ grid.addColumn(person -> Optional.ofNullable(person.getAddress())
+ .map(Address::getCity).orElse(null)).setCaption("City");
return grid;
}