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.user.client.Command;
032
033/**
034 * Fade animation. Fading the element into view or fading it out.<p>
035 *
036 * @since 8.0.0
037 */
038public class CmsFadeAnimation extends A_CmsAnimation {
039
040    /** The element to animate. */
041    private Element m_element;
042
043    /** Show or hide flag. */
044    private boolean m_show;
045
046    /**
047     * Constructor.<p>
048     *
049     * @param element the element to animate
050     * @param show <code>true</code> to show the element, <code>false</code> to hide it away
051     * @param callback the callback executed after the animation is completed
052     */
053    public CmsFadeAnimation(Element element, boolean show, Command callback) {
054
055        super(callback);
056        m_show = show;
057        m_element = element;
058    }
059
060    /**
061     * Fades the given element into view executing the callback afterwards.<p>
062     *
063     * @param element the element to fade in
064     * @param callback the callback
065     * @param duration the animation duration
066     *
067     * @return the running animation object
068     */
069    public static CmsFadeAnimation fadeIn(Element element, Command callback, int duration) {
070
071        CmsFadeAnimation animation = new CmsFadeAnimation(element, true, callback);
072        animation.run(duration);
073        return animation;
074    }
075
076    /**
077     * Fades the given element out of view executing the callback afterwards.<p>
078     *
079     * @param element the element to fade out
080     * @param callback the callback
081     * @param duration the animation duration
082     *
083     * @return the running animation object
084     */
085    public static CmsFadeAnimation fadeOut(Element element, Command callback, int duration) {
086
087        CmsFadeAnimation animation = new CmsFadeAnimation(element, false, callback);
088        animation.run(duration);
089        return animation;
090    }
091
092    /**
093     * @see com.google.gwt.animation.client.Animation#onComplete()
094     */
095    @Override
096    protected void onComplete() {
097
098        super.onComplete();
099
100        // using own implementation as GWT won't do it properly on IE7-8
101        CmsDomUtil.clearOpacity(m_element);
102    }
103
104    /**
105     * @see com.google.gwt.animation.client.Animation#onUpdate(double)
106     */
107    @Override
108    protected void onUpdate(double progress) {
109
110        if (m_show) {
111            m_element.getStyle().setOpacity(progress);
112        } else {
113            m_element.getStyle().setOpacity(1 - progress);
114        }
115    }
116}