1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.xmlEditor.actions;
13
14 import org.eclipse.swt.dnd.Clipboard;
15 import org.eclipse.ui.ISharedImages;
16 import org.eclipse.ui.IWorkbenchPart;
17 import org.eclipse.ui.PlatformUI;
18 import org.eclipse.ui.actions.ActionFactory;
19 import org.w3c.dom.DocumentFragment;
20
21 import sk.baka.xml.gene.ExportException;
22 import sk.uniba.euromath.editor.textEditor.actions.lang.Messages;
23
24 /***
25 * Paste action to insert at position.
26 *
27 * @author Tomáš Studva 8.7.2005
28 */
29 public class PasteAction extends InsertAction {
30
31 /***
32 * Id of action.
33 */
34 public static final String id = ActionFactory.PASTE.getId();
35
36 /***
37 * Default constructor. Insertion position is before.
38 *
39 * @param part
40 * associated workbench part
41 */
42 public PasteAction(IWorkbenchPart part) {
43 super(InsertPosition.Before, part);
44 setId(id);
45 setText(Messages.getString("PasteAction.Text"));
46 ISharedImages sharedImages = PlatformUI.getWorkbench()
47 .getSharedImages();
48 setImageDescriptor(sharedImages
49 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
50 setDisabledImageDescriptor(sharedImages
51 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED));
52 }
53
54 /***
55 * Constructor.
56 *
57 * @param part
58 * associated workbench part
59 * @param position
60 * place where to insert node
61 */
62 public PasteAction(InsertPosition position, IWorkbenchPart part) {
63 super(position, part);
64 setId(id);
65 setText(Messages.getString("PasteAction.Text"));
66 ISharedImages sharedImages = PlatformUI.getWorkbench()
67 .getSharedImages();
68 setImageDescriptor(sharedImages
69 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
70 setDisabledImageDescriptor(sharedImages
71 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED));
72 }
73
74 /***
75 * Enabled if cursor is active in EditPart with id.
76 */
77 @Override
78 protected boolean calculateEnabled() {
79 if (!super.calculateEnabled())
80 return false;
81 if (getPointer() == null)
82 return false;
83
84 Object docFragment = new Clipboard(getShell().getDisplay())
85 .getContents(CopyAction.docFragmentTransfer);
86 if (docFragment == null)
87 return false;
88 return getModifyHelper().canInsertFragment(getPointer(),
89 (DocumentFragment) docFragment) == null;
90 }
91
92 @Override
93 public void runAsCommand() {
94 Clipboard clipboard = new Clipboard(getShell().getDisplay());
95 DocumentFragment docfragment = (DocumentFragment) clipboard
96 .getContents(CopyAction.docFragmentTransfer);
97 clipboard.dispose();
98
99 try {
100 getModifyHelper().insertFragment(getShell(),
101 getPointer(), docfragment);
102 } catch (ExportException e) {
103 handleExportException(e);
104 }
105 }
106
107 }