
com.vaadin.event.selection.SingleSelectionEvent Maven / Gradle / Ivy
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* license.
*/
package com.vaadin.event.selection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.ui.AbstractSingleSelect;
import com.vaadin.ui.Component;
import com.vaadin.ui.SingleSelect;
/**
* Fired when the selection changes in a listing component.
*
* @author Vaadin Ltd.
*
* @param
* the type of the selected item
* @since 8.0
*/
public class SingleSelectionEvent extends ValueChangeEvent
implements SelectionEvent {
/**
* Creates a new selection change event.
*
* @param source
* the listing that fired the event
* @param oldSelection
* the item that was previously selected
* @param userOriginated
* {@code true} if this event originates from the client,
* {@code false} otherwise.
*/
public SingleSelectionEvent(AbstractSingleSelect source, T oldSelection,
boolean userOriginated) {
super(source, oldSelection, userOriginated);
}
/**
* Creates a new selection change event in a component.
*
* @param component
* the component where the event originated
* @param source
* the single select source
* @param oldSelection
* the item that was previously selected
* @param userOriginated
* {@code true} if this event originates from the client,
* {@code false} otherwise.
*/
public SingleSelectionEvent(Component component, SingleSelect source,
T oldSelection, boolean userOriginated) {
super(component, source, oldSelection, userOriginated);
}
/**
* Returns an optional of the item that was selected, or an empty optional
* if a previously selected item was deselected.
*
* The result is the current selection of the source
* {@link AbstractSingleSelect} object. So it's always exactly the same as
* optional describing {@link AbstractSingleSelect#getValue()}.
*
* @see #getValue()
*
* @return the selected item or an empty optional if deselected
*
* @see com.vaadin.data.SelectionModel.Single#getSelectedItem()
* SelectionModel.Single#getSelectedItem()
*/
public Optional getSelectedItem() {
return Optional.ofNullable(getValue());
}
/**
* The single select on which the Event initially occurred.
*
* @return The single select on which the Event initially occurred.
*/
@Override
public SingleSelect getSource() {
return (SingleSelect) super.getSource();
}
@Override
public Optional getFirstSelectedItem() {
return getSelectedItem();
}
@Override
public Set getAllSelectedItems() {
Optional selectedItem = getSelectedItem();
if (selectedItem.isPresent()) {
return Collections.singleton(selectedItem.get());
} else {
return Collections.emptySet();
}
}
}