1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.anim;
15  
16  import demo.view.DemoBase;
17  import y.anim.AnimationFactory;
18  import y.anim.AnimationObject;
19  import y.anim.AnimationPlayer;
20  import y.base.GraphEvent;
21  import y.base.GraphListener;
22  import y.base.Node;
23  import y.view.Drawable;
24  import y.view.Graph2D;
25  import y.view.Graph2DView;
26  import y.view.Graph2DViewRepaintManager;
27  import y.view.NodeRealizer;
28  import y.view.ViewAnimationFactory;
29  
30  /**
31   * Demonstrates how to visually fade-in newly created nodes and
32   * fade-out deleted nodes. This nice animation effect is triggered by a
33   * special <code>GraphListener</code> implementation.
34   * Note that this demo makes use of the yFiles class <code>
35   * Graph2DViewRepaintManager</code> to increase the speed
36   * of the animation effect.
37   */
38  public class FadeInFadeOutDemo extends DemoBase
39  {
40    private static final long PREFERRED_DURATION = 500;
41  
42    /**
43     * Creates a new FadeInFadeOutDemo and initializes the AnimationPlayer for
44     * the fade effects.
45     */
46    public FadeInFadeOutDemo()
47    {
48      view.getGraph2D().addGraphListener(new FadeHandler(view));
49    }
50  
51    public static void main( String[] args )
52    {
53      initLnF();
54      final FadeInFadeOutDemo demo = new FadeInFadeOutDemo();
55      demo.start("Fade Demo");
56    }
57  
58    /**
59     * Triggers fading effects on node creation and node removal.
60     */
61    private final class FadeHandler implements GraphListener
62    {
63      private AnimationPlayer player;
64      private ViewAnimationFactory factory;
65  
66      public FadeHandler(Graph2DView view) {
67        factory = new ViewAnimationFactory(new Graph2DViewRepaintManager(view));
68        player = factory.createConfiguredPlayer();
69      }
70  
71      public void onGraphEvent( final GraphEvent e )
72      {
73        final Graph2D graph = (Graph2D)e.getGraph();
74        switch (e.getType())
75        {
76          case GraphEvent.NODE_CREATION:
77          {
78            final NodeRealizer nr = graph.getRealizer((Node)e.getData());
79            nr.setVisible(false);
80            player.animate(factory.fadeIn(nr, PREFERRED_DURATION));
81            break;
82          }
83          case GraphEvent.PRE_NODE_REMOVAL:
84          {
85            final NodeRealizer nr = graph.getRealizer((Node)e.getData());
86  
87            // let's create a drawable, so the animation can run no matter
88            // if the node is in the graph or not
89            final Drawable dnr = ViewAnimationFactory.createDrawable(nr);
90            nr.setVisible(false);
91  
92            final AnimationObject fadeOut = factory.fadeOut(dnr, PREFERRED_DURATION);
93  
94            // let's start the animation with some delay so edges "vanish"
95            // before nodes
96            player.animate(
97                    AnimationFactory.createSequence(
98                      AnimationFactory.createPause(100), fadeOut));
99            break;
100         }
101       }
102     }
103   }
104 }
105