blob: c7795c729713400e53fb86f7771da68ce8d7e842 [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 java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.core.databinding.observable.list.ListDiff;
import org.eclipse.core.databinding.observable.list.ListDiffVisitor;
import org.eclipse.core.databinding.property.INativePropertyListener;
import org.eclipse.core.databinding.property.ISimplePropertyListener;
import org.eclipse.osbp.runtime.web.vaadin.databinding.container.IEnhancedFilterableContainer;
import org.eclipse.osbp.runtime.web.vaadin.databinding.properties.AbstractVaadinListProperty;
import org.eclipse.osbp.runtime.web.vaadin.databinding.properties.Util;
import com.vaadin.data.Container;
import com.vaadin.data.Container.Indexed;
import com.vaadin.ui.AbstractSelect;
/**
*/
public class ContainerItemSetContentProperty extends AbstractVaadinListProperty {
private Class<?> collectionType;
public ContainerItemSetContentProperty(Class<?> collectionType) {
this.collectionType = collectionType;
}
@Override
public INativePropertyListener adaptListener(
ISimplePropertyListener listener) {
return new ContainerItemSetChangeListener(this, listener);
}
public Object getElementType() {
return collectionType;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected List<?> doGetList(Object source) {
Container ds = Util.getContainer(source);
Collection<?> result = null;
if (ds instanceof IEnhancedFilterableContainer<?>) {
result = ((IEnhancedFilterableContainer<?>) ds)
.getUnfilteredItemIds();
} else {
result = ds.getItemIds();
}
return (List<?>) ((result instanceof List) ? result : new ArrayList(
result));
}
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void doSetList(final Object source, List list, ListDiff diff) {
final Container ds = Util.getContainer(source);
// set changes on datasource. It will notify listener - BUT ensure
// that selection is unset. Vaadin does not!
diff.accept(new ListDiffVisitor() {
@Override
public void handleRemove(int index, Object element) {
//
if (source instanceof AbstractSelect) {
AbstractSelect select = (AbstractSelect) source;
}
ds.removeItem(element);
}
@Override
public void handleAdd(int index, Object element) {
if (ds instanceof Container.Indexed) {
Container.Indexed indexedDs = (Indexed) ds;
indexedDs.addItemAt(index, element);
} else {
ds.addItem(element);
}
}
});
}
}