| /* |
| * |
| * Copyright (c) 2011, 2016 - 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 |
| * Loetz GmbH&Co.KG |
| * |
| */ |
| package org.eclipse.osbp.vaadin.emf.data.fields; |
| |
| import java.util.Collection; |
| |
| import org.eclipse.emf.common.notify.AdapterFactory; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EStructuralFeature; |
| import org.eclipse.emf.edit.provider.IItemLabelProvider; |
| |
| import com.vaadin.data.Item; |
| import com.vaadin.data.util.ObjectProperty; |
| import com.vaadin.ui.ComboBox; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.CustomField; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * A combobox to select referenced EObjects. |
| */ |
| @SuppressWarnings("serial") |
| public class EObjectComboBox extends CustomField<EObject> { |
| |
| /** The adapter factory. */ |
| private final AdapterFactory adapterFactory; |
| |
| /** The combobox. */ |
| private ComboBox combobox; |
| |
| /** The property. */ |
| private ObjectProperty<EObject> property; |
| |
| /** |
| * Instantiates a new e object combo box. |
| * |
| * @param adapterFactory |
| * the adapter factory |
| */ |
| public EObjectComboBox(AdapterFactory adapterFactory) { |
| this.adapterFactory = adapterFactory; |
| |
| // eager init |
| initContent(); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.ui.CustomField#initContent() |
| */ |
| @Override |
| protected Component initContent() { |
| if (combobox != null) { |
| return combobox; |
| } |
| |
| combobox = new ComboBox(); |
| combobox.setWidth("100%"); |
| combobox.setItemCaptionPropertyId("name"); |
| |
| combobox.addContainerProperty("name", String.class, ""); |
| |
| property = new ObjectProperty<EObject>(null, EObject.class); |
| combobox.setPropertyDataSource(property); |
| |
| property.addValueChangeListener(e -> { |
| super.setValue((EObject) e.getProperty().getValue()); |
| }); |
| |
| return combobox; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.ui.AbstractField#getType() |
| */ |
| @Override |
| public Class<EObject> getType() { |
| return EObject.class; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.ui.AbstractField#setInternalValue(java.lang.Object) |
| */ |
| @Override |
| protected void setInternalValue(EObject newValue) { |
| super.setInternalValue(newValue); |
| |
| if (property != null) { |
| property.setValue(newValue); |
| } |
| } |
| |
| /** |
| * Gets the collection. |
| * |
| * @return the collection |
| */ |
| @SuppressWarnings("unchecked") |
| public Collection<EObject> getCollection() { |
| return (Collection<EObject>) combobox.getItemIds(); |
| } |
| |
| /** |
| * Sets the collection. |
| * |
| * @param eObjects |
| * the new collection |
| */ |
| @SuppressWarnings("unchecked") |
| public void setCollection(Collection<? extends EObject> eObjects) { |
| initContent(); |
| |
| combobox.removeAllItems(); |
| |
| for (EObject eObject : eObjects) { |
| if (eObject == null) { |
| continue; |
| } |
| IItemLabelProvider provider = (IItemLabelProvider) adapterFactory |
| .adapt(eObject, IItemLabelProvider.class); |
| if (provider != null) { |
| Item item = combobox.addItem(eObject); |
| item.getItemProperty("name") |
| .setValue(provider.getText(eObject)); |
| } else if (eObject.eClass().getEStructuralFeature("name") != null) { |
| Item item = combobox.addItem(eObject); |
| item.getItemProperty("name").setValue( |
| eObject.eGet(eObject.eClass().getEStructuralFeature( |
| "name"))); |
| } |
| } |
| } |
| } |