1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2007</p>
22 * <p>Company: Ontotext Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.bpmo.ui.actions;
26
27 import java.util.*;
28
29 import org.eclipse.gef.EditPart;
30 import org.eclipse.gef.ui.actions.SelectionAction;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.sbpm.bpmo.*;
33 import org.wsmostudio.bpmo.model.WorkflowEntitiesContainer;
34 import org.wsmostudio.bpmo.model.WorkflowEntityNode;
35 import org.wsmostudio.bpmo.model.connectors.*;
36 import org.wsmostudio.bpmo.model.io.BPMOImporter;
37 import org.wsmostudio.bpmo.ui.editor.BpmoEditor;
38 import org.wsmostudio.bpmo.ui.editor.layout.BPMOModelLayouter;
39 import org.wsmostudio.runtime.LogManager;
40
41 public abstract class AbstractDiagramAction extends SelectionAction {
42
43 public AbstractDiagramAction(IWorkbenchPart part) {
44 super(part);
45 }
46
47 /***
48 * Runtime check if the selected object can be processed by this action. In case of
49 * success this action appears in the context menu.
50 * @param selected
51 * @return
52 */
53 public abstract boolean isApplicableTo(Object selected);
54
55 /***
56 * Transform the selection from UI model to BPMO model
57 * @return A BPMO process fragment or <code>null</code> if there is no current selection.
58 */
59 protected Set<WorkflowEntityNode> getDiagramSelection() {
60 List<?> sel = getSelectedObjects();
61 Set<WorkflowEntityNode> uiModels = new HashSet<WorkflowEntityNode>();
62 for(Object uiPart : sel) {
63 uiModels.add((WorkflowEntityNode)((EditPart)uiPart).getModel());
64 }
65 Set<WorkflowEntityNode> rootUIModels = new HashSet<WorkflowEntityNode>(uiModels);
66 for(Iterator<WorkflowEntityNode> iter = rootUIModels.iterator(); iter.hasNext();) {
67 if (uiModels.contains(iter.next().getOwner())) {
68 iter.remove();
69 }
70 }
71 return rootUIModels;
72 }
73
74 /***
75 * Incorporates the result of the execution of this action in the diagram.
76 * Depending on the result of <code>isReplacingSelection()</code> the result either replaces
77 * the initial selection or not.
78 * @param fragment The result of the execution
79 * @return <code>True</code> if the result is incorporated successfully
80 */
81 protected boolean replaceDiagramSelection(ProcessFragment fragment) {
82
83 Set<WorkflowEntityNode> uiNodes =
84 new BPMOImporter().importProcessFragment(fragment);
85
86 WorkflowEntitiesContainer owner = null;
87 Set<GraphConnector> inConns = new HashSet<GraphConnector>();
88 Set<GraphConnector> outConns = new HashSet<GraphConnector>();
89 Set<WorkflowEntityNode> oldSelection = new HashSet<WorkflowEntityNode>();
90 for (Object uiPart : getSelectedObjects()) {
91 if (false == uiPart instanceof EditPart
92 || false == ((EditPart)uiPart).getModel() instanceof WorkflowEntityNode) {
93 continue;
94 }
95 WorkflowEntityNode oldUINode = (WorkflowEntityNode)((EditPart)uiPart).getModel();
96 oldSelection.add(oldUINode);
97 inConns.addAll(oldUINode.listInArcs());
98 outConns.addAll(oldUINode.listOutArcs());
99 if (owner == null) {
100 owner = ((WorkflowEntityNode)((EditPart)uiPart).getModel()).getOwner();
101 }
102 }
103 if (owner == null) {
104 LogManager.logError("Unable to find the container of selection " + getSelectedObjects());
105 return false;
106 }
107 for(Iterator<GraphConnector> iter = inConns.iterator(); iter.hasNext(); ) {
108 if (oldSelection.contains(iter.next().getSource())) {
109 iter.remove();
110 }
111 }
112 for(Iterator<GraphConnector> iter = outConns.iterator(); iter.hasNext(); ) {
113 if (oldSelection.contains(iter.next().getTarget())) {
114 iter.remove();
115 }
116 }
117
118 List<WorkflowEntityNode> startNodes = new LinkedList<WorkflowEntityNode>();
119 List<WorkflowEntityNode> endNodes = new LinkedList<WorkflowEntityNode>();
120 for (WorkflowEntityNode guiNode : uiNodes) {
121 if (guiNode.getOwner() == null) {
122 owner.addWorkflow(guiNode);
123 }
124 if (guiNode.listInArcs().size() == 0) {
125 startNodes.add(guiNode);
126 }
127 if (guiNode.listOutArcs().size() == 0) {
128 endNodes.add(guiNode);
129 }
130 }
131
132
133 for(GraphConnector conn : inConns) {
134 if (conn instanceof ControlflowArc) {
135 for (WorkflowEntityNode newTarget : startNodes) {
136 new ControlflowArc(conn.getSource(), null, newTarget, null);
137 }
138 }
139 conn.getSource().removeOutConnection(conn);
140 conn.getTarget().removeInConnection(conn);
141 }
142
143
144 for(GraphConnector conn : outConns) {
145 if (conn instanceof ControlflowArc) {
146 for (WorkflowEntityNode newSource : endNodes) {
147 new ControlflowArc(newSource, null, conn.getTarget(), null);
148 }
149 }
150 conn.getSource().removeOutConnection(conn);
151 conn.getTarget().removeInConnection(conn);
152 }
153
154
155 for(WorkflowEntityNode oldNode : oldSelection) {
156 oldNode.getOwner().removeWorkflow(oldNode);
157 }
158 BPMOModelLayouter.doLayout(((BpmoEditor)getWorkbenchPart()).getModel());
159 ((BpmoEditor)getWorkbenchPart()).updateView();
160 ((BpmoEditor)getWorkbenchPart()).markDirty();
161
162 return true;
163 }
164
165
166 public abstract void run();
167
168 }