View Javadoc
1   /*****************************************************************************
2    * Copyright (c) 2015 CEA LIST and others.
3    * 
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Contributors:
10   *  Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation
11   *   
12   *****************************************************************************/
13  package org.eclipse.papyrus.sysml14.service.types.command;
14  
15  import org.eclipse.core.commands.ExecutionException;
16  import org.eclipse.core.runtime.IAdaptable;
17  import org.eclipse.core.runtime.IProgressMonitor;
18  import org.eclipse.emf.common.util.EList;
19  import org.eclipse.gmf.runtime.common.core.command.CommandResult;
20  import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand;
21  import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
22  import org.eclipse.uml2.uml.Classifier;
23  import org.eclipse.uml2.uml.InstanceSpecification;
24  import org.eclipse.uml2.uml.Property;
25  import org.eclipse.uml2.uml.Slot;
26  import org.eclipse.uml2.uml.UMLPackage;
27  
28  /**
29   * 
30   * A command to configure an instance specification from a classifier
31   * @author Benoit Maggi
32   *
33   */
34  public class InstanceSpecificationWithSlotConfigureElementCommand extends ConfigureElementCommand {
35  
36  	private Classifier classifier;
37  	
38  	/**
39  	 * Configure the instance specification to use this classifier
40  	 *
41  	 * @param request
42  	 * @param classifier
43  	 */
44  	public InstanceSpecificationWithSlotConfigureElementCommand(ConfigureRequest request,Classifier classifier) {
45  		super(request);
46  		this.classifier = classifier;
47  	}	
48  	
49  	
50  	/**
51  	 * Add the classifier to the instance specification and create slots for each attributes
52  	 * 
53  	 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
54  	 *
55  	 * @param monitor
56  	 * @param info
57  	 * @return
58  	 * @throws ExecutionException
59  	 */
60  	@Override
61  	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
62  		final InstanceSpecification instanceSpecification = (InstanceSpecification) getElementToEdit();
63  		if (classifier != null){
64  			// set instance classifier
65  			instanceSpecification.getClassifiers().add(classifier);
66  			EList<Property> attributes = classifier.getAttributes();		
67  			// create the slots
68  			for (Property property : attributes) {
69  				Slot slot = instanceSpecification.createSlot();
70  				slot.createValue(property.getName(), property.getType(), UMLPackage.eINSTANCE.getLiteralString());
71  				slot.setDefiningFeature(property);
72  			}						
73  		}
74  		return CommandResult.newOKCommandResult(instanceSpecification);
75  	}
76  
77  }