From e0fcd1cfe0cea7dac124df0d86bf74f8d8c4f9be Mon Sep 17 00:00:00 2001 From: Aleksi Hietanen Date: Mon, 17 Oct 2016 10:13:33 +0300 Subject: Grid html/component content in headers Change-Id: Ie6129b51d15d4f30a6b4c034999ff02deec1c6a7 --- server/src/main/java/com/vaadin/ui/Grid.java | 62 +++++++++- .../vaadin/ui/components/grid/StaticSection.java | 131 ++++++++++++++++++--- 2 files changed, 173 insertions(+), 20 deletions(-) (limited to 'server/src/main/java/com/vaadin/ui') diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index db85ec1fb2..490f9bf8f2 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -54,6 +54,7 @@ import com.vaadin.shared.ui.grid.GridConstants; import com.vaadin.shared.ui.grid.GridConstants.Section; import com.vaadin.shared.ui.grid.GridServerRpc; import com.vaadin.shared.ui.grid.GridState; +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; @@ -1573,6 +1574,44 @@ public class Grid extends AbstractSingleSelect implements HasComponents { * the header caption to set, not null */ public void setText(String text); + + /** + * Returns the HTML content displayed in this cell. + * + * @return the html + * + */ + public String getHtml(); + + /** + * Sets the HTML content displayed in this cell. + * + * @param html + * the html to set + */ + public void setHtml(String html); + + /** + * Returns the component displayed in this cell. + * + * @return the component + */ + public Component getComponent(); + + /** + * Sets the component displayed in this cell. + * + * @param component + * the component to set + */ + public void setComponent(Component component); + + /** + * Returns the type of content stored in this cell. + * + * @return cell content type + */ + public GridStaticCellType getCellType(); } /** @@ -1628,6 +1667,11 @@ public class Grid extends AbstractSingleSelect implements HasComponents { private class HeaderImpl extends Header { + @Override + protected Grid getGrid() { + return Grid.this; + } + @Override protected SectionState getState(boolean markAsDirty) { return Grid.this.getState(markAsDirty).header; @@ -1641,6 +1685,11 @@ public class Grid extends AbstractSingleSelect implements HasComponents { private class FooterImpl extends Footer { + @Override + protected Grid getGrid() { + return Grid.this; + } + @Override protected SectionState getState(boolean markAsDirty) { return Grid.this.getState(markAsDirty).footer; @@ -1885,7 +1934,18 @@ public class Grid extends AbstractSingleSelect implements HasComponents { @Override public Iterator iterator() { - return Collections.unmodifiableSet(extensionComponents).iterator(); + Set componentSet = new LinkedHashSet<>(extensionComponents); + Header header = getHeader(); + for (int i = 0; i < header.getRowCount(); ++i) { + HeaderRow row = header.getRow(i); + getColumns().forEach(column -> { + HeaderCell cell = row.getCell(column); + if (cell.getCellType() == GridStaticCellType.WIDGET) { + componentSet.add(cell.getComponent()); + } + }); + } + return Collections.unmodifiableSet(componentSet).iterator(); } /** diff --git a/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java b/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java index e1c64f31e6..eb02be2bbc 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java @@ -1,12 +1,12 @@ /* * 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 @@ -24,16 +24,19 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import com.vaadin.shared.ui.grid.GridStaticCellType; import com.vaadin.shared.ui.grid.SectionState; import com.vaadin.shared.ui.grid.SectionState.CellState; import com.vaadin.shared.ui.grid.SectionState.RowState; +import com.vaadin.ui.Component; +import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.Column; /** * Represents the header or footer section of a Grid. * * @author Vaadin Ltd. - * + * * @param * the type of the rows in the section * @@ -57,7 +60,7 @@ public abstract class StaticSection> /** * Creates a new row belonging to the given section. - * + * * @param section * the section of the row */ @@ -74,14 +77,14 @@ public abstract class StaticSection> /** * Returns the declarative tag name used for the cells in this row. - * + * * @return the cell tag name */ protected abstract String getCellTagName(); /** * Adds a cell to this section, corresponding to the given column id. - * + * * @param columnId * the id of the column for which to add a cell */ @@ -95,7 +98,7 @@ public abstract class StaticSection> /** * Removes the cell from this section that corresponds to the given * column id. If there is no such cell, does nothing. - * + * * @param columnId * the id of the column from which to remove the cell */ @@ -108,7 +111,7 @@ public abstract class StaticSection> /** * Returns the shared state of this row. - * + * * @return the row state */ protected RowState getRowState() { @@ -122,7 +125,7 @@ public abstract class StaticSection> * @param columnId * the id of the column * @return the cell for the given column - * + * * @throws IllegalArgumentException * if no cell was found for the column id */ @@ -134,6 +137,12 @@ public abstract class StaticSection> } return cell; } + + void detach() { + for (CELL cell : cells.values()) { + cell.detach(); + } + } } /** @@ -167,7 +176,7 @@ public abstract class StaticSection> /** * Returns the shared state of this cell. - * + * * @return the cell state */ protected CellState getCellState() { @@ -182,7 +191,9 @@ public abstract class StaticSection> */ public void setText(String text) { Objects.requireNonNull(text, "text cannot be null"); + removeComponentIfPresent(); cellState.text = text; + cellState.type = GridStaticCellType.TEXT; row.section.markAsDirty(); } @@ -194,20 +205,99 @@ public abstract class StaticSection> public String getText() { return cellState.text; } + + /** + * Returns the HTML content displayed in this cell. + * + * @return the html + * + */ + public String getHtml() { + if (cellState.type != GridStaticCellType.HTML) { + throw new IllegalStateException( + "Cannot fetch HTML from a cell with type " + + cellState.type); + } + return cellState.html; + } + + /** + * Sets the HTML content displayed in this cell. + * + * @param html + * the html to set, not null + */ + public void setHtml(String html) { + Objects.requireNonNull(html, "html cannot be null"); + removeComponentIfPresent(); + cellState.html = html; + cellState.type = GridStaticCellType.HTML; + row.section.markAsDirty(); + } + + /** + * Returns the component displayed in this cell. + * + * @return the component + */ + public Component getComponent() { + if (cellState.type != GridStaticCellType.WIDGET) { + throw new IllegalStateException( + "Cannot fetch Component from a cell with type " + + cellState.type); + } + return (Component) cellState.connector; + } + + /** + * Sets the component displayed in this cell. + * + * @param component + * the component to set, not null + */ + public void setComponent(Component component) { + Objects.requireNonNull(component, "component cannot be null"); + removeComponentIfPresent(); + component.setParent(row.section.getGrid()); + cellState.connector = component; + cellState.type = GridStaticCellType.WIDGET; + row.section.markAsDirty(); + } + + /** + * Returns the type of content stored in this cell. + * + * @return cell content type + */ + public GridStaticCellType getCellType() { + return cellState.type; + } + + private void removeComponentIfPresent() { + Component component = (Component) cellState.connector; + if (component != null) { + component.setParent(null); + cellState.connector = null; + } + } + + void detach() { + removeComponentIfPresent(); + } } private final List rows = new ArrayList<>(); /** * Creates a new row instance. - * + * * @return the new row */ protected abstract ROW createRow(); /** * Returns the shared state of this section. - * + * * @param markAsDirty * {@code true} to mark the state as modified, {@code false} * otherwise @@ -215,6 +305,8 @@ public abstract class StaticSection> */ protected abstract SectionState getState(boolean markAsDirty); + protected abstract Grid getGrid(); + protected abstract Collection> getColumns(); /** @@ -226,7 +318,7 @@ public abstract class StaticSection> /** * Adds a new row at the given index. - * + * * @param index * the index of the new row * @return the added row @@ -245,20 +337,21 @@ public abstract class StaticSection> /** * Removes the row at the given index. - * + * * @param index * the index of the row to remove * @throws IndexOutOfBoundsException * if {@code index < 0 || index >= getRowCount()} */ public void removeRow(int index) { - rows.remove(index); + ROW row = rows.remove(index); + row.detach(); getState(true).rows.remove(index); } /** * Removes the given row from this section. - * + * * @param row * the row to remove, not null * @throws IllegalArgumentException @@ -276,7 +369,7 @@ public abstract class StaticSection> /** * Returns the row at the given index. - * + * * @param index * the index of the row * @return the row at the index @@ -322,7 +415,7 @@ public abstract class StaticSection> /** * Returns an unmodifiable list of the rows in this section. - * + * * @return the rows in this section */ protected List getRows() { -- cgit v1.2.3