All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.v7.client.widget.grid.selection.SelectionEvent Maven / Gradle / Ivy

There is a newer version: 8.27.3
Show newest version
/*
 * Copyright (C) 2000-2023 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */
package com.vaadin.v7.client.widget.grid.selection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.google.gwt.event.shared.GwtEvent;
import com.vaadin.v7.client.widgets.Grid;

/**
 * Event object describing a change in Grid row selection state.
 *
 * @since 7.4
 * @author Vaadin Ltd
 */
@SuppressWarnings("rawtypes")
public class SelectionEvent extends GwtEvent {

    private static final Type EVENT_TYPE = new Type();

    private final Grid grid;
    private final List added;
    private final List removed;
    private final boolean batched;

    /**
     * Creates an event with a single added or removed row.
     *
     * @param grid
     *            grid reference, used for getSource
     * @param added
     *            the added row, or null if a row was not added
     * @param removed
     *            the removed row, or null if a row was not removed
     * @param batched
     *            whether or not this selection change event is triggered during
     *            a batched selection/deselection action
     * @see SelectionModel.Multi.Batched
     */
    public SelectionEvent(Grid grid, T added, T removed, boolean batched) {
        this.grid = grid;
        this.batched = batched;

        if (added != null) {
            this.added = Collections.singletonList(added);
        } else {
            this.added = Collections.emptyList();
        }

        if (removed != null) {
            this.removed = Collections.singletonList(removed);
        } else {
            this.removed = Collections.emptyList();
        }
    }

    /**
     * Creates an event where several rows have been added or removed.
     *
     * @param grid
     *            Grid reference, used for getSource
     * @param added
     *            a collection of added rows, or null if no rows
     *            were added
     * @param removed
     *            a collection of removed rows, or null if no rows
     *            were removed
     * @param batched
     *            whether or not this selection change event is triggered during
     *            a batched selection/deselection action
     * @see SelectionModel.Multi.Batched
     */
    public SelectionEvent(Grid grid, Collection added,
            Collection removed, boolean batched) {
        this.grid = grid;
        this.batched = batched;

        if (added != null) {
            this.added = new ArrayList(added);
        } else {
            this.added = Collections.emptyList();
        }

        if (removed != null) {
            this.removed = new ArrayList(removed);
        } else {
            this.removed = Collections.emptyList();
        }
    }

    /**
     * Gets a reference to the Grid object that fired this event.
     *
     * @return a grid reference
     */
    @Override
    public Grid getSource() {
        return grid;
    }

    /**
     * Gets all rows added to the selection since the last
     * {@link SelectionEvent} .
     *
     * @return a collection of added rows. Empty collection if no rows were
     *         added.
     */
    public Collection getAdded() {
        return Collections.unmodifiableCollection(added);
    }

    /**
     * Gets all rows removed from the selection since the last
     * {@link SelectionEvent}.
     *
     * @return a collection of removed rows. Empty collection if no rows were
     *         removed.
     */
    public Collection getRemoved() {
        return Collections.unmodifiableCollection(removed);
    }

    /**
     * Gets currently selected rows.
     *
     * @return a non-null collection containing all currently selected rows.
     */
    public Collection getSelected() {
        return grid.getSelectedRows();
    }

    /**
     * Gets a type identifier for this event.
     *
     * @return a {@link Type} identifier.
     */
    public static Type getType() {
        return EVENT_TYPE;
    }

    @Override
    public Type getAssociatedType() {
        return EVENT_TYPE;
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void dispatch(SelectionHandler handler) {
        handler.onSelect(this);
    }

    /**
     * Checks if this selection change event is fired during a batched
     * selection/deselection operation.
     *
     * @return true if this event is fired during a batched
     *         selection/deselection operation
     */
    public boolean isBatchedSelection() {
        return batched;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy