blob: 5db0e7cd5993def2704c587044db1c740bffe59c [file] [log] [blame]
/*
*
* 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.EClass;
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 EStructuralFeatureComboBox extends CustomField<EStructuralFeature> {
/** The adapter factory. */
private final AdapterFactory adapterFactory;
/** The combobox. */
private ComboBox combobox;
/** The property. */
private ObjectProperty<EStructuralFeature> property;
/** The filter. */
private Filter filter;
/**
* Instantiates a new e structural feature combo box.
*
* @param caption
* the caption
* @param adapterFactory
* the adapter factory
*/
public EStructuralFeatureComboBox(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<EStructuralFeature>(null,
EStructuralFeature.class);
combobox.setPropertyDataSource(property);
property.addValueChangeListener(e -> {
super.setValue((EStructuralFeature) e.getProperty().getValue());
});
return combobox;
}
/* (non-Javadoc)
* @see com.vaadin.ui.AbstractField#getType()
*/
@Override
public Class<EStructuralFeature> getType() {
return EStructuralFeature.class;
}
/* (non-Javadoc)
* @see com.vaadin.ui.AbstractField#setInternalValue(java.lang.Object)
*/
@Override
protected void setInternalValue(EStructuralFeature newValue) {
super.setInternalValue(newValue);
if (property != null) {
property.setValue(newValue);
}
}
/**
* Sets the type which should be used to find all features.
*
* @param eClass
* the e class
* @param filter
* the filter to filter features before displaying
*/
@SuppressWarnings("unchecked")
public void setType(EClass eClass, Filter filter) {
this.filter = filter;
initContent();
combobox.removeAllItems();
for (EStructuralFeature feature : eClass.getEAllStructuralFeatures()) {
if (feature == null) {
continue;
}
if (filter != null && !filter.matches(feature)) {
continue;
}
IItemLabelProvider provider = (IItemLabelProvider) adapterFactory
.adapt(feature, IItemLabelProvider.class);
Item item = combobox.addItem(feature);
String name = provider.getText(feature);
if (feature.isMany()) {
name += "[*]";
}
item.getItemProperty("name").setValue(name);
}
}
/**
* Is used to filter all available features.
*/
public static interface Filter {
/**
* Returns true, if feature should be available.
*
* @param feature
* the feature
* @return true, if successful
*/
boolean matches(EStructuralFeature feature);
}
}