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.Graph2DClipboard;
18  
19  import javax.swing.Action;
20  import javax.swing.ImageIcon;
21  import javax.swing.JToolBar;
22  import javax.swing.KeyStroke;
23  import java.awt.event.KeyEvent;
24  
25  /**
26   * This class demonstrates how to use the yFiles clipboard
27   * functionality to cut, copy and paste parts of a graph.
28   */
29  public class ClipboardDemo extends DemoBase
30  {
31    Action cutAction;
32    Action copyAction;
33    Action pasteAction;
34  
35    public ClipboardDemo()
36    {
37      view.getCanvasComponent().getActionMap().put("CUT", cutAction);
38      view.getCanvasComponent().getInputMap().put(
39          KeyStroke.getKeyStroke(KeyEvent.VK_X,  KeyEvent.CTRL_MASK),"CUT");
40  
41      view.getCanvasComponent().getActionMap().put("COPY", copyAction);
42      view.getCanvasComponent().getInputMap().put(
43          KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), "COPY");
44  
45      view.getCanvasComponent().getActionMap().put("PASTE", pasteAction);
46      view.getCanvasComponent().getInputMap().put(
47          KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK), "PASTE");
48    }
49  
50    protected void registerViewActions() {
51      super.registerViewActions();
52      //create new clipboard.
53      Graph2DClipboard clipboard = new Graph2DClipboard(view);
54  
55      //get Cut action from clipboard
56      cutAction = clipboard.getCutAction();
57      cutAction.putValue(Action.SMALL_ICON,
58          new ImageIcon( DemoBase.class.getResource("resource/Cut16.gif")));
59      cutAction.putValue(Action.SHORT_DESCRIPTION, "Cut");
60  
61      //get Copy action from clipboard
62      copyAction = clipboard.getCopyAction();
63      copyAction.putValue(Action.SMALL_ICON,
64          new ImageIcon( DemoBase.class.getResource("resource/Copy16.gif")));
65      copyAction.putValue(Action.SHORT_DESCRIPTION, "Copy");
66  
67      //get Paste action from clipboard
68      pasteAction = clipboard.getPasteAction();
69      pasteAction.putValue(Action.SMALL_ICON,
70          new ImageIcon( DemoBase.class.getResource("resource/Paste16.gif")));
71      pasteAction.putValue(Action.SHORT_DESCRIPTION, "Paste");
72    }
73  
74    protected JToolBar createToolBar() {
75      JToolBar bar = super.createToolBar();
76      bar.addSeparator();
77  
78      bar.add(cutAction);
79      bar.add(copyAction);
80      bar.add(pasteAction);
81  
82      return bar;
83    }
84  
85    public static void main(String args[])
86    {
87      initLnF();
88      ClipboardDemo demo = new ClipboardDemo();
89      demo.start(demo.getClass().getName());
90    }
91  
92  }
93  
94      
95  
96        
97