blob: 290ef225ebab8e8ff34e9c2b0ed10544aa732b9c (
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
|
/*******************************************************************************
* Copyright (c) 2009, 2014 THALES GLOBAL SERVICES and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.diagram.ui.edit.internal.part;
import java.util.Collections;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.tools.api.layout.PinHelper;
import org.eclipse.sirius.diagram.tools.internal.commands.PinElementsCommand;
import org.eclipse.sirius.diagram.ui.edit.api.part.IDiagramElementEditPart;
import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin;
import org.eclipse.sirius.diagram.ui.tools.api.command.GMFCommandWrapper;
import org.eclipse.sirius.diagram.ui.tools.internal.preferences.SiriusDiagramUiInternalPreferencesKeys;
/**
* Common operations for edit parts.
*
* @author mchauvin
*/
public final class CommonEditPartOperation {
/**
* Avoid instantiation.
*/
private CommonEditPartOperation() {
}
/**
* Appends a "pin element" command to the normally returned command if
* appropriate.
*
* @param self
* the edit part
* @param request
* the request which was sent to the edit part
* @param cmd
* the normal command the edit-part returned for the request.
* @return a command which execute <code>cmd</code> and then optionally
* marks the edit-part as pinned if this is an interactive move and
* auto-pin-on-move is enabled.
*/
public static Command handleAutoPinOnInteractiveMove(final IDiagramElementEditPart self, final Request request, final Command cmd) {
Command result = cmd;
EObject semanticElement = self.resolveSemanticElement();
if (semanticElement instanceof DDiagramElement) {
DDiagramElement dDiagramElement = (DDiagramElement) semanticElement;
if (RequestConstants.REQ_MOVE.equals(request.getType()) && !new PinHelper().isPinned(dDiagramElement) && CommonEditPartOperation.autoPinOnMoveEnabled()
&& CommonEditPartOperation.isInteractiveMove()) {
if (PinHelper.allowsPinUnpin(dDiagramElement)) {
CompoundCommand cc = new CompoundCommand();
cc.add(cmd);
PinElementsCommand emfCommand = new PinElementsCommand(Collections.singleton(dDiagramElement));
Command pinCmd = new ICommandProxy(new GMFCommandWrapper(self.getEditingDomain(), emfCommand));
cc.add(pinCmd);
result = cc.unwrap();
}
}
}
return result;
}
private static boolean autoPinOnMoveEnabled() {
return DiagramUIPlugin.getPlugin().getPreferenceStore().getBoolean(SiriusDiagramUiInternalPreferencesKeys.PREF_AUTO_PIN_ON_MOVE.name());
}
/*
* WARNING. This code tries to detect interactive moves, as opposed to move
* requests initiated from automatic layout. It is ugly, brittle, and
* inefficient, but it seems to work and I do not see any other way to do
* this. Another approach might be to use custom drag trackers to set a
* flag, but these things are complex and touching them seems like it could
* have bad side effects if it is not 100% right.
*/
private static boolean isInteractiveMove() {
final RuntimeException re = new RuntimeException();
final StackTraceElement[] stack = re.getStackTrace();
// We look at several different stack elements in an attempt to make the
// code more resilient to changes.
for (int i = 3; i < 6 && i < stack.length; i++) {
try {
if (DragTracker.class.isAssignableFrom(Class.forName(stack[i].getClassName()))) {
return true;
}
} catch (final ClassNotFoundException e) {
break;
}
}
return false;
}
}
|