Java tutorial
/** * Copyright (c) 2012 Florian Pirchner (Vienna, Austria) and others. * 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 API and implementation */ package org.lunifera.web.ecp.uimodel.presentation.vaadin.internal; import org.eclipse.emf.ecp.ecview.common.editpart.IElementEditpart; import org.eclipse.emf.ecp.ecview.extension.model.extension.YCheckBox; import org.eclipse.emf.ecp.ecview.ui.core.editparts.extension.ICheckboxEditpart; import com.vaadin.ui.CheckBox; import com.vaadin.ui.Component; import com.vaadin.ui.ComponentContainer; import com.vaadin.ui.CssLayout; /** * This presenter is responsible to render a checkBox on the given layout. */ public class CheckBoxPresentation extends AbstractVaadinWidgetPresenter<Component> { private final ModelAccess modelAccess; private CssLayout componentBase; private CheckBox checkBox; /** * Constructor. * * @param editpart The editpart of that presenter */ public CheckBoxPresentation(IElementEditpart editpart) { super((ICheckboxEditpart) editpart); this.modelAccess = new ModelAccess((YCheckBox) editpart.getModel()); } /** * {@inheritDoc} */ @Override public Component createWidget(Object parent) { if (componentBase == null) { componentBase = new CssLayout(); componentBase.addStyleName(CSS_CLASS__CONTROL_BASE); if (modelAccess.isCssIdValid()) { componentBase.setId(modelAccess.getCssID()); } else { componentBase.setId(getEditpart().getId()); } checkBox = new CheckBox(); checkBox.addStyleName(CSS_CLASS__CONTROL); checkBox.setSizeFull(); componentBase.addComponent(checkBox); if (modelAccess.isCssClassValid()) { checkBox.addStyleName(modelAccess.getCssClass()); } if (modelAccess.isLabelValid()) { checkBox.setCaption(modelAccess.getLabel()); } } return componentBase; } @Override public Component getWidget() { return componentBase; } @Override public boolean isRendered() { return componentBase != null; } /** * {@inheritDoc} */ @Override public void unrender() { if (componentBase != null) { ComponentContainer parent = ((ComponentContainer) componentBase.getParent()); if (parent != null) { parent.removeComponent(componentBase); } componentBase = null; checkBox = null; } } /** * {@inheritDoc} */ @Override protected void internalDispose() { // unrender the ui component unrender(); } /** * A helper class. */ private static class ModelAccess { private final YCheckBox yCheckBox; public ModelAccess(YCheckBox yCheckBox) { super(); this.yCheckBox = yCheckBox; } /** * @return * @see org.eclipse.emf.ecp.ecview.ui.core.model.core.YCssAble#getCssClass() */ public String getCssClass() { return yCheckBox.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.eclipse.emf.ecp.ecview.ui.core.model.core.YCssAble#getCssID() */ public String getCssID() { return yCheckBox.getCssID(); } /** * Returns true, if the css id is not null and not empty. * * @return */ public boolean isCssIdValid() { return getCssID() != null && !getCssID().equals(""); } /** * Returns true, if the label is valid. * * @return */ public boolean isLabelValid() { return yCheckBox.getDatadescription() != null && yCheckBox.getDatadescription().getLabel() != null; } /** * Returns the label. * * @return */ public String getLabel() { return yCheckBox.getDatadescription().getLabel(); } } }