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.textEditor.actions;
13  
14  import org.eclipse.swt.dnd.Clipboard;
15  import org.eclipse.swt.dnd.TextTransfer;
16  import org.eclipse.ui.ISharedImages;
17  import org.eclipse.ui.IWorkbenchPart;
18  import org.eclipse.ui.PlatformUI;
19  import org.w3c.dom.Node;
20  
21  import sk.baka.xml.gene.ExportException;
22  import sk.uniba.euromath.editor.textEditor.actions.lang.Messages;
23  import sk.uniba.euromath.editor.xmlEditor.actions.InsertAction;
24  import sk.uniba.euromath.editor.xmlEditor.actions.InsertPosition;
25  
26  /***
27   * @author Tomáš Studva 8.9.2006
28   */
29  public class PasteTextAction extends InsertAction {
30  
31          /***
32           * Id of action.
33           */
34          public static final String id = PasteTextAction.class.toString();
35  
36          /***
37           * Default constructor. Insertion is at caret.
38           * 
39           * @param part
40           *                associated workbench part
41           */
42          public PasteTextAction(IWorkbenchPart part) {
43                  super(InsertPosition.AtCaret, part);
44                  setId(id);
45                  setText(Messages.getString("PasteTextAction.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 PasteTextAction(InsertPosition position, IWorkbenchPart part) {
63                  super(position, part);
64                  setId(id);
65                  setText(Messages.getString("PasteTextAction.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 text
84                  Object text = new Clipboard(getShell().getDisplay())
85                                  .getContents(TextTransfer.getInstance());
86                  if (text == null)
87                          return false;
88                  return getModifyHelper().canInsertTextAt(getPointer(),
89                                  (String) text) == null;
90          }
91  
92          @Override
93          public void runAsCommand() {
94                  Clipboard clipboard = new Clipboard(getShell().getDisplay());
95                  String text = (String) new Clipboard(getShell().getDisplay())
96                                  .getContents(TextTransfer.getInstance());
97                  clipboard.dispose();
98  
99                  try {
100                         getModifyHelper().insertTextAt(getShell(),
101                                         getPointer(), text, Node.TEXT_NODE);
102                 } catch (ExportException e) {
103                         handleExportException(e);
104                 }
105         }
106 }