blob: 4fec7d288c0d10f85af2562fa96fd7c465da2e61 [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.HashSet;
import java.util.Locale;
import java.util.Set;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.EMFProperties;
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.YEmbeddableCollectionEndpoint;
import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddableMultiSelectionEndpoint;
import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddableSelectionEndpoint;
import org.eclipse.osbp.ecview.core.databinding.emf.model.ECViewModelBindable;
import org.eclipse.osbp.ecview.core.extension.model.extension.ExtensionModelPackage;
import org.eclipse.osbp.ecview.core.extension.model.extension.YSelectionType;
import org.eclipse.osbp.ecview.core.extension.model.extension.YTree;
import org.eclipse.osbp.ecview.core.ui.core.editparts.extension.ITreeEditpart;
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.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Field;
import com.vaadin.ui.Tree;
// TODO: Auto-generated Javadoc
/**
* This presenter is responsible to render a tree on the given layout.
*/
@SuppressWarnings("restriction")
public class TreePresentation extends AbstractFieldWidgetPresenter<Component> {
/** The model access. */
private final ModelAccess modelAccess;
/** The tree. */
private Tree tree;
/** The property. */
private ObjectProperty<?> property;
/**
* Constructor.
*
* @param editpart
* The editpart of that presenter
*/
public TreePresentation(IElementEditpart editpart) {
super((ITreeEditpart) editpart);
this.modelAccess = new ModelAccess((YTree) editpart.getModel());
}
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Component doCreateWidget(Object parent) {
if (tree == null) {
tree = new Tree();
tree.addStyleName(CSS_CLASS_CONTROL);
tree.setMultiSelect(modelAccess.yField.getSelectionType() == YSelectionType.MULTI);
tree.setImmediate(true);
setupComponent(tree, getCastedModel());
associateWidget(tree, modelAccess.yField);
if (modelAccess.isCssIdValid()) {
tree.setId(modelAccess.getCssID());
} else {
tree.setId(getEditpart().getId());
}
if (tree.isMultiSelect()) {
property = new ObjectProperty(new HashSet(), Set.class);
} else {
property = new ObjectProperty(null,
modelAccess.yField.getType());
}
tree.setPropertyDataSource(property);
// creates the binding for the field
createBindings(modelAccess.yField, tree);
if (modelAccess.isCssClassValid()) {
tree.addStyleName(modelAccess.getCssClass());
}
applyCaptions();
initializeField(tree);
}
return tree;
}
/* (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(), tree);
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common.AbstractFieldWidgetPresenter#doGetField()
*/
@Override
protected Field<?> doGetField() {
return tree;
}
/* (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 YEmbeddableCollectionEndpoint) {
return internalGetCollectionEndpoint();
} else if (bindableValue instanceof YEmbeddableSelectionEndpoint) {
return internalGetSelectionEndpoint((YEmbeddableSelectionEndpoint) bindableValue);
} else if (bindableValue instanceof YEmbeddableMultiSelectionEndpoint) {
return internalGetMultiSelectionEndpoint();
}
throw new IllegalArgumentException("Not a valid input: "
+ bindableValue);
}
/**
* Returns the observable to observe the collection.
*
* @return the i observable list
*/
protected IObservableList internalGetCollectionEndpoint() {
// return the observable value for text
return EMFProperties.list(
ExtensionModelPackage.Literals.YTREE__COLLECTION).observe(
getModel());
}
/**
* Returns the observable to observe the selection.
*
* @return the i observable list
*/
protected IObservableList internalGetMultiSelectionEndpoint() {
// return the observable value for text
return EMFProperties.list(
ExtensionModelPackage.Literals.YTREE__MULTI_SELECTION).observe(
getModel());
}
/**
* Returns the observable to observe the selection.
*
* @param yEndpoint
* the y endpoint
* @return the i observable value
*/
protected IObservableValue internalGetSelectionEndpoint(
YEmbeddableSelectionEndpoint yEndpoint) {
String attributePath = ECViewModelBindable.getAttributePath(
ExtensionModelPackage.Literals.YTREE__SELECTION,
yEndpoint.getAttributePath());
// return the observable value for text
return ECViewModelBindable.observeValue(castEObject(getModel()),
attributePath, modelAccess.yField.getType(),
modelAccess.yField.getEmfNsURI());
}
/**
* Creates the bindings for the given values.
*
* @param yField
* the y field
* @param field
* the field
*/
protected void createBindings(YTree yField, Tree field) {
// create the model binding from ridget to ECView-model
registerBinding(createBindings_ContainerContents(
castEObject(getModel()),
ExtensionModelPackage.Literals.YTREE__COLLECTION, field,
yField.getType()));
// create the model binding from ridget to ECView-model
if (modelAccess.yField.getSelectionType() == YSelectionType.MULTI) {
// create the model binding from ridget to ECView-model
registerBinding(createBindingsMultiSelection(
castEObject(getModel()),
ExtensionModelPackage.Literals.YTREE__MULTI_SELECTION,
field, yField.getType()));
} else {
// create the model binding from ridget to ECView-model
registerBinding(createBindingsSelection(castEObject(getModel()),
ExtensionModelPackage.Literals.YTREE__SELECTION, field,
yField.getType()));
}
super.createBindings(yField, field, null);
}
/* (non-Javadoc)
* @see org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#getWidget()
*/
@Override
public Component getWidget() {
return tree;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.ecview.core.common.presentation.IWidgetPresentation#isRendered()
*/
@Override
public boolean isRendered() {
return tree != null;
}
/**
* {@inheritDoc}
*/
@Override
public void doUnrender() {
if (tree != null) {
// unbind all active bindings
unbind();
Component parent = ((Component) tree.getParent());
if (parent != null && parent instanceof ComponentContainer) {
((ComponentContainer) parent).removeComponent(tree);
}
// remove assocations
unassociateWidget(tree);
tree = null;
}
}
/**
* {@inheritDoc}
*/
@Override
protected void internalDispose() {
try {
unrender();
} finally {
super.internalDispose();
}
}
/**
* A helper class.
*/
private static class ModelAccess {
/** The y field. */
private final YTree yField;
/**
* Instantiates a new model access.
*
* @param yField
* the y field
*/
public ModelAccess(YTree 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;
}
}
}