|
| 1 | +/* |
| 2 | + * Copyright 2000-2024 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.data.renderer.tests; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +import com.vaadin.flow.component.html.Div; |
| 23 | +import com.vaadin.flow.data.binder.HasDataProvider; |
| 24 | +import com.vaadin.flow.data.provider.ArrayUpdater; |
| 25 | +import com.vaadin.flow.data.provider.CompositeDataGenerator; |
| 26 | +import com.vaadin.flow.data.provider.DataCommunicator; |
| 27 | +import com.vaadin.flow.data.provider.DataProvider; |
| 28 | +import com.vaadin.flow.data.renderer.LitRenderer; |
| 29 | +import com.vaadin.flow.internal.JsonUtils; |
| 30 | + |
| 31 | +import elemental.json.JsonValue; |
| 32 | + |
| 33 | +public class LitRendererTestComponentWrapper extends Div |
| 34 | + implements HasDataProvider<String> { |
| 35 | + private final DataCommunicator<String> dataCommunicator; |
| 36 | + private final CompositeDataGenerator<String> dataGenerator = new CompositeDataGenerator<>(); |
| 37 | + |
| 38 | + private final ArrayUpdater arrayUpdater = new ArrayUpdater() { |
| 39 | + @Override |
| 40 | + public Update startUpdate(int sizeChange) { |
| 41 | + return new Update() { |
| 42 | + @Override |
| 43 | + public void clear(int start, int length) { |
| 44 | + // not essential for this test |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void set(int start, List<JsonValue> items) { |
| 49 | + getChildren().forEach((component) -> { |
| 50 | + component.getElement().executeJs("this.items = $0", |
| 51 | + items.stream().collect(JsonUtils.asArray())); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void commit(int updateId) { |
| 57 | + // not essential for this test |
| 58 | + } |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void initialize() { |
| 64 | + // not essential for this test |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a wrapper with one LitRendererTestComponent. |
| 70 | + */ |
| 71 | + public LitRendererTestComponentWrapper() { |
| 72 | + this(1); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates a wrapper with the given number of LitRendererTestComponent. |
| 77 | + * |
| 78 | + * @param childCount |
| 79 | + * the number of children to add |
| 80 | + */ |
| 81 | + public LitRendererTestComponentWrapper(int childCount) { |
| 82 | + dataCommunicator = new DataCommunicator<>(dataGenerator, arrayUpdater, |
| 83 | + data -> { |
| 84 | + }, getElement().getNode()); |
| 85 | + |
| 86 | + for (int i = 0; i < childCount; i++) { |
| 87 | + add(new LitRendererTestComponent(dataCommunicator, dataGenerator)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Sets the renderer for all children. |
| 93 | + */ |
| 94 | + public void setRenderer(LitRenderer<String> renderer) { |
| 95 | + getComponents().forEach(component -> { |
| 96 | + component.setRenderer(renderer); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Sets the renderer for the component at the given index. |
| 102 | + */ |
| 103 | + public void setRenderer(int componentIndex, LitRenderer<String> renderer) { |
| 104 | + getComponents().get(componentIndex).setRenderer(renderer); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Sets the details renderer for all children. |
| 109 | + */ |
| 110 | + public void setDetailsRenderer(LitRenderer<String> renderer) { |
| 111 | + getComponents().forEach(component -> { |
| 112 | + component.setDetailsRenderer(renderer); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets the details renderer for the component at the given index. |
| 118 | + */ |
| 119 | + public void setDetailsRenderer(int componentIndex, |
| 120 | + LitRenderer<String> renderer) { |
| 121 | + getComponents().get(componentIndex).setDetailsRenderer(renderer); |
| 122 | + } |
| 123 | + |
| 124 | + private List<LitRendererTestComponent> getComponents() { |
| 125 | + return getChildren().map(LitRendererTestComponent.class::cast) |
| 126 | + .collect(Collectors.toList()); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void setItems(Collection<String> items) { |
| 131 | + HasDataProvider.super.setItems(items); |
| 132 | + dataCommunicator.setRequestedRange(0, items.size()); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void setDataProvider(DataProvider<String, ?> dataProvider) { |
| 137 | + dataCommunicator.setDataProvider(dataProvider, null); |
| 138 | + } |
| 139 | +} |
0 commit comments