001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.components.extensions;
029
030import org.opencms.ui.shared.components.CmsMaxHeightState;
031import org.opencms.ui.shared.rpc.I_CmsMaxHeightServerRpc;
032
033import java.util.List;
034
035import com.google.common.collect.Lists;
036import com.vaadin.server.AbstractExtension;
037import com.vaadin.server.Sizeable.Unit;
038import com.vaadin.ui.AbstractComponent;
039
040/**
041 * Allows the use of max height in combination with vaadin layout components.<p>
042 */
043public class CmsMaxHeightExtension extends AbstractExtension implements I_CmsMaxHeightServerRpc {
044
045    /**
046     * Callback interfaces for height change notifications.<p>
047     */
048    public interface I_HeightChangeHandler {
049
050        /**
051         * Called when the fixHeight RPC call is received.<p>
052         *
053         * @param height the height from the RPC call
054         */
055        void onChangeHeight(int height);
056    }
057
058    /** The serial version id. */
059    private static final long serialVersionUID = 3978957151754705873L;
060
061    /** The extended component. */
062    private AbstractComponent m_component;
063
064    /** The list of height change handlers. */
065    private List<I_HeightChangeHandler> m_heightChangeHandlers = Lists.newArrayList();
066
067    /**
068     * Constructor.<p>
069     *
070     * @param component the component to extend
071     * @param maxHeight the max height
072     */
073    public CmsMaxHeightExtension(AbstractComponent component, int maxHeight) {
074        m_component = component;
075        extend(component);
076        registerRpc(this);
077        getState().setMaxHeight(maxHeight);
078
079    }
080
081    /**
082     * Adds an action to execute when the height is changed.<p>
083     *
084     * @param action the action
085     */
086    public void addHeightChangeHandler(I_HeightChangeHandler action) {
087
088        m_heightChangeHandlers.add(action);
089    }
090
091    /**
092     * @see org.opencms.ui.shared.rpc.I_CmsMaxHeightServerRpc#fixHeight(int)
093     */
094    public void fixHeight(int height) {
095
096        if (height < 1) {
097            m_component.setHeightUndefined();
098        } else {
099            m_component.setHeight(height, Unit.PIXELS);
100        }
101        for (I_HeightChangeHandler handler : m_heightChangeHandlers) {
102            handler.onChangeHeight(height);
103        }
104    }
105
106    /**
107     * Removes a height change handler.<p>
108     *
109     * @param action the handler to remove
110     */
111    public void removeHeightChangeHandler(Runnable action) {
112
113        m_heightChangeHandlers.remove(action);
114    }
115
116    /**
117     * Updates the maximum height.<p>
118     *
119     * @param maxHeight the new value for the maximum height
120     */
121    public void updateMaxHeight(int maxHeight) {
122
123        getState().setMaxHeight(maxHeight);
124    }
125
126    /**
127     * @see com.vaadin.server.AbstractClientConnector#getState()
128     */
129    @Override
130    protected CmsMaxHeightState getState() {
131
132        return (CmsMaxHeightState)super.getState();
133    }
134
135}