EditHelper for creation of attributes and relations

Aus SDQ-Wiki

In this tutorial we describe how to create attributes and relations with EditHelpers.

Registration of EditHelper

First you have to register your EditHelper so that they will be called when a new element is created. See GMF_Tutorial:_Elemente_konfigurieren_während_sie_angelegt_werden for the details.

Create new element

We will use an EditHelper which extends an AbstractEditHelperAdvice. Then we overwrite the getBeforeConfigureCommand() method so that this method returns a CompositeCommand. We use a CompositeCommand because we want to chain several commands. In order to create a new element, we use a CreateElementRequest as input for a CreateElementCommand. Note that we use the setContainmentFeature() method to set the relations. Afterwards we add this command to our compositeCommand.

IElementType elementType = ElementTypeRegistry.getInstance().getType(RESOURCE_COMMUNICATION_LINK_RESOURCE_SPECIFICATION);

CreateElementRequest createElementRequest = new CreateElementRequest(request.getElementToConfigure(), elementType);
createElementRequest.setContainmentFeature(ResourceenvironmentPackage.eINSTANCE.getLinkingResource_CommunicationLinkResourceSpecifications_LinkingResource());
CreateElementCommand createElementCommand = new CreateElementCommand(createElementRequest);

CompositeCommand compositeCommand = new CompositeCommand("");
compositeCommand.add(createElementCommand);

Set new attributes

Now we want to set the attributes of the element we have just created. If we used the just requested to create element as input for a SetValueCommand we would get a NullPointerException because the element has not been created yet. Therefore we use our own command to do the work. We create a new class which extends ConfigureElementCommand. Then we overwrite the DoExecuteWithResult() method. This method sets all the attributes. This new command will be executed when the element has already been created. So we won't get a NullPointerException.

	/**
	 * Method opens latency and throughput StoEx-dialogs and sets attributes of
	 * Communication Link Resource Specification accordingly
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		// get Linking Resource and Communication Link Resource Specification
		LinkingResource linkingResource = (LinkingResource) request.getElementToConfigure();
		// this containment was set before by AddCommunicationLinkResourceSpecificationEditHelperAdvice.getBeforeConfigureCommand()
		CommunicationLinkResourceSpecification communicationLinkResourceSpecification = linkingResource.getCommunicationLinkResourceSpecifications_LinkingResource();
		
		PCMRandomVariable latency = getRandomVariableFromStoExDialog(LATENCY_DISPLAY_TITLE);
		if (latency == null) {
			return CommandResult.newCancelledCommandResult();
		}
		communicationLinkResourceSpecification.setLatency_CommunicationLinkResourceSpecification(latency);
		
		PCMRandomVariable throughput = getRandomVariableFromStoExDialog(THROUGHPUT_DISPLAY_TITLE);
		if (throughput == null) {
			return CommandResult.newCancelledCommandResult();
		}
		communicationLinkResourceSpecification.setThroughput_CommunicationLinkResourceSpecification(throughput);
		
		return CommandResult.newOKCommandResult();
	}

References

You can find the whole source code in our SVN: de.uka.ipd.sdq.pcm.gmf.resource.helper.SetLatencyThroughputAndLanTypeCommand and de.uka.ipd.sdq.pcm.gmf.resource.helper.AddCommunicationLinkResourceSpecificationEditHelperAdvice.

Keywords: GMFGMF, Editor, PCMPCM, PCM, GMF, EditHelper, Tutorial, PCM Development, Palladio Component Model