View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2008, Ontotext Lab. / SIRMA Group
4   
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2008</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.bpmo.ui.actions;
26  
27  
28  import java.util.List;
29  
30  import org.eclipse.draw2d.geometry.Point;
31  import org.eclipse.gef.EditPart;
32  import org.eclipse.gef.ui.actions.SelectionAction;
33  import org.eclipse.ui.ISharedImages;
34  import org.eclipse.ui.IWorkbenchPart;
35  import org.eclipse.ui.actions.ActionFactory;
36  import org.wsmostudio.bpmo.model.*;
37  import org.wsmostudio.bpmo.ui.editor.ClipboardManager;
38  
39  public class PasteAction extends SelectionAction {
40  
41      public PasteAction(IWorkbenchPart part) {
42          super(part);
43          setId(ActionFactory.PASTE.getId());
44          setText("Paste");
45          ISharedImages sharedImages = 
46              part.getSite().getWorkbenchWindow().getWorkbench().getSharedImages();
47          setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
48          setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED));
49      }
50  
51      @Override
52      protected boolean calculateEnabled() {
53          
54          ClipboardManager clipboard = ClipboardManager.getDefault();
55          if (false == clipboard.hasContent()) {
56              return false;
57          }
58          WorkflowEntitiesContainer target = getSelectedContainer();
59          if (target == null) {
60              return false;
61          }
62          return (target instanceof BpmoModel) == clipboard.checkContentType(ProcessNode.class);
63      }
64      
65      public void run() {
66          WorkflowEntitiesContainer modelNode = getSelectedContainer();
67          if (modelNode == null) {
68              return;
69          }
70          WorkflowEntityNode newNode = ClipboardManager.getDefault().getContent();
71          if (newNode == null) {
72              return;
73          }
74          newNode.setLocation(new Point(5, 5)); // TODO: place the content in the proper location
75          
76          if (modelNode instanceof BpmoModel 
77                  && newNode instanceof ProcessNode) {
78              ((BpmoModel)modelNode).addProcess((ProcessNode)newNode);
79          }
80          else {
81              modelNode.addWorkflow(newNode);
82          }
83          getSelectedPart().refresh();
84      }
85      
86      private WorkflowEntitiesContainer getSelectedContainer() {
87          EditPart newHost = getSelectedPart();
88          if (newHost == null) {
89              return null;
90          }
91          if (false == newHost.getModel() instanceof WorkflowEntitiesContainer) {
92              return null;
93          }
94          return (WorkflowEntitiesContainer)newHost.getModel();
95      }
96      
97      private EditPart getSelectedPart() {
98          List<?> targets = getSelectedObjects();
99          if (targets.size() == 0) {
100             return null;
101         }
102         return (EditPart)targets.get(0);
103     }
104 
105 }