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

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

There is a newer version: 8.27.5
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.dom.client.KeyCodes;
import com.google.gwt.event.shared.HandlerRegistration;
import com.vaadin.v7.client.widget.grid.DataAvailableEvent;
import com.vaadin.v7.client.widget.grid.DataAvailableHandler;
import com.vaadin.v7.client.widget.grid.events.BodyKeyDownHandler;
import com.vaadin.v7.client.widget.grid.events.BodyKeyUpHandler;
import com.vaadin.v7.client.widget.grid.events.GridKeyDownEvent;
import com.vaadin.v7.client.widget.grid.events.GridKeyUpEvent;
import com.vaadin.v7.client.widgets.Grid;
import com.vaadin.v7.shared.ui.grid.ScrollDestination;

/**
 * Generic class to perform selections when pressing space key.
 *
 * @author Vaadin Ltd
 * @param 
 *            row data type
 * @since 7.4
 */
public class SpaceSelectHandler {

    /**
     * Handler for space key down events in Grid Body
     */
    private class SpaceKeyDownHandler implements BodyKeyDownHandler {
        private HandlerRegistration scrollHandler = null;

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

            if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE || spaceDown) {
                return;
            }

            // Prevent space page scrolling
            event.getNativeEvent().preventDefault();

            spaceDown = true;
            final int rowIndex = event.getFocusedCell().getRowIndex();

            if (scrollHandler != null) {
                scrollHandler.removeHandler();
                scrollHandler = null;
            }

            scrollHandler = grid
                    .addDataAvailableHandler(new DataAvailableHandler() {

                        @Override
                        public void onDataAvailable(
                                DataAvailableEvent dataAvailableEvent) {
                            if (dataAvailableEvent.getAvailableRows()
                                    .contains(rowIndex)) {
                                setSelected(grid, rowIndex);
                                scrollHandler.removeHandler();
                                scrollHandler = null;
                            }
                        }
                    });
            grid.scrollToRow(rowIndex, ScrollDestination.ANY);
        }

        protected void setSelected(Grid grid, int rowIndex) {
            T row = grid.getDataSource().getRow(rowIndex);

            if (!grid.isSelected(row)) {
                grid.select(row);
            } else if (deselectAllowed) {
                grid.deselect(row);
            }
        }
    }

    private boolean spaceDown = false;
    private Grid grid;
    private HandlerRegistration spaceUpHandler;
    private HandlerRegistration spaceDownHandler;
    private boolean deselectAllowed = true;

    /**
     * Constructor for SpaceSelectHandler. This constructor will add all
     * necessary handlers for selecting rows with space.
     *
     * @param grid
     *            grid to attach to
     */
    public SpaceSelectHandler(Grid grid) {
        this.grid = grid;
        spaceDownHandler = grid
                .addBodyKeyDownHandler(new SpaceKeyDownHandler());
        spaceUpHandler = grid.addBodyKeyUpHandler(new BodyKeyUpHandler() {

            @Override
            public void onKeyUp(GridKeyUpEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_SPACE) {
                    spaceDown = false;
                }
            }
        });
    }

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

    /**
     * Sets whether pressing space for 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 - 2025 Weber Informatics LLC | Privacy Policy