blob: 5c03f1101c382baeb38712e2f879def1fbf90b15 [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.observable.IObservable;
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.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.YTextArea;
import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.ITextAreaEditpart;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractFieldWidgetPresenter;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.internal.util.Util;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.server.ErrorMessage;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextArea;
/**
* This presenter is responsible to render a text area on the given layout.
*/
public class TextAreaPresentation extends
AbstractFieldWidgetPresenter<Component> {
/** The model access. */
private final ModelAccess modelAccess;
/** The text area. */
private CustomField textArea;
/** The property. */
private ObjectProperty<String> property;
/**
* Constructor.
*
* @param editpart
* The editpart of that presenter
*/
public TextAreaPresentation(IElementEditpart editpart) {
super((ITextAreaEditpart) editpart);
this.modelAccess = new ModelAccess((YTextArea) editpart.getModel());
}
/**
* {@inheritDoc}
*/
@Override
public Component doCreateWidget(Object parent) {
if (textArea == null) {
textArea = new CustomField();
int rows = modelAccess.yField.getRows();
if (rows > 0) {
textArea.setRows(modelAccess.yField.getRows());
}
textArea.setSizeFull();
textArea.addStyleName(CSS_CLASS_CONTROL);
textArea.setImmediate(true);
textArea.setNullRepresentation("");
setupComponent(textArea, getCastedModel());
associateWidget(textArea, modelAccess.yField);
if (modelAccess.isCssIdValid()) {
textArea.setId(modelAccess.getCssID());
} else {
textArea.setId(getEditpart().getId());
}
property = new ObjectProperty<String>(null, String.class);
textArea.setPropertyDataSource(property);
// creates the binding for the field
createBindings(modelAccess.yField, textArea);
if (modelAccess.isCssClassValid()) {
textArea.addStyleName(modelAccess.getCssClass());
}
applyCaptions();
initializeField(textArea);
textArea.setRows(modelAccess.yField.getRows());
textArea.setWordwrap(modelAccess.yField.isWordWrap());
}
return textArea;
}
/* (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(), textArea);
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractFieldWidgetPresenter#doGetField()
*/
@Override
protected Field<?> doGetField() {
return textArea;
}
/* (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.YTEXT_AREA__VALUE);
}
/**
* Creates the bindings for the given values.
*
* @param yField
* the y field
* @param field
* the field
*/
protected void createBindings(final YTextArea yField, TextArea field) {
registerBinding(createBindings_Value(castEObject(getModel()),
ExtensionModelPackage.Literals.YTEXT_AREA__VALUE, textArea));
field.addTextChangeListener(e -> {
yField.setLastTextChange(e.getText());
});
super.createBindings(yField, field, null);
}
/* (non-Javadoc)
* @see org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#getWidget()
*/
@Override
public Component getWidget() {
return textArea;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#isRendered()
*/
@Override
public boolean isRendered() {
return textArea != null;
}
/**
* {@inheritDoc}
*/
@Override
public void doUnrender() {
if (textArea != null) {
// unbind all active bindings
unbind();
Component parent = ((Component) textArea.getParent());
if (parent != null && parent instanceof ComponentContainer) {
((ComponentContainer) parent).removeComponent(textArea);
}
// remove assocations
unassociateWidget(textArea);
textArea = null;
}
}
/**
* {@inheritDoc}
*/
@Override
protected void internalDispose() {
try {
unrender();
} finally {
super.internalDispose();
}
}
/**
* A helper class.
*/
private static class ModelAccess {
/** The y field. */
private final YTextArea yField;
/**
* Instantiates a new model access.
*
* @param yField
* the y field
*/
public ModelAccess(YTextArea 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.
*/
@SuppressWarnings("serial")
private class CustomField extends TextArea {
/* (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;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractField#setConvertedValue(java.lang.Object)
*/
@Override
public void setConvertedValue(Object value) {
boolean oldReadonly = isReadOnly();
try {
setReadOnly(false);
super.setConvertedValue(value);
} finally {
setReadOnly(oldReadonly);
}
}
}
}