Skip to content

Commit

Permalink
Merge pull request #20 from FlowingCode/issue-18
Browse files Browse the repository at this point in the history
Add read only binder support
  • Loading branch information
flang authored Mar 26, 2024
2 parents 7c5e0e2 + 04c6c22 commit a68d432
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>badge-list-addon</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<name>Badge List Add-on</name>
<description>Badge List Add-on for Vaadin Flow</description>
<url>https://www.flowingcode.com/en/open-source/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -39,22 +39,43 @@
public class BadgeList extends Component implements HasTheme, HasSize {

private List<Badge> 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}.
*
* @param badges list of badges
*/
public BadgeList(List<Badge> 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<Badge> 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();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -38,6 +38,7 @@ public class BadgeListDemoView extends TabbedDemo {
public BadgeListDemoView() {
addDemo(BadgeListDemo.class);
addDemo(StyledBadgesDemo.class);
addDemo(ReadOnlyBinderDemo.class);
setSizeFull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*-
* #%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;
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<String> rolesComboBox =
new MultiSelectComboBox<String>("Select Roles");
private BadgeList rolesBadgeList = new BadgeList();
private ReadOnlyHasValue<List<Badge>> readonlyBadgeList;
private Div rolesBadgeListDiv = new Div();
private Button editModeButton = new Button("Edit Mode");
private Button readOnlyModeButton = new Button("Read Only Mode");
private Binder<Person> binder;

public ReadOnlyBinderDemo() {

Person person = TestData.singlePerson();

List<String> roles =
IntStream.rangeClosed(1, 12).mapToObj(i -> "ROLE" + i).collect(Collectors.toList());
rolesComboBox.setItems(roles);

rolesBadgeList = new BadgeList();
readonlyBadgeList = new ReadOnlyHasValue<List<Badge>>(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<String>(person.getRoles()),
(person, roles) -> {
person.setRoles(new ArrayList<String>(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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -44,6 +44,10 @@ private static synchronized Person newPerson() {
public static List<Person> initializeData() {
return Stream.generate(TestData::newPerson).limit(8).collect(Collectors.toList());
}

public static Person singlePerson() {
return newPerson();
}

private static List<String> generateRoles() {
List<String> roles = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

0 comments on commit a68d432

Please sign in to comment.