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

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

There is a newer version: 8.27.3
Show newest version
/*
 * 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.v7.client.widget.grid.selection;

import com.google.gwt.event.shared.HandlerRegistration;
import com.vaadin.v7.client.widget.grid.events.BodyClickHandler;
import com.vaadin.v7.client.widget.grid.events.GridClickEvent;
import com.vaadin.v7.client.widgets.Grid;

/**
 * Generic class to perform selections when clicking on cells in body of Grid.
 *
 * @since 7.4
 * @author Vaadin Ltd
 */
public class ClickSelectHandler {

    private Grid grid;
    private HandlerRegistration clickHandler;
    private boolean deselectAllowed = true;

    private class RowClickHandler implements BodyClickHandler {

        @Override
        public void onClick(GridClickEvent event) {
            if (!grid.isUserSelectionAllowed()) {
                return;
            }

            T row = (T) event.getTargetCell().getRow();
            if (!grid.isSelected(row)) {
                grid.select(row);
            } else if (deselectAllowed) {
                grid.deselect(row);
            }
        }
    }

    /**
     * Constructor for ClickSelectHandler. This constructor will add all
     * necessary handlers for selecting rows by clicking cells.
     *
     * @param grid
     *            grid to attach to
     */
    public ClickSelectHandler(Grid grid) {
        this.grid = grid;
        clickHandler = grid.addBodyClickHandler(new RowClickHandler());
    }

    /**
     * Clean up function for removing all now obsolete handlers.
     */
    public void removeHandler() {
        clickHandler.removeHandler();
    }

    /**
     * Sets whether clicking the currently selected row should deselect the row.
     *
     * @param deselectAllowed
     *            true to allow deselecting the selected row;
     *            otherwise false
     */
    public void setDeselectAllowed(boolean deselectAllowed) {
        this.deselectAllowed = deselectAllowed;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy