org.lunifera.web.ecp.uimodel.presentation.vaadin.internal.TablePresentation.java Source code

Java tutorial

Introduction

Here is the source code for org.lunifera.web.ecp.uimodel.presentation.vaadin.internal.TablePresentation.java

Source

/**
 * 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.YTable;
import org.eclipse.emf.ecp.ecview.ui.core.editparts.extension.ITableEditpart;

import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Table;

/**
 * This presenter is responsible to render a table on the given layout.
 */
public class TablePresentation extends AbstractVaadinWidgetPresenter<Component> {

    private final ModelAccess modelAccess;
    private CssLayout componentBase;
    private Table table;

    /**
     * Constructor.
     * 
     * @param editpart The editpart of that presenter
     */
    public TablePresentation(IElementEditpart editpart) {
        super((ITableEditpart) editpart);
        this.modelAccess = new ModelAccess((YTable) 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());
            }

            table = new Table();
            table.addStyleName(CSS_CLASS__CONTROL);
            table.setSizeFull();
            componentBase.addComponent(table);

            if (modelAccess.isCssClassValid()) {
                table.addStyleName(modelAccess.getCssClass());
            }

            if (modelAccess.isLabelValid()) {
                table.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;
            table = null;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void internalDispose() {
        // unrender the ui component
        unrender();
    }

    /**
     * A helper class.
     */
    private static class ModelAccess {
        private final YTable yTable;

        public ModelAccess(YTable yTable) {
            super();
            this.yTable = yTable;
        }

        /**
         * @return
         * @see org.eclipse.emf.ecp.ecview.ui.core.model.core.YCssAble#getCssClass()
         */
        public String getCssClass() {
            return yTable.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 yTable.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 yTable.getDatadescription() != null && yTable.getDatadescription().getLabel() != null;
        }

        /**
         * Returns the label.
         * 
         * @return
         */
        public String getLabel() {
            return yTable.getDatadescription().getLabel();
        }
    }
}