From b20166dbaf5dcd7377e8a11f2f8e06a738e8b739 Mon Sep 17 00:00:00 2001 From: Paola De Bartolo Date: Tue, 26 Mar 2024 16:34:26 -0300 Subject: [PATCH 1/3] feat: add read only binder support Close #18 --- .../vaadin/addons/badgelist/BadgeList.java | 31 +++++- .../addons/badgelist/BadgeListDemoView.java | 1 + .../vaadin/addons/badgelist/Person.java | 5 +- .../addons/badgelist/ReadOnlyBinderDemo.java | 100 ++++++++++++++++++ .../vaadin/addons/badgelist/TestData.java | 4 + .../styles/badge-list-demo-styles.css | 6 ++ 6 files changed, 139 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/flowingcode/vaadin/addons/badgelist/ReadOnlyBinderDemo.java diff --git a/src/main/java/com/flowingcode/vaadin/addons/badgelist/BadgeList.java b/src/main/java/com/flowingcode/vaadin/addons/badgelist/BadgeList.java index f64330f..793f1ba 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/badgelist/BadgeList.java +++ b/src/main/java/com/flowingcode/vaadin/addons/badgelist/BadgeList.java @@ -39,6 +39,11 @@ public class BadgeList extends Component implements HasTheme, HasSize { private List badges = new ArrayList<>(); + + /** + * Creates a new instance of BadgeList. + */ + public BadgeList() {} /** * Creates a new instance of BadgeList with the supplied list of {@link Badge badges}. @@ -46,15 +51,31 @@ public class BadgeList extends Component implements HasTheme, HasSize { * @param badges list of badges */ public BadgeList(List badges) { + this.setBadges(badges); + } + + /** + * Sets a list of {@link Badge badges} to the BadgeList component. + * + * @param badges the list of badges to add + */ + public void setBadges(List badges) { + this.clearBadges(); this.badges = badges; this.addBadges(); } private void addBadges() { - this.badges.forEach( - badge -> { - badge.getElement().setAttribute("slot", "badges"); - this.getElement().appendChild(badge.getElement()); - }); + this.badges.forEach(badge -> { + badge.getElement().setAttribute("slot", "badges"); + this.getElement().appendChild(badge.getElement()); + }); + } + + private void clearBadges() { + this.badges.forEach(badge -> { + badge.getElement().removeAttribute("slot"); + badge.removeFromParent(); + }); } } diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemoView.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemoView.java index ccf2be0..b1a5cbc 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemoView.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemoView.java @@ -38,6 +38,7 @@ public class BadgeListDemoView extends TabbedDemo { public BadgeListDemoView() { addDemo(BadgeListDemo.class); addDemo(StyledBadgesDemo.class); + addDemo(ReadOnlyBinderDemo.class); setSizeFull(); } } diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/Person.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/Person.java index 298e81c..6bc2ef0 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/Person.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/Person.java @@ -20,16 +20,15 @@ package com.flowingcode.vaadin.addons.badgelist; import java.util.List; -import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.EqualsAndHashCode; import lombok.Getter; -import lombok.experimental.FieldDefaults; +import lombok.Setter; @Getter +@Setter @AllArgsConstructor -@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) @EqualsAndHashCode @Builder public class Person { diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/ReadOnlyBinderDemo.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/ReadOnlyBinderDemo.java new file mode 100644 index 0000000..8212f3b --- /dev/null +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/ReadOnlyBinderDemo.java @@ -0,0 +1,100 @@ +package com.flowingcode.vaadin.addons.badgelist; + +import com.flowingcode.vaadin.addons.demo.DemoSource; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.combobox.MultiSelectComboBox; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.html.Span; +import com.vaadin.flow.component.orderedlayout.FlexComponent.JustifyContentMode; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.data.binder.Binder; +import com.vaadin.flow.data.binder.ReadOnlyHasValue; +import com.vaadin.flow.router.PageTitle; +import com.vaadin.flow.router.Route; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +@DemoSource +@PageTitle("Read Only Binder Demo") +@SuppressWarnings("serial") +@Route(value = "badge-list/readonly", layout = BadgeListDemoView.class) +public class ReadOnlyBinderDemo extends BaseBadgeListDemo { + + private TextField firstName = new TextField("First Name"); + private TextField lastName = new TextField("Last Name"); + private MultiSelectComboBox rolesComboBox = + new MultiSelectComboBox("Select Roles"); + private BadgeList rolesBadgeList = new BadgeList(); + private ReadOnlyHasValue> readonlyBadgeList; + private Div rolesBadgeListDiv = new Div(); + private Button editModeButton = new Button("Edit Mode"); + private Button readOnlyModeButton = new Button("Read Only Mode"); + private Binder binder; + + public ReadOnlyBinderDemo() { + + Person person = TestData.singlePerson(); + + List roles = + IntStream.rangeClosed(1, 12).mapToObj(i -> "ROLE" + i).collect(Collectors.toList()); + rolesComboBox.setItems(roles); + + rolesBadgeList = new BadgeList(); + readonlyBadgeList = new ReadOnlyHasValue>(rolesBadgeList::setBadges); + + binder = new Binder<>(); + binder.bind(firstName, Person::getFirstName, Person::setFirstName); + binder.bind(lastName, Person::getLastName, Person::setLastName); + readOnlyMode(); + binder.setBean(person); + + editModeButton.addClickListener(e -> editMode()); + readOnlyModeButton.addClickListener(e -> readOnlyMode()); + + HorizontalLayout buttonsLayout = new HorizontalLayout(); + buttonsLayout.add(editModeButton, readOnlyModeButton); + buttonsLayout.setMargin(true); + buttonsLayout.setJustifyContentMode(JustifyContentMode.END); + + VerticalLayout layout = new VerticalLayout(); + Span span = new Span("Roles"); + span.addClassName("readonly-badge-list-label"); + rolesBadgeListDiv.add(span); + rolesBadgeListDiv.setWidth("450px"); + rolesBadgeListDiv.add(rolesBadgeList); + layout.add(firstName, lastName, rolesComboBox, rolesBadgeListDiv); + + add(layout, buttonsLayout); + } + + private void editMode() { + editModeButton.setEnabled(false); + readOnlyModeButton.setEnabled(true); + rolesBadgeListDiv.setVisible(false); + binder.removeBinding(readonlyBadgeList); + rolesComboBox.setVisible(true); + binder.bind(rolesComboBox, person -> new HashSet(person.getRoles()), + (person, roles) -> { + person.setRoles(new ArrayList(roles)); + }); + binder.setReadOnly(false); + } + + private void readOnlyMode() { + readOnlyModeButton.setEnabled(false); + editModeButton.setEnabled(true); + rolesComboBox.setVisible(false); + binder.removeBinding(rolesComboBox); + rolesBadgeListDiv.setVisible(true); + binder.forField(readonlyBadgeList).bindReadOnly(person -> { + return person.getRoles().stream() + .collect(Collectors.mapping(role -> new Badge(role), Collectors.toList())); + }); + binder.setReadOnly(true); + } +} diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/TestData.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/TestData.java index ba61d3a..f264870 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/TestData.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/TestData.java @@ -44,6 +44,10 @@ private static synchronized Person newPerson() { public static List initializeData() { return Stream.generate(TestData::newPerson).limit(8).collect(Collectors.toList()); } + + public static Person singlePerson() { + return newPerson(); + } private static List generateRoles() { List roles = new ArrayList<>(); diff --git a/src/test/resources/META-INF/frontend/styles/badge-list-demo-styles.css b/src/test/resources/META-INF/frontend/styles/badge-list-demo-styles.css index 710c4d2..723332b 100644 --- a/src/test/resources/META-INF/frontend/styles/badge-list-demo-styles.css +++ b/src/test/resources/META-INF/frontend/styles/badge-list-demo-styles.css @@ -36,3 +36,9 @@ fc-badge-list[class="styled-badges-second-example"]::part(overflow-badge), .cust border: 1px green dashed; border-radius: 0; } + +.readonly-badge-list-label { + color: var(--lumo-secondary-text-color); + font-weight: 500; + font-size: var(--lumo-font-size-s); +} From 85bbab3a44df00e092bf34b21b82eb9938a54f21 Mon Sep 17 00:00:00 2001 From: Paola De Bartolo Date: Tue, 26 Mar 2024 16:35:22 -0300 Subject: [PATCH 2/3] build: update version to 1.1.0-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f374a0d..34add2e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.flowingcode.vaadin.addons badge-list-addon - 1.0.2-SNAPSHOT + 1.1.0-SNAPSHOT Badge List Add-on Badge List Add-on for Vaadin Flow https://www.flowingcode.com/en/open-source/ From 04c6c22da0eb2ebd9635601d071cf80e84fed196 Mon Sep 17 00:00:00 2001 From: Paola De Bartolo Date: Tue, 26 Mar 2024 16:40:19 -0300 Subject: [PATCH 3/3] chore: update license headers --- .../vaadin/addons/badgelist/Badge.java | 2 +- .../vaadin/addons/badgelist/BadgeList.java | 2 +- .../resources/frontend/src/fc-badge-list.ts | 2 +- .../flowingcode/vaadin/addons/DemoLayout.java | 2 +- .../addons/badgelist/BadgeListDemo.java | 2 +- .../addons/badgelist/BadgeListDemoView.java | 2 +- .../addons/badgelist/BaseBadgeListDemo.java | 2 +- .../vaadin/addons/badgelist/DemoView.java | 2 +- .../vaadin/addons/badgelist/Person.java | 2 +- .../addons/badgelist/ReadOnlyBinderDemo.java | 19 +++++++++++++++++++ .../addons/badgelist/StyledBadgesDemo.java | 2 +- .../vaadin/addons/badgelist/TestData.java | 2 +- .../addons/badgelist/it/AbstractViewTest.java | 2 +- .../vaadin/addons/badgelist/it/ViewIT.java | 2 +- .../badgelist/test/SerializationTest.java | 2 +- .../styles/badge-list-demo-styles.css | 2 +- 16 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/badgelist/Badge.java b/src/main/java/com/flowingcode/vaadin/addons/badgelist/Badge.java index 8994385..a9fea38 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/badgelist/Badge.java +++ b/src/main/java/com/flowingcode/vaadin/addons/badgelist/Badge.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/com/flowingcode/vaadin/addons/badgelist/BadgeList.java b/src/main/java/com/flowingcode/vaadin/addons/badgelist/BadgeList.java index 793f1ba..eca46dc 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/badgelist/BadgeList.java +++ b/src/main/java/com/flowingcode/vaadin/addons/badgelist/BadgeList.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/resources/META-INF/resources/frontend/src/fc-badge-list.ts b/src/main/resources/META-INF/resources/frontend/src/fc-badge-list.ts index 1498b5e..928218a 100644 --- a/src/main/resources/META-INF/resources/frontend/src/fc-badge-list.ts +++ b/src/main/resources/META-INF/resources/frontend/src/fc-badge-list.ts @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/DemoLayout.java b/src/test/java/com/flowingcode/vaadin/addons/DemoLayout.java index 2c94686..69007a9 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/DemoLayout.java +++ b/src/test/java/com/flowingcode/vaadin/addons/DemoLayout.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemo.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemo.java index 2553510..8d6aac0 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemo.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemoView.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemoView.java index b1a5cbc..1560058 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemoView.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/BadgeListDemoView.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/BaseBadgeListDemo.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/BaseBadgeListDemo.java index 6fdc0dc..941f975 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/BaseBadgeListDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/BaseBadgeListDemo.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/DemoView.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/DemoView.java index 1a4bdf6..c1b812d 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/DemoView.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/DemoView.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/Person.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/Person.java index 6bc2ef0..a20332f 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/Person.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/Person.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/ReadOnlyBinderDemo.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/ReadOnlyBinderDemo.java index 8212f3b..8d0c4d2 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/ReadOnlyBinderDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/ReadOnlyBinderDemo.java @@ -1,3 +1,22 @@ +/*- + * #%L + * Badge List Add-on + * %% + * Copyright (C) 2023 - 2024 Flowing Code + * %% + * 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. + * #L% + */ package com.flowingcode.vaadin.addons.badgelist; import com.flowingcode.vaadin.addons.demo.DemoSource; diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/StyledBadgesDemo.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/StyledBadgesDemo.java index ae87e13..78e9617 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/StyledBadgesDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/StyledBadgesDemo.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/TestData.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/TestData.java index f264870..dd90c93 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/TestData.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/TestData.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/it/AbstractViewTest.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/it/AbstractViewTest.java index 1b393a0..ec65558 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/it/AbstractViewTest.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/it/AbstractViewTest.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/it/ViewIT.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/it/ViewIT.java index f09671d..40b525b 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/it/ViewIT.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/it/ViewIT.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/com/flowingcode/vaadin/addons/badgelist/test/SerializationTest.java b/src/test/java/com/flowingcode/vaadin/addons/badgelist/test/SerializationTest.java index 33a0a03..9808a4d 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/badgelist/test/SerializationTest.java +++ b/src/test/java/com/flowingcode/vaadin/addons/badgelist/test/SerializationTest.java @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/resources/META-INF/frontend/styles/badge-list-demo-styles.css b/src/test/resources/META-INF/frontend/styles/badge-list-demo-styles.css index 723332b..3474075 100644 --- a/src/test/resources/META-INF/frontend/styles/badge-list-demo-styles.css +++ b/src/test/resources/META-INF/frontend/styles/badge-list-demo-styles.css @@ -2,7 +2,7 @@ * #%L * Badge List Add-on * %% - * Copyright (C) 2023 Flowing Code + * Copyright (C) 2023 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.