001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx.ui.fx;
021 
022 import js.Js;
023 import js.JsApplet;
024 import js.ObjectLike;
025 import jsx.dom.Styles;
026 import jsx.ui.Component;
027 import jsx.ui.event.Style;
028 import jsx.ui.fx.event.Animation;
029 
030 /**
031  * <p>A base class for widgets providing fade-in effects to their components.</p>
032  * 
033  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
034  */
035 public class FadeIn extends Effects
036 {
037     private double t;
038 
039     /**
040      * <p>Constructs a fade-in effects over a component.</p>
041      * <p>This constructor simply invokes its super constructor with the component
042      * as argument.</p>
043      * @param e A component to have the visual effects.
044      * @since 1.0
045      */
046     public FadeIn(Component e) {
047         super(e);
048     }
049 
050     /**
051      * <p>Defines an action for the given frame.</p>
052      * <p>This method changes the transparency of the widget.</p>
053      * @param i An index of the current frame ranging from 0 and the total number 
054      * of frames obtained by {@link #getFrames(Effects)}.
055      * @since 1.0
056      */
057     @Override
058     protected void frame(int i) {
059         Component.transparency(
060                 unwrap(),
061                 1 - getMotion(this).get(i) * t
062         );
063     }
064 
065     /**
066      * <p>Finishes an animation with a given style object applied to the widget.</p>
067      * <p>Subclasses overrides or invokes this method to provide different visual effects.</p>
068      * <p>This method changes the transparency of the widget.</p>
069      * @param style The final style of the widget.
070      * @since 1.0
071      */
072     @Override
073     protected void stop(ObjectLike style) {
074         super.stop(style);
075         Component.transparency(unwrap(), 1 - t);
076     }
077 
078     /**
079      * <p>Performs an action on the dispatched event.</p>
080      * <p>This method animates the widget with fade-in effects.</p>
081      * @param evt The event dispatched to this listener.
082      * @since 1.0
083      */
084     public void onEvent(Animation evt) {
085         Class<? extends Effects> fx = ini(evt).var(Animation.FX);
086         if (Js.not(fx) || fx == FadeIn.class) {
087             ObjectLike style = ini(evt).var(Style.STYLE);
088             t = Js.be(style) ? Styles.transparency(style) : 0;
089             t = 1 - t;
090             if (Js.not(fx)) {
091                 style = null;
092             }
093             animate(style);
094         }
095     }
096 
097     /**
098      * <p>Animates a component with fade-in effects.</p>
099      * @param e A component to fade in.
100      * @since 1.0
101      */
102     public static final void animate(Component e) {
103         synchronized(JsApplet.class) {
104             FadeIn fi = new FadeIn(e);
105             fi.exec(new Animation(FadeIn.class));
106             e.removeListener(Animation.class, fi);
107         }
108     }
109 }