Java tutorial
/** * Copyright (c) 2011 - 2015, Lunifera GmbH (Gross Enzersdorf), Loetz KG (Heidelberg) * 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.lunifera.mobile.vaadin.ecview.presentation.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.lunifera.ecview.core.common.editpart.IElementEditpart; import org.lunifera.ecview.core.common.model.core.YEmbeddableBindingEndpoint; import org.lunifera.ecview.core.common.model.core.YEmbeddableValueEndpoint; import org.lunifera.ecview.core.extension.model.extension.ExtensionModelPackage; import org.lunifera.mobile.vaadin.ecview.editparts.ISwitchEditpart; import org.lunifera.mobile.vaadin.ecview.model.VMSwitch; import org.lunifera.mobile.vaadin.ecview.model.VaadinMobilePackage; import org.lunifera.runtime.web.ecview.presentation.vaadin.internal.AbstractFieldWidgetPresenter; import org.lunifera.runtime.web.ecview.presentation.vaadin.internal.util.Util; import com.vaadin.addon.touchkit.ui.Switch; import com.vaadin.data.util.ObjectProperty; import com.vaadin.ui.Component; import com.vaadin.ui.ComponentContainer; import com.vaadin.ui.Field; /** * This presenter is responsible to render a xswitch on the given layout. */ @SuppressWarnings("restriction") public class SwitchPresentation extends AbstractFieldWidgetPresenter<Component> { private final ModelAccess modelAccess; private Switch xswitch; private ObjectProperty<Boolean> property; /** * Constructor. * * @param editpart * The editpart of that presenter */ public SwitchPresentation(IElementEditpart editpart) { super((ISwitchEditpart) editpart); this.modelAccess = new ModelAccess((VMSwitch) editpart.getModel()); } /** * {@inheritDoc} */ @Override public Component doCreateWidget(Object parent) { if (xswitch == null) { xswitch = new Switch(); xswitch.addStyleName(CSS_CLASS_CONTROL); xswitch.setImmediate(true); if (modelAccess.isCssIdValid()) { xswitch.setId(modelAccess.getCssID()); } else { xswitch.setId(getEditpart().getId()); } associateWidget(xswitch, modelAccess.yField); property = new ObjectProperty<Boolean>(false, Boolean.class); xswitch.setPropertyDataSource(property); // creates the binding for the field createBindings(modelAccess.yField, xswitch); if (modelAccess.isCssClassValid()) { xswitch.addStyleName(modelAccess.getCssClass()); } applyCaptions(); initializeField(xswitch); } return xswitch; } @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(), xswitch); } @Override protected Field<?> doGetField() { return xswitch; } @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 */ protected IObservableValue internalGetValueEndpoint() { // return the observable value for text return EMFObservables.observeValue(castEObject(getModel()), ExtensionModelPackage.Literals.YCHECK_BOX__VALUE); } /** * Creates the bindings for the given values. * * @param yField * @param field */ protected void createBindings(VMSwitch yField, Switch field) { // create the model binding from ridget to ECView-model registerBinding(createBindings_Value(castEObject(getModel()), VaadinMobilePackage.Literals.VM_SWITCH__VALUE, field)); super.createBindings(yField, field, null); } @Override public Component getWidget() { return xswitch; } @Override public boolean isRendered() { return xswitch != null; } /** * {@inheritDoc} */ @Override public void doUnrender() { if (xswitch != null) { // unbind all active bindings unbind(); ComponentContainer parent = ((ComponentContainer) xswitch.getParent()); if (parent != null) { parent.removeComponent(xswitch); } // remove assocations unassociateWidget(xswitch); xswitch = null; } } /** * {@inheritDoc} */ @Override protected void internalDispose() { try { unrender(); } finally { super.internalDispose(); } } /** * A helper class. */ private static class ModelAccess { private final VMSwitch yField; public ModelAccess(VMSwitch yField) { super(); this.yField = yField; } /** * @return * @see org.lunifera.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 */ public boolean isCssClassValid() { return getCssClass() != null && !getCssClass().equals(""); } /** * @return * @see org.lunifera.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 */ public boolean isCssIdValid() { return getCssID() != null && !getCssID().equals(""); } /** * Returns the label. * * @return */ public String getLabel() { return yField.getDatadescription() != null ? yField.getDatadescription().getLabel() : null; } /** * Returns the label. * * @return */ public String getLabelI18nKey() { return yField.getDatadescription() != null ? yField.getDatadescription().getLabelI18nKey() : null; } } }