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.application;
29  
30  import demo.view.DemoBase;
31  import y.view.Graph2DUndoManager;
32  
33  import javax.swing.JToolBar;
34  import javax.swing.Action;
35  import java.awt.EventQueue;
36  import java.util.Locale;
37  
38  /**
39   * This Demo shows how to use Undo/Redo functionality built into yFiles.
40   *
41   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/advanced_stuff#undo_redo" target="_blank">Section Undo/Redo</a> in the yFiles for Java Developer's Guide
42   */
43  public class UndoRedoDemo extends DemoBase
44  {
45    private Graph2DUndoManager undoManager;
46  
47    /**
48     * Returns the undo manager for this application. Also, if not already done - it creates 
49     * and configures it.
50     */
51    protected Graph2DUndoManager getUndoManager()
52    {
53      if(undoManager == null)
54      {
55        //create one and make it listen to graph structure changes
56        undoManager = new Graph2DUndoManager(view.getGraph2D());
57  
58        //assign the graph view as view container so we get view updates
59        //after undo/redo actions have been performed. 
60        undoManager.setViewContainer(view);
61      }
62      return undoManager;
63    }
64  
65  
66    public JToolBar createToolBar()
67    {
68      JToolBar bar = super.createToolBar();
69  
70      bar.addSeparator();
71      
72      //add undo action to toolbar
73      Action action = getUndoManager().getUndoAction();
74      action.putValue(Action.SMALL_ICON, getIconResource("resource/undo.png"));
75      action.putValue(Action.SHORT_DESCRIPTION, "Undo");
76      bar.add(action);
77  
78      //add redo action to toolbar
79      action = getUndoManager().getRedoAction();
80      action.putValue(Action.SMALL_ICON, getIconResource("resource/redo.png"));
81      action.putValue(Action.SHORT_DESCRIPTION, "Redo");
82      bar.add(action);
83      return bar;
84    }
85  
86    /**
87     * Overwritten to disable the default undo/redo support.
88     */
89    protected boolean isUndoRedoEnabled() {
90      return false;
91    }
92  
93    public static void main(String[] args) {
94      EventQueue.invokeLater(new Runnable() {
95        public void run() {
96          Locale.setDefault(Locale.ENGLISH);
97          initLnF();
98          (new UndoRedoDemo()).start("Undo/Redo Demo");
99        }
100     });
101   }
102 
103 }
104 
105     
106 
107       
108