| 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.EventType; |
| 14 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
| 15 | import de.uka.ipd.sdq.pcm.repository.RepositoryFactory; |
| 16 | import de.uka.ipd.sdq.pcmbench.tabs.generic.ObservableCellModifier; |
| 17 | |
| 18 | /** |
| 19 | * This class implements an ICellModifier for the modification of event types. |
| 20 | * An ICellModifier is called when the user modifies a cell in the tableViewer |
| 21 | * |
| 22 | * @author Benjamin Klatt |
| 23 | */ |
| 24 | |
| 25 | public class EventTypesCellModifier extends ObservableCellModifier { |
| 26 | |
| 27 | private List<String> columnNames; |
| 28 | private EventType eventType; |
| 29 | |
| 30 | /** |
| 31 | * The transactional editing domain which is used to get the commands and |
| 32 | * alter the model |
| 33 | */ |
| 34 | protected TransactionalEditingDomain editingDomain = null; |
| 35 | |
| 36 | public EventTypesCellModifier() { |
| 37 | this.columnNames = Arrays.asList(EventTypesEditorSection.columnNames); |
| 38 | } |
| 39 | |
| 40 | /* (non-Javadoc) |
| 41 | * @see org.eclipse.jface.viewers.ICellModifier#canModify(Object element, |
| 42 | * String property) |
| 43 | */ |
| 44 | public boolean canModify(Object element, String property) { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | public Object getValue(Object element, String property) { |
| 49 | return (new EventTypesTabItemProvider(null)).getColumnText(element, |
| 50 | columnNames.indexOf(property)); |
| 51 | } |
| 52 | |
| 53 | public void modify(Object element, String property, Object value) { |
| 54 | |
| 55 | // Find the index of the column |
| 56 | int columnIndex = columnNames.indexOf(property); |
| 57 | |
| 58 | Assert.isNotNull(element); |
| 59 | TableItem item = (TableItem) element; |
| 60 | eventType = (EventType) item.getData(); |
| 61 | editingDomain = TransactionUtil.getEditingDomain(eventType); |
| 62 | |
| 63 | switch (columnIndex) { |
| 64 | case EventTypesEditorSection.ICON_COLUMN_INDEX: // COMPLETED_COLUMN |
| 65 | break; |
| 66 | case EventTypesEditorSection.EVENTTYPENAME_COLUMN_INDEX: // EVENT TYPE NAME_COLUMN |
| 67 | String valueString = ((String) value).trim(); |
| 68 | setEventTypeName(valueString); |
| 69 | break; |
| 70 | case EventTypesEditorSection.PARAMETER_NAME_COLUMN_INDEX: // OWNEDPARAMETER_COLUMN |
| 71 | String parameterNameString = ((String) value).trim(); |
| 72 | setEventTypeParameterName(parameterNameString); |
| 73 | break; |
| 74 | case EventTypesEditorSection.PARAMETER_TYPE_COLUMN_INDEX: // OWNEDPARAMETER TYPE COLUMN |
| 75 | if (value instanceof DataType) |
| 76 | setParameterType((DataType) value); |
| 77 | break; |
| 78 | default: |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Set the name of the event type. |
| 84 | * @param valueString The name to be set. |
| 85 | */ |
| 86 | private void setEventTypeName(String valueString) { |
| 87 | final String value = valueString; |
| 88 | |
| 89 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
| 90 | @Override |
| 91 | protected void doExecute() { |
| 92 | eventType.setEntityName(value); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | if (!value.equals(eventType.getEntityName())) { |
| 97 | recCommand.setDescription("Edit EventType Property"); |
| 98 | recCommand.setLabel("Set name"); |
| 99 | editingDomain.getCommandStack().execute(recCommand); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Set the name of the event type parameter. |
| 105 | * @param valueString The name to be set. |
| 106 | */ |
| 107 | private void setEventTypeParameterName(String valueString) { |
| 108 | final String value = valueString; |
| 109 | |
| 110 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
| 111 | @Override |
| 112 | protected void doExecute() { |
| 113 | |
| 114 | // create the parameter object if it did not exist before |
| 115 | if(eventType.getParameter__EventType() == null){ |
| 116 | Parameter parameter = RepositoryFactory.eINSTANCE.createParameter(); |
| 117 | eventType.setParameter__EventType(parameter); |
| 118 | } |
| 119 | |
| 120 | // set the parameter name |
| 121 | eventType.getParameter__EventType().setParameterName(value); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | if (eventType.getParameter__EventType() == null || |
| 126 | !value.equals(eventType.getParameter__EventType().getParameterName())) { |
| 127 | recCommand.setDescription("Edit EventType Parameter Property"); |
| 128 | recCommand.setLabel("Set name"); |
| 129 | editingDomain.getCommandStack().execute(recCommand); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Set the data type of the parameter |
| 135 | * @param type The type to be set |
| 136 | */ |
| 137 | private void setParameterType(DataType type) { |
| 138 | final DataType dataType = type; |
| 139 | |
| 140 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
| 141 | @Override |
| 142 | protected void doExecute() { |
| 143 | |
| 144 | // create the parameter object if it did not exist before |
| 145 | if(eventType.getParameter__EventType() == null){ |
| 146 | Parameter parameter = RepositoryFactory.eINSTANCE.createParameter(); |
| 147 | eventType.setParameter__EventType(parameter); |
| 148 | } |
| 149 | |
| 150 | // set the parameter data type |
| 151 | eventType.getParameter__EventType().setDataType__Parameter(dataType); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | if (eventType.getParameter__EventType() == null || |
| 156 | !dataType.equals(eventType.getParameter__EventType().getDataType__Parameter())) { |
| 157 | recCommand.setDescription("Edit EventType Parameter Property"); |
| 158 | recCommand.setLabel("Set parameter type"); |
| 159 | editingDomain.getCommandStack().execute(recCommand); |
| 160 | // sent message to observer |
| 161 | notifyObservers(); |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | } |