001
002package org.opencms.ui.components;
003
004import org.opencms.ui.shared.components.CmsExternalLayoutState;
005
006import java.util.Collections;
007import java.util.Iterator;
008
009import com.vaadin.ui.AbstractComponent;
010import com.vaadin.ui.Component;
011import com.vaadin.ui.HasComponents;
012
013/**
014 * Single component container that can render the given component in any HTML
015 * element.<p>
016 *
017 * @author Risto Yrjänä / Vaadin Ltd.
018 */
019public class CmsExternalLayout extends AbstractComponent implements HasComponents {
020
021    /** The serial version id. */
022    private static final long serialVersionUID = 4970339558483506331L;
023
024    /** The child component. */
025    private final Component m_childComponent;
026
027    /**
028     * Create a layout that renders the given component to an external HTML element that
029     * has the specified id
030     *
031     * @param divId
032     *            id for the target element, cannot be null
033     * @param component
034     *            component to be rendered, cannot be null
035     */
036    public CmsExternalLayout(String divId, Component component) {
037        if ((divId == null) || (component == null)) {
038            throw new IllegalArgumentException("The div id or the child component cannot be null.");
039        }
040
041        getState().m_externalComponentId = divId;
042        m_childComponent = component;
043        m_childComponent.setParent(this);
044    }
045
046    /**
047     * @see com.vaadin.ui.AbstractComponent#getState()
048     */
049    @Override
050    public CmsExternalLayoutState getState() {
051
052        return (CmsExternalLayoutState)super.getState();
053    }
054
055    /**
056     * @see com.vaadin.ui.HasComponents#iterator()
057     */
058    @Override
059    public Iterator<Component> iterator() {
060
061        return Collections.singleton(m_childComponent).iterator();
062    }
063}