package brn.gui.linktable.diagram.edit.policies;

import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.gmf.runtime.notation.View;
import brn.gui.graph.model.LinkTableLink;
import brn.gui.graph.model.ModelPackage;
import brn.gui.graph.model.Node;

import brn.gui.linktable.diagram.providers.ModelElementTypes;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.gef.commands.UnexecutableCommand;

import org.eclipse.gmf.runtime.emf.type.core.commands.CreateRelationshipCommand;

/**
 * @generated
 */
public class NodeItemSemanticEditPolicy extends ModelBaseItemSemanticEditPolicy {

  /**
   * @generated
   */
  protected Command getDestroyElementCommand(DestroyElementRequest req) {
    return getMSLWrapper(new DestroyElementCommand(req) {

      protected EObject getElementToDestroy() {
        View view = (View) getHost().getModel();
        EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
        if (annotation != null) {
          return view;
        }
        return super.getElementToDestroy();
      }

    });
  }

  /**
   * @generated
   */
  protected Command getCreateRelationshipCommand(CreateRelationshipRequest req) {
    if (ModelElementTypes.LinkTableLink_3001 == req.getElementType()) {
      return req.getTarget() == null ? getCreateStartOutgoingLinkTableLink3001Command(req)
          : getCreateCompleteIncomingLinkTableLink3001Command(req);
    }
    return super.getCreateRelationshipCommand(req);
  }

  /**
   * @generated
   */
  protected Command getCreateStartOutgoingLinkTableLink3001Command(
      CreateRelationshipRequest req) {
    return new Command() {
    };
  }

  /**
   * @generated
   */
  protected Command getCreateCompleteIncomingLinkTableLink3001Command(
      CreateRelationshipRequest req) {
    if (!(req.getSource() instanceof Node)) {
      return UnexecutableCommand.INSTANCE;
    }
    final Node element = (Node) getRelationshipContainer(req.getSource(),
        ModelPackage.eINSTANCE.getNode(), req.getElementType());
    if (element == null) {
      return UnexecutableCommand.INSTANCE;
    }
    if (req.getContainmentFeature() == null) {
      req.setContainmentFeature(ModelPackage.eINSTANCE
          .getNode_SourceConnections());
    }
    return getMSLWrapper(new CreateIncomingLinkTableLink3001Command(req) {

      /**
       * @generated
       */
      protected EObject getElementToEdit() {
        return element;
      }
    });
  }

  /**
   * @generated
   */
  private static class CreateIncomingLinkTableLink3001Command extends
      CreateRelationshipCommand {

    /**
     * @generated
     */
    public CreateIncomingLinkTableLink3001Command(CreateRelationshipRequest req) {
      super(req);
    }

    /**
     * @generated
     */
    protected EClass getEClassToEdit() {
      return ModelPackage.eINSTANCE.getNode();
    };

    /**
     * @generated
     */
    protected void setElementToEdit(EObject element) {
      throw new UnsupportedOperationException();
    }

    /**
     * @generated
     */
    protected EObject doDefaultElementCreation() {
      LinkTableLink newElement = (LinkTableLink) super
          .doDefaultElementCreation();
      if (newElement != null) {
        newElement.setTarget((Node) getTarget());
        newElement.setSource((Node) getSource());
      }
      return newElement;
    }
  }
}
