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    /** If false, disable automatic height changes. */
068    private boolean m_enabled = true;
069
070    /**
071     * Constructor.<p>
072     *
073     * @param component the component to extend
074     * @param maxHeight the max height
075     */
076    public CmsMaxHeightExtension(AbstractComponent component, int maxHeight) {
077
078        m_component = component;
079        extend(component);
080        registerRpc(this);
081        getState().setMaxHeight(maxHeight);
082
083    }
084
085    /**
086     * Adds an action to execute when the height is changed.<p>
087     *
088     * @param action the action
089     */
090    public void addHeightChangeHandler(I_HeightChangeHandler action) {
091
092        m_heightChangeHandlers.add(action);
093    }
094
095    /**
096     * @see org.opencms.ui.shared.rpc.I_CmsMaxHeightServerRpc#fixHeight(int)
097     */
098    public void fixHeight(int height) {
099
100        if (!m_enabled) {
101            return;
102        }
103        if (height < 1) {
104            m_component.setHeightUndefined();
105        } else {
106            m_component.setHeight(height, Unit.PIXELS);
107        }
108        for (I_HeightChangeHandler handler : m_heightChangeHandlers) {
109            handler.onChangeHeight(height);
110        }
111    }
112
113    /**
114     * Removes a height change handler.<p>
115     *
116     * @param action the handler to remove
117     */
118    public void removeHeightChangeHandler(Runnable action) {
119
120        m_heightChangeHandlers.remove(action);
121    }
122
123    /**
124     * Enables / disables automatic height changes.
125     *
126     * @param enabled if true, enable automatic height changes
127     */
128    public void setEnabled(boolean enabled) {
129
130        m_enabled = enabled;
131    }
132
133    /**
134     * Updates the maximum height.<p>
135     *
136     * @param maxHeight the new value for the maximum height
137     */
138    public void updateMaxHeight(int maxHeight) {
139
140        getState().setMaxHeight(maxHeight);
141    }
142
143    /**
144     * @see com.vaadin.server.AbstractClientConnector#getState()
145     */
146    @Override
147    protected CmsMaxHeightState getState() {
148
149        return (CmsMaxHeightState)super.getState();
150    }
151
152}