1 | package de.uka.ipd.sdq.pcmbench.tabs.parameters; |
2 | |
3 | import java.util.Arrays; |
4 | import java.util.List; |
5 | |
6 | import org.eclipse.core.runtime.Assert; |
7 | import org.eclipse.emf.common.util.EList; |
8 | import org.eclipse.emf.transaction.RecordingCommand; |
9 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
10 | import org.eclipse.emf.transaction.util.TransactionUtil; |
11 | import org.eclipse.swt.SWT; |
12 | import org.eclipse.swt.widgets.TableItem; |
13 | |
14 | import de.uka.ipd.sdq.pcm.core.CoreFactory; |
15 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
16 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext; |
17 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisation; |
18 | import de.uka.ipd.sdq.pcm.parameter.VariableUsage; |
19 | import de.uka.ipd.sdq.pcm.stochasticexpressions.PCMStoExPrettyPrintVisitor; |
20 | import de.uka.ipd.sdq.pcmbench.tabs.generic.ObservableCellModifier; |
21 | |
22 | /** |
23 | * The class define a cell modifier is used to access the data model from a cell |
24 | * editor. |
25 | * |
26 | * @author Roman Andrej |
27 | */ |
28 | public class ComponentParametersCellModifier extends ObservableCellModifier { |
29 | |
30 | private List<String> columnNames; |
31 | private VariableUsageWrapper wrapper; |
32 | |
33 | /** In property view selected VariableUsage. */ |
34 | private VariableUsage variableUsage; |
35 | /** In diagram selected AssemblyContext. */ |
36 | private AssemblyContext context; |
37 | /** The value wont for print VariableUsage name uses. */ |
38 | private PCMStoExPrettyPrintVisitor print; |
39 | |
40 | /** |
41 | * The transactional editing domain which is used to get the commands and |
42 | * alter the model |
43 | */ |
44 | protected TransactionalEditingDomain editingDomain = null; |
45 | |
46 | public ComponentParametersCellModifier() { |
47 | this.columnNames = Arrays |
48 | .asList(ComponentParametersEditorSection.columnNames); |
49 | this.print = new PCMStoExPrettyPrintVisitor(); |
50 | } |
51 | |
52 | /** |
53 | * {@inheritDoc} |
54 | */ |
55 | public boolean canModify(Object element, String property) { |
56 | |
57 | int columnIndex = columnNames.indexOf(property); |
58 | |
59 | switch (columnIndex) { |
60 | case ComponentParametersEditorSection.ICON_COLUMN_INDEX: |
61 | return false; |
62 | case ComponentParametersEditorSection.VARIABLE_COLUMN_INDEX: |
63 | return false; |
64 | case ComponentParametersEditorSection.STOEX_COLUMN_INDEX: |
65 | return true; |
66 | default: |
67 | break; |
68 | } |
69 | |
70 | return true; |
71 | } |
72 | |
73 | /** |
74 | * {@inheritDoc} |
75 | */ |
76 | public Object getValue(Object element, String property) { |
77 | return (new ParametersTabItemProvider(null)).getColumnText(element, |
78 | columnNames.indexOf(property)); |
79 | } |
80 | |
81 | /** |
82 | * {@inheritDoc} |
83 | */ |
84 | public void modify(Object element, String property, Object value) { |
85 | // Find the index of the column |
86 | int columnIndex = columnNames.indexOf(property); |
87 | |
88 | Assert.isNotNull(element); |
89 | TableItem item = (TableItem) element; |
90 | |
91 | wrapper = (VariableUsageWrapper) item.getData(); |
92 | variableUsage = wrapper.getVariableUsage(); |
93 | |
94 | switch (columnIndex) { |
95 | case ComponentParametersEditorSection.ICON_COLUMN_INDEX: |
96 | break; |
97 | case ComponentParametersEditorSection.VARIABLE_COLUMN_INDEX: |
98 | break; |
99 | case ComponentParametersEditorSection.STOEX_COLUMN_INDEX: |
100 | setSpecification(((String) value).trim(), item); |
101 | break; |
102 | default: |
103 | break; |
104 | } |
105 | |
106 | } |
107 | |
108 | /** |
109 | * Update Specification of selected VariableUsage. |
110 | */ |
111 | private void setSpecification(final String value, TableItem item) { |
112 | editingDomain = TransactionUtil.getEditingDomain(variableUsage); |
113 | |
114 | VariableCharacterisation variableCharacterisation = variableUsage |
115 | .getVariableCharacterisation_VariableUsage().get(0); |
116 | |
117 | if (!variableCharacterisation |
118 | .getSpecification_VariableCharacterisation().getSpecification() |
119 | .equals(value)) { |
120 | |
121 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
122 | @Override |
123 | protected void doExecute() { |
124 | |
125 | // create/set RandomVariable |
126 | PCMRandomVariable randomVariable = CoreFactory.eINSTANCE |
127 | .createPCMRandomVariable(); |
128 | randomVariable.setSpecification(value); |
129 | |
130 | // test if VariableUsaged exist |
131 | VariableUsage existedVariableUsage = isExisted(context, |
132 | variableUsage); |
133 | |
134 | if (existedVariableUsage == null) { |
135 | VariableUsage contexVariableUsage = null; |
136 | |
137 | // create VariableUsage for AssemblyContext |
138 | ParameterUtil copier = new ParameterUtil(); |
139 | contexVariableUsage = (VariableUsage) copier |
140 | .copy(variableUsage); |
141 | copier.copyReferences(); |
142 | |
143 | // create VariableCharacterisation for AssemblyContext |
144 | VariableCharacterisation characterisation = contexVariableUsage |
145 | .getVariableCharacterisation_VariableUsage() |
146 | .get(0); |
147 | // set specification |
148 | characterisation |
149 | .setSpecification_VariableCharacterisation(randomVariable); |
150 | // add edited VariableUsage to AssemblyContext |
151 | |
152 | context.getConfigParameterUsages__AssemblyContext().add( |
153 | contexVariableUsage); |
154 | } else { |
155 | // create VariableCharacterisation for AssemblyContext |
156 | VariableCharacterisation characterisation = existedVariableUsage |
157 | .getVariableCharacterisation_VariableUsage() |
158 | .get(0); |
159 | // set specification |
160 | characterisation |
161 | .setSpecification_VariableCharacterisation(randomVariable); |
162 | |
163 | } |
164 | |
165 | } |
166 | }; |
167 | |
168 | recCommand.setLabel("Set specification"); |
169 | editingDomain.getCommandStack().execute(recCommand); |
170 | // set color for TableItem |
171 | setTableItemColor(item); |
172 | // make TableItem edited |
173 | wrapper.setEdited(true); |
174 | // update observer |
175 | notifyObservers(wrapper); |
176 | |
177 | } |
178 | } |
179 | |
180 | /** |
181 | * The Method test if VariableUsage already exist in the AssemblyContext. |
182 | * Return null if VarableUsage not exist this AssemblyContect. |
183 | * |
184 | * @param contex |
185 | * The associated AssembyContex |
186 | * @param newVariable |
187 | * The associated VariableUsage |
188 | * @return |
189 | */ |
190 | private VariableUsage isExisted(AssemblyContext context, |
191 | VariableUsage newVariable) { |
192 | EList<VariableUsage> variables = context |
193 | .getConfigParameterUsages__AssemblyContext(); |
194 | |
195 | String newName = print.prettyPrint(newVariable); |
196 | for (VariableUsage existedVariable : variables) { |
197 | String existedName = print.prettyPrint(existedVariable); |
198 | if (newName.equals(existedName)) { |
199 | return existedVariable; |
200 | } |
201 | } |
202 | return null; |
203 | } |
204 | |
205 | private void setTableItemColor(TableItem item) { |
206 | item.setForeground(item.getDisplay().getSystemColor(SWT.COLOR_BLACK)); |
207 | } |
208 | |
209 | /** |
210 | * @param context |
211 | * the context to set |
212 | */ |
213 | public void setContext(AssemblyContext context) { |
214 | this.context = context; |
215 | } |
216 | } |