View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
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")); //$NON-NLS-1$
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")); //$NON-NLS-1$
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                  // look in clipboard for doc fragment
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 }