Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5ca5564929aab5dca961c7514802cbc502bdd629 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package org.eclipse.papyrus.uml.diagram.sequence.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.PopupMenuCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.menus.PopupMenu;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.EditHelperContext;
import org.eclipse.gmf.runtime.emf.type.core.IClientContext;
import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.AbstractEditCommandRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.CombinedFragmentEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.InteractionInteractionCompartmentEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.InteractionOperandEditPart;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.CombinedFragment;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.InteractionOperand;

public class CombinedFragmentDeleteHelper {

	public static class EditRequest extends AbstractEditCommandRequest {

		/** element to edit */
		private Element elementToEdit;

		public EditRequest(TransactionalEditingDomain editingDomain, Element elementToEdit) {
			super(editingDomain);
			this.elementToEdit = elementToEdit;
		}

		public Object getEditHelperContext() {
			IClientContext context = getClientContext();

			if(context == null) {
				return getElementToEdit();
			} else {
				return new EditHelperContext(getElementToEdit(), context);
			}
		}

		public Element getElementToEdit() {
			return elementToEdit;
		}
	}

	public static class MoveOperandFragmentsCommand extends EditElementCommand {

		private InteractionFragment parent;

		private EList<InteractionFragment> fragmentsToMove;

		private InteractionOperand sourceOperand;

		private CombinedFragment combinedFragment;

		private CombinedFragmentEditPart host;

		public MoveOperandFragmentsCommand(CombinedFragmentEditPart host, CombinedFragment cf, InteractionOperand op, EditRequest editRequest) {
			super(null, editRequest.getElementToEdit(), editRequest);
			InteractionOperand enclosingOp = cf.getEnclosingOperand();
			if(enclosingOp != null) {
				Element owner = enclosingOp.getOwner();
				if(owner instanceof CombinedFragment) {
					this.parent = enclosingOp;
				}
			} else {
				Element owner = cf.getOwner();
				if(owner instanceof Interaction) {
					this.parent = (InteractionFragment)owner;
				}
			}
			this.host = host;
			this.sourceOperand = op;
			this.combinedFragment = cf;
			fragmentsToMove = op.getFragments();
		}

		public boolean canExecute() {
			return (sourceOperand != null && combinedFragment != null);
		}

		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			if(parent != null && !fragmentsToMove.isEmpty()) {
				List<CombinedFragmentEditPart> parts = collectCombinedFragmentParts();
				if(parent instanceof InteractionOperand) {
					((InteractionOperand)parent).getFragments().addAll(fragmentsToMove);
					moveToOperandPart((InteractionOperandEditPart)host.getParent(), parts);  
				} else if(parent instanceof Interaction) {
					((Interaction)parent).getFragments().addAll(fragmentsToMove);
					moveToInteractionPart((InteractionInteractionCompartmentEditPart)host.getParent(), parts);  
				}
			}
			sourceOperand.getFragments().clear();
			return CommandResult.newOKCommandResult();
		}
		
		private void moveToOperandPart(GraphicalEditPart op, List<CombinedFragmentEditPart> keepParts) {
			if(!keepParts.isEmpty()) {
				Rectangle parentBounds = getAbsoluteBounds((AbstractGraphicalEditPart)op.getParent());
				for(CombinedFragmentEditPart cef : keepParts) {
					View view = cef.getNotationView();
					op.getNotationView().getPersistedChildren().add(view);

					Bounds bounds = (Bounds)((Shape)cef.getNotationView()).getLayoutConstraint();
					Rectangle absolute = getAbsoluteBounds(cef);
					bounds.setX(absolute.x() - parentBounds.x());
					bounds.setY(absolute.y() - parentBounds.y());
				}
			}
		}

		private void moveToInteractionPart(GraphicalEditPart op, List<CombinedFragmentEditPart> keepParts) {
			if(!keepParts.isEmpty()) {
				Rectangle b = getAbsoluteBounds(op);
				for(CombinedFragmentEditPart cef : keepParts) {
					View view = cef.getNotationView();
					op.getNotationView().getPersistedChildren().add(view);
					Bounds bounds = (Bounds)((Shape)cef.getNotationView()).getLayoutConstraint();

					Rectangle absolute = getAbsoluteBounds(cef);
					absolute.performTranslate(-b.x, -b.y);

					bounds.setX(absolute.x() - 5);
					bounds.setY(absolute.y() - 6);
				}
			}
		}

		private List<CombinedFragmentEditPart> collectCombinedFragmentParts() {
			List<CombinedFragmentEditPart> parts = new ArrayList<CombinedFragmentEditPart>();
			for(InteractionFragment f : fragmentsToMove) {
				if(f instanceof CombinedFragment) {
					EditPart p = findEditPartByModel(host, f);
					if(p instanceof CombinedFragmentEditPart)
						parts.add((CombinedFragmentEditPart)p);
				}
			}
			return parts;
		}
	}

	static public class SelectLabelProvider extends org.eclipse.jface.viewers.LabelProvider {

		public String getText(Object object) {
			return object.toString();
		}

		public Image getImage(Object object) {
			return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ETOOL_DELETE);
		}
	}

	public static class SelectAndDeleteCommand extends PopupMenuCommand {

		private String choice;

		private CompositeTransactionalCommand deletaAllCommand;

		private CompositeTransactionalCommand keepCommand;

		private String[] labels;

		public SelectAndDeleteCommand(CompositeTransactionalCommand deletaAllCommand, CompositeTransactionalCommand keepCommand, String[] labels) {
			super("prompt for delete", Display.getCurrent().getActiveShell());
			PopupMenu popupMenu = new PopupMenu(Arrays.asList(labels), new SelectLabelProvider());
			setPopupMenu(popupMenu);
			this.labels = labels;
			this.deletaAllCommand = deletaAllCommand;
			this.keepCommand = keepCommand;
		}

		protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, org.eclipse.core.runtime.IAdaptable info) throws ExecutionException {
			CommandResult cmdResult = super.doExecuteWithResult(progressMonitor, info);
			if(!cmdResult.getStatus().isOK()) {
				return cmdResult;
			}
			this.choice = cmdResult.getReturnValue().toString();
			if(choice.contains(labels[0])) {
				deletaAllCommand.execute(progressMonitor, info);
				return deletaAllCommand.getCommandResult();
			} else {
				keepCommand.execute(progressMonitor, info);
				return keepCommand.getCommandResult();
			}
		}

		protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			if(choice.contains(labels[0])) {
				deletaAllCommand.undo(progressMonitor, info);
				return deletaAllCommand.getCommandResult();
			} else {
				keepCommand.undo(progressMonitor, info);
				return keepCommand.getCommandResult();
			}
		}

		protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			if(choice.contains(labels[0])) {
				deletaAllCommand.redo(progressMonitor, info);
				return deletaAllCommand.getCommandResult();
			} else {
				keepCommand.redo(progressMonitor, info);
				return keepCommand.getCommandResult();
			}
		}
	}

	static class DeleteCombinedFragmentCommand extends AbstractTransactionalCommand {

		private IElementEditService provider;

		private DestroyElementRequest req;

		public DeleteCombinedFragmentCommand(TransactionalEditingDomain domain, IElementEditService provider, DestroyElementRequest req) {
			super(domain, null, null);
			this.provider = provider;
			this.req = req;
		}

		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			ICommand deleteCommand = provider.getEditCommand(req);
			try {
				deleteCommand.execute(monitor, info);
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
			return CommandResult.newOKCommandResult();
		}
	}


	public static ICommand createDestroyElementCommand(CombinedFragment cf, TransactionalEditingDomain transactionalEditingDomain, IElementEditService provider, DestroyElementRequest req, ICommand deleteCommand, CombinedFragmentEditPart host) {
		CompositeTransactionalCommand deleteAll = new CompositeTransactionalCommand(transactionalEditingDomain, null);
		deleteAll.add(deleteCommand);

		CompositeTransactionalCommand deleteCfOnly = new CompositeTransactionalCommand(transactionalEditingDomain, null);
		{
			// remove children from operands and append to parent combined fragment
			for(InteractionOperand op : cf.getOperands()) {
				deleteCfOnly.add(new MoveOperandFragmentsCommand(host, cf, op, new EditRequest(transactionalEditingDomain, cf)));
			}

			deleteCfOnly.add(new DeleteCombinedFragmentCommand(transactionalEditingDomain, provider, req));
		}
		return new SelectAndDeleteCommand(deleteAll, deleteCfOnly, new String[]{ "Delete all", "Keep contents" });
	}

	public static ICommand createDeleteViewCommand(CombinedFragment cf, TransactionalEditingDomain transactionalEditingDomain, CombinedFragmentEditPart host) {
		CompositeTransactionalCommand deleteAll = new CompositeTransactionalCommand(transactionalEditingDomain, null);
		{
			// Get the elements associated with the CF
			List<Element> elements = SequenceUtil.getCombinedFragmentAssociatedElement(cf);
			// Create the delete view commands
			SequenceDeleteHelper.deleteView(deleteAll, elements, transactionalEditingDomain);
		}

		CompositeTransactionalCommand deleteCfOnly = new CompositeTransactionalCommand(transactionalEditingDomain, null);
		{
			// remove children from operands and append to parent combined fragment
			for(InteractionOperand op : cf.getOperands()) {
				deleteCfOnly.add(new MoveOperandFragmentsCommand(host, cf, op, new EditRequest(transactionalEditingDomain, cf)));
			}

			// delete operands from combined fragment
			for(Element element : cf.getOperands()) {
				SequenceDeleteHelper.deleteView(deleteCfOnly, element, transactionalEditingDomain);
			}
		}
		return new SelectAndDeleteCommand(deleteAll, deleteCfOnly, new String[]{ "Hide all", "Keep contents" });
	}

	static Rectangle getAbsoluteBounds(AbstractGraphicalEditPart part) {
		Rectangle b = part.getFigure().getBounds().getCopy();
		part.getFigure().translateToAbsolute(b);
		return b;
	}

	public static EditPart findEditPartByModel(EditPart part, EObject m) {
		List children = part.getChildren();
		if(children != null && children.size() > 0) {
			for(Object o : children) {
				EditPart p = (EditPart)o;

				Object model = p.getModel();
				if(model != null && model instanceof View && m.equals(((View)model).getElement()))
					return p;

				EditPart res = findEditPartByModel(p, m);
				if(res != null)
					return res;
			}
		}
		return null;
	}
}

Back to the top