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
|
package org.eclipse.papyrus.diagram.statemachine.custom.commands;
import java.util.Iterator;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest.ViewDescriptor;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationFactory;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.diagram.common.commands.SemanticAdapter;
import org.eclipse.papyrus.diagram.statemachine.custom.helpers.Zone;
import org.eclipse.papyrus.diagram.statemachine.edit.parts.RegionEditPart;
import org.eclipse.papyrus.diagram.statemachine.edit.parts.StateCompartmentEditPart;
import org.eclipse.papyrus.diagram.statemachine.edit.parts.StateNameEditPart;
import org.eclipse.papyrus.diagram.statemachine.part.UMLVisualIDRegistry;
import org.eclipse.papyrus.diagram.statemachine.providers.ElementInitializers;
import org.eclipse.papyrus.diagram.statemachine.providers.UMLElementTypes;
import org.eclipse.uml2.uml.Region;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.UMLFactory;
public class CustomFirstRegionInCompositeStateCreateElementCommand extends AbstractTransactionalCommand {
IAdaptable adaptable;
IAdaptable adaptableForDropped = null;
PreferencesHint prefHints;
CreateViewRequest.ViewDescriptor viewDescriptor;
CreateElementRequest createElementRequest;
String dropLocation = Zone.NONE;
public CustomFirstRegionInCompositeStateCreateElementCommand(IAdaptable adaptable, IAdaptable adaptableForDropped, PreferencesHint prefHints, TransactionalEditingDomain domain, String label, String dropLocation) {
super(domain, label, null);
this.adaptable = adaptable;
this.adaptableForDropped = adaptableForDropped;
this.prefHints = prefHints;
viewDescriptor = new ViewDescriptor(adaptable, prefHints);
// make sure the return object is available even before
// executing/undoing/redoing
setResult(CommandResult.newOKCommandResult(viewDescriptor));
this.dropLocation = dropLocation;
}
@Override
public boolean canExecute() {
View compartment = (View)adaptable.getAdapter(View.class);
if(compartment.getChildren().isEmpty()) {
View owner = (View)compartment.eContainer();
State state = (State)owner.getElement();
if(state.getSubmachine() == null)
return true;
return false;
}
// CHECK THIS
// else{
// ENamedElement namedElement = PackageUtil.getElement("notation.View.visible");
// if(ViewUtil.getStructuralFeatureValue(compartment, (EStructuralFeature)namedElement).equals(new Boolean(false))){
// SetPropertyCommand showCompartment = new SetPropertyCommand(getEditingDomain(), adaptable, "notation.View.visible", "Visibility", true);
// showCompartment.setOptions(Collections.singletonMap(Transaction.OPTION_UNPROTECTED, Boolean.TRUE));
//
// try {
// showCompartment.execute(null, null);
// } catch (ExecutionException e) {
// }
//
// }
// return false;
// }
return false;
}
protected void doConfigure(Region newElement, IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
IElementType elementType = createElementRequest.getElementType();
ConfigureRequest configureRequest = new ConfigureRequest(getEditingDomain(), newElement, elementType);
configureRequest.setClientContext(createElementRequest.getClientContext());
configureRequest.addParameters(createElementRequest.getParameters());
ICommand configureCommand = elementType.getEditCommand(configureRequest);
if(configureCommand != null && configureCommand.canExecute()) {
configureCommand.execute(monitor, info);
}
}
@Override
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
// adapt the view at execution time
View compartment = (View)adaptable.getAdapter(View.class);
View ownerView = (View)compartment.eContainer();
// get state bounds
int height = Zone.getHeight(ownerView);
int width = Zone.getWidth(ownerView);
if(height < Zone.defaultHeight) {
height = Zone.defaultHeight;
Zone.setHeight(ownerView, height);
}
if(width < Zone.defaultWidth) {
width = Zone.defaultWidth;
Zone.setWidth(ownerView, width);
}
if(adaptableForDropped == null) {
Region umlRegion = UMLFactory.eINSTANCE.createRegion();
createElementRequest = new CreateElementRequest(getEditingDomain(), ownerView, UMLElementTypes.Region_3000);
State umlState = (State)ownerView.getElement();
umlState.getRegions().add(umlRegion);
ElementInitializers.getInstance().init_Region_3000(umlRegion);
doConfigure(umlRegion, monitor, info);
adaptableForDropped = new SemanticAdapter(umlRegion, null);
}
// create a view for the new region on the stateMachineCompartment
String semanticHint = ((IHintedType)UMLElementTypes.Region_3000).getSemanticHint();
View newRegion = (View)ViewService.getInstance().createNode(adaptableForDropped, compartment, semanticHint, -1, prefHints);
// add region specific annotation
Zone.createRegionDefaultAnnotation(newRegion);
// adjust bounds and zone
Iterator<Node> it = ownerView.getChildren().iterator();
while(it.hasNext()) {
Node currentNode = it.next();
if(currentNode.getLayoutConstraint() == null) {
currentNode.setLayoutConstraint(NotationFactory.eINSTANCE.createBounds());
}
if(UMLVisualIDRegistry.getVisualID(currentNode.getType()) == StateNameEditPart.VISUAL_ID) {
Zone.setWidth(currentNode, width);
Zone.setHeight(currentNode, Zone.defaultHeader);
} else if(UMLVisualIDRegistry.getVisualID(currentNode.getType()) == StateCompartmentEditPart.VISUAL_ID) {
Zone.setY(currentNode, Zone.defaultHeader);
Zone.setWidth(currentNode, width);
Zone.setHeight(currentNode, height - Zone.defaultHeader);
Iterator<Node> subit = currentNode.getChildren().iterator();
while(subit.hasNext()) {
Node subCurrentNode = subit.next();
if(subCurrentNode.getLayoutConstraint() == null) {
subCurrentNode.setLayoutConstraint(NotationFactory.eINSTANCE.createBounds());
}
if(UMLVisualIDRegistry.getVisualID(subCurrentNode.getType()) == RegionEditPart.VISUAL_ID) {
Zone.setWidth(subCurrentNode, width);
Zone.setHeight(subCurrentNode, height - Zone.defaultHeader);
}
}
}
}
viewDescriptor.setView(newRegion);
return CommandResult.newOKCommandResult(viewDescriptor);
}
}
|