1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.usage.helper; |
5 | |
6 | import java.util.ArrayList; |
7 | |
8 | import org.eclipse.emf.ecore.EObject; |
9 | import org.eclipse.emf.ecore.EReference; |
10 | import org.eclipse.emf.edit.provider.ComposedAdapterFactory; |
11 | import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory; |
12 | import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory; |
13 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
14 | import org.eclipse.gmf.runtime.common.core.command.ICommand; |
15 | import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand; |
16 | import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice; |
17 | import org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelperAdvice; |
18 | import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
19 | import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
20 | import org.eclipse.ui.PlatformUI; |
21 | |
22 | import de.uka.ipd.sdq.pcm.dialogs.datatype.DataTypeContainer; |
23 | import de.uka.ipd.sdq.pcm.dialogs.datatype.InnerDeclarationContainer; |
24 | import de.uka.ipd.sdq.pcm.dialogs.selection.PalladioSelectEObjectDialog; |
25 | import de.uka.ipd.sdq.pcm.dialogs.variableusage.VariableUsageInputParameterContentProvider; |
26 | import de.uka.ipd.sdq.pcm.dialogs.variableusage.VariableUsageItemProviderAdapterFactory; |
27 | import de.uka.ipd.sdq.pcm.parameter.ParameterPackage; |
28 | import de.uka.ipd.sdq.pcm.repository.InnerDeclaration; |
29 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
30 | import de.uka.ipd.sdq.pcm.repository.Repository; |
31 | import de.uka.ipd.sdq.pcm.repository.Signature; |
32 | import de.uka.ipd.sdq.pcm.repository.provider.RepositoryItemProviderAdapterFactory; |
33 | import de.uka.ipd.sdq.pcm.usagemodel.EntryLevelSystemCall; |
34 | import de.uka.ipd.sdq.pcmbench.ui.provider.PalladioItemProviderAdapterFactory; |
35 | import de.uka.ipd.sdq.stoex.AbstractNamedReference; |
36 | import de.uka.ipd.sdq.stoex.NamespaceReference; |
37 | import de.uka.ipd.sdq.stoex.StoexFactory; |
38 | |
39 | /** |
40 | * @author admin |
41 | * |
42 | */ |
43 | public class VariableUsageEditHelperAdvice extends AbstractEditHelperAdvice |
44 | implements IEditHelperAdvice { |
45 | |
46 | /* (non-Javadoc) |
47 | * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#getAfterConfigureCommand(org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest) |
48 | */ |
49 | @Override |
50 | protected ICommand getAfterConfigureCommand(ConfigureRequest request) { |
51 | EObject resource = null; |
52 | EntryLevelSystemCall systemCall = getCall(request.getElementToConfigure()); |
53 | |
54 | ArrayList<Object> filterList = new ArrayList<Object>(); |
55 | filterList.add(Repository.class); |
56 | filterList.add(Signature.class); |
57 | |
58 | ArrayList<EReference> additionalReferences = new ArrayList<EReference>(); |
59 | PalladioSelectEObjectDialog dialog = new PalladioSelectEObjectDialog( |
60 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), |
61 | filterList, |
62 | additionalReferences, |
63 | systemCall.getOperationSignature__EntryLevelSystemCall()); |
64 | /** |
65 | * set a ContentProvider for dialog TreeViewer |
66 | */ |
67 | dialog.setViewerContentProvider(new VariableUsageInputParameterContentProvider()); |
68 | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(); |
69 | adapterFactory |
70 | .addAdapterFactory(new ResourceItemProviderAdapterFactory()); |
71 | adapterFactory |
72 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
73 | adapterFactory |
74 | .addAdapterFactory(new ReflectiveItemProviderAdapterFactory()); |
75 | /** |
76 | * set a LabelProvider for dialog TreeViewer |
77 | */ |
78 | dialog |
79 | .setViewerLabelProvider(new AdapterFactoryLabelProvider( |
80 | new VariableUsageItemProviderAdapterFactory( |
81 | new PalladioItemProviderAdapterFactory( |
82 | adapterFactory)))); |
83 | dialog.open(); |
84 | |
85 | if (dialog.getResult() == null) |
86 | return new CanceledCommand(); |
87 | if (!(dialog.getResult() instanceof DataTypeContainer) |
88 | && !(dialog.getResult() instanceof InnerDeclarationContainer) |
89 | && !(dialog.getResult() instanceof Parameter)) |
90 | return new CanceledCommand(); |
91 | |
92 | resource = (EObject) dialog.getResult(); |
93 | |
94 | AbstractNamedReference namedReference = setNamedReference(resource, null, true); |
95 | |
96 | ICommand cmd = new SetValueCommand(new SetRequest(request |
97 | .getElementToConfigure(), ParameterPackage.eINSTANCE |
98 | .getVariableUsage_NamedReference__VariableUsage(), |
99 | namedReference)); |
100 | return cmd; |
101 | } |
102 | |
103 | /** |
104 | * @return - String: specification of VariableCharacterisation |
105 | */ |
106 | private AbstractNamedReference setNamedReference(EObject object, |
107 | AbstractNamedReference namedReference, boolean last) { |
108 | /** |
109 | * Parameter |
110 | */ |
111 | if (object instanceof Parameter) { |
112 | Parameter parameter = (Parameter) object; |
113 | AbstractNamedReference parameterReference = referenceFactory( |
114 | parameter.getParameterName(), last); |
115 | if (!last) { |
116 | ((NamespaceReference) parameterReference) |
117 | .setInnerReference_NamespaceReference(namedReference); |
118 | } |
119 | return parameterReference; |
120 | } |
121 | |
122 | /** |
123 | * TreeType container for CollectionDataType |
124 | * |
125 | * @see de.uka.ipd.sdq.pcm.gmf.seff.helper.VariableUsageContentProvider.TreeType |
126 | */ |
127 | if (object instanceof DataTypeContainer) { |
128 | DataTypeContainer treeType = (DataTypeContainer) object; |
129 | if (treeType.getParent() != null) { |
130 | AbstractNamedReference treetypeReference = referenceFactory( |
131 | "INNER", last); |
132 | if (!last) { |
133 | ((NamespaceReference) treetypeReference) |
134 | .setInnerReference_NamespaceReference(namedReference); |
135 | } |
136 | return setNamedReference((EObject) treeType.getParent(), |
137 | treetypeReference, false); |
138 | } |
139 | } |
140 | |
141 | /** |
142 | * TreeDeclaration container for InnerDeclaration |
143 | * |
144 | * @see de.uka.ipd.sdq.pcm.gmf.seff.helper.VariableUsageContentProvider.TreeDeclaration |
145 | */ |
146 | if (object instanceof InnerDeclarationContainer) { |
147 | InnerDeclarationContainer treeDeclaration = (InnerDeclarationContainer) object; |
148 | InnerDeclaration declaration = (InnerDeclaration) treeDeclaration |
149 | .getObject(); |
150 | |
151 | AbstractNamedReference declarationReference = referenceFactory( |
152 | declaration.getEntityName(), last); |
153 | if (!last) { |
154 | ((NamespaceReference) declarationReference) |
155 | .setInnerReference_NamespaceReference(namedReference); |
156 | } |
157 | return setNamedReference((EObject) treeDeclaration.getParent(), |
158 | declarationReference, false); |
159 | } |
160 | |
161 | return namedReference; |
162 | } |
163 | |
164 | /** |
165 | * Create the AbstractNamedReference and set a string parameter |
166 | */ |
167 | private AbstractNamedReference referenceFactory(String string, |
168 | boolean shouldGenerateVariableReference) { |
169 | AbstractNamedReference parameterReference = null; |
170 | if (shouldGenerateVariableReference) { |
171 | parameterReference = StoexFactory.eINSTANCE |
172 | .createVariableReference(); |
173 | } else { |
174 | parameterReference = StoexFactory.eINSTANCE |
175 | .createNamespaceReference(); |
176 | } |
177 | parameterReference.setReferenceName(string); |
178 | return parameterReference; |
179 | } |
180 | |
181 | private EntryLevelSystemCall getCall(EObject a) { |
182 | EObject container = a; |
183 | while (!(container instanceof EntryLevelSystemCall)) |
184 | container = container.eContainer(); |
185 | if (!(container instanceof EntryLevelSystemCall)) |
186 | return null; |
187 | return (EntryLevelSystemCall) container; |
188 | } |
189 | } |