1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.view.anim;
29  
30  import demo.view.DemoBase;
31  import y.anim.AnimationFactory;
32  import y.anim.AnimationObject;
33  import y.anim.AnimationPlayer;
34  import y.base.GraphEvent;
35  import y.base.GraphListener;
36  import y.base.Node;
37  import y.view.Drawable;
38  import y.view.Graph2D;
39  import y.view.Graph2DView;
40  import y.view.Graph2DViewRepaintManager;
41  import y.view.NodeRealizer;
42  import y.view.ViewAnimationFactory;
43  
44  import javax.swing.Action;
45  import java.awt.EventQueue;
46  import java.util.Locale;
47  
48  /**
49   * Demonstrates how to visually fade-in newly created nodes and
50   * fade-out deleted nodes. This nice animation effect is triggered by a
51   * special <code>GraphListener</code> implementation.
52   * Note that this demo makes use of the yFiles class <code>
53   * Graph2DViewRepaintManager</code> to increase the speed
54   * of the animation effect.
55   *
56   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/animation" target="_blank">Section Animations for Graph Elements</a> in the yFiles for Java Developer's Guide
57   */
58  public class FadeInFadeOutDemo extends DemoBase {
59    private static final long PREFERRED_DURATION = 500;
60  
61    /**
62     * Creates a new FadeInFadeOutDemo and initializes the AnimationPlayer for
63     * the fade effects.
64     */
65    public FadeInFadeOutDemo() {
66      view.getGraph2D().addGraphListener(new FadeHandler(view));
67    }
68  
69    protected Action createLoadAction() {
70      //Overridden method to disable the Load menu in the demo
71      return null;
72    }
73  
74    protected Action createSaveAction() {
75      //Overridden method to disable the Save menu in the demo
76      return null;
77    }
78  
79    public static void main(String[] args) {
80      EventQueue.invokeLater(new Runnable() {
81        public void run() {
82          Locale.setDefault(Locale.ENGLISH);
83          initLnF();
84          (new FadeInFadeOutDemo()).start("Fade Demo");
85        }
86      });
87    }
88  
89    /**
90     * Triggers fading effects on node creation and node removal.
91     */
92    private static final class FadeHandler implements GraphListener {
93      private AnimationPlayer player;
94      private ViewAnimationFactory factory;
95  
96      public FadeHandler(Graph2DView view) {
97        factory = new ViewAnimationFactory(new Graph2DViewRepaintManager(view));
98        player = factory.createConfiguredPlayer();
99      }
100 
101     public void onGraphEvent(final GraphEvent e) {
102       final Graph2D graph = (Graph2D) e.getGraph();
103       switch (e.getType()) {
104         case GraphEvent.NODE_CREATION:
105         case GraphEvent.NODE_REINSERTION: {
106           final NodeRealizer nr = graph.getRealizer((Node) e.getData());
107           nr.setVisible(false);
108           player.animate(factory.fadeIn(nr, PREFERRED_DURATION));
109           break;
110         }
111         case GraphEvent.PRE_NODE_REMOVAL: {
112           final NodeRealizer nr = graph.getRealizer((Node) e.getData());
113 
114           // let's create a drawable, so the animation can run no matter
115           // if the node is in the graph or not
116           final Drawable dnr = ViewAnimationFactory.createDrawable(nr);
117           nr.setVisible(false);
118 
119           final AnimationObject fadeOut = factory.fadeOut(dnr, PREFERRED_DURATION);
120 
121           // let's start the animation with some delay so edges "vanish"
122           // before nodes
123           player.animate(
124               AnimationFactory.createSequence(
125                   AnimationFactory.createPause(100), fadeOut));
126           break;
127         }
128       }
129     }
130   }
131 }
132