/*****************************************************************************
* Copyright (c) 2010 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.editpolicies;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.commands.SemanticCreateCommand;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewAndElementRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.EditCommandRequestWrapper;
import org.eclipse.gmf.runtime.diagram.ui.requests.RefreshConnectionsRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.View;
/**
* Specific creation edit policy for Papyrus. It checks that the view creation
* can be done before returning the result of the getCommand
*/
public class PapyrusCreationEditPolicy extends CreationEditPolicy {
/**
* Method getCreateElementAndViewCommand.
*
* @param request
* @return Command Which creates the sematnic and the view command for the
* given CreateViewAndElementRequest
*/
protected Command getCreateElementAndViewCommand(CreateViewAndElementRequest request) {
// get the element descriptor
CreateElementRequestAdapter requestAdapter = request.getViewAndElementDescriptor().getCreateElementRequestAdapter();
// get the semantic request
CreateElementRequest createElementRequest = (CreateElementRequest)requestAdapter.getAdapter(CreateElementRequest.class);
if(createElementRequest.getContainer() == null) {
// complete the semantic request by filling in the host's semantic
// element as the context
View view = (View)getHost().getModel();
EObject hostElement = ViewUtil.resolveSemanticElement(view);
if(hostElement == null && view.getElement() == null) {
hostElement = view;
}
// Returns null if host is unresolvable so that trying to create a
// new element in an unresolved shape will not be allowed.
if(hostElement == null) {
return null;
}
createElementRequest.setContainer(hostElement);
}
// get the create element command based on the elementdescriptor's
// request
Command createElementCommand = getHost().getCommand(new EditCommandRequestWrapper((CreateElementRequest)requestAdapter.getAdapter(CreateElementRequest.class), request.getExtendedData()));
if(createElementCommand == null) {
return UnexecutableCommand.INSTANCE;
}
if(!createElementCommand.canExecute()) {
return createElementCommand;
}
// create the semantic create wrapper command
SemanticCreateCommand semanticCommand = new SemanticCreateCommand(requestAdapter, createElementCommand);
Command viewCommand = getCreateCommand(request);
if(viewCommand == null) {
return UnexecutableCommand.INSTANCE;
}
if(!viewCommand.canExecute()) {
return UnexecutableCommand.INSTANCE;
}
Command refreshConnectionCommand = getHost().getCommand(new RefreshConnectionsRequest(((List)request.getNewObject())));
// form the compound command and return
CompositeCommand cc = new CompositeCommand(semanticCommand.getLabel());
cc.compose(semanticCommand);
cc.compose(new CommandProxy(viewCommand));
if(refreshConnectionCommand != null) {
cc.compose(new CommandProxy(refreshConnectionCommand));
}
return new ICommandProxy(cc);
}
}