| /** |
| * 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 java.util.Collections; |
| import java.util.HashSet; |
| import java.util.Set; |
| |
| import org.eclipse.core.databinding.observable.set.SetDiff; |
| import org.eclipse.core.databinding.property.INativePropertyListener; |
| import org.eclipse.core.databinding.property.ISimplePropertyListener; |
| import org.eclipse.osbp.runtime.web.vaadin.databinding.properties.AbstractVaadinSetProperty; |
| |
| import com.vaadin.ui.Grid; |
| import com.vaadin.ui.Grid.SelectionModel; |
| import com.vaadin.ui.Grid.SelectionModel.Multi; |
| |
| /** |
| */ |
| public class GridSelectionSetProperty extends AbstractVaadinSetProperty { |
| |
| private Class<?> collectionType; |
| |
| public GridSelectionSetProperty(Class<?> collectionType) { |
| this.collectionType = collectionType; |
| } |
| |
| @Override |
| public INativePropertyListener adaptListener( |
| ISimplePropertyListener listener) { |
| return new SelectionNotifierSetDiffChangeListener(this, listener); |
| } |
| |
| public Object getElementType() { |
| return collectionType; |
| } |
| |
| @SuppressWarnings({ "rawtypes", "unchecked" }) |
| @Override |
| protected Set doGetSet(Object source) { |
| SelectionModel model = ((Grid) source).getSelectionModel(); |
| if (model instanceof SelectionModel.Single) { |
| return Collections.singleton(((SelectionModel.Single) model) |
| .getSelectedRow()); |
| } else if (model instanceof SelectionModel.Multi) { |
| SelectionModel.Multi cModel = (Multi) model; |
| return new HashSet(cModel.getSelectedRows()); |
| } |
| return Collections.emptySet(); |
| } |
| |
| @SuppressWarnings("rawtypes") |
| @Override |
| protected void doSetSet(Object source, Set set, SetDiff diff) { |
| SelectionModel model = ((Grid) source).getSelectionModel(); |
| if (model instanceof SelectionModel.Single) { |
| Object itemId = diff.getAdditions().size() > 0 ? diff |
| .getAdditions().iterator().next() : null; |
| ((SelectionModel.Single) model).select(itemId); |
| } else if (model instanceof SelectionModel.Multi) { |
| SelectionModel.Multi cModel = (Multi) model; |
| cModel.deselect(diff.getRemovals()); |
| cModel.select(diff.getAdditions()); |
| } |
| } |
| } |