/
CreateExtensionAction.java
126 lines (99 loc) · 3.63 KB
/
CreateExtensionAction.java
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
/*
* Copyright (c) 2005, 2018 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM - initial API and implementation
* Kenn Hussey - 286329, 535301
*
*/
package org.eclipse.uml2.uml.editor.actions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.IdentityCommand;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.celleditor.FeatureEditorDialog;
import org.eclipse.jface.action.IAction;
import org.eclipse.uml2.common.edit.command.ChangeCommand;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.editor.UMLEditorPlugin;
public class CreateExtensionAction
extends UMLCommandAction {
public CreateExtensionAction() {
super();
}
@Override
protected Command createActionCommand(EditingDomain editingDomain,
Collection<?> collection) {
if (collection.size() == 1
&& collection.iterator().next() instanceof Stereotype) {
return IdentityCommand.INSTANCE;
}
return UnexecutableCommand.INSTANCE;
}
@Override
public void run(IAction action) {
if (command != UnexecutableCommand.INSTANCE) {
final Stereotype stereotype = (Stereotype) collection.iterator()
.next();
EList<org.eclipse.uml2.uml.Class> allExtendedMetaclasses = stereotype
.getAllExtendedMetaclasses();
List<org.eclipse.uml2.uml.Class> choiceOfValues = new ArrayList<org.eclipse.uml2.uml.Class>();
Profile profile = stereotype.getProfile();
if (profile != null) {
for (Model referencedMetamodel : profile
.getReferencedMetamodels()) {
for (Type ownedType : referencedMetamodel.getOwnedTypes()) {
if (ownedType instanceof org.eclipse.uml2.uml.Class) {
org.eclipse.uml2.uml.Class ownedClass = (org.eclipse.uml2.uml.Class) ownedType;
if (ownedClass.isMetaclass()
&& !allExtendedMetaclasses.contains(ownedClass)) {
choiceOfValues.add(ownedClass);
}
}
}
}
for (org.eclipse.uml2.uml.Class referencedMetaclass : profile
.getReferencedMetaclasses()) {
if (!allExtendedMetaclasses.contains(referencedMetaclass)
&& !choiceOfValues.contains(referencedMetaclass)) {
choiceOfValues.add(referencedMetaclass);
}
}
}
Collections.<org.eclipse.uml2.uml.Class> sort(choiceOfValues,
new TextComparator<org.eclipse.uml2.uml.Class>());
String label = UMLEditorPlugin.INSTANCE
.getString("_UI_CreateExtensionActionCommand_label"); //$NON-NLS-1$
final FeatureEditorDialog dialog = new FeatureEditorDialog(
workbenchPart.getSite().getShell(), getLabelProvider(),
stereotype, UMLPackage.Literals.STEREOTYPE,
Collections.EMPTY_LIST, label, choiceOfValues, false, false,
true);
dialog.open();
if (dialog.getReturnCode() == FeatureEditorDialog.OK) {
editingDomain.getCommandStack().execute(
new ChangeCommand(editingDomain, new Runnable() {
public void run() {
for (Object result : dialog.getResult()) {
stereotype.createExtension(
(org.eclipse.uml2.uml.Class) result, false);
}
}
}, label));
}
}
}
}