01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.ui.fx;
21 
22 import js.Js;
23 import js.JsApplet;
24 import js.ObjectLike;
25 import jsx.dom.Styles;
26 import jsx.ui.Component;
27 import jsx.ui.event.Style;
28 import jsx.ui.fx.event.Animation;
29 
30 /**
31  * <p>A base class for widgets providing fade-out effects to their components.</p>
32  * 
33  * @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>
34  */
35 public class FadeOut extends Effects
36 {
37     private double t;
38 
39     /**
40      * <p>Constructs a fade-out effects over a component.</p>
41      * <p>This constructor simply invokes its super constructor with the component
42      * as argument.</p>
43      * @param e A component to have the visual effects.
44      * @since 1.0
45      */
46     public FadeOut(Component e) {
47         super(e);
48     }
49 
50     /**
51      * <p>Defines an action for the given frame.</p>
52      * <p>This method changes the transparency of the widget.</p>
53      * @param i An index of the current frame ranging from 0 and the total number 
54      * of frames obtained by {@link #getFrames(Effects)}.
55      * @since 1.0
56      */
57     @Override
58     protected void frame(int i) {
59         Component.transparency(
60                 unwrap(),
61                 getMotion(this).get(i) * t
62         );
63     }
64 
65     /**
66      * <p>Performs an action on the dispatched event.</p>
67      * <p>This method animates the widget with fade-out effects.</p>
68      * @param evt The event dispatched to this listener.
69      * @since 1.0
70      */
71     public void onEvent(Animation evt) {
72         Class<? extends Effects> fx = ini(evt).var(Animation.FX);
73         if (Js.not(fx) || fx == FadeOut.class) {
74             ObjectLike style = ini(evt).var(Style.STYLE);
75             t = Js.be(style) ? Styles.transparency(style) : 0;
76             if (Js.not(fx)) {
77                 style = null;
78             }
79             animate(style);
80         }
81     }
82 
83     /**
84      * <p>Animates a component with fade-out effects.</p>
85      * @param e A component to fade out.
86      * @param p A final style to the component.
87      * @since 1.0
88      */
89     public static final void animate(Component e, ObjectLike p) {
90         synchronized(JsApplet.class) {
91             FadeOut fo = new FadeOut(e);
92             fo.exec(new Animation(FadeOut.class, p));
93             e.removeListener(Animation.class, fo);
94         }
95     }
96 }