@Override
  public Object getAdapter(Class key) {
    if (PictogramElement.class == key) {
      EObject bpmnModel = (EObject) super.getModel();
      if (bpmnModel instanceof BPMNDiagram) {
        BPMNDiagram bpmnDiagram = (BPMNDiagram) bpmnModel;
        bpmnModel = bpmnDiagram.getPlane().getBpmnElement();
      }

      if (diagramEditPart != null) {
        // the model is actually a BPMN element - convert this
        // to a PictogramElement for the SelectionSynchronizer
        for (Diagram diagram : diagramEditPart.getAllDiagrams()) {
          if (diagram != null) {
            List<PictogramElement> pes =
                Graphiti.getLinkService().getPictogramElements(diagram, bpmnModel);
            for (PictogramElement pe : pes) {
              if (pe instanceof ContainerShape) return pe;
              if (pe instanceof FreeFormConnection) return pe;
            }
          }
        }
      }
    }
    return super.getAdapter(key);
  }
 private boolean isMissingDIElement(BaseElement be) {
   // ignore DataStores - there are bound to be references
   // to these, which *should* be rendered
   if (be instanceof DataStore) return false;
   BPMNDiagram bpmnDiagram = DIUtils.findBPMNDiagram(be);
   if (bpmnDiagram != null) return false;
   // couldn't find a BPMNDiagram entry for this BaseElement
   // check its container to see if it has a BPMNDiagram
   FlowElementsContainer container = this.getRootElementContainer(be);
   bpmnDiagram = DIUtils.findBPMNDiagram(container);
   if (bpmnDiagram != null) {
     // is the BaseElement defined as a BPMNShape or BPMNEdge in its
     // container's BPMNDiagram?
     if (bpmnDiagram.getPlane().getPlaneElement().contains(be)) return false;
   }
   boolean missing = (elements.get(be) == null && diagnostics.get(be) == null);
   if (missing)
     GraphicsUtil.dump(
         "Missing DI element for: "
             + be.eClass().getName()
             + " '"
             + ExtendedPropertiesProvider.getTextValue(be)
             + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
   return missing;
 }
  private BPMNDiagram createDIDiagram(BaseElement bpmnElement) {

    BPMNDiagram bpmnDiagram = DIUtils.findBPMNDiagram(bpmnElement, true);

    // if this container does not have a BPMNDiagram, create one
    if (bpmnElement instanceof Process) {
      if (bpmnDiagram == null) {
        // unless this Process is referenced by a Pool
        for (Collaboration c :
            ModelUtil.getAllObjectsOfType(bpmnElement.eResource(), Collaboration.class)) {
          for (Participant p : c.getParticipants()) {
            if (!ModelUtil.isParticipantBand(p)) {
              if (p.getProcessRef() == bpmnElement) {
                bpmnDiagram = DIUtils.findBPMNDiagram(p, true);
                break;
              }
            }
          }
          if (bpmnDiagram != null) break;
        }
      } else {
        // Always create a new BPMNDiagram if this Process is being referenced by a Participant Band
        //				for (Collaboration c : ModelUtil.getAllObjectsOfType(bpmnElement.eResource(),
        // Collaboration.class)) {
        //					for (Participant p : c.getParticipants()) {
        //						if (ModelUtil.isParticipantBand(p)) {
        //							if (p.getProcessRef() == bpmnElement) {
        //								bpmnDiagram = null;
        //								break;
        //							}
        //						}
        //					}
        //					if (bpmnDiagram==null)
        //						break;
        //				}
      }
    }

    if (bpmnDiagram == null) {
      FlowElementsContainer container = getRootElementContainer(bpmnElement);
      if (container == null) {
        diagnostics.add(IStatus.ERROR, bpmnElement, Messages.DIGenerator_No_Diagram);
        return this.bpmnDiagram;
      }
      BPMNPlane plane = BpmnDiFactory.eINSTANCE.createBPMNPlane();
      plane.setBpmnElement(container);

      bpmnDiagram = BpmnDiFactory.eINSTANCE.createBPMNDiagram();
      bpmnDiagram.setName(ExtendedPropertiesProvider.getTextValue(container));
      bpmnDiagram.setPlane(plane);

      definitions.getDiagrams().add(bpmnDiagram);
    }

    return bpmnDiagram;
  }
  private BPMNShape createDIShape(
      BPMNDiagram bpmnDiagram, BaseElement bpmnElement, float x, float y, boolean doImport) {

    BPMNPlane plane = bpmnDiagram.getPlane();
    BPMNShape bpmnShape = null;
    for (DiagramElement de : plane.getPlaneElement()) {
      if (de instanceof BPMNShape) {
        if (bpmnElement == ((BPMNShape) de).getBpmnElement()) {
          bpmnShape = (BPMNShape) de;
          break;
        }
      }
    }

    if (bpmnShape == null) {
      bpmnShape = BpmnDiFactory.eINSTANCE.createBPMNShape();
      bpmnShape.setBpmnElement(bpmnElement);
      Bounds bounds = DcFactory.eINSTANCE.createBounds();
      bounds.setX(x);
      bounds.setY(y);
      ShapeStyle ss = preferences.getShapeStyle(bpmnElement);
      bounds.setWidth(ss.getDefaultWidth());
      bounds.setHeight(ss.getDefaultHeight());
      bpmnShape.setBounds(bounds);
      plane.getPlaneElement().add(bpmnShape);
      preferences.applyBPMNDIDefaults(bpmnShape, null);

      ModelUtil.setID(bpmnShape);
      if (doImport) importer.importShape(bpmnShape);
    }

    return bpmnShape;
  }
  public DiagramElement findDIElement(Diagram diagram, BaseElement element) {
    List<BPMNDiagram> diagrams = getAll(BPMNDiagram.class);

    for (BPMNDiagram d : diagrams) {
      List<DiagramElement> planeElement = d.getPlane().getPlaneElement();

      for (DiagramElement elem : planeElement) {
        if (elem instanceof BPMNShape
            && element.getId() != null
            && element.getId().equals(((BPMNShape) elem).getBpmnElement().getId())) {
          return (elem);
        } else if (elem instanceof BPMNEdge
            && element.getId() != null
            && element.getId().equals(((BPMNEdge) elem).getBpmnElement().getId())) {
          return (elem);
        }
      }
    }

    return null;
  }
  public static BPMNShape createDIShape(
      Shape shape,
      BaseElement elem,
      int x,
      int y,
      int w,
      int h,
      IFeatureProvider fp,
      Diagram diagram) {

    EList<EObject> businessObjects =
        Graphiti.getLinkService().getLinkForPictogramElement(diagram).getBusinessObjects();
    BPMNShape bpmnShape = null;

    for (EObject eObject : businessObjects) {
      if (eObject instanceof BPMNDiagram) {
        BPMNDiagram bpmnDiagram = (BPMNDiagram) eObject;

        bpmnShape = BpmnDiFactory.eINSTANCE.createBPMNShape();
        //				bpmnShape.setId(EcoreUtil.generateUUID());
        bpmnShape.setBpmnElement(elem);
        Bounds bounds = DcFactory.eINSTANCE.createBounds();
        bounds.setX(x);
        bounds.setY(y);
        bounds.setWidth(w);
        bounds.setHeight(h);
        bpmnShape.setBounds(bounds);

        List<DiagramElement> elements = bpmnDiagram.getPlane().getPlaneElement();
        elements.add(bpmnShape);
        ModelUtil.setID(shape);

        fp.link(shape, new Object[] {elem, bpmnShape});
        break;
      }
    }

    return bpmnShape;
  }
 @Override
 public void delete(IDeleteContext context) {
   Participant participant =
       BusinessObjectUtil.getFirstElementOfType(context.getPictogramElement(), Participant.class);
   Definitions defs = ModelUtil.getDefinitions(participant);
   Process process = participant.getProcessRef();
   if (process != null) {
     BPMNDiagram bpmnDiagram = null;
     if (defs != null) {
       for (BPMNDiagram d : defs.getDiagrams()) {
         BPMNPlane plane = d.getPlane();
         if (plane.getBpmnElement() == process) {
           bpmnDiagram = d;
           break;
         }
       }
     }
     deleteBusinessObject(process);
     if (bpmnDiagram != null) deleteBusinessObject(bpmnDiagram);
   }
   super.delete(context);
 }
  /**
   * Check if the given BaseElement has
   *
   * @param baseElement
   * @return
   */
  public static boolean hasBpmnDiagram(BaseElement baseElement) {
    BaseElement process = null;
    if (baseElement instanceof Participant) {
      process = ((Participant) baseElement).getProcessRef();
    } else if (baseElement instanceof CallActivity) {
      CallableElement ce = ((CallActivity) baseElement).getCalledElementRef();
      if (ce instanceof Process) process = (Process) ce;
    }

    if (process != null) {
      baseElement = process;
    }

    try {
      Definitions definitions = ModelUtil.getDefinitions(baseElement);
      for (BPMNDiagram d : definitions.getDiagrams()) {
        if (d.getPlane().getBpmnElement() == baseElement) return true;
      }
    } catch (Exception e) {
    }

    return false;
  }
  private BPMNEdge createDIEdge(BPMNDiagram bpmnDiagram, BaseElement bpmnElement) {
    BPMNPlane plane = bpmnDiagram.getPlane();
    BPMNEdge bpmnEdge = null;
    for (DiagramElement de : plane.getPlaneElement()) {
      if (de instanceof BPMNEdge) {
        if (bpmnElement == ((BPMNEdge) de).getBpmnElement()) {
          bpmnEdge = (BPMNEdge) de;
          break;
        }
      }
    }

    if (bpmnEdge == null) {
      bpmnEdge = BpmnDiFactory.eINSTANCE.createBPMNEdge();
      bpmnEdge.setBpmnElement(bpmnElement);

      BaseElement sourceElement = null;
      BaseElement targetElement = null;
      if (bpmnElement instanceof SequenceFlow) {
        sourceElement = ((SequenceFlow) bpmnElement).getSourceRef();
        targetElement = ((SequenceFlow) bpmnElement).getTargetRef();
      } else if (bpmnElement instanceof MessageFlow) {
        sourceElement = (BaseElement) ((MessageFlow) bpmnElement).getSourceRef();
        targetElement = (BaseElement) ((MessageFlow) bpmnElement).getTargetRef();
      } else if (bpmnElement instanceof Association) {
        sourceElement = ((Association) bpmnElement).getSourceRef();
        targetElement = ((Association) bpmnElement).getTargetRef();
      }

      if (sourceElement != null && targetElement != null) {
        DiagramElement de;
        de = DIUtils.findPlaneElement(plane.getPlaneElement(), sourceElement);
        bpmnEdge.setSourceElement(de);

        de = DIUtils.findPlaneElement(plane.getPlaneElement(), targetElement);
        bpmnEdge.setTargetElement(de);

        // the source and target elements should already have been created:
        // we know the PictogramElements for these can be found in our elements map
        Shape sourceShape = (Shape) elements.get(sourceElement);
        Shape targetShape = (Shape) elements.get(targetElement);
        //				if (sourceShape!=null && targetShape!=null) {
        //					Tuple<FixPointAnchor,FixPointAnchor> anchors =
        //							AnchorUtil.getSourceAndTargetBoundaryAnchors(sourceShape, targetShape, null);
        //					org.eclipse.graphiti.mm.algorithms.styles.Point sourceLoc =
        // GraphicsUtil.createPoint(anchors.getFirst());
        //					org.eclipse.graphiti.mm.algorithms.styles.Point targetLoc =
        // GraphicsUtil.createPoint(anchors.getSecond());
        //					Point point = DcFactory.eINSTANCE.createPoint();
        //					point.setX(sourceLoc.getX());
        //					point.setY(sourceLoc.getY());
        //					bpmnEdge.getWaypoint().add(point);
        //
        //					point = DcFactory.eINSTANCE.createPoint();
        //					point.setX(targetLoc.getX());
        //					point.setY(targetLoc.getY());
        //					bpmnEdge.getWaypoint().add(point);
        //
        //					plane.getPlaneElement().add(bpmnEdge);
        //
        //					ModelUtil.setID(bpmnEdge);
        //					importer.importConnection(bpmnEdge);
        //				}
      }
    }

    return bpmnEdge;
  }
 private void fixZOrder(BPMNDiagram bpmnDiagram) {
   EList<DiagramElement> elements =
       (EList<DiagramElement>) bpmnDiagram.getPlane().getPlaneElement();
   ECollections.sort(elements, new DIZorderComparator());
 }