Ejemplo n.º 1
0
  /**
   * Returns the name of the type with its qualified name
   *
   * @param type a type
   * @return the name of the type with its qualified name
   */
  public static String getTypeLabel(Type type, Namespace model) {
    String label = ""; // $NON-NLS-1$

    List<Package> importedPackages = new ArrayList<Package>(model.getImportedPackages());

    List<Package> visitedPackages = new ArrayList<Package>();
    Package currentPackage = type.getNearestPackage();

    boolean rootFound = false;

    while (currentPackage != null && !rootFound) {
      visitedPackages.add(currentPackage);
      if (importedPackages.contains(currentPackage) || currentPackage == model) {
        rootFound = true;
      }
      Element owner = currentPackage.getOwner();
      while (owner != null && !(owner instanceof Package)) owner = owner.getOwner();

      currentPackage = owner != null ? (Package) owner : null;
    }

    for (int i = visitedPackages.size() - 1; i >= 0; i--) {
      label += visitedPackages.get(i).getName() + "::"; // $NON-NLS-1$
    }

    return label + type.getName();
  }
Ejemplo n.º 2
0
 /**
  * return the top-level owner of an element. This function returns the same value as getModel, if
  * the top-level element is a model. While this is the case for models, model libraries have a
  * top-level package (not a model). In this case, getTop returns the top-level package whereas
  * getModel would return null.
  *
  * @param element
  * @return the top-level owning package
  */
 public static Package getTop(Element element) {
   while (element != null) {
     Element owner = element.getOwner();
     if (owner == null) {
       if (element instanceof Package) {
         return (Package) element;
       }
     }
     element = owner;
   }
   return null;
 }
  /**
   * Drop a semantic element and create the corresponding view in the given container
   *
   * @param newContainer Semantic container
   * @param semanticElement Element to drop
   * @param containerView Container view
   * @param moveSemanticElement True to move the dropped semantic element or false to just show the
   *     element on a diagram
   */
  private void drop(
      final Element newContainer,
      final Element semanticElement,
      final DSemanticDecorator containerView,
      boolean moveSemanticElement) {
    final Session session = SessionManager.INSTANCE.getSession(newContainer);
    final Element oldContainer = semanticElement.getOwner();
    if (moveSemanticElement && oldContainer != newContainer) {
      // Manage stereotypes and profiles
      final List<Stereotype> stereotypesToApply = Lists.newArrayList();
      for (final Stereotype stereotype : semanticElement.getAppliedStereotypes()) {
        stereotypesToApply.add(stereotype);
      }

      // Move the semantic element to the selected container
      final TransactionalEditingDomain domain = session.getTransactionalEditingDomain();
      // The feature is set to null because the domain will deduce it
      Command cmd = AddCommand.create(domain, newContainer, null, semanticElement);
      if (cmd.canExecute()) {
        cmd.execute();
      }
      cmd = RemoveCommand.create(domain, oldContainer, null, semanticElement);
      if (cmd.canExecute()) {
        cmd.execute();
      }

      if (semanticElement instanceof UseCase) {
        // Reset the current element as subject
        cmd =
            SetCommand.create(
                domain,
                semanticElement,
                UMLPackage.Literals.USE_CASE__SUBJECT,
                SetCommand.UNSET_VALUE);
        if (cmd.canExecute()) {
          cmd.execute();
        }
        final List<Element> subjects = new ArrayList<Element>();
        subjects.add(newContainer);
        cmd =
            SetCommand.create(
                domain, semanticElement, UMLPackage.Literals.USE_CASE__SUBJECT, subjects);
        if (cmd.canExecute()) {
          cmd.execute();
        }
      }
      // Apply stereotypes on dropped element and apply the necessary
      // profiles automatically
      StereotypeServices.INSTANCE.applyAllStereotypes(semanticElement, stereotypesToApply);

      // Check if profile should be unapplied
      for (final Stereotype stereotype : stereotypesToApply) {
        StereotypeServices.INSTANCE.unapplyProfile(oldContainer, stereotype);
      }
    }

    // Show the semantic element on the diagram
    showView(
        semanticElement,
        containerView,
        session,
        "[self.getContainerView(newContainerView)/]"); //$NON-NLS-1$
  }