| /** |
| * 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal; |
| |
| import java.util.Locale; |
| |
| import org.eclipse.core.databinding.observable.IObservable; |
| import org.eclipse.core.databinding.observable.value.IObservableValue; |
| import org.eclipse.emf.databinding.EMFObservables; |
| import org.eclipse.osbp.ecview.core.common.beans.InMemoryBeanProvider; |
| import org.eclipse.osbp.ecview.core.common.editpart.IElementEditpart; |
| import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddableBindingEndpoint; |
| import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddableValueEndpoint; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelPackage; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YBeanReferenceField; |
| import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.IBeanReferenceFieldEditpart; |
| import org.eclipse.osbp.runtime.common.annotations.TargetEnumConstraints; |
| import org.eclipse.osbp.runtime.common.state.ISharedStateContext; |
| import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractFieldWidgetPresenter; |
| import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal.util.Util; |
| import org.eclipse.osbp.runtime.web.vaadin.common.data.IBeanSearchService; |
| import org.eclipse.osbp.runtime.web.vaadin.common.data.IBeanSearchServiceFactory; |
| import org.eclipse.osbp.runtime.web.vaadin.common.data.StatefulInMemoryBeanSearchService; |
| import org.eclipse.osbp.runtime.web.vaadin.common.services.filter.AnnotationToVaadinFilterConverter; |
| import org.eclipse.osbp.runtime.web.vaadin.components.fields.BeanReferenceField; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.vaadin.data.Container.Filter; |
| import com.vaadin.server.ErrorMessage; |
| import com.vaadin.server.Resource; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.ComponentContainer; |
| import com.vaadin.ui.Field; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * This presenter is responsible to render a text field on the given layout. |
| */ |
| public class BeanReferenceFieldPresentation extends AbstractFieldWidgetPresenter<Component> { |
| |
| /** The Constant LOGGER. */ |
| private static final Logger LOGGER = LoggerFactory.getLogger(BeanReferenceFieldPresentation.class); |
| |
| /** The model access. */ |
| private final ModelAccess modelAccess; |
| |
| /** The field. */ |
| private CustomField<?> field; |
| |
| /** |
| * Constructor. |
| * |
| * @param editpart |
| * The editpart of that presenter |
| */ |
| public BeanReferenceFieldPresentation(IElementEditpart editpart) { |
| super((IBeanReferenceFieldEditpart) editpart); |
| this.modelAccess = new ModelAccess((YBeanReferenceField) editpart.getModel()); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @SuppressWarnings({ "unchecked", "rawtypes" }) |
| @Override |
| public Component doCreateWidget(Object parent) { |
| if (field == null) { |
| IBeanSearchServiceFactory searchServiceFactory = getViewContext().getService(IBeanSearchServiceFactory.class.getName()); |
| |
| IBeanSearchService<?> service = getSearchService(searchServiceFactory); |
| ISharedStateContext sharedState = getViewContext().getService(ISharedStateContext.class.getName()); |
| field = new CustomField(getEditpart().getId(), "", modelAccess.yField.getType(), service, createFilter(), sharedState); |
| |
| field.addStyleName(CSS_CLASS_CONTROL); |
| field.setNullRepresentation(""); |
| field.setImmediate(true); |
| setupComponent(field, getCastedModel()); |
| String captionPropertyPath = modelAccess.yField.getCaptionPropertyPath(); |
| if (captionPropertyPath == null || captionPropertyPath.isEmpty()) { |
| captionPropertyPath = modelAccess.yField.getDescriptionProperty(); |
| } |
| field.setItemCaptionPropertyId(captionPropertyPath); |
| field.setItemIconPropertyId(modelAccess.yField.getImagePropertyPath()); |
| |
| associateWidget(field, modelAccess.yField); |
| if (modelAccess.isCssIdValid()) { |
| field.setId(modelAccess.getCssID()); |
| } else { |
| field.setId(getEditpart().getId()); |
| } |
| |
| field.setNullSelectionAllowed(!modelAccess.yField.isRequired()); |
| |
| // creates the binding for the field |
| createBindings(modelAccess.yField, field); |
| |
| if (modelAccess.isCssClassValid()) { |
| field.addStyleName(modelAccess.getCssClass()); |
| } |
| |
| applyCaptions(); |
| |
| initializeField(field); |
| } |
| return field; |
| } |
| |
| /** |
| * Creates a vaadin filter for the field annotations. |
| * |
| * @return the filter |
| */ |
| protected Filter createFilter() { |
| Class<?> sourceType = modelAccess.yField.getReferenceSourceType(); |
| String sourceProperty = modelAccess.yField.getReferenceSourceTypeProperty(); |
| if (sourceType == null || sourceProperty == null || sourceProperty.equals("")) { |
| return null; |
| } |
| |
| try { |
| java.lang.reflect.Field sourceField = sourceType.getDeclaredField(sourceProperty); |
| |
| TargetEnumConstraints annotation = sourceField.getAnnotation(TargetEnumConstraints.class); |
| if (annotation != null) { |
| return AnnotationToVaadinFilterConverter.createFilter(annotation); |
| } |
| |
| } catch (Exception e) { |
| LOGGER.warn("{}", e); |
| return null; |
| } |
| |
| return null; |
| } |
| |
| /** |
| * Returns the bean search service. |
| * |
| * @param searchServiceFactory |
| * the search service factory |
| * @return the search service |
| */ |
| @SuppressWarnings({ "rawtypes", "unchecked" }) |
| protected IBeanSearchService<?> getSearchService(IBeanSearchServiceFactory searchServiceFactory) { |
| IBeanSearchService<?> service = null; |
| Class<? extends InMemoryBeanProvider> inMemoryProviderClass = (Class<? extends InMemoryBeanProvider>) modelAccess.yField |
| .getInMemoryBeanProvider(); |
| if (inMemoryProviderClass != null) { |
| try { |
| InMemoryBeanProvider provider = inMemoryProviderClass.newInstance(); |
| StatefulInMemoryBeanSearchService tempService = new StatefulInMemoryBeanSearchService(modelAccess.yField.getType()); |
| tempService.addBeans(provider.getBeans()); |
| service = tempService; |
| } catch (InstantiationException e) { |
| LOGGER.error("{}", e); |
| } catch (IllegalAccessException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| if (service == null) { |
| service = searchServiceFactory.createService(modelAccess.yField.getType()); |
| } |
| return service; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractVaadinWidgetPresenter#doUpdateLocale(java.util.Locale) |
| */ |
| @Override |
| protected void doUpdateLocale(Locale locale) { |
| // no need to set the locale to the ui elements. Is handled by vaadin |
| // internally. |
| |
| // update the captions |
| applyCaptions(); |
| } |
| |
| /** |
| * Applies the labels to the widgets. |
| */ |
| protected void applyCaptions() { |
| Util.applyCaptions(getI18nService(), modelAccess.getLabel(), modelAccess.getLabelI18nKey(), getLocale(), field); |
| Util.applyCaptionIcons(getI18nService(), getResourceProvider(), modelAccess.getLabelI18nKey(), getLocale(), |
| new Util.ResourceCallback() { |
| @Override |
| public void setIcon(Resource resource) { |
| field.setSearchButtonIcon(resource); |
| } |
| }); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractFieldWidgetPresenter#doGetField() |
| */ |
| @Override |
| protected Field<?> doGetField() { |
| return field; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractVaadinWidgetPresenter |
| * #internalGetObservableEndpoint(org.eclipse.osbp |
| * .ecview.core.common.model.core.YEmbeddableBindingEndpoint) |
| */ |
| @Override |
| protected IObservable internalGetObservableEndpoint(YEmbeddableBindingEndpoint bindableValue) { |
| if (bindableValue == null) { |
| throw new IllegalArgumentException("BindableValue must not be null!"); |
| } |
| |
| if (bindableValue instanceof YEmbeddableValueEndpoint) { |
| return internalGetValueEndpoint(); |
| } |
| throw new IllegalArgumentException("Not a valid input: " + bindableValue); |
| } |
| |
| /** |
| * Returns the observable to observe value. |
| * |
| * @return the i observable value |
| */ |
| protected IObservableValue internalGetValueEndpoint() { |
| // return the observable value for text |
| return EMFObservables.observeValue(castEObject(getModel()), ExtensionModelPackage.Literals.YBEAN_REFERENCE_FIELD__VALUE); |
| } |
| |
| /** |
| * Creates the bindings for the given values. |
| * |
| * @param yField |
| * the y field |
| * @param field |
| * the field |
| */ |
| protected void createBindings(YBeanReferenceField yField, BeanReferenceField<?> field) { |
| // create the model binding from ridget to ECView-model |
| registerBinding(createBindings_Value(castEObject(getModel()), ExtensionModelPackage.Literals.YBEAN_REFERENCE_FIELD__VALUE, field)); |
| |
| super.createBindings(yField, field, null); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation# |
| * getWidget() |
| */ |
| @Override |
| public Component getWidget() { |
| return field; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation# |
| * isRendered() |
| */ |
| @Override |
| public boolean isRendered() { |
| return field != null; |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public void doUnrender() { |
| if (field != null) { |
| |
| // unbind all active bindings |
| unbind(); |
| |
| Component parent = ((Component) field.getParent()); |
| if (parent != null && parent instanceof ComponentContainer) { |
| ((ComponentContainer) parent).removeComponent(field); |
| } |
| |
| // remove assocations |
| unassociateWidget(field); |
| |
| if (field.getInternalComboBox() != null) { |
| unassociateWidget(field.getInternalComboBox()); |
| } |
| |
| field = null; |
| } |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| protected void internalDispose() { |
| try { |
| unrender(); |
| } finally { |
| super.internalDispose(); |
| } |
| } |
| |
| /** |
| * A helper class. |
| */ |
| private static class ModelAccess { |
| |
| /** The y field. */ |
| private final YBeanReferenceField yField; |
| |
| /** |
| * Instantiates a new model access. |
| * |
| * @param yField |
| * the y field |
| */ |
| public ModelAccess(YBeanReferenceField yField) { |
| super(); |
| this.yField = yField; |
| } |
| |
| /** |
| * Gets the css class. |
| * |
| * @return the css class |
| * @see org.eclipse.osbp.ecview.core.ui.core.model.core.YCssAble#getCssClass() |
| */ |
| public String getCssClass() { |
| return yField.getCssClass(); |
| } |
| |
| /** |
| * Returns true, if the css class is not null and not empty. |
| * |
| * @return true, if is css class valid |
| */ |
| public boolean isCssClassValid() { |
| return getCssClass() != null && !getCssClass().equals(""); |
| } |
| |
| /** |
| * Gets the css id. |
| * |
| * @return the css id |
| * @see org.eclipse.osbp.ecview.core.ui.core.model.core.YCssAble#getCssID() |
| */ |
| public String getCssID() { |
| return yField.getCssID(); |
| } |
| |
| /** |
| * Returns true, if the css id is not null and not empty. |
| * |
| * @return true, if is css id valid |
| */ |
| public boolean isCssIdValid() { |
| return getCssID() != null && !getCssID().equals(""); |
| } |
| |
| /** |
| * Returns the label. |
| * |
| * @return the label |
| */ |
| public String getLabel() { |
| return yField.getDatadescription() != null ? yField.getDatadescription().getLabel() : null; |
| } |
| |
| /** |
| * Returns the label. |
| * |
| * @return the label i18n key |
| */ |
| public String getLabelI18nKey() { |
| return yField.getDatadescription() != null ? yField.getDatadescription().getLabelI18nKey() : null; |
| } |
| } |
| |
| /** |
| * The Class CustomField. |
| * |
| * @param <M> |
| * the generic type |
| */ |
| @SuppressWarnings("serial") |
| private class CustomField<M> extends BeanReferenceField<M> { |
| |
| /** |
| * Instantiates a new custom field. |
| * |
| * @param id |
| * the id |
| * @param propertyId |
| * the property id |
| * @param type |
| * the type |
| * @param searchService |
| * the search service |
| * @param filter |
| * the filter |
| * @param sharedState |
| * the shared state |
| */ |
| public CustomField(String id, Object propertyId, Class<M> type, IBeanSearchService<M> searchService, Filter filter, |
| ISharedStateContext sharedState) { |
| super(id, propertyId, type, searchService, filter, sharedState); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.runtime.web.vaadin.components.fields.BeanReferenceField |
| * #initContent() |
| */ |
| @Override |
| protected Component initContent() { |
| Component result = super.initContent(); |
| |
| associateWidget(field.getInternalComboBox(), getCastedModel()); |
| |
| return result; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.ui.AbstractField#getErrorMessage() |
| */ |
| @Override |
| public ErrorMessage getErrorMessage() { |
| if (isDisposed()) { |
| // after disposal, Vaadin will call this method once. |
| return null; |
| } |
| |
| ErrorMessage message = super.getErrorMessage(); |
| reportValidationError(message); |
| return message; |
| } |
| } |
| } |