/*
* (c) 2011 - 2013 University of Mannheim: Software Engineering Group
*/
package de.uni_mannheim.informatik.swt.models.plm.diagram.edit.commands;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.ui.PlatformUI;
/**
* @generated
*/
public class EntityCreateCommand extends EditElementCommand {
/**
* @generated
*/
public EntityCreateCommand(CreateElementRequest req) {
super(req.getLabel(), null, req);
}
/**
* FIXME: replace with setElementToEdit()
* @generated
*/
protected EObject getElementToEdit() {
EObject container = ((CreateElementRequest) getRequest())
.getContainer();
if (container instanceof View) {
container = ((View) container).getElement();
}
return container;
}
/**
* @generated
*/
public boolean canExecute() {
de.uni_mannheim.informatik.swt.models.plm.diagram.part.PLMDiagramEditor editor = (de.uni_mannheim.informatik.swt.models.plm.diagram.part.PLMDiagramEditor) PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
String typeName = editor.getPalette().getActiveTool().getLabel();
String typeToolId = editor.getPalette().getActiveTool().getId();
//check if we have a dsl element -> if we have no dsl element we can create
if (!typeToolId.startsWith("dsl."))
return true;
de.uni_mannheim.informatik.swt.models.plm.PLM.Model m = getElementToEdit() instanceof de.uni_mannheim.informatik.swt.models.plm.PLM.Model ? (de.uni_mannheim.informatik.swt.models.plm.PLM.Model) getElementToEdit()
: ((de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject) getElementToEdit())
.getModel();
de.uni_mannheim.informatik.swt.models.plm.PLM.Ontology ont = (de.uni_mannheim.informatik.swt.models.plm.PLM.Ontology) m
.eContainer();
//We cannot create an instance on the highest ontological layer
if (ont.getContent().indexOf(m) < 1)
return false;
de.uni_mannheim.informatik.swt.models.plm.PLM.Model typeModel = ont
.getContent().get(ont.getContent().indexOf(m) - 1);
de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject type = null;
for (EObject obj : typeModel.getContent())
//We found the type -> allowed to create an instance here
if (obj instanceof de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject
&& ((de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject) obj)
.getName() != null
&& ((de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject) obj)
.getName().equals(typeName)) {
type = (de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject) obj;
break;
}
//no type found
if (type == null)
return false;
//contained in model and added to model => OK
if (type.getContainer() instanceof de.uni_mannheim.informatik.swt.models.plm.PLM.Model
&& getElementToEdit() instanceof de.uni_mannheim.informatik.swt.models.plm.PLM.Model)
return true;
if (type.getContainer() instanceof de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject
&& getElementToEdit() instanceof de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject)
return ((de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject) getElementToEdit())
.isInstanceOf((de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject) type
.getContainer());
//We found no type -> not allowed to create an instance here
return false;
}
/**
* @generated
*/
protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
de.uni_mannheim.informatik.swt.models.plm.PLM.Entity newElement = de.uni_mannheim.informatik.swt.models.plm.PLM.PLMFactory.eINSTANCE
.createEntity();
de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject owner = (de.uni_mannheim.informatik.swt.models.plm.PLM.Clabject) getElementToEdit();
owner.getContent().add(newElement);
de.uni_mannheim.informatik.swt.models.plm.diagram.providers.ElementInitializers
.getInstance().init_Entity_3085(newElement);
doConfigure(newElement, monitor, info);
((CreateElementRequest) getRequest()).setNewElement(newElement);
return CommandResult.newOKCommandResult(newElement);
}
/**
* @generated
*/
protected void doConfigure(
de.uni_mannheim.informatik.swt.models.plm.PLM.Entity newElement,
IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
IElementType elementType = ((CreateElementRequest) getRequest())
.getElementType();
ConfigureRequest configureRequest = new ConfigureRequest(
getEditingDomain(), newElement, elementType);
configureRequest.setClientContext(((CreateElementRequest) getRequest())
.getClientContext());
configureRequest.addParameters(getRequest().getParameters());
ICommand configureCommand = elementType
.getEditCommand(configureRequest);
if (configureCommand != null && configureCommand.canExecute()) {
configureCommand.execute(monitor, info);
}
}
}