1 | package de.uka.ipd.sdq.pcm.dialogs.parameters; |
2 | |
3 | import org.eclipse.core.runtime.Assert; |
4 | import org.eclipse.emf.common.util.EList; |
5 | import org.eclipse.emf.transaction.RecordingCommand; |
6 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
7 | import org.eclipse.emf.transaction.util.TransactionUtil; |
8 | import org.eclipse.swt.events.SelectionAdapter; |
9 | import org.eclipse.swt.events.SelectionEvent; |
10 | |
11 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
12 | import de.uka.ipd.sdq.pcm.repository.RepositoryFactory; |
13 | import de.uka.ipd.sdq.pcm.repository.Signature; |
14 | |
15 | /** |
16 | * The class define an action, which a new parameter for the signature adds. |
17 | * |
18 | * @author Roman Andrej |
19 | */ |
20 | public class AddParameterAction extends SelectionAdapter{ |
21 | |
22 | /** Default name of a newly created parameter. Not guaranteed to be unique. */ |
23 | private static final String DEFAULT_PARAMETER_NAME = "parameter"; |
24 | |
25 | private Signature parentSignature = null; |
26 | private String parameterName = DEFAULT_PARAMETER_NAME; |
27 | |
28 | /** |
29 | * The transactional editing domain which is used to get the commands and |
30 | * alter the model |
31 | */ |
32 | private TransactionalEditingDomain editingDomain = null; |
33 | |
34 | public AddParameterAction(Signature parentSignature) { |
35 | this.parentSignature = parentSignature; |
36 | this.editingDomain = TransactionUtil.getEditingDomain(parentSignature); |
37 | } |
38 | |
39 | /* (non-Javadoc) |
40 | * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
41 | */ |
42 | @Override |
43 | public void widgetSelected(SelectionEvent e) { |
44 | Assert.isNotNull(parentSignature); |
45 | |
46 | final EList<Parameter> parameters = ParametersUtil.getParametersOfSignature(parentSignature); |
47 | |
48 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
49 | @Override |
50 | protected void doExecute() { |
51 | Parameter parameter = RepositoryFactory.eINSTANCE |
52 | .createParameter(); |
53 | parameter.setParameterName(parameterName + parameters.size()); |
54 | parameters.add(parameter); |
55 | } |
56 | }; |
57 | |
58 | recCommand.setDescription("Add new parameter to the signature"); |
59 | editingDomain.getCommandStack().execute(recCommand); |
60 | } |
61 | } |