/**
   * {@inheritDoc}
   *
   * @see
   *     org.eclipse.sirius.diagram.business.api.componentization.DiagramMappingsManagerRegistry#getDiagramMappingsManager(Session,
   *     DDiagram)
   */
  @Override
  public DiagramMappingsManager getDiagramMappingsManager(
      final Session session, final DDiagram diagram) {
    if (diagram == null) {
      throw new IllegalArgumentException(
          Messages.DiagramMappingsManagerRegistryImpl_diagramParamErrorMsg);
    }
    if (diagramMappingsManagers.containsKey(diagram)) {
      return diagramMappingsManagers.get(diagram);
    } else {
      final DiagramDescription desc = diagram.getDescription();
      final DiagramDescriptionMappingsRegistry mappingsRegistry =
          DiagramDescriptionMappingsRegistry.INSTANCE;
      final DiagramDescriptionMappingsManager descManager =
          mappingsRegistry.getDiagramDescriptionMappingsManager(session, desc);

      final DiagramMappingsManager newManager =
          new DiagramMappingsManagerImpl(diagram, descManager);
      diagram.eAdapters().add(this);
      if (session != null) {
        newManager.computeMappings(session.getSelectedViewpoints(false), false);
      } else {
        newManager.computeMappings(null, false);
      }
      diagramMappingsManagers.put(diagram, newManager);
      return newManager;
    }
  }
  /**
   * Get all available elements.
   *
   * @return the available elements
   */
  protected Collection<?> getAvailableElements() {
    final Collection<FilterDescription> result = new HashSet<FilterDescription>();

    if (diagram != null && diagram.getDescription() != null) {
      final DiagramDescription desc = diagram.getDescription();
      result.addAll(desc.getFilters());
    }
    result.removeAll(getAppliedElements());
    return result;
  }
 /**
  * Get the {@link ComputedStyleDescriptionRegistry} of the specified {@link DDiagram}.
  *
  * @param createIfNotExists true if we want to create a {@link ComputedStyleDescriptionRegistry}
  *     for the specified {@link DDiagram} if there is not one, false otherwise
  * @return the {@link ComputedStyleDescriptionRegistry} of the {@link DDiagram} or null if this
  *     last has not one
  */
 public ComputedStyleDescriptionRegistry getComputedStyleDescriptionRegistry(
     boolean createIfNotExists) {
   ComputedStyleDescriptionRegistry computedStyleDescriptionRegistry = null;
   AnnotationEntry annotationEntry = null;
   Collection<AnnotationEntry> annotationEntries =
       new DRepresentationQuery(dDiagram)
           .getAnnotation(BestStyleDescriptionRegistry.DANNOTATION_CUSTOMIZATION_KEY);
   if (annotationEntries == null || annotationEntries.isEmpty()) {
     annotationEntry = DescriptionFactory.eINSTANCE.createAnnotationEntry();
     annotationEntry.setSource(BestStyleDescriptionRegistry.DANNOTATION_CUSTOMIZATION_KEY);
     dDiagram.getOwnedAnnotationEntries().add(annotationEntry);
   } else {
     annotationEntry = annotationEntries.iterator().next();
   }
   if (annotationEntry.getData() == null
       || !(annotationEntry.getData() instanceof ComputedStyleDescriptionRegistry)) {
     computedStyleDescriptionRegistry =
         DiagramFactory.eINSTANCE.createComputedStyleDescriptionRegistry();
     annotationEntry.setData(computedStyleDescriptionRegistry);
   } else {
     computedStyleDescriptionRegistry =
         (ComputedStyleDescriptionRegistry) annotationEntry.getData();
   }
   return computedStyleDescriptionRegistry;
 }
 /**
  * Get all applied elements.
  *
  * @return the applied elements.
  */
 protected Collection<?> getAppliedElements() {
   final Collection<FilterDescription> result = new HashSet<FilterDescription>();
   if (diagram != null) {
     result.addAll(diagram.getActivatedFilters());
   }
   return result;
 }
 /**
  * Return all activated filters for the specified diagram.
  *
  * @return all activated filters for the specified diagram.
  */
 public Collection<FilterDescription> getAllFilters() {
   final Collection<FilterDescription> result = new ArrayList<FilterDescription>();
   /*
    * check the activated filters
    */
   result.addAll(dDiagram.getActivatedFilters());
   return result;
 }
  /**
   * Indicates if the given ddiagram is allowing pin/unpin.
   *
   * @param diagram the diagram to inspect
   * @return true if the given ddiagram is allowing layouting mode, false otherwise
   */
  private static Predicate<DDiagramElement> allowsPinUnpin(DDiagram diagram) {
    // default return value is true for non-Region element (for basic
    // DDiagram that are not handled
    // by any DiagramDescriptionProvider).
    Predicate<DDiagramElement> result =
        new Predicate<DDiagramElement>() {
          public boolean apply(DDiagramElement dde) {
            if (dde instanceof DDiagramElementContainer) {
              DDiagramElementContainerExperimentalQuery query =
                  new DDiagramElementContainerExperimentalQuery((DDiagramElementContainer) dde);
              return !query.isRegion();
            }
            return true;
          }
        };

    // If an aird has been opened from the Package Explorer View, then
    // we return false as no diagram is associated to this editor
    if (diagram == null || diagram.getDescription() == null) {
      return Predicates.alwaysFalse();
    }

    // If diagram is not null, we search for a possible
    // DiagramDescriptionProvider handling this type of diagram
    for (final IDiagramTypeDescriptor diagramTypeDescriptor :
        DiagramTypeDescriptorRegistry.getInstance().getAllDiagramTypeDescriptors()) {
      if (diagramTypeDescriptor
          .getDiagramDescriptionProvider()
          .handles(diagram.getDescription().eClass().getEPackage())) {
        // This DiagramDescriptionProvider may forbid pin/unpin actions.
        final IDiagramDescriptionProvider provider =
            diagramTypeDescriptor.getDiagramDescriptionProvider();
        result =
            new Predicate<DDiagramElement>() {
              @Override
              public boolean apply(DDiagramElement input) {
                return provider.allowsPinUnpin(input);
              }
            };
        break;
      }
    }

    return result;
  }
 /**
  * Return all edges owned by the specified diagram.
  *
  * @return all edges owned by the specified diagram.
  */
 public Collection<DEdge> getEdges() {
   final Collection<DEdge> result = new ArrayList<DEdge>();
   final Iterator<DDiagramElement> it = dDiagram.getOwnedDiagramElements().iterator();
   while (it.hasNext()) {
     final DDiagramElement elem = it.next();
     if (elem instanceof DEdge) {
       result.add((DEdge) elem);
     }
   }
   return result;
 }
 /**
  * Return all node list elements owned by the specified diagram.
  *
  * @return Return all node list elements owned by the specified diagram.
  */
 public Collection<DNodeListElement> getNodeListElements() {
   final Collection<DNodeListElement> result = new ArrayList<DNodeListElement>();
   final Iterator<DDiagramElement> it = dDiagram.getOwnedDiagramElements().iterator();
   while (it.hasNext()) {
     final DDiagramElement elem = it.next();
     if (elem instanceof DNodeListElement) {
       final DNodeListElement nodeListElement = (DNodeListElement) elem;
       result.add(nodeListElement);
     }
     if (elem instanceof DDiagramElementContainer) {
       addDNodeListElements((DDiagramElementContainer) elem, result);
     }
   }
   return result;
 }
 /**
  * Implementation of {@link DDiagram#getContainers()}.
  *
  * @return all containers of the diagram.
  */
 public Collection<DDiagramElementContainer> getContainers() {
   final Collection<DDiagramElementContainer> result = new ArrayList<DDiagramElementContainer>();
   final Iterator<DDiagramElement> it = dDiagram.getOwnedDiagramElements().iterator();
   while (it.hasNext()) {
     final DDiagramElement elem = it.next();
     if (elem instanceof DDiagramElementContainer) {
       result.add((DDiagramElementContainer) elem);
     }
     if (elem instanceof DNodeContainer) {
       addDiagramElementContainers(dDiagram, (DNodeContainer) elem, result);
     }
   }
   return result;
   // return new EcoreEList.UnmodifiableEList(eInternalContainer(),
   // ViewpointPackage.eINSTANCE.getSirius_Containers(), result.size(),
   // result.toArray());
 }
  /**
   * Check if a diagram is empty.
   *
   * @param diagram diagram to check
   * @return true if no element is present in diagram
   */
  public boolean isDiagramEmpty(DDiagram diagram) {
    final List<EObject> elements = new ArrayList<EObject>();
    for (final EObject object : diagram.getDiagramElements()) {
      if (!(object instanceof DSemanticDiagram)) {
        if (object instanceof DNodeSpec) {
          // ignore empty diagram image
          if (!((DNodeSpec) object)
              .getActualMapping()
              .getName()
              .equals("Empty Diagram")) { // $NON-NLS-1$
            elements.add(object);
          }
        } else {
          elements.add(object);
        }
      }
    }

    if (elements.size() == 0) {
      return true;
    }
    return false;
  }
 /**
  * Returns the description of this D&D target.
  *
  * @return the description of this D&D target.
  */
 public DragAndDropTargetDescription getDragAndDropDescription() {
   return dDiagram.getDescription();
 }
 private void removeSemanticListener(final DDiagram dia) {
   dia.eAdapters().remove(viewpointListener);
 }
 private void addSemanticListener(final DDiagram viewPoint) {
   viewPoint.eAdapters().add(viewpointListener);
 }