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

com.vaadin.client.widget.grid.selection.SelectionModelSingle Maven / Gradle / Ivy

/*
 * Copyright 2000-2014 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.client.widget.grid.selection;

import java.util.Collection;
import java.util.Collections;

import com.vaadin.client.data.DataSource.RowHandle;
import com.vaadin.client.renderers.Renderer;
import com.vaadin.client.widgets.Grid;

/**
 * Single-row selection model.
 * 
 * @author Vaadin Ltd
 * @since 7.4
 */
public class SelectionModelSingle extends AbstractRowHandleSelectionModel
        implements SelectionModel.Single {

    private Grid grid;
    private RowHandle selectedRow;

    /** Event handling for selection with space key */
    private SpaceSelectHandler spaceSelectHandler;

    /** Event handling for selection by clicking cells */
    private ClickSelectHandler clickSelectHandler;

    private boolean deselectAllowed = true;

    @Override
    public boolean isSelected(T row) {
        return selectedRow != null
                && selectedRow.equals(grid.getDataSource().getHandle(row));
    }

    @Override
    public Renderer getSelectionColumnRenderer() {
        // No Selection column renderer for single selection
        return null;
    }

    @Override
    public void setGrid(Grid grid) {
        if (this.grid != null && grid != null) {
            // Trying to replace grid
            throw new IllegalStateException(
                    "Selection model is already attached to a grid. "
                            + "Remove the selection model first from "
                            + "the grid and then add it.");
        }

        this.grid = grid;
        if (this.grid != null) {
            spaceSelectHandler = new SpaceSelectHandler(grid);
            clickSelectHandler = new ClickSelectHandler(grid);
            updateHandlerDeselectAllowed();
        } else {
            spaceSelectHandler.removeHandler();
            clickSelectHandler.removeHandler();
            spaceSelectHandler = null;
            clickSelectHandler = null;
        }
    }

    @Override
    public boolean select(T row) {

        if (row == null) {
            throw new IllegalArgumentException("Row cannot be null");
        }

        T removed = getSelectedRow();
        if (selectByHandle(grid.getDataSource().getHandle(row))) {
            grid.fireEvent(new SelectionEvent(grid, row, removed, false));

            return true;
        }
        return false;
    }

    @Override
    public boolean deselect(T row) {

        if (row == null) {
            throw new IllegalArgumentException("Row cannot be null");
        }

        if (isSelected(row)) {
            deselectByHandle(selectedRow);
            grid.fireEvent(new SelectionEvent(grid, null, row, false));
            return true;
        }

        return false;
    }

    @Override
    public T getSelectedRow() {
        return (selectedRow != null ? selectedRow.getRow() : null);
    }

    @Override
    public void reset() {
        if (selectedRow != null) {
            deselect(getSelectedRow());
        }
    }

    @Override
    public Collection getSelectedRows() {
        if (getSelectedRow() != null) {
            return Collections.singleton(getSelectedRow());
        }
        return Collections.emptySet();
    }

    @Override
    protected boolean selectByHandle(RowHandle handle) {
        if (handle != null && !handle.equals(selectedRow)) {
            deselectByHandle(selectedRow);
            selectedRow = handle;
            selectedRow.pin();
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected boolean deselectByHandle(RowHandle handle) {
        if (handle != null && handle.equals(selectedRow)) {
            selectedRow.unpin();
            selectedRow = null;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void setDeselectAllowed(boolean deselectAllowed) {
        this.deselectAllowed = deselectAllowed;
        updateHandlerDeselectAllowed();
    }

    @Override
    public boolean isDeselectAllowed() {
        return deselectAllowed;
    }

    private void updateHandlerDeselectAllowed() {
        if (spaceSelectHandler != null) {
            spaceSelectHandler.setDeselectAllowed(deselectAllowed);
        }
        if (clickSelectHandler != null) {
            clickSelectHandler.setDeselectAllowed(deselectAllowed);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy