blob: 3b4b1dc41e6103a6dd0f2186f82ff5951d663673 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.vaadin.databinding.model.internal;
import org.eclipse.core.databinding.property.INativePropertyListener;
import org.eclipse.core.databinding.property.ISimplePropertyListener;
import org.eclipse.osbp.runtime.web.vaadin.databinding.properties.AbstractVaadinValueProperty;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.SelectionModel;
import com.vaadin.ui.Grid.SelectionModel.Multi;
/**
*/
public class GridSelectionValueProperty extends AbstractVaadinValueProperty {
private Class<?> valueType;
public GridSelectionValueProperty(Class<?> valueType) {
this.valueType = valueType;
}
@Override
public Object getValueType() {
return valueType;
}
@Override
public INativePropertyListener adaptListener(
ISimplePropertyListener listener) {
return new SelectionNotifierValueChangeListener(this, listener);
}
@Override
protected Object doGetValue(Object source) {
SelectionModel model = ((Grid) source).getSelectionModel();
if (model instanceof SelectionModel.Single) {
return ((SelectionModel.Single) model).getSelectedRow();
} else if (model instanceof SelectionModel.Multi) {
SelectionModel.Multi cModel = (Multi) model;
return cModel.getSelectedRows().stream().findFirst().orElse(null);
}
return null;
}
@Override
protected void doSetValue(Object source, Object value) {
SelectionModel model = ((Grid) source).getSelectionModel();
if (model instanceof SelectionModel.Single) {
((SelectionModel.Single) model).select(value);
} else if (model instanceof SelectionModel.Multi) {
SelectionModel.Multi cModel = (Multi) model;
cModel.deselectAll();
cModel.select(value);
}
}
}