blob: f56497972db91db90db184af167a04145c5e4b4c [file] [log] [blame]
/**
* 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 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal;
import java.util.Locale;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.osbp.ecview.core.common.editpart.IElementEditpart;
import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelPackage;
import org.eclipse.osbp.ecview.core.extension.model.extension.YImage;
import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.IImageEditpart;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractEmbeddedWidgetPresenter;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal.util.Util;
import org.eclipse.osbp.runtime.web.vaadin.databinding.VaadinObservables;
import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Embedded;
// TODO: Auto-generated Javadoc
/**
* This presenter is responsible to render a text area on the given layout.
*/
public class ImagePresentation extends AbstractEmbeddedWidgetPresenter<Component> {
/** The model access. */
private final ModelAccess modelAccess;
/** The image. */
private Embedded image;
/**
* Constructor.
*
* @param editpart
* The editpart of that presenter
*/
public ImagePresentation(IElementEditpart editpart) {
super((IImageEditpart) editpart);
this.modelAccess = new ModelAccess((YImage) editpart.getModel());
}
/**
* {@inheritDoc}
*/
@Override
public Component doCreateWidget(Object parent) {
if (image == null) {
image = new Embedded();
image.addStyleName(CSS_CLASS_CONTROL);
image.setImmediate(true);
setupComponent(image, getCastedModel());
associateWidget(image, modelAccess.yField);
if (modelAccess.isCssIdValid()) {
image.setId(modelAccess.getCssID());
} else {
image.setId(getEditpart().getId());
}
// creates the binding for the field
createBindings(modelAccess.yField, image, null);
if (modelAccess.isCssClassValid()) {
image.addStyleName(modelAccess.getCssClass());
}
// set the captions
applyCaptions();
}
return image;
}
// TODO: check the return type - the documentation and the implementation do
// not match
/**
* Creates the bindings from the ECView EMF model to the given UI element.
*
* @param yField
* the y field
* @param widget
* the widget
* @param container
* the container
*
* return Binding - the created binding
*/
protected void createBindings(YImage yField, AbstractComponent widget, AbstractComponent container) {
// binding for YImage#value
IObservableValue modelObservable = EMFObservables.observeValue(yField,
ExtensionModelPackage.Literals.YIMAGE__VALUE);
IObservableValue uiObservable = VaadinObservables.observeSource(image);
IConverter stringToResourceConverter = new Converter(String.class, Resource.class) {
@Override
public Object convert(Object fromObject) {
return (fromObject != null && !fromObject.equals("")) ? new ThemeResource((String) fromObject) : null;
}
};
registerBinding(createBindings(uiObservable, modelObservable,
new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER),
new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE).setConverter(stringToResourceConverter)));
// binding for YImage#resource
IObservableValue resModelObservable = EMFObservables.observeValue(yField,
ExtensionModelPackage.Literals.YIMAGE__RESOURCE);
IObservableValue resUiObservable = VaadinObservables.observeSource(image);
registerBinding(createBindings(resUiObservable, resModelObservable,
new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER),
new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE)));
super.createBindings(yField, widget, container);
}
/*
* (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(), image);
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#
* getWidget()
*/
@Override
public Component getWidget() {
return image;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#
* isRendered()
*/
@Override
public boolean isRendered() {
return image != null;
}
/**
* {@inheritDoc}
*/
@Override
public void doUnrender() {
if (image != null) {
// unbind all active bindings
unbind();
// remove assocations
unassociateWidget(image);
Component parent = ((Component) image.getParent());
if (parent != null && parent instanceof ComponentContainer) {
((ComponentContainer) parent).removeComponent(image);
}
image = null;
}
}
/**
* {@inheritDoc}
*/
@Override
protected void internalDispose() {
try {
unrender();
} finally {
super.internalDispose();
}
}
/**
* A helper class.
*/
private static class ModelAccess {
/** The y field. */
private final YImage yField;
/**
* Instantiates a new model access.
*
* @param yField
* the y field
*/
public ModelAccess(YImage 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;
}
}
}