1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
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-2011 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  import javax.swing.Action;
31  import java.awt.EventQueue;
32  import java.util.Locale;
33  
34  /**
35   * Demonstrates how to visually fade-in newly created nodes and
36   * fade-out deleted nodes. This nice animation effect is triggered by a
37   * special <code>GraphListener</code> implementation.
38   * Note that this demo makes use of the yFiles class <code>
39   * Graph2DViewRepaintManager</code> to increase the speed
40   * of the animation effect.
41   *
42   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/animation.html">Section Animations for Graph Elements</a> in the yFiles for Java Developer's Guide
43   */
44  public class FadeInFadeOutDemo extends DemoBase {
45    private static final long PREFERRED_DURATION = 500;
46  
47    /**
48     * Creates a new FadeInFadeOutDemo and initializes the AnimationPlayer for
49     * the fade effects.
50     */
51    public FadeInFadeOutDemo() {
52      view.getGraph2D().addGraphListener(new FadeHandler(view));
53    }
54  
55    protected Action createLoadAction() {
56      //Overridden method to disable the Load menu in the demo
57      return null;
58    }
59  
60    protected Action createSaveAction() {
61      //Overridden method to disable the Save menu in the demo
62      return null;
63    }
64  
65    public static void main(String[] args) {
66      EventQueue.invokeLater(new Runnable() {
67        public void run() {
68          Locale.setDefault(Locale.ENGLISH);
69          initLnF();
70          (new FadeInFadeOutDemo()).start("Fade Demo");
71        }
72      });
73    }
74  
75    /**
76     * Triggers fading effects on node creation and node removal.
77     */
78    private static final class FadeHandler implements GraphListener {
79      private AnimationPlayer player;
80      private ViewAnimationFactory factory;
81  
82      public FadeHandler(Graph2DView view) {
83        factory = new ViewAnimationFactory(new Graph2DViewRepaintManager(view));
84        player = factory.createConfiguredPlayer();
85      }
86  
87      public void onGraphEvent(final GraphEvent e) {
88        final Graph2D graph = (Graph2D) e.getGraph();
89        switch (e.getType()) {
90          case GraphEvent.NODE_CREATION: {
91            final NodeRealizer nr = graph.getRealizer((Node) e.getData());
92            nr.setVisible(false);
93            player.animate(factory.fadeIn(nr, PREFERRED_DURATION));
94            break;
95          }
96          case GraphEvent.PRE_NODE_REMOVAL: {
97            final NodeRealizer nr = graph.getRealizer((Node) e.getData());
98  
99            // let's create a drawable, so the animation can run no matter
100           // if the node is in the graph or not
101           final Drawable dnr = ViewAnimationFactory.createDrawable(nr);
102           nr.setVisible(false);
103 
104           final AnimationObject fadeOut = factory.fadeOut(dnr, PREFERRED_DURATION);
105 
106           // let's start the animation with some delay so edges "vanish"
107           // before nodes
108           player.animate(
109               AnimationFactory.createSequence(
110                   AnimationFactory.createPause(100), fadeOut));
111           break;
112         }
113       }
114     }
115   }
116 }
117