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.YComboBox; import org.eclipse.emf.ecp.ecview.ui.core.editparts.extension.IComboBoxEditpart; import com.vaadin.ui.ComboBox; import com.vaadin.ui.Component; import com.vaadin.ui.ComponentContainer; import com.vaadin.ui.CssLayout; /** * This presenter is responsible to render a combo box on the given layout. */ public class ComboBoxPresentation extends AbstractVaadinWidgetPresenter<Component> { private final ModelAccess modelAccess; private CssLayout componentBase; private ComboBox combo; /** * Constructor. * * @param editpart * The editpart of that presenter */ public ComboBoxPresentation(IElementEditpart editpart) { super((IComboBoxEditpart) editpart); this.modelAccess = new ModelAccess((YComboBox) 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()); } combo = new ComboBox(); combo.addStyleName(CSS_CLASS__CONTROL); combo.setSizeFull(); componentBase.addComponent(combo); if (modelAccess.isCssClassValid()) { combo.addStyleName(modelAccess.getCssClass()); } if (modelAccess.isLabelValid()) { combo.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; combo = null; } } /** * {@inheritDoc} */ @Override protected void internalDispose() { // unrender the ui component unrender(); } /** * A helper class. */ private static class ModelAccess { private final YComboBox yCombo; public ModelAccess(YComboBox yCombo) { super(); this.yCombo = yCombo; } /** * @return * @see org.eclipse.emf.ecp.ecview.ui.core.model.core.YCssAble#getCssClass() */ public String getCssClass() { return yCombo.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 yCombo.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 yCombo.getDatadescription() != null && yCombo.getDatadescription().getLabel() != null; } /** * Returns the label. * * @return */ public String getLabel() { return yCombo.getDatadescription().getLabel(); } } }