diff options
author | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2017-03-09 09:29:51 +0200 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-03-09 09:29:51 +0200 |
commit | 761c94ab2e1019f7d83d4e8d63254de1ee591d75 (patch) | |
tree | bd167811a86c0c666d321bdbfdb57dea980cb942 /server/src/main | |
parent | 264ee7696568827815604f1e22ce7e330775b3ce (diff) | |
download | vaadin-framework-761c94ab2e1019f7d83d4e8d63254de1ee591d75.tar.gz vaadin-framework-761c94ab2e1019f7d83d4e8d63254de1ee591d75.zip |
Initial implementation of ComponentRenderer for Grid (#8743)
Fixes #8622
Fixes #8623
Diffstat (limited to 'server/src/main')
-rw-r--r-- | server/src/main/java/com/vaadin/ui/Grid.java | 39 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/ui/renderers/ComponentRenderer.java | 64 |
2 files changed, 103 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 5d03825c6f..d0b908a956 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -115,6 +115,7 @@ import com.vaadin.ui.declarative.DesignContext; import com.vaadin.ui.declarative.DesignException; import com.vaadin.ui.declarative.DesignFormatter; import com.vaadin.ui.renderers.AbstractRenderer; +import com.vaadin.ui.renderers.ComponentRenderer; import com.vaadin.ui.renderers.HtmlRenderer; import com.vaadin.ui.renderers.Renderer; import com.vaadin.ui.renderers.TextRenderer; @@ -831,6 +832,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, private DescriptionGenerator<T> descriptionGenerator; private Binding<T, ?> editorBinding; + private Map<T, Component> activeComponents = new HashMap<>(); private String userId; @@ -961,6 +963,11 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, V providerValue = valueProvider.apply(data); + // Make Grid track components. + if (renderer instanceof ComponentRenderer + && providerValue instanceof Component) { + addComponent(data, (Component) providerValue); + } JsonValue rendererValue = renderer.encode(providerValue); obj.put(communicationId, rendererValue); @@ -981,6 +988,38 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, } } + private void addComponent(T data, Component component) { + if (activeComponents.containsKey(data)) { + if (activeComponents.get(data).equals(component)) { + // Reusing old component + return; + } + removeComponent(data); + } + activeComponents.put(data, component); + addComponentToGrid(component); + } + + @Override + public void destroyData(T item) { + removeComponent(item); + } + + private void removeComponent(T item) { + Component component = activeComponents.remove(item); + if (component != null) { + removeComponentFromGrid(component); + } + } + + @Override + public void destroyAllData() { + // Make a defensive copy of keys, as the map gets cleared when + // removing components. + new HashSet<>(activeComponents.keySet()) + .forEach(this::removeComponent); + } + /** * Gets a data object with the given key from the given JsonObject. If * there is no object with the key, this method creates a new diff --git a/server/src/main/java/com/vaadin/ui/renderers/ComponentRenderer.java b/server/src/main/java/com/vaadin/ui/renderers/ComponentRenderer.java new file mode 100644 index 0000000000..1053907723 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/renderers/ComponentRenderer.java @@ -0,0 +1,64 @@ +/* + * 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 + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.renderers; + +import com.vaadin.shared.ui.grid.renderers.ComponentRendererState; +import com.vaadin.ui.Component; + +import elemental.json.Json; +import elemental.json.JsonValue; + +/** + * A renderer for presenting Components. + * <p> + * <strong>Note:</strong> The use of ComponentRenderer causes the Grid to + * generate components for all items currently available in the client-side. + * This means that a number of components is always generated and sent to the + * client. Using complex structures of many nested components might be heavy to + * generate and store, which will lead to performance problems. + * <p> + * <strong>Note:</strong> Components will occasionally be generated again during + * runtime e.g. when selection changes. If your component has an internal state + * that is not stored into the object, you should reuse the same component + * instances. + * + * @author Vaadin Ltd + * @since 8.1 + */ +public class ComponentRenderer extends AbstractRenderer<Object, Component> { + + /** + * Constructor for ComponentRenderer. + */ + public ComponentRenderer() { + super(Component.class); + } + + @Override + public JsonValue encode(Component value) { + return Json.create(value.getConnectorId()); + } + + @Override + protected ComponentRendererState getState(boolean markAsDirty) { + return (ComponentRendererState) super.getState(markAsDirty); + } + + @Override + protected ComponentRendererState getState() { + return (ComponentRendererState) super.getState(); + } +} |