blob: 2a8260fcea6707aef60a9e757bf932048349292b [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 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.binding.IECViewBindingManager;
import org.eclipse.osbp.ecview.core.common.editpart.IElementEditpart;
import org.eclipse.osbp.ecview.core.common.filter.IFilterProvidingPresentation;
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.YBooleanSearchField;
import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.IBooleanSearchFieldEditpart;
import org.eclipse.osbp.runtime.common.i18n.II18nService;
import org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.IConstants;
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.components.fields.search.BooleanSearchField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.data.util.ObjectProperty;
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 BooleanSearchFieldPresentation extends
AbstractFieldWidgetPresenter<Component> implements IFilterProvidingPresentation {
/** The Constant LOGGER. */
@SuppressWarnings("unused")
private static final Logger LOGGER = LoggerFactory
.getLogger(BooleanSearchFieldPresentation.class);
/** The model access. */
private final ModelAccess modelAccess;
/** The field. */
private BooleanSearchField field;
/** The property. */
private ObjectProperty<String> property;
/**
* Constructor.
*
* @param editpart
* The editpart of that presenter
*/
public BooleanSearchFieldPresentation(IElementEditpart editpart) {
super((IBooleanSearchFieldEditpart) editpart);
this.modelAccess = new ModelAccess(
(YBooleanSearchField) editpart.getModel());
}
/**
* {@inheritDoc}
*/
@Override
public Component doCreateWidget(Object parent) {
if (field == null) {
IECViewBindingManager bm = getViewContext().getService(
IECViewBindingManager.class.getName());
field = new BooleanSearchField(getEditpart().getId(),
modelAccess.yField.getPropertyPath(), bm.getDatabindingContext());
field.addStyleName(CSS_CLASS_CONTROL);
field.addStyleName(IConstants.CSS_CLASS_SEARCHFIELD);
field.setImmediate(true);
setupComponent(field, getCastedModel());
if (modelAccess.isCssIdValid()) {
field.setId(modelAccess.getCssID());
} else {
field.setId(getEditpart().getId());
}
associateWidget(field, modelAccess.yField);
property = new ObjectProperty<String>(null, String.class);
field.setPropertyDataSource(property);
// creates the binding for the field
createBindings(modelAccess.yField, field);
if (modelAccess.isCssClassValid()) {
field.addStyleName(modelAccess.getCssClass());
}
applyCaptions();
initializeField(field);
}
return field;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.ecview.core.common.filter.IFilterProvidingPresentation#getFilter()
*/
@Override
public Object getFilter() {
return field != null ? field.getFilter() : null;
}
/* (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() {
II18nService service = getI18nService();
Util.applyCaptions(service, modelAccess.getLabel(),
modelAccess.getLabelI18nKey(), getLocale(), field);
field.setDescription(service.getValue(IConstants.I18N_TOOLTIP_BOOLEANSEARCHFIELD, getLocale()));
}
/* (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.YTEXT_SEARCH_FIELD__VALUE);
}
/**
* Creates the bindings for the given values.
*
* @param yField
* the y field
* @param field
* the field
*/
protected void createBindings(YBooleanSearchField yField,
BooleanSearchField field) {
// create the model binding from ridget to ECView-model
registerBinding(createBindings_Value(castEObject(getModel()),
ExtensionModelPackage.Literals.YTEXT_SEARCH_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);
field.dispose();
field = null;
}
}
/**
* {@inheritDoc}
*/
@Override
protected void internalDispose() {
try {
unrender();
} finally {
super.internalDispose();
}
}
/**
* A helper class.
*/
private static class ModelAccess {
/** The y field. */
private final YBooleanSearchField yField;
/**
* Instantiates a new model access.
*
* @param yField
* the y field
*/
public ModelAccess(YBooleanSearchField 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;
}
}
}