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.ViewActionDemo;
17  import y.anim.AnimationFactory;
18  import y.anim.AnimationObject;
19  import y.anim.AnimationPlayer;
20  import y.anim.CompositeAnimationObject;
21  import y.util.DefaultMutableValue2D;
22  import y.util.Value2D;
23  import y.view.Graph2D;
24  import y.view.Graph2DView;
25  import y.view.Graph2DViewRepaintManager;
26  import y.view.NodeRealizer;
27  import y.view.ViewAnimationFactory;
28  
29  import javax.swing.JFrame;
30  import javax.swing.JPanel;
31  import javax.swing.Timer;
32  import javax.swing.JRootPane;
33  import java.awt.BorderLayout;
34  
35  /**
36   * Demonstrates usage and effects of ease in and/or ease out for animation
37   * effects.
38   */
39  public class EaseInEaseOutDemo
40  {
41    private static final int PREFERRED_DURATION = 2000;
42  
43    private final Graph2DView view;
44    private Value2D[][] positions;
45    private Timer timer;
46  
47    AnimationPlayer player;
48    ViewAnimationFactory factory;
49  
50    /**
51     * Creates a new EaseInEaseOutDemo and initializes a Timer that triggers the
52     * animation effects.
53     */
54    public EaseInEaseOutDemo()
55    {
56      this.view = new Graph2DView();
57      init();
58    }
59  
60    /**
61     * Initializes the start and end points for the animated movement effects.
62     * Creates nodes to demonstrate animated movement.
63     */
64    private void init()
65    {
66      positions = new Value2D[4][2];
67      for (int i = 0, n = positions.length; i < n; ++i)
68      {
69        positions[i][0] = DefaultMutableValue2D.create( 70, 110 + i*60);
70        positions[i][1] = DefaultMutableValue2D.create(410, 110 + i*60);
71      }
72  
73      final String[] labels = {
74              "Normal", "Ease In", "Ease In, Ease Out", "Ease Out"
75      };
76  
77      final Graph2D graph = view.getGraph2D();
78  
79      for (int i = 0, n = positions.length; i < n; ++i)
80      {
81        final Value2D pos = positions[i][0];
82        final NodeRealizer nr = graph.getRealizer(
83        graph.createNode(pos.getX(), pos.getY()));
84        nr.setSize(120, 30);
85        nr.setLabelText(labels[i]);
86      }
87  
88  
89      timer = new Timer(PREFERRED_DURATION + 500,
90                                    new java.awt.event.ActionListener()
91      {
92        private boolean invert = false;
93        public void actionPerformed(final java.awt.event.ActionEvent e)
94        {
95          play(invert);
96          invert = !invert;
97        }
98      });
99      timer.setInitialDelay(1000);
100     timer.start();
101   }
102 
103   /**
104    * Plays the movement animation for the nodes in the graph.
105    * Four different kinds of movement animations are created:
106    * <ul>
107    * <li> normal (i.e. no ease effect) </li>
108    * <li> ease in </li>
109    * <li> ease in and ease out </li>
110    * <li> ease out </li>
111    * </ul>
112    *
113    * @param invert   if <code>true</code> the nodes move from right to left;
114    *                 otherwise the nodes move from left to right.
115    */
116   private void play( final boolean invert )
117   {
118     final Graph2D graph = view.getGraph2D();
119 
120     if(factory == null) {
121       factory = new ViewAnimationFactory(new Graph2DViewRepaintManager(view));
122     }
123 
124     // we want to play all four animations at the same time
125     final CompositeAnimationObject moves = AnimationFactory.createConcurrency();
126 
127     for (int i = 0, n = positions.length; i < n; ++i)
128     {
129       final Value2D dest = positions[i][invert ? 0 : 1];
130       final NodeRealizer nr = graph.getRealizer(graph.getNodeArray()[i]);
131 
132       // create a movement effect from the realizer's current position to
133       // the specified destination
134       AnimationObject move =
135               factory.move(nr, dest, ViewAnimationFactory.APPLY_EFFECT,
136                            PREFERRED_DURATION);
137 
138       switch (i)
139       {
140         case 1:
141           // create an ease in effect
142           move = AnimationFactory.createEasedAnimation(move, 1, 1);
143           break;
144         case 2:
145           // create an ease in and ease out effect
146           move = AnimationFactory.createEasedAnimation(move);
147           break;
148         case 3:
149           // create an ease out effect
150           move = AnimationFactory.createEasedAnimation(move, 0, 0);
151           break;
152       }
153 
154       // register the individual animations for concurrent processing
155       moves.addAnimation(move);
156     }
157 
158     if (player == null) {
159       player = factory.createConfiguredPlayer();
160     }
161     // play the animations
162     player.animate(moves);
163   }
164 
165 
166   /**
167    * Creates an application frame for this demo
168    * and displays it. The given string is the title of
169    * the displayed frame.
170    */
171   private void start( final String title )
172   {
173     final JFrame frame = new JFrame(title);
174 
175     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
176     addContentTo(frame.getRootPane());
177     frame.pack();
178     frame.setLocationRelativeTo(null);
179     frame.setVisible(true);
180   }
181 
182   public final void addContentTo( final JRootPane rootPane )
183   {
184     final JPanel contentPane = new JPanel(new BorderLayout());
185     contentPane.add(view, BorderLayout.CENTER);
186 
187     rootPane.setContentPane( contentPane );
188   }
189 
190   public void dispose()
191   {
192     if (timer != null) {
193       if (timer.isRunning()) {
194         timer.stop();
195       }
196       timer = null;
197     }
198   }
199 
200   public static void main( String[] args )
201   {
202     ViewActionDemo.initLnF();
203     final EaseInEaseOutDemo demo = new EaseInEaseOutDemo();
204     demo.start("Ease Demo");
205   }
206 }
207