/**
   * This method looks for inconsistent views to delete in case the message is deleted or the
   * connector of the message is re-oriented.
   *
   * @param destructee the modified message
   * @param request the request to destroy a message or to re-orient a message
   * @return the list of {@link View} to delete
   */
  public static Set<View> getMemberViewsToDestroy(Message destructee, IEditCommandRequest request) {
    Set<View> viewsToDestroy = new HashSet<View>();
    CrossReferenceAdapter crossReferenceAdapter =
        InconsistentMessageViewsHelper.getCrossReferenceAdapter(request, destructee);
    if (crossReferenceAdapter != null) {

      Collection<Setting> revRefs =
          crossReferenceAdapter.getNonNavigableInverseReferences(destructee);
      if (!revRefs.isEmpty()) {
        for (Setting current : revRefs) {
          // test if the view is linked with the removed message
          if (current.getEObject() instanceof View) {
            View view = (View) current.getEObject();
            // we remove the view only if they are owned by the CompositeStructureDiagram
            if (InconsistentMessageViewsHelper.isOwnedByCommunicationDiagram(view)) {

              viewsToDestroy.add(view);
              if (InconsistentMessageViewsHelper.isLastMessageOnConnection(view)) {
                // remove the connector
                // System.err.println("+-> the connector will be deleted : " +
                // (View)view.eContainer());
                // System.err.println("+-> the parent of the connector is : " +
                // (View)view.eContainer().eContainer());
                viewsToDestroy.add((View) view.eContainer());
              }
            }
          }
        }
      }
    }

    return viewsToDestroy;
  }
Пример #2
0
 public void findLocalReferences(
     Set<? extends EObject> targets,
     IAcceptor<IReferenceDescription> acceptor,
     Predicate<IReferenceDescription> filter,
     IProgressMonitor monitor) {
   if (monitor != null && monitor.isCanceled()) return;
   if (targets != null && !targets.isEmpty()) {
     Set<Resource> targetResources = new HashSet<Resource>();
     for (EObject target : targets) {
       targetResources.add(target.eResource());
     }
     Map<EObject, Collection<Setting>> targetResourceInternalCrossRefs =
         CrossReferencer.find(targetResources);
     Map<EObject, URI> exportedElementsMap = null;
     SubMonitor subMonitor =
         SubMonitor.convert(monitor, Messages.ReferenceQuery_monitor, targets.size());
     for (EObject target : targets) {
       Collection<Setting> crossRefSettings = targetResourceInternalCrossRefs.get(target);
       if (crossRefSettings != null) {
         SubMonitor subSubMonitor = subMonitor.newChild(crossRefSettings.size());
         for (Setting crossRefSetting : crossRefSettings) {
           if (subSubMonitor.isCanceled()) return;
           EObject source = crossRefSetting.getEObject();
           if (crossRefSetting.getEStructuralFeature() instanceof EReference) {
             EReference reference = (EReference) crossRefSetting.getEStructuralFeature();
             int index = 0;
             if (reference.isMany()) {
               List<?> values = (List<?>) source.eGet(reference);
               for (int i = 0; i < values.size(); ++i) {
                 if (target == values.get(i)) {
                   index = i;
                   break;
                 }
               }
             }
             if (exportedElementsMap == null)
               exportedElementsMap = createExportedElementsMap(target.eResource());
             IReferenceDescription localReferenceDescription =
                 new DefaultReferenceDescription(
                     source,
                     target,
                     reference,
                     index,
                     findClosestExportedContainerURI(source, exportedElementsMap));
             if (filter == null || filter.apply(localReferenceDescription))
               acceptor.accept(localReferenceDescription);
           }
           subSubMonitor.worked(1);
         }
       }
     }
   }
 }
  /**
   * Returns a list that holds the opposite elements of the given reference for the given owner. The
   * opposite elements are those of type E that have the reference to owner.
   *
   * <p>The collection corresponding to opposite in the following picture is returned, for given
   * owner and reference.
   *
   * <pre>
   *    <b>opposite</b>            reference
   *  E ----------------------------- owner
   *  </pre>
   *
   * reference has to be a key of the map observedRefToOpposite.
   *
   * @param <E>
   * @param <E> The type of the elements in the collection.
   * @param dataClass The class of the elements in the collection.
   * @param owner The object whose list is retrieved.
   * @param reference The reference whose opposite reference is retrieved.
   * @return The opposite of reference for owner.
   */
  public <E> List<E> getOppositeList(
      Class<E> dataClass, InternalEObject owner, EReference reference) {
    EReference opposite = observedRefToOpposite.get(reference);
    if (opposite == null)
      throw new IllegalArgumentException(
          "This reference is not observed by this adapter: " + reference.toString());

    EObjectEList<E> result = new EObjectEList<E>(dataClass, owner, opposite.getFeatureID());

    for (Setting cur : getNonNavigableInverseReferences(owner, false)) {
      if (cur.getEStructuralFeature().equals(reference))
        result.add(dataClass.cast(cur.getEObject()));
    }

    return result;
  }
 public boolean apply(Setting input) {
   return ((ReferenceChange) input.getEObject()).getReference()
       == UMLPackage.Literals.LIFELINE__COVERED_BY;
 }
  /** {@inheritDoc} */
  @Override
  protected ICommand getBeforeDuplicateCommand(DuplicateElementsRequest request) {
    Object additional =
        request.getParameter(IPapyrusDuplicateCommandConstants.ADDITIONAL_DUPLICATED_ELEMENTS);
    // additional element should be a set of elements that will be duplicated. If this is null, the
    // request will be ignored.
    if (!(additional instanceof Set<?>)) {
      return super.getBeforeDuplicateCommand(request);
    }

    Set<Object> duplicatedObjects = ((Set<Object>) additional);
    EObject object = getDuplicatedEObject(request);
    if (object == null || object.eResource() == null) {
      return super.getBeforeDuplicateCommand(request);
    }

    // retrieve the Tables linked to the object
    List<Table> tablesToDuplicate = new ArrayList<Table>();

    ResourceSet resourceSet = object.eResource().getResourceSet();
    ECrossReferenceAdapter adapter = ECrossReferenceAdapter.getCrossReferenceAdapter(resourceSet);
    if (adapter == null) {
      adapter = new ECrossReferenceAdapter();
      resourceSet.eAdapters().add(adapter);
    }

    // do not proceed for graphical elements, which will have evident relationships to Diagrams
    // (owner, etc.)
    // not required for Table
    //		if(object instanceof View) {
    //			return super.getBeforeDuplicateCommand(request);
    //		}

    // check for the element itself
    Collection<Setting> settings = adapter.getInverseReferences(object, false);
    for (Setting setting : settings) {
      EObject value = setting.getEObject();
      if (value instanceof Table) {
        tablesToDuplicate.add((Table) value);
      }
    }

    // check for sub-elements
    for (Iterator<EObject> it = object.eAllContents(); it.hasNext(); ) {
      EObject child = it.next();
      settings = adapter.getInverseReferences(child, false);

      for (Setting setting : settings) {
        EObject value = setting.getEObject();
        if (value instanceof Table) {
          tablesToDuplicate.add((Table) value);
        }
      }
    }

    if (!tablesToDuplicate.isEmpty()) {
      CompositeCommand command = null;
      // create the command for all the Tables that have no command ready
      for (Table TableToDuplicate : tablesToDuplicate) {
        if (!duplicatedObjects.contains(TableToDuplicate)) {
          if (command == null) {
            command =
                new CompositeCommand(
                    "",
                    Arrays.asList(
                        new DuplicateTableCommand(
                            request.getEditingDomain(),
                            "Duplicate Table",
                            TableToDuplicate,
                            request.getAllDuplicatedElementsMap()))); // $NON-NLS-1$ //$NON-NLS-2$
          } else {
            command.add(
                new DuplicateTableCommand(
                    request.getEditingDomain(),
                    "Duplicate Table",
                    TableToDuplicate,
                    request.getAllDuplicatedElementsMap())); // $NON-NLS-1$
          }
          duplicatedObjects.add(TableToDuplicate);
        }
      }

      if (command != null) {
        if (super.getBeforeDuplicateCommand(request) != null) {
          command.add(super.getBeforeDuplicateCommand(request));
          return command.reduce();
        } else {
          return command.reduce();
        }
      }
    }

    return super.getBeforeDuplicateCommand(request);
  }