| /* |
| * |
| * 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * 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.ecore.EObject; |
| import org.eclipse.osbp.runtime.web.vaadin.common.resource.IResourceProvider; |
| import org.eclipse.osbp.vaadin.emf.api.IModelingContext; |
| import org.eclipse.osbp.vaadin.emf.data.JvmSubTypesContainer; |
| import org.eclipse.osbp.vaadin.emf.data.JvmSubTypesContainer.SubTypesCallback; |
| import org.eclipse.xtext.common.types.JvmDeclaredType; |
| |
| import com.vaadin.data.util.ObjectProperty; |
| import com.vaadin.ui.ComboBox; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.CustomField; |
| |
| /** |
| * A combobox to select referenced JvmDeclaredType. |
| */ |
| @SuppressWarnings("serial") |
| public class JvmSubTypesComboBox extends CustomField<JvmDeclaredType> { |
| |
| /** The combobox. */ |
| private ComboBox combobox; |
| |
| /** The property. */ |
| private ObjectProperty<JvmDeclaredType> property; |
| |
| JvmSubTypesContainer container; |
| |
| /** |
| * Instantiates a new e object combo box. |
| * |
| * @param adapterFactory |
| * the adapter factory |
| */ |
| public JvmSubTypesComboBox(IModelingContext modelingContext, |
| IResourceProvider resourceProvider, SubTypesCallback callback) { |
| container = new JvmSubTypesContainer(modelingContext, resourceProvider, |
| callback); |
| |
| // 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.setContainerDataSource(container); |
| |
| combobox.setItemCaptionPropertyId(JvmSubTypesContainer.LABEL); |
| combobox.setItemIconPropertyId(JvmSubTypesContainer.ICON); |
| |
| property = new ObjectProperty<JvmDeclaredType>(null, |
| JvmDeclaredType.class); |
| combobox.setPropertyDataSource(property); |
| |
| property.addValueChangeListener(e -> { |
| super.setValue((JvmDeclaredType) e.getProperty().getValue()); |
| }); |
| |
| return combobox; |
| } |
| |
| /** |
| * Returns the root type. |
| * |
| * @return |
| */ |
| public EObject getRootType() { |
| return container.getRootElement(); |
| } |
| |
| /** |
| * Sets the root type. |
| * |
| * @param rootElement |
| */ |
| public void setRootType(JvmDeclaredType rootElement) { |
| container.setRootElement(rootElement); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.ui.AbstractField#getType() |
| */ |
| @Override |
| public Class<JvmDeclaredType> getType() { |
| return JvmDeclaredType.class; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.ui.AbstractField#setInternalValue(java.lang.Object) |
| */ |
| @Override |
| protected void setInternalValue(JvmDeclaredType newValue) { |
| super.setInternalValue(newValue); |
| |
| if (property != null) { |
| property.setValue(newValue); |
| } |
| } |
| |
| /** |
| * Gets the collection. |
| * |
| * @return the collection |
| */ |
| @SuppressWarnings("unchecked") |
| public Collection<JvmDeclaredType> getCollection() { |
| return (Collection<JvmDeclaredType>) combobox.getItemIds(); |
| } |
| |
| } |