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.advanced;
15  
16  import demo.view.DemoBase;
17  import y.view.Graph2DUndoManager;
18  
19  import javax.swing.JToolBar;
20  import javax.swing.Action;
21  import javax.swing.ImageIcon;
22  
23  /**
24   * This Demo shows how to use Undo/Redo functionality built into yFiles.
25   */
26  public class UndoRedoDemo extends DemoBase
27  {
28    private Graph2DUndoManager undoManager;
29  
30    protected void init()
31    {
32  
33    }
34  
35    /**
36     * Returns the undo manager for this application. Also, if not already done - it creates 
37     * and configures it.
38     */
39    protected Graph2DUndoManager getUndoManager()
40    {
41      if(undoManager == null)
42      {
43        undoManager = new Graph2DUndoManager();
44        //make it listen to graph structure changes
45        view.getGraph2D().addGraphListener(undoManager);
46        //make it handle backup realizer requests. 
47        view.getGraph2D().setBackupRealizersHandler(undoManager);
48        //assign the graph view as view container so we get view updates
49        //after undo/redo actions have been performed. 
50        undoManager.setViewContainer(view);
51      }
52      return undoManager;
53    }
54  
55  
56    public JToolBar createToolBar()
57    {
58      JToolBar bar = super.createToolBar();
59  
60      bar.addSeparator();
61      
62      //add undo action to toolbar
63      Action action = getUndoManager().getUndoAction();
64      action.putValue(Action.SMALL_ICON,
65          new ImageIcon(DemoBase.class.getResource("resource/Undo16.gif")));
66      action.putValue(Action.SHORT_DESCRIPTION, "Undo");
67      bar.add(action);
68  
69      //add redo action to toolbar
70      action = getUndoManager().getRedoAction();
71      action.putValue(Action.SMALL_ICON,
72          new ImageIcon(DemoBase.class.getResource("resource/Redo16.gif")));
73      action.putValue(Action.SHORT_DESCRIPTION, "Redo");
74      bar.add(action);
75      return bar;
76    }
77  
78    public static void main(String args[])
79    {
80      initLnF();
81      UndoRedoDemo demo = new UndoRedoDemo();
82      demo.start("Undo/Redo Demo");
83    }
84  
85  }
86  
87      
88  
89        
90