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