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.gwt.client.util;
029
030import com.google.gwt.dom.client.Element;
031import com.google.gwt.dom.client.Style;
032import com.google.gwt.dom.client.Style.Unit;
033import com.google.gwt.user.client.Command;
034
035/**
036 * Changes the elements height until the target height is reached.<p>
037 *
038 * @since 8.0.0
039 */
040public class CmsChangeHeightAnimation extends A_CmsAnimation {
041
042    /** The element style. */
043    private Style m_elementStyle;
044
045    /** The height of the fully visible element. */
046    private int m_height;
047
048    /** The difference between the target height and the original height. */
049    private int m_heightDiff;
050
051    /** The height when the animation should stop. */
052    private int m_targetHeight;
053
054    /**
055     * Constructor.<p>
056     *
057     * @param element the element to animate
058     * @param targetHeight the height when the animation should stop
059     * @param callback the callback executed after the animation is completed
060     */
061    public CmsChangeHeightAnimation(Element element, int targetHeight, Command callback) {
062
063        super(callback);
064
065        m_elementStyle = element.getStyle();
066
067        m_height = CmsDomUtil.getCurrentStyleInt(element, CmsDomUtil.Style.height);
068        m_targetHeight = targetHeight;
069        m_heightDiff = m_targetHeight - m_height;
070    }
071
072    /**
073     * Slides the given element into view executing the callback afterwards.<p>
074     *
075     * @param element the element to slide in
076     * @param targetHeight the height when the animation should stop
077     * @param callback the callback executed after the animation is completed
078     * @param duration the animation duration
079     *
080     * @return the running animation object
081     */
082    public static CmsChangeHeightAnimation change(Element element, int targetHeight, Command callback, int duration) {
083
084        CmsChangeHeightAnimation animation = new CmsChangeHeightAnimation(element, targetHeight, callback);
085        animation.run(duration);
086        return animation;
087    }
088
089    /**
090     * @see com.google.gwt.animation.client.Animation#onComplete()
091     */
092    @Override
093    protected void onComplete() {
094
095        m_elementStyle.setHeight(m_targetHeight, Unit.PX);
096        if (m_callback != null) {
097            m_callback.execute();
098        }
099    }
100
101    /**
102     * @see com.google.gwt.animation.client.Animation#onUpdate(double)
103     */
104    @Override
105    protected void onUpdate(double progress) {
106
107        m_elementStyle.setHeight((m_heightDiff * progress * progress) + m_height, Unit.PX);
108    }
109}