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.Graph2DClipboard;
18  
19  import javax.swing.Action;
20  import javax.swing.JToolBar;
21  import javax.swing.KeyStroke;
22  import java.awt.EventQueue;
23  import java.awt.event.InputEvent;
24  import java.awt.event.KeyEvent;
25  import java.util.Locale;
26  
27  /**
28   * This class demonstrates how to use the yFiles clipboard
29   * functionality to cut, copy and paste parts of a graph.
30   *
31   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/advanced_stuff.html">Section Advanced Application Logic</a> in the yFiles for Java Developer's Guide
32   */
33  public class ClipboardDemo extends DemoBase
34  {
35    Action cutAction;
36    Action copyAction;
37    Action pasteAction;
38  
39    public ClipboardDemo()
40    {
41      view.getCanvasComponent().getActionMap().put("CUT", cutAction);
42      view.getCanvasComponent().getInputMap().put(
43          KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK),"CUT");
44  
45      view.getCanvasComponent().getActionMap().put("COPY", copyAction);
46      view.getCanvasComponent().getInputMap().put(
47          KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "COPY");
48  
49      view.getCanvasComponent().getActionMap().put("PASTE", pasteAction);
50      view.getCanvasComponent().getInputMap().put(
51          KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "PASTE");
52  
53      loadInitialGraph();
54    }
55  
56    protected void loadInitialGraph() {
57      loadGraph("resource/ClipboardDemo.graphml");
58    }
59  
60    protected void registerViewActions() {
61      super.registerViewActions();
62      //create new clipboard.
63      Graph2DClipboard clipboard = new Graph2DClipboard(view);
64  
65      //get Cut action from clipboard
66      cutAction = clipboard.getCutAction();
67      cutAction.putValue(Action.SMALL_ICON, getIconResource("resource/cut.png"));
68      cutAction.putValue(Action.SHORT_DESCRIPTION, "Cut");
69  
70      //get Copy action from clipboard
71      copyAction = clipboard.getCopyAction();
72      copyAction.putValue(Action.SMALL_ICON, getIconResource("resource/copy.png"));
73      copyAction.putValue(Action.SHORT_DESCRIPTION, "Copy");
74  
75      //get Paste action from clipboard
76      pasteAction = clipboard.getPasteAction();
77      pasteAction.putValue(Action.SMALL_ICON, getIconResource("resource/paste.png"));
78      pasteAction.putValue(Action.SHORT_DESCRIPTION, "Paste");
79    }
80  
81    protected JToolBar createToolBar() {
82      JToolBar jtb = super.createToolBar();
83      jtb.addSeparator();
84      jtb.add(cutAction);
85      jtb.add(copyAction);
86      jtb.add(pasteAction);
87      return jtb;
88    }
89  
90    public static void main(String[] args) {
91      EventQueue.invokeLater(new Runnable() {
92        public void run() {
93          Locale.setDefault(Locale.ENGLISH);
94          initLnF();
95          new ClipboardDemo().start();
96        }
97      });
98    }
99  }
100