1 | package de.uka.ipd.sdq.pcm.dialogs.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.transaction.RecordingCommand; |
8 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
9 | import org.eclipse.jface.viewers.ICellModifier; |
10 | import org.eclipse.jface.viewers.TableViewer; |
11 | import org.eclipse.swt.widgets.TableItem; |
12 | |
13 | import de.uka.ipd.sdq.pcm.repository.DataType; |
14 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
15 | |
16 | /** |
17 | * The class define the CellModifire for the ParametersDialog |
18 | * |
19 | * @author Roman Andrej |
20 | */ |
21 | public class ParametersCellModifier implements ICellModifier { |
22 | |
23 | private TableViewer viewer; |
24 | private List<String> columnNames; |
25 | private Parameter parameter; |
26 | |
27 | /** |
28 | * The transactional editing domain which is used to get the commands and |
29 | * alter the model |
30 | */ |
31 | private TransactionalEditingDomain editingDomain = null; |
32 | |
33 | public ParametersCellModifier(TableViewer viewer, TransactionalEditingDomain editingDomain) { |
34 | this.viewer = viewer; |
35 | this.columnNames = Arrays.asList(CreateEditorContents |
36 | .getColumnNames()); |
37 | this.editingDomain = editingDomain; |
38 | } |
39 | |
40 | /* |
41 | * (non-Javadoc) |
42 | * |
43 | * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, |
44 | * java.lang.String) |
45 | */ |
46 | public boolean canModify(Object element, String property) { |
47 | return true; |
48 | } |
49 | |
50 | /* (non-Javadoc) |
51 | * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, |
52 | * java.lang.String) |
53 | */ |
54 | public Object getValue(Object element, String property) { |
55 | return (new ParametersItemProvider(null)).getColumnText(element, |
56 | columnNames.indexOf(property)); |
57 | } |
58 | |
59 | /* (non-Javadoc) |
60 | * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, |
61 | * java.lang.String, java.lang.Object) |
62 | */ |
63 | public void modify(Object element, String property, Object value) { |
64 | |
65 | // Find the index of the column |
66 | int columnIndex = columnNames.indexOf(property); |
67 | |
68 | Assert.isNotNull(element); |
69 | TableItem item = (TableItem) element; |
70 | parameter = (Parameter) item.getData(); |
71 | |
72 | switch (columnIndex) { |
73 | case CreateEditorContents.ICON_COLUMN_INDEX: // COMPLETED_COLUMN |
74 | break; |
75 | case CreateEditorContents.CONTEXT_COLUMN_INDEX: // RETURNTYPE_COLUMN |
76 | break; |
77 | case CreateEditorContents.NAME_COLUMN_INDEX: // SERVICENAME_COLUMN |
78 | String valueString = ((String) value).trim(); |
79 | setParameterName(valueString); |
80 | break; |
81 | case CreateEditorContents.TYPE_COLUMN_INDEX: // OWNEDPARAMETER_COLUMN |
82 | if (value instanceof DataType) { |
83 | DataType valueDataType = (DataType) value; |
84 | setDataType(valueDataType); |
85 | } |
86 | break; |
87 | default: |
88 | } |
89 | } |
90 | |
91 | /** set parameter DataType */ |
92 | private void setDataType(final DataType dataType) { |
93 | |
94 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
95 | @Override |
96 | protected void doExecute() { |
97 | parameter.setDataType__Parameter(dataType); |
98 | } |
99 | }; |
100 | |
101 | if (!dataType.equals(parameter.getDataType__Parameter())) { |
102 | recCommand.setDescription("Edit Parameter Property"); |
103 | recCommand.setLabel("Set parameter DataType"); |
104 | editingDomain.getCommandStack().execute(recCommand); |
105 | } |
106 | |
107 | reloadParametersViewer(); |
108 | } |
109 | |
110 | /** set the parameter name */ |
111 | private void setParameterName(String valueString) { |
112 | final String value = valueString; |
113 | |
114 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
115 | @Override |
116 | protected void doExecute() { |
117 | parameter.setParameterName(value); |
118 | } |
119 | }; |
120 | |
121 | if (!value.equals(parameter.getParameterName())) { |
122 | recCommand.setLabel("Set ParameterName"); |
123 | editingDomain.getCommandStack().execute(recCommand); |
124 | } |
125 | reloadParametersViewer(); |
126 | } |
127 | |
128 | private void reloadParametersViewer() { |
129 | viewer.refresh(); |
130 | } |
131 | } |