1 | package de.uka.ipd.sdq.pcm.dialogs.datatype; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import org.eclipse.emf.common.util.EList; |
7 | import org.eclipse.emf.common.util.URI; |
8 | import org.eclipse.emf.ecore.EReference; |
9 | import org.eclipse.emf.ecore.resource.Resource; |
10 | import org.eclipse.emf.edit.provider.ComposedAdapterFactory; |
11 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
12 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
13 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
14 | import org.eclipse.emf.transaction.util.TransactionUtil; |
15 | import org.eclipse.swt.events.SelectionEvent; |
16 | import org.eclipse.swt.widgets.Composite; |
17 | import org.eclipse.swt.widgets.Shell; |
18 | |
19 | import de.uka.ipd.sdq.pcm.dialogs.Messages; |
20 | import de.uka.ipd.sdq.pcm.dialogs.parameters.CreateEditorContents; |
21 | import de.uka.ipd.sdq.pcm.dialogs.parameters.UpDownButtonsValidator; |
22 | import de.uka.ipd.sdq.pcm.repository.CollectionDataType; |
23 | import de.uka.ipd.sdq.pcm.repository.CompositeDataType; |
24 | import de.uka.ipd.sdq.pcm.repository.DataType; |
25 | import de.uka.ipd.sdq.pcm.repository.InnerDeclaration; |
26 | import de.uka.ipd.sdq.pcm.repository.Repository; |
27 | import de.uka.ipd.sdq.pcm.repository.provider.RepositoryItemProviderAdapterFactory; |
28 | import de.uka.ipd.sdq.pcmbench.ui.provider.PalladioItemProviderAdapterFactory; |
29 | |
30 | /** |
31 | * The class initialize the DataTypeDialog with the Palladio Component Model |
32 | * specific characteristics. |
33 | * |
34 | * @author Roman Andrej |
35 | */ |
36 | public class PalladioDataTypeDialog extends DataTypeDialog { |
37 | |
38 | private final String UNNAMED_REPOSITORY = "<Unnamed Repository>"; |
39 | |
40 | private ComposedAdapterFactory adapterFactory; |
41 | |
42 | private DataType innerDataType; |
43 | private DataType editedDataType; |
44 | private CreateEditorContents editorContents; |
45 | |
46 | private Repository editedRepository; |
47 | private CompositeDataType compositeDataType; |
48 | |
49 | /** |
50 | * The transactional editing domain which is used to get the commands and |
51 | * alter the model |
52 | */ |
53 | private TransactionalEditingDomain editingDomain = null; |
54 | |
55 | public PalladioDataTypeDialog(Shell parentShell, |
56 | TransactionalEditingDomain editingDomain) { |
57 | super(parentShell); |
58 | this.editingDomain = editingDomain; |
59 | } |
60 | |
61 | public PalladioDataTypeDialog(Shell parentShell, DataType editeDataType) { |
62 | super(parentShell); |
63 | this.editingDomain = TransactionUtil.getEditingDomain(editeDataType); |
64 | this.editedDataType = editeDataType; |
65 | initDialog(editeDataType); |
66 | } |
67 | |
68 | /** call if datatype set (edite button) */ |
69 | private void initDialog(DataType editeDataType) { |
70 | |
71 | String entityName; |
72 | String entityInnerType; |
73 | String repository; |
74 | |
75 | if (editeDataType instanceof CollectionDataType) { |
76 | CollectionDataType collectionDataType = (CollectionDataType) editeDataType; |
77 | |
78 | entityName = collectionDataType.getEntityName(); |
79 | repository = collectionDataType.getRepository__DataType() |
80 | .getEntityName(); |
81 | |
82 | /** |
83 | * PalladioLabelProvider - representation a inner DataType name whit |
84 | * Palladio look |
85 | */ |
86 | entityInnerType = ParameterRepresentation |
87 | .dataTypeToString(collectionDataType |
88 | .getInnerType_CollectionDataType()); |
89 | |
90 | // create DataTypeDialog |
91 | create(); |
92 | // Call constructor of DataTypeDialog |
93 | super.init(DataTypeEnum.COLLECTION, repository, entityName, |
94 | entityInnerType); |
95 | } |
96 | |
97 | if (editeDataType instanceof CompositeDataType) { |
98 | compositeDataType = (CompositeDataType) editeDataType; |
99 | |
100 | entityName = compositeDataType.getEntityName(); |
101 | repository = compositeDataType.getRepository__DataType() |
102 | .getEntityName(); |
103 | // create DataTypeDialog |
104 | create(); |
105 | // Call constructor of DataTypeDialog |
106 | super.init(DataTypeEnum.COMPOSITE, repository, entityName, null); |
107 | } |
108 | } |
109 | |
110 | /* (non-Javadoc) |
111 | * @see de.uka.ipd.sdq.pcmbench.tabs.dialog.CreateDataTypeDialog#getLoadedReposetorys() |
112 | */ |
113 | @Override |
114 | public String[] getLoadedRepositories() { |
115 | EList<Resource> resources = editingDomain.getResourceSet() |
116 | .getResources(); |
117 | |
118 | List<String> tList = new ArrayList<String>(); |
119 | |
120 | for (Resource r : resources) { |
121 | URI uri = r.getURI(); |
122 | if (hasRepositoryExtension(uri) |
123 | && !isPrimitiveTypesRepository(uri) |
124 | && (!r.getContents().isEmpty() && r.getContents().get(0) instanceof Repository)) { |
125 | Repository repository = (Repository) r.getContents().get(0); |
126 | String repositoryName = repository.getEntityName(); |
127 | tList.add(repositoryName == null ? UNNAMED_REPOSITORY |
128 | : repositoryName); |
129 | } |
130 | } |
131 | // convert to String[] |
132 | return (String[]) tList.toArray(new String[tList.size()]); |
133 | } |
134 | |
135 | private boolean hasRepositoryExtension(URI uri) { |
136 | if (uri.fileExtension().equals("repository")) |
137 | return true; |
138 | return false; |
139 | } |
140 | |
141 | private boolean isPrimitiveTypesRepository(URI uri) { |
142 | String exp = "/PrimitiveTypes.repository"; |
143 | if (uri.path().endsWith(exp)) |
144 | return true; |
145 | return false; |
146 | } |
147 | |
148 | /* (non-Javadoc) |
149 | * @see de.uka.ipd.sdq.pcmbench.tabs.dialogs.CreateDataTypeDialog#setEditedResource(java.lang.String) |
150 | */ |
151 | @Override |
152 | public void setEditedResource(String repositoryName) { |
153 | EList<Resource> resources = editingDomain.getResourceSet() |
154 | .getResources(); |
155 | |
156 | // Provide a list with loaded resources without primitive DataType |
157 | for (Resource r : resources) { |
158 | if (!r.getContents().isEmpty() |
159 | && r.getContents().get(0) instanceof Repository) { |
160 | Repository repository = (Repository) r.getContents().get(0); |
161 | String entityName = repository.getEntityName() == null ? UNNAMED_REPOSITORY |
162 | : repository.getEntityName(); |
163 | |
164 | if (entityName.contains(repositoryName)) |
165 | editedRepository = repository; |
166 | } |
167 | } |
168 | } |
169 | |
170 | /* (non-Javadoc) |
171 | * @see de.uka.ipd.sdq.pcmbench.tabs.dialog.CreateDataTypeDialog#innerSectionCompositeDataType(org.eclipse.swt.widgets.Composite) |
172 | */ |
173 | @Override |
174 | public void createInnerSectionCompositeGroup(Composite group) { |
175 | |
176 | adapterFactory = new ComposedAdapterFactory(); |
177 | adapterFactory |
178 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
179 | |
180 | editorContents = CreateEditorContents.create(group); |
181 | editorContents |
182 | .setViewerContentProvider(new AdapterFactoryContentProvider( |
183 | adapterFactory)); |
184 | editorContents |
185 | .setViewerLabelProvider(new AdapterFactoryLabelProvider( |
186 | new InnerDeclarationItemProviderAdapterFactory( |
187 | new PalladioItemProviderAdapterFactory( |
188 | adapterFactory)))); |
189 | editorContents.setViewerCellModifier(new InnerDeclarationCellModifier( |
190 | this, editingDomain)); |
191 | editorContents.createNameColumnCellEditor(); |
192 | editorContents.createTypeColumnCellEditor(editingDomain); |
193 | editorContents |
194 | .setAddButtonActionListener(new AddInnerDeclarationAction(this, |
195 | editingDomain)); |
196 | |
197 | DeleteInnerDeclarationAction deleteInnerDeclarationAction = new DeleteInnerDeclarationAction( |
198 | this, editingDomain); |
199 | UpInnerDeclarationAction upInnerDeclarationAction = new UpInnerDeclarationAction( |
200 | this, editingDomain); |
201 | DownInnerDeclarationAction downInnerDeclarationAction = new DownInnerDeclarationAction( |
202 | this, editingDomain); |
203 | |
204 | editorContents |
205 | .setDeleteButtonActionListener(deleteInnerDeclarationAction); |
206 | editorContents.setUpButtonActionListener(upInnerDeclarationAction); |
207 | editorContents.setDownButtonActionListener(downInnerDeclarationAction); |
208 | /** set SelectionChangedListener for viewer on the EditorContents */ |
209 | editorContents |
210 | .setViewerSelectionChangedListener(deleteInnerDeclarationAction); |
211 | editorContents |
212 | .setViewerSelectionChangedListener(upInnerDeclarationAction); |
213 | editorContents |
214 | .setViewerSelectionChangedListener(downInnerDeclarationAction); |
215 | editorContents.setViewerInput(editedDataType); |
216 | |
217 | } |
218 | |
219 | /* (non-Javadoc) |
220 | * @see de.uka.ipd.sdq.pcmbench.tabs.dialogs.CreateDataTypeDialog#defeniereActionTypeButton(org.eclipse.swt.events.SelectionEvent) |
221 | */ |
222 | @Override |
223 | public String getSelectedInnerType(SelectionEvent e) { |
224 | |
225 | String selectedType = "null"; |
226 | |
227 | ArrayList<Object> filterList = new ArrayList<Object>(); |
228 | filterList.add(DataType.class); |
229 | filterList.add(Repository.class); |
230 | |
231 | CallDataTypeDialog dialog = new CallDataTypeDialog(e.display |
232 | .getActiveShell(), filterList, new ArrayList<EReference>(), |
233 | editingDomain.getResourceSet()); |
234 | dialog.setProvidedService(DataType.class); |
235 | dialog.open(); |
236 | |
237 | if (dialog.getResult() != null |
238 | && dialog.getResult() instanceof DataType) { |
239 | innerDataType = (DataType) dialog.getResult(); |
240 | |
241 | selectedType = ParameterRepresentation |
242 | .dataTypeToString(innerDataType); |
243 | } |
244 | return selectedType; |
245 | } |
246 | |
247 | /* (non-Javadoc) |
248 | * @see de.uka.ipd.sdq.dialogs.datatype.DataTypeDialog#validateCompositeDataType() |
249 | */ |
250 | @Override |
251 | public boolean validateCompositeDataType() { |
252 | boolean state = true; |
253 | |
254 | if (compositeDataType == null |
255 | || compositeDataType.getInnerDeclaration_CompositeDataType() |
256 | .isEmpty()) { |
257 | setErrorMessage(Messages.DataTypeDialog_ErrorMsgInner); |
258 | return false; |
259 | } else { |
260 | EList<InnerDeclaration> declarations = compositeDataType |
261 | .getInnerDeclaration_CompositeDataType(); |
262 | for (InnerDeclaration declaration : declarations) { |
263 | state &= UpDownButtonsValidator.getSingelton() |
264 | .validdateDeclarationInnerDataType(declaration, this); |
265 | } |
266 | } |
267 | return state; |
268 | } |
269 | |
270 | /* (non-Javadoc) |
271 | * @see de.uka.ipd.sdq.pcmbench.tabs.dialogs.CreateDataTypeDialog#createCollectionDataType() |
272 | */ |
273 | @Override |
274 | public void createCollectionDataType() { |
275 | new DataTypeCommand(editingDomain).createCollectionDataType( |
276 | editedRepository, editedDataType, innerDataType, |
277 | getEntityName()); |
278 | } |
279 | |
280 | /* (non-Javadoc) |
281 | * @see de.uka.ipd.sdq.pcmbench.tabs.dialogs.CreateDataTypeDialog#createCompositeDataType() |
282 | */ |
283 | @Override |
284 | public void createCompositeDataType() { |
285 | new DataTypeCommand(editingDomain).createCompositeDataType( |
286 | editedRepository, compositeDataType, getEntityName()); |
287 | } |
288 | |
289 | public DataType getEditedDataType() { |
290 | return editedDataType; |
291 | } |
292 | |
293 | /** |
294 | * @return the compositeDataType |
295 | */ |
296 | public CompositeDataType getCompositeDataType() { |
297 | return compositeDataType; |
298 | } |
299 | |
300 | /** |
301 | * @param compositeDataType |
302 | * the compositeDataType to set |
303 | */ |
304 | public void setCompositeDataType(CompositeDataType compositeDataType) { |
305 | this.compositeDataType = compositeDataType; |
306 | } |
307 | |
308 | /** |
309 | * @return the editorContents |
310 | */ |
311 | public CreateEditorContents getEditorContents() { |
312 | return editorContents; |
313 | } |
314 | |
315 | public void refresh() { |
316 | editorContents.getViewer().refresh(); |
317 | validateInput(); |
318 | } |
319 | } |