| 1 | package de.uka.ipd.sdq.pcmbench.tabs.operations; |
| 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.emf.transaction.util.TransactionUtil; |
| 10 | import org.eclipse.swt.widgets.TableItem; |
| 11 | |
| 12 | import de.uka.ipd.sdq.pcm.repository.DataType; |
| 13 | import de.uka.ipd.sdq.pcm.repository.OperationSignature; |
| 14 | import de.uka.ipd.sdq.pcmbench.tabs.generic.ObservableCellModifier; |
| 15 | |
| 16 | /** |
| 17 | * @author Roman Andrej |
| 18 | * |
| 19 | * This class implements an ICellModifier. An ICellModifier is called when the |
| 20 | * user modifes a cell in the tableViewer |
| 21 | */ |
| 22 | |
| 23 | public class OperationsCellModifier extends ObservableCellModifier { |
| 24 | |
| 25 | private List<String> columnNames; |
| 26 | private OperationSignature signature; |
| 27 | |
| 28 | /** |
| 29 | * The transactional editing domain which is used to get the commands and |
| 30 | * alter the model |
| 31 | */ |
| 32 | protected TransactionalEditingDomain editingDomain = null; |
| 33 | |
| 34 | public OperationsCellModifier() { |
| 35 | this.columnNames = Arrays.asList(OperationsEditorSection.columnNames); |
| 36 | } |
| 37 | |
| 38 | /* (non-Javadoc) |
| 39 | * @see org.eclipse.jface.viewers.ICellModifier#canModify(Object element, |
| 40 | * String property) |
| 41 | */ |
| 42 | public boolean canModify(Object element, String property) { |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | public Object getValue(Object element, String property) { |
| 47 | return (new OperationsTabItemProvider(null)).getColumnText(element, |
| 48 | columnNames.indexOf(property)); |
| 49 | } |
| 50 | |
| 51 | public void modify(Object element, String property, Object value) { |
| 52 | |
| 53 | // Find the index of the column |
| 54 | int columnIndex = columnNames.indexOf(property); |
| 55 | |
| 56 | Assert.isNotNull(element); |
| 57 | TableItem item = (TableItem) element; |
| 58 | signature = (OperationSignature) item.getData(); |
| 59 | editingDomain = TransactionUtil.getEditingDomain(signature); |
| 60 | |
| 61 | switch (columnIndex) { |
| 62 | case OperationsEditorSection.ICON_COLUMN_INDEX: // COMPLETED_COLUMN |
| 63 | break; |
| 64 | case OperationsEditorSection.RETURNTYPE_COLUMN_INDEX: // RETURNTYPE_COLUMN |
| 65 | if (value instanceof DataType) |
| 66 | setReturnType((DataType) value); |
| 67 | break; |
| 68 | case OperationsEditorSection.SIGNATURENAME_COLUMN_INDEX: // SERVICENAME_COLUMN |
| 69 | String valueString = ((String) value).trim(); |
| 70 | setServiceName(valueString); |
| 71 | break; |
| 72 | case OperationsEditorSection.PARAMETER_COLUMN_INDEX: // OWNEDPARAMETER_COLUMN |
| 73 | break; |
| 74 | case OperationsEditorSection.EXCEPTIONS_COLUMN_INDEX: // EXEPTIONTYPE_COLUM |
| 75 | break; |
| 76 | default: |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private void setServiceName(String valueString) { |
| 81 | final String value = valueString; |
| 82 | |
| 83 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
| 84 | @Override |
| 85 | protected void doExecute() { |
| 86 | signature.setEntityName(value); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | if (!value.equals(signature.getEntityName())) { |
| 91 | recCommand.setDescription("Edit Signature Property"); |
| 92 | recCommand.setLabel("Set name"); |
| 93 | editingDomain.getCommandStack().execute(recCommand); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private void setReturnType(DataType type) { |
| 98 | final DataType dataType = type; |
| 99 | |
| 100 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
| 101 | @Override |
| 102 | protected void doExecute() { |
| 103 | signature.setReturnType__OperationSignature(dataType); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | if (!dataType.equals(signature.getReturnType__OperationSignature())) { |
| 108 | recCommand.setDescription("Edit Signature Property"); |
| 109 | recCommand.setLabel("Set return type"); |
| 110 | editingDomain.getCommandStack().execute(recCommand); |
| 111 | // sent message to observer |
| 112 | notifyObservers(); |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | } |