aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/ui
diff options
context:
space:
mode:
authorAleksi Hietanen <aleksi@vaadin.com>2016-10-17 10:13:33 +0300
committerAleksi Hietanen <aleksi@vaadin.com>2016-10-25 14:38:11 +0300
commite0fcd1cfe0cea7dac124df0d86bf74f8d8c4f9be (patch)
tree826e734991743aa7531f026f74eb2f830715f247 /server/src/main/java/com/vaadin/ui
parent63d218efc0ee5825633b4950e3355f4e8cc7bc29 (diff)
downloadvaadin-framework-e0fcd1cfe0cea7dac124df0d86bf74f8d8c4f9be.tar.gz
vaadin-framework-e0fcd1cfe0cea7dac124df0d86bf74f8d8c4f9be.zip
Grid html/component content in headers
Change-Id: Ie6129b51d15d4f30a6b4c034999ff02deec1c6a7
Diffstat (limited to 'server/src/main/java/com/vaadin/ui')
-rw-r--r--server/src/main/java/com/vaadin/ui/Grid.java62
-rw-r--r--server/src/main/java/com/vaadin/ui/components/grid/StaticSection.java131
2 files changed, 173 insertions, 20 deletions
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<T> extends AbstractSingleSelect<T> 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();
}
/**
@@ -1629,6 +1668,11 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
private class HeaderImpl extends Header {
@Override
+ protected Grid<T> getGrid() {
+ return Grid.this;
+ }
+
+ @Override
protected SectionState getState(boolean markAsDirty) {
return Grid.this.getState(markAsDirty).header;
}
@@ -1642,6 +1686,11 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
private class FooterImpl extends Footer {
@Override
+ protected Grid<T> getGrid() {
+ return Grid.this;
+ }
+
+ @Override
protected SectionState getState(boolean markAsDirty) {
return Grid.this.getState(markAsDirty).footer;
}
@@ -1885,7 +1934,18 @@ public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
@Override
public Iterator<Component> iterator() {
- return Collections.unmodifiableSet(extensionComponents).iterator();
+ Set<Component> 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 <ROW>
* the type of the rows in the section
*
@@ -57,7 +60,7 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>>
/**
* Creates a new row belonging to the given section.
- *
+ *
* @param section
* the section of the row
*/
@@ -74,14 +77,14 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>>
/**
* 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<ROW extends StaticSection.StaticRow<?>>
/**
* 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<ROW extends StaticSection.StaticRow<?>>
/**
* Returns the shared state of this row.
- *
+ *
* @return the row state
*/
protected RowState getRowState() {
@@ -122,7 +125,7 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>>
* @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<ROW extends StaticSection.StaticRow<?>>
}
return cell;
}
+
+ void detach() {
+ for (CELL cell : cells.values()) {
+ cell.detach();
+ }
+ }
}
/**
@@ -167,7 +176,7 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>>
/**
* Returns the shared state of this cell.
- *
+ *
* @return the cell state
*/
protected CellState getCellState() {
@@ -182,7 +191,9 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>>
*/
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<ROW extends StaticSection.StaticRow<?>>
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<ROW> 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<ROW extends StaticSection.StaticRow<?>>
*/
protected abstract SectionState getState(boolean markAsDirty);
+ protected abstract Grid<?> getGrid();
+
protected abstract Collection<? extends Column<?, ?>> getColumns();
/**
@@ -226,7 +318,7 @@ public abstract class StaticSection<ROW extends StaticSection.StaticRow<?>>
/**
* 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<ROW extends StaticSection.StaticRow<?>>
/**
* 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<ROW extends StaticSection.StaticRow<?>>
/**
* 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<ROW extends StaticSection.StaticRow<?>>
/**
* Returns an unmodifiable list of the rows in this section.
- *
+ *
* @return the rows in this section
*/
protected List<ROW> getRows() {