1 | package de.uka.ipd.sdq.pcmbench.tabs.parameters; |
2 | |
3 | import java.util.Observable; |
4 | |
5 | import org.eclipse.core.runtime.Assert; |
6 | import org.eclipse.emf.common.util.EList; |
7 | import org.eclipse.emf.ecore.EObject; |
8 | import org.eclipse.jface.dialogs.Dialog; |
9 | import org.eclipse.jface.viewers.CellEditor; |
10 | import org.eclipse.jface.viewers.DialogCellEditor; |
11 | import org.eclipse.jface.viewers.StructuredSelection; |
12 | import org.eclipse.jface.viewers.TextCellEditor; |
13 | import org.eclipse.swt.SWT; |
14 | import org.eclipse.swt.events.SelectionListener; |
15 | import org.eclipse.swt.widgets.Composite; |
16 | import org.eclipse.swt.widgets.Control; |
17 | import org.eclipse.swt.widgets.Table; |
18 | import org.eclipse.swt.widgets.TableColumn; |
19 | import org.eclipse.swt.widgets.TableItem; |
20 | |
21 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
22 | import de.uka.ipd.sdq.pcm.dialogs.stoex.StochasticExpressionEditDialog; |
23 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisation; |
24 | import de.uka.ipd.sdq.pcm.stochasticexpressions.PCMStoExPrettyPrintVisitor; |
25 | import de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection; |
26 | import de.uka.ipd.sdq.pcmbench.tabs.generic.ObservableCellModifier; |
27 | import de.uka.ipd.sdq.stoex.RandomVariable; |
28 | import de.uka.ipd.sdq.stoex.analyser.visitors.TypeEnum; |
29 | |
30 | /** |
31 | * @author Roman Andrej |
32 | */ |
33 | public class ComponentParametersEditorSection extends EditorSection { |
34 | |
35 | public static final int ICON_COLUMN_INDEX = 0; |
36 | public static final int VARIABLE_COLUMN_INDEX = 1; |
37 | public static final int STOEX_COLUMN_INDEX = 2; |
38 | |
39 | /** |
40 | * Columns of a table, which is used into operations table |
41 | */ |
42 | public final static String PARAMETERS_ICON_COLUMN = ""; |
43 | public final static String VARIABLE_COLUMN = "Variable Name"; |
44 | public final static String STOEX_COLUMN = "Specification"; |
45 | |
46 | /** Set column names of Tabele. */ |
47 | public static String[] columnNames = new String[] { PARAMETERS_ICON_COLUMN, |
48 | VARIABLE_COLUMN, STOEX_COLUMN }; |
49 | |
50 | /** Define the CellModifier. */ |
51 | private ComponentParametersCellModifier parametersCellModifier; |
52 | |
53 | public ComponentParametersEditorSection(Composite composite) { |
54 | super(composite); |
55 | } |
56 | |
57 | /* (non-Javadoc) |
58 | * TODO! remove.... |
59 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#createAddButtonActionListener(java.lang.Object) |
60 | */ |
61 | @Override |
62 | protected SelectionListener createAddButtonActionListener() { |
63 | return new AddComponentParameterAction(); |
64 | } |
65 | |
66 | /* (non-Javadoc) |
67 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#createDeleteButtonListener() |
68 | */ |
69 | @Override |
70 | protected SelectionListener createDeleteButtonListener() { |
71 | DeleteComponentParameterAction deleteButtonListener = new DeleteComponentParameterAction(); |
72 | deleteButtonListener.addObserver(this); |
73 | return deleteButtonListener; |
74 | } |
75 | |
76 | /* (non-Javadoc) |
77 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#createTableColumns(org.eclipse.swt.widgets.Table) |
78 | */ |
79 | @Override |
80 | protected void createTableColumns(Table table) { |
81 | final TableColumn iconColumn = new TableColumn(table, SWT.NONE); |
82 | iconColumn.setWidth(25); |
83 | iconColumn.setText(PARAMETERS_ICON_COLUMN); |
84 | |
85 | final TableColumn variableColumn = new TableColumn(table, SWT.NONE); |
86 | variableColumn.setWidth(240); |
87 | variableColumn.setText(VARIABLE_COLUMN); |
88 | |
89 | final TableColumn stoexColumn = new TableColumn(table, SWT.CENTER); |
90 | stoexColumn.setWidth(100); |
91 | stoexColumn.setText(STOEX_COLUMN); |
92 | } |
93 | |
94 | /* (non-Javadoc) |
95 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#createViewerCellEditors(org.eclipse.swt.widgets.Table) |
96 | */ |
97 | @Override |
98 | protected CellEditor[] createViewerCellEditors(Table table) { |
99 | // create CellEditors |
100 | CellEditor[] editors = new CellEditor[columnNames.length]; |
101 | |
102 | editors[VARIABLE_COLUMN_INDEX] = new TextCellEditor(table); |
103 | |
104 | editors[STOEX_COLUMN_INDEX] = new DialogCellEditor(table) { |
105 | |
106 | /* (non-Javadoc) |
107 | * @see org.eclipse.jface.viewers.DialogCellEditor#openDialogBox(org.eclipse.swt.widgets.Control) |
108 | */ |
109 | @Override |
110 | protected Object openDialogBox(Control cellEditorWindow) { |
111 | Assert.isNotNull(getSelectedVariableUsage()); |
112 | EList<VariableCharacterisation> characterisations = getSelectedVariableUsage() |
113 | .getVariableUsage() |
114 | .getVariableCharacterisation_VariableUsage(); |
115 | RandomVariable randVar = (RandomVariable) characterisations |
116 | .get(0).getSpecification_VariableCharacterisation(); |
117 | StochasticExpressionEditDialog dialog = new StochasticExpressionEditDialog( |
118 | cellEditorWindow.getShell(), getExpectedType(randVar), |
119 | randVar); |
120 | dialog.setInitialExpression(randVar); |
121 | dialog.open(); |
122 | if (dialog.getReturnCode() == Dialog.OK) { |
123 | return new PCMStoExPrettyPrintVisitor().prettyPrint(dialog |
124 | .getResult()); |
125 | } |
126 | |
127 | return null; |
128 | } |
129 | }; |
130 | return editors; |
131 | } |
132 | |
133 | /* (non-Javadoc) |
134 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#getTableColumnNames() |
135 | */ |
136 | @Override |
137 | protected String[] getTableColumnNames() { |
138 | return columnNames; |
139 | } |
140 | |
141 | /** |
142 | * @return the selectedVariableUsage |
143 | */ |
144 | public VariableUsageWrapper getSelectedVariableUsage() { |
145 | return (VariableUsageWrapper) getSelectedObject(); |
146 | } |
147 | |
148 | protected TypeEnum getExpectedType(RandomVariable rv) { |
149 | TypeEnum expectedType = TypeEnum.ANY; |
150 | VariableCharacterisation vc = null; |
151 | |
152 | if (rv instanceof VariableCharacterisation) { |
153 | vc = (VariableCharacterisation) rv; |
154 | } |
155 | if (rv instanceof PCMRandomVariable && rv.eContainer() instanceof VariableCharacterisation) { |
156 | vc = (VariableCharacterisation) rv.eContainer(); |
157 | } |
158 | if (vc != null) { |
159 | expectedType = StochasticExpressionEditDialog |
160 | .getTypeFromVariableCharacterisation(vc); |
161 | } |
162 | return expectedType; |
163 | } |
164 | |
165 | /* (non-Javadoc) |
166 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#createViewerCellModifier() |
167 | */ |
168 | @Override |
169 | protected ObservableCellModifier createViewerCellModifier() { |
170 | parametersCellModifier = new ComponentParametersCellModifier(); |
171 | return parametersCellModifier; |
172 | } |
173 | |
174 | /* (non-Javadoc) |
175 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#canAddButonCreated() |
176 | */ |
177 | @Override |
178 | protected boolean canAddButonCreated() { |
179 | return false; |
180 | } |
181 | |
182 | /* (non-Javadoc) |
183 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#canDeleteButonCreated() |
184 | */ |
185 | @Override |
186 | protected boolean canDeleteButonCreated() { |
187 | return true; |
188 | } |
189 | |
190 | /* (non-Javadoc) |
191 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#inputValidation(org.eclipse.emf.ecore.EObject) |
192 | */ |
193 | @Override |
194 | protected boolean inputValidation(EObject object) { |
195 | if (object instanceof VariableUsageWrapper) { |
196 | VariableUsageWrapper wrapper = (VariableUsageWrapper) object; |
197 | return wrapper.isEdited(); |
198 | } |
199 | |
200 | return false; |
201 | } |
202 | |
203 | /* (non-Javadoc) |
204 | * @see de.uka.ipd.sdq.pcmbench.tabs.generic.EditorSection#update(java.util.Observable, java.lang.Object) |
205 | */ |
206 | @Override |
207 | public void update(Observable o, Object arg) { |
208 | super.update(o, arg); |
209 | |
210 | if (arg instanceof VariableUsageWrapper && viewer != null) { |
211 | VariableUsageWrapper wrapper = (VariableUsageWrapper) arg; |
212 | |
213 | if (!wrapper.isEdited()) { |
214 | // set gray color for removed table item |
215 | TableItem[] items = viewer.getTable().getItems(); |
216 | |
217 | for (int i = 0; i < items.length; i++) { |
218 | TableItem item = items[i]; |
219 | |
220 | VariableUsageWrapper data = (VariableUsageWrapper) item |
221 | .getData(); |
222 | |
223 | if (wrapper.equals(data)) { |
224 | item.setForeground(item.getDisplay().getSystemColor( |
225 | SWT.COLOR_GRAY)); |
226 | } |
227 | |
228 | } |
229 | } |
230 | |
231 | // set viewer selection if for Enable/Disable of Delete-Button |
232 | // relevant |
233 | viewer.setSelection(new StructuredSelection( |
234 | new Object[] { wrapper })); |
235 | |
236 | } |
237 | } |
238 | |
239 | /** |
240 | * @return the addButtonListener |
241 | */ |
242 | public ComponentParametersCellModifier getCellModifier() { |
243 | return parametersCellModifier; |
244 | } |
245 | } |