Skip to content

Commit

Permalink
feat: retrieve item object from chip events
Browse files Browse the repository at this point in the history
Add ChipEvent as superclass of removed, created and clicked events.
Update demo in order to user the new getter.

Close #26
  • Loading branch information
javier-godoy committed Jan 27, 2021
1 parent 52627ff commit 4d4984a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,48 @@ public class ChipField<T> extends AbstractField<ChipField<T>, List<T>>
private ItemLabelGenerator<T> itemLabelGenerator;
private SerializableFunction<String, T> newItemHandler;

public abstract static class ChipEvent<T> extends ComponentEvent<ChipField<T>> {

private final String chipLabel;
private final T item;

public ChipEvent(ChipField<T> source, boolean fromClient, String chipLabel) {
super(source, fromClient);
this.chipLabel = chipLabel;
this.item = source.findItemByLabel(chipLabel).orElse(null);
}

public String getChipLabel() {
return chipLabel;
}

public T getItem() {
return item;
}

}

@DomEvent("chip-removed")
public static class ChipRemovedEvent<T> extends ChipEvent<T> {
public ChipRemovedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
super(source, fromClient, chipLabel);
}
}

@DomEvent("chip-created")
public static class ChipCreatedEvent<T> extends ChipEvent<T> {
public ChipCreatedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
super(source, fromClient, chipLabel);
}
}

@DomEvent("chip-clicked")
public static class ChipClickedEvent<T> extends ChipEvent<T> {
public ChipClickedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
super(source, fromClient, chipLabel);
}
}

@SafeVarargs
public ChipField(String label, ItemLabelGenerator<T> itemLabelGenerator, T... availableItems) {
super(new ArrayList<>());
Expand Down Expand Up @@ -231,37 +273,6 @@ public void validate() {
getElement().callJsFunction("validate");
}

// EVENTS
@DomEvent("chip-removed")
public static class ChipRemovedEvent<T> extends ComponentEvent<ChipField<T>> {

private final String chipLabel;

public ChipRemovedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
super(source, fromClient);
this.chipLabel = chipLabel;
}

public String getChipLabel() {
return chipLabel;
}
}

@DomEvent("chip-created")
public static class ChipCreatedEvent<T> extends ComponentEvent<ChipField<T>> {

private final String chipLabel;

public ChipCreatedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
super(source, fromClient);
this.chipLabel = chipLabel;
}

public String getChipLabel() {
return chipLabel;
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Registration addChipRemovedListener(ComponentEventListener<ChipRemovedEvent<T>> listener) {
return addListener(ChipRemovedEvent.class, (ComponentEventListener) listener);
Expand Down Expand Up @@ -324,21 +335,6 @@ private void removeSelectedItem(T itemToRemove, boolean fromClient) {
}
}

@DomEvent("chip-clicked")
public static class ChipClickedEvent<T> extends ComponentEvent<ChipField<T>> {

private final String chipLabel;

public ChipClickedEvent(ChipField<T> source, boolean fromClient, @EventData(CHIP_LABEL) String chipLabel) {
super(source, fromClient);
this.chipLabel = chipLabel;
}

public String getChipLabel() {
return chipLabel;
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public Registration addChipClickedListener(ComponentEventListener<ChipClickedEvent<T>> listener) {
return addListener(ChipClickedEvent.class, (ComponentEventListener) listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public DataProviderDemo() {
}));

chf.addChipCreatedListener(
ev -> Notification.show("Chip: " + ev.getChipLabel() + " Created by client: " + ev.isFromClient() + "!",
ev -> Notification.show("Chip: " + ev.getItem() + " Created by client: " + ev.isFromClient() + "!",
5000, Position.BOTTOM_START));
chf.addChipRemovedListener(
ev -> Notification.show("Chip: " + ev.getChipLabel() + " Removed by client: " + ev.isFromClient() + "!",
ev -> Notification.show("Chip: " + ev.getItem() + " Removed by client: " + ev.isFromClient() + "!",
5000, Position.BOTTOM_START));
chf.addChipClickedListener(
ev -> Notification.show("Chip: " + ev.getChipLabel() + " Clicked!", 5000, Position.BOTTOM_END));
ev -> Notification.show("Chip: " + ev.getItem() + " Clicked!", 5000, Position.BOTTOM_END));

buttons.add(new Button("All planets", ev -> {
chf.setValue(Planet.all());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,10 @@ public static Planet random() {
return new Planet("Planet " + Integer.toString((int) Math.round(Math.random() * 36 * 36 * 36), 36).toUpperCase());
}

@Override
public String toString() {
return name;
}

}

0 comments on commit 4d4984a

Please sign in to comment.