| /** |
| * 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.Binding; |
| import org.eclipse.core.databinding.UpdateValueStrategy; |
| import org.eclipse.core.databinding.conversion.IConverter; |
| import org.eclipse.core.databinding.observable.IObservable; |
| import org.eclipse.core.databinding.observable.value.IObservableValue; |
| import org.eclipse.emf.databinding.EMFObservables; |
| import org.eclipse.emf.ecore.EObject; |
| 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.common.model.core.YOrientation; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelPackage; |
| import org.eclipse.osbp.ecview.core.extension.model.extension.YSlider; |
| import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.ISliderEditpart; |
| import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.IBindingManager; |
| 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.databinding.VaadinProperties; |
| |
| import com.vaadin.data.util.ObjectProperty; |
| import com.vaadin.server.ErrorMessage; |
| import com.vaadin.shared.ui.slider.SliderOrientation; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.ComponentContainer; |
| import com.vaadin.ui.Field; |
| import com.vaadin.ui.Slider; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * This presenter is responsible to render a slider field on the given layout. |
| */ |
| public class SliderPresentation extends AbstractFieldWidgetPresenter<Component> { |
| |
| /** The model access. */ |
| private final ModelAccess modelAccess; |
| |
| /** The slider. */ |
| private CustomSlider slider; |
| |
| /** The property. */ |
| private ObjectProperty<Double> property; |
| |
| /** |
| * Constructor. |
| * |
| * @param editpart |
| * The editpart of that presenter |
| */ |
| public SliderPresentation(IElementEditpart editpart) { |
| super((ISliderEditpart) editpart); |
| this.modelAccess = new ModelAccess((YSlider) editpart.getModel()); |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public Component doCreateWidget(Object parent) { |
| if (slider == null) { |
| |
| slider = new CustomSlider(); |
| slider.addStyleName(CSS_CLASS_CONTROL); |
| slider.setImmediate(true); |
| setupComponent(slider, getCastedModel()); |
| |
| associateWidget(slider, modelAccess.yField); |
| if (modelAccess.isCssIdValid()) { |
| slider.setId(modelAccess.getCssID()); |
| } else { |
| slider.setId(getEditpart().getId()); |
| } |
| |
| property = new ObjectProperty<Double>(0d, Double.class); |
| slider.setPropertyDataSource(property); |
| |
| // creates the binding for the field |
| createBindings(modelAccess.yField, slider); |
| |
| if (modelAccess.isCssClassValid()) { |
| slider.addStyleName(modelAccess.getCssClass()); |
| } |
| |
| applyCaptions(); |
| |
| initializeField(slider); |
| } |
| return slider; |
| } |
| |
| /* (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(), slider); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractFieldWidgetPresenter#doGetField() |
| */ |
| @Override |
| protected Field<?> doGetField() { |
| return slider; |
| } |
| |
| /* (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 slider |
| return EMFObservables.observeValue(castEObject(getModel()), |
| ExtensionModelPackage.Literals.YSLIDER__VALUE); |
| } |
| |
| /** |
| * Creates the bindings for the given values. |
| * |
| * @param yField |
| * the y field |
| * @param field |
| * the field |
| */ |
| protected void createBindings(YSlider yField, Slider field) { |
| // create the model binding from widget to ECView-model |
| registerBinding(createBindings_Value(castEObject(getModel()), |
| ExtensionModelPackage.Literals.YSLIDER__VALUE, slider)); |
| |
| registerBinding(createBindingsByAccessor(castEObject(getModel()), |
| ExtensionModelPackage.Literals.YSLIDER__MAX_VALUE, slider, |
| "max")); |
| |
| registerBinding(createBindingsByAccessor(castEObject(getModel()), |
| ExtensionModelPackage.Literals.YSLIDER__MIN_VALUE, slider, |
| "min")); |
| |
| registerBinding(createBindingsByAccessor(castEObject(getModel()), |
| ExtensionModelPackage.Literals.YSLIDER__RESOLUTION, slider, |
| "resolution")); |
| |
| registerBinding(createBindingsOrientation(castEObject(getModel()), |
| slider)); |
| |
| super.createBindings(yField, field, null); |
| } |
| |
| /** |
| * Bind the orientation field. |
| * |
| * @param model |
| * the model |
| * @param field |
| * the field |
| * @return the binding |
| */ |
| protected Binding createBindingsOrientation(EObject model, Field<?> field) { |
| IBindingManager bindingManager = getViewContext() |
| .getService( |
| org.eclipse.osbp.ecview.core.common.binding.IECViewBindingManager.class |
| .getName()); |
| if (bindingManager != null) { |
| // bind the value of yText to slider |
| IObservableValue modelObservable = EMFObservables.observeValue( |
| model, ExtensionModelPackage.Literals.YSLIDER__RESOLUTION); |
| IObservableValue uiObservable = VaadinProperties.accessor( |
| Slider.class, "orientation").observe(field); |
| UpdateValueStrategy modelToTarget = new UpdateValueStrategy( |
| UpdateValueStrategy.POLICY_UPDATE) |
| .setConverter(new IConverter() { |
| @Override |
| public Object getToType() { |
| return SliderOrientation.class; |
| } |
| |
| @Override |
| public Object getFromType() { |
| return YOrientation.class; |
| } |
| |
| @Override |
| public Object convert(Object fromObject) { |
| SliderOrientation orientation = SliderOrientation.HORIZONTAL; |
| if (fromObject == YOrientation.VERTICAL) { |
| orientation = SliderOrientation.VERTICAL; |
| } |
| return orientation; |
| } |
| }); |
| |
| return bindingManager.bindValue(uiObservable, modelObservable, |
| new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), |
| modelToTarget); |
| } |
| return null; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#getWidget() |
| */ |
| @Override |
| public Component getWidget() { |
| return slider; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#isRendered() |
| */ |
| @Override |
| public boolean isRendered() { |
| return slider != null; |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| public void doUnrender() { |
| if (slider != null) { |
| |
| // unbind all active bindings |
| unbind(); |
| |
| Component parent = ((Component) slider.getParent()); |
| if (parent != null && parent instanceof ComponentContainer) { |
| ((ComponentContainer) parent).removeComponent(slider); |
| } |
| |
| // remove assocations |
| unassociateWidget(slider); |
| |
| slider = null; |
| } |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| @Override |
| protected void internalDispose() { |
| try { |
| unrender(); |
| } finally { |
| super.internalDispose(); |
| } |
| } |
| |
| /** |
| * A helper class. |
| */ |
| private static class ModelAccess { |
| |
| /** The y field. */ |
| private final YSlider yField; |
| |
| /** |
| * Instantiates a new model access. |
| * |
| * @param yField |
| * the y field |
| */ |
| public ModelAccess(YSlider 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 CustomSlider. |
| */ |
| @SuppressWarnings("serial") |
| private class CustomSlider extends Slider { |
| |
| /* (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; |
| } |
| } |
| } |