| /* |
| * |
| * 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 org.eclipse.emf.common.notify.AdapterFactory; |
| import org.eclipse.emf.ecore.EEnum; |
| import org.eclipse.emf.ecore.EEnumLiteral; |
| 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 {@link EEnumLiteral}. |
| */ |
| @SuppressWarnings("serial") |
| public class EEnumComboBox extends CustomField<EEnumLiteral> { |
| |
| /** The adapter factory. */ |
| private final AdapterFactory adapterFactory; |
| |
| /** The combobox. */ |
| private ComboBox combobox; |
| |
| /** The property. */ |
| private ObjectProperty<EEnumLiteral> property; |
| |
| /** |
| * Instantiates a new e enum combo box. |
| * |
| * @param caption |
| * the caption |
| * @param adapterFactory |
| * the adapter factory |
| */ |
| public EEnumComboBox(String caption, AdapterFactory adapterFactory) { |
| setCaption(caption); |
| 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<EEnumLiteral>(null, EEnumLiteral.class); |
| combobox.setPropertyDataSource(property); |
| |
| property.addValueChangeListener(e -> { |
| super.setValue((EEnumLiteral) e.getProperty().getValue()); |
| }); |
| |
| return combobox; |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.ui.AbstractField#getType() |
| */ |
| @Override |
| public Class<EEnumLiteral> getType() { |
| return EEnumLiteral.class; |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.ui.AbstractField#setInternalValue(java.lang.Object) |
| */ |
| @Override |
| protected void setInternalValue(EEnumLiteral newValue) { |
| super.setInternalValue(newValue); |
| |
| if (property != null) { |
| property.setValue(newValue); |
| } |
| } |
| |
| /** |
| * Sets the type which should be used to find all literals. |
| * |
| * @param eenum |
| * the new type |
| */ |
| @SuppressWarnings("unchecked") |
| public void setType(EEnum eenum) { |
| initContent(); |
| |
| combobox.removeAllItems(); |
| |
| for (EEnumLiteral lit : eenum.getELiterals()) { |
| if (lit == null) { |
| continue; |
| } |
| |
| IItemLabelProvider provider = (IItemLabelProvider) adapterFactory |
| .adapt(lit, IItemLabelProvider.class); |
| Item item = combobox.addItem(lit); |
| String name = provider.getText(lit); |
| item.getItemProperty("name").setValue(name); |
| } |
| } |
| |
| /** |
| * Is used to filter all available literals. |
| */ |
| public static interface Filter { |
| |
| /** |
| * Returns true, if feature should be available. |
| * |
| * @param feature |
| * the feature |
| * @return true, if successful |
| */ |
| boolean matches(EStructuralFeature feature); |
| } |
| } |