1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
package org.argouml.model.euml; |
13 | |
|
14 | |
import org.eclipse.emf.common.command.Command; |
15 | |
import org.eclipse.emf.common.command.StrictCompoundCommand; |
16 | |
import org.eclipse.emf.common.util.EList; |
17 | |
import org.eclipse.emf.common.util.URI; |
18 | |
import org.eclipse.emf.ecore.resource.Resource; |
19 | |
import org.eclipse.emf.edit.command.CopyToClipboardCommand; |
20 | |
import org.eclipse.emf.edit.command.PasteFromClipboardCommand; |
21 | |
import org.eclipse.uml2.uml.Association; |
22 | |
import org.eclipse.uml2.uml.AssociationClass; |
23 | |
import org.eclipse.uml2.uml.Element; |
24 | |
import org.eclipse.uml2.uml.NamedElement; |
25 | |
import org.eclipse.uml2.uml.Operation; |
26 | |
import org.eclipse.uml2.uml.Property; |
27 | |
import org.eclipse.uml2.uml.Type; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class UMLUtil extends org.eclipse.uml2.uml.util.UMLUtil { |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public static final URI DEFAULT_URI = |
41 | |
URI.createURI("http://argouml.tigris.org/euml/resource/default_uri.xmi"); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public static EList<Property> getOwnedAttributes(Type type) { |
51 | 0 | if (type instanceof AssociationClass) { |
52 | 0 | return ((AssociationClass) type).getOwnedAttributes(); |
53 | 0 | } else if (type instanceof Association) { |
54 | 0 | return ((Association) type).getOwnedEnds(); |
55 | |
} else { |
56 | 0 | return org.eclipse.uml2.uml.util.UMLUtil.getOwnedAttributes(type); |
57 | |
} |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public static EList<Operation> getOwnedOperations(Type type) { |
68 | 0 | return org.eclipse.uml2.uml.util.UMLUtil.getOwnedOperations(type); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static Element copy(EUMLModelImplementation modelImplementation, |
83 | |
Element source, Element destination) { |
84 | 0 | Command copyToClipboard = CopyToClipboardCommand.create( |
85 | |
modelImplementation.getEditingDomain(), source); |
86 | 0 | Command pasteFromClipboard = PasteFromClipboardCommand.create( |
87 | |
modelImplementation.getEditingDomain(), destination, null); |
88 | 0 | StrictCompoundCommand copyCommand = new StrictCompoundCommand() { |
89 | |
{ |
90 | 0 | isPessimistic = true; |
91 | 0 | } |
92 | |
}; |
93 | 0 | copyCommand.append(copyToClipboard); |
94 | 0 | copyCommand.append(pasteFromClipboard); |
95 | 0 | copyCommand.setLabel("Copy a tree of UML elements to a destination"); |
96 | 0 | if (copyCommand.canExecute()) { |
97 | 0 | modelImplementation.getModelEventPump().getRootContainer().setHoldEvents( |
98 | |
true); |
99 | 0 | modelImplementation.getEditingDomain().getCommandStack().execute( |
100 | |
copyCommand); |
101 | 0 | if (modelImplementation.getEditingDomain().getCommandStack().getMostRecentCommand().getAffectedObjects().size() == 1) { |
102 | 0 | modelImplementation.getModelEventPump().getRootContainer().setHoldEvents( |
103 | |
false); |
104 | 0 | return (Element) modelImplementation.getEditingDomain().getCommandStack().getMostRecentCommand().getAffectedObjects().iterator().next(); |
105 | |
} else { |
106 | 0 | modelImplementation.getEditingDomain().getCommandStack().undo(); |
107 | 0 | modelImplementation.getModelEventPump().getRootContainer().clearHeldEvents(); |
108 | 0 | modelImplementation.getModelEventPump().getRootContainer().setHoldEvents( |
109 | |
false); |
110 | |
} |
111 | |
} |
112 | 0 | return null; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public static String toString(Object o) { |
126 | 0 | if (o == null) { |
127 | 0 | return "null"; |
128 | |
} |
129 | 0 | if (!(o instanceof Element)) { |
130 | 0 | return o.toString(); |
131 | |
} |
132 | |
|
133 | 0 | StringBuilder sb = new StringBuilder("'"); |
134 | 0 | boolean named = false; |
135 | |
|
136 | 0 | if (o instanceof NamedElement && ((NamedElement) o).getName() != null |
137 | |
&& !((NamedElement) o).getName().equals("")) { |
138 | 0 | named = true; |
139 | 0 | sb.append(((NamedElement) o).getName() + " ["); |
140 | |
} |
141 | |
|
142 | 0 | sb.append(((Element) o).eClass().getName()); |
143 | |
|
144 | 0 | if (named) { |
145 | 0 | sb.append("]"); |
146 | |
} |
147 | |
|
148 | 0 | sb.append("'"); |
149 | |
|
150 | 0 | return sb.toString(); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
static Resource getResource( |
164 | |
EUMLModelImplementation modelImplementation, URI uri, |
165 | |
boolean readOnly) { |
166 | 0 | if (!"xmi".equals(uri.fileExtension()) |
167 | |
&& !"uml".equals(uri.fileExtension())) { |
168 | |
|
169 | 0 | uri = uri.appendFileExtension("xmi"); |
170 | |
} |
171 | 0 | Resource r = modelImplementation.getEditingDomain().getResourceSet() |
172 | |
.getResource(uri, false); |
173 | 0 | if (r == null) { |
174 | 0 | r = modelImplementation.getEditingDomain().getResourceSet() |
175 | |
.createResource(uri); |
176 | |
} |
177 | 0 | if (r == null) { |
178 | 0 | throw new NullPointerException("Failed to create resource for URI " |
179 | |
+ uri); |
180 | |
} |
181 | 0 | modelImplementation.getReadOnlyMap().put(r, Boolean.valueOf(readOnly)); |
182 | 0 | return r; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
@SuppressWarnings("unchecked") |
187 | |
static void checkArgs(Object[] args, Class[] types) { |
188 | 0 | if (args.length != types.length) { |
189 | 0 | throw new IllegalArgumentException( |
190 | |
"Mismatched array lengths for args and types"); |
191 | |
} |
192 | 0 | for (int i = 0; i < args.length; i++) { |
193 | 0 | if (!types[i].isAssignableFrom(args[i].getClass())) { |
194 | 0 | throw new IllegalArgumentException( |
195 | |
"Parameter " + i + " must be of type " + types[i]); |
196 | |
} |
197 | |
} |
198 | 0 | } |
199 | |
} |