@Override
  public String eURIFragmentSegment(EStructuralFeature eStructuralFeature, EObject eObject) {
    if (eObject instanceof ENamedElement) {
      ENamedElement eNamedElement = (ENamedElement) eObject;
      String name = eNamedElement.getName();
      if (name != null) {
        int count = 0;
        for (Object otherEObject : eContents()) {
          if (otherEObject == eObject) {
            break;
          }
          if (otherEObject instanceof ENamedElement) {
            ENamedElement otherENamedElement = (ENamedElement) otherEObject;
            if (name.equals(otherENamedElement.getName())) {
              ++count;
            }
          }
        }
        name = eEncodeValue(name);
        return count > 0 ? name + "." + count : name;
      }
    } else if (eObject instanceof EAnnotation) {
      EAnnotation eAnnotation = (EAnnotation) eObject;
      String source = eAnnotation.getSource();
      if (source != null) {
        int count = 0;
        for (Object otherEObject : eContents()) {
          if (otherEObject == eObject) {
            break;
          }
          if (otherEObject instanceof EAnnotation) {
            EAnnotation otherEAnnotation = (EAnnotation) otherEObject;
            if (source.equals(otherEAnnotation.getSource())) {
              ++count;
            }
          }
        }

        StringBuffer result = new StringBuffer(source.length() + 5);
        result.append('%');
        result.append(URI.encodeSegment(source, false));
        result.append('%');
        if (count > 0) {
          result.append('.');
          result.append(count);
        }
        return result.toString();
      }
    }
    return super.eURIFragmentSegment(eStructuralFeature, eObject);
  }
 @Override
 public HyperLinkEditor getHyperLinkObjectForEAnnotation(EAnnotation eAnnotation) {
   if (eAnnotation.getSource().equals(HyperLinkDiagramConstants.HYPERLINK_DIAGRAM)) {
     HyperLinkDiagram hyperLinkDiagram = new HyperLinkDiagram();
     EList<EObject> list = eAnnotation.getReferences();
     if (list.size() > 0) {
       if (eAnnotation.getReferences().get(0).eResource() != null) {
         hyperLinkDiagram.setDiagram((Diagram) eAnnotation.getReferences().get(0));
         hyperLinkDiagram.setTooltipText(
             eAnnotation.getDetails().get(HyperLinkConstants.HYPERLINK_TOOLTYPE_TEXT));
         if (eAnnotation.getDetails().get(HyperLinkConstants.HYPERLINK_IS_DEFAULT_NAVIGATION)
             != null) {
           String stringboolean =
               eAnnotation.getDetails().get(HyperLinkConstants.HYPERLINK_IS_DEFAULT_NAVIGATION);
           boolean isDefaultNaviagation = Boolean.parseBoolean(stringboolean);
           hyperLinkDiagram.setIsDefault(isDefaultNaviagation);
         } else {
           hyperLinkDiagram.setIsDefault(false);
         }
         return hyperLinkDiagram;
       }
     }
   }
   return null;
 }
  /**
   *
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   *
   * @generated NOT
   */
  public EAnnotation getEAnnotation(String source) {
    if (eAnnotations != null) {
      if (eAnnotations instanceof BasicEList) {
        int size = eAnnotations.size();
        if (size > 0) {
          EAnnotation[] eAnnotationArray = (EAnnotation[]) ((BasicEList<?>) eAnnotations).data();
          if (source == null) {
            for (int i = 0; i < size; ++i) {
              EAnnotation eAnnotation = eAnnotationArray[i];
              if (eAnnotation.getSource() == null) {
                return eAnnotation;
              }
            }
          } else {
            for (int i = 0; i < size; ++i) {
              EAnnotation eAnnotation = eAnnotationArray[i];
              if (source.equals(eAnnotation.getSource())) {
                return eAnnotation;
              }
            }
          }
        }
      } else {
        if (source == null) {
          for (EAnnotation eAnnotation : eAnnotations) {
            if (eAnnotation.getSource() == null) {
              return eAnnotation;
            }
          }
        } else {
          for (EAnnotation eAnnotation : eAnnotations) {
            if (source.equals(eAnnotation.getSource())) {
              return eAnnotation;
            }
          }
        }
      }
    }

    return null;
  }
  protected String getPivotModelAnnotation(GenClass genClass) {

    // find out the pivot model type this class should be mapped to
    for (EAnnotation annotation : genClass.getEcoreClass().getEAnnotations()) {
      if (annotation
          .getSource()
          .equalsIgnoreCase("http://www.tu-dresden.de/ocl20/pivot/2007/pivotmodel")) {
        return PivotAdapterGeneratorUtil.getAnnotationDetails(annotation);
      }
    }
    return null;
  }
 private boolean removeAnnotation(EOperation existingOperation) {
   List<EAnnotation> annotations = existingOperation.getEAnnotations();
   EAnnotation annotationToRemove = null;
   for (EAnnotation annotation : annotations) {
     if (annotation.getSource().equals(EcoreModelRefactorer.class.getName())) {
       annotationToRemove = annotation;
     }
   }
   if (annotationToRemove != null) {
     return annotations.remove(annotationToRemove);
   }
   return false;
 }
  public Object execute(ExecutionEvent event) throws ExecutionException {
    String tag = null;
    boolean copyLibraries = false;
    try {
      copyLibraries =
          (event.getParameter("emf-rest.copyLibraries") == null
              ? false
              : Boolean.valueOf(event.getParameter("emf-rest.copyLibraries")));
      tag = event.getParameter("emf-rest.tagParameter");
    } catch (Exception e) {
      e.printStackTrace();
    }
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    IStructuredSelection selection =
        (IStructuredSelection) window.getSelectionService().getSelection();
    IResource ecoreResource = (IResource) selection.getFirstElement();

    if (tag != null && tag.equals("None")) {
      GenerationListener genlistener =
          new GenerationListener(ecoreResource.getProject(), Platform.getBundle("emf-rest"), true);
      genlistener.afterFinishExecution(null, null, null);
      return null;
    }

    HashMap<String, Object> variables = new HashMap<String, Object>();
    variables.put("ecore_file", ecoreResource.getLocation().toOSString());
    variables.put(
        "genModel_file",
        ecoreResource.getLocation().toOSString().replaceFirst(".ecore", ".genmodel"));
    variables.put(
        "output", ecoreResource.getProject().findMember("src").getLocation().toOSString());
    try {
      variables.put(
          "modelDirectory",
          ecoreResource.getProject().findMember("WebContent/models").getLocation().toOSString());
    } catch (Exception ex) {
      variables.put("modelDirectory", "");
    }
    File scriptDir =
        new File(ecoreResource.getProject().getLocation().toOSString() + "/WebContent/scripts");
    if (tag == null || tag.equals("javascript")) {
      scriptDir.mkdirs();
    }
    variables.put(
        "output_js", ecoreResource.getProject().getLocation().toOSString() + "/WebContent/scripts");
    HashMap<String, GenPackage> usedGenPackages = new HashMap<String, GenPackage>();

    try {
      ResourceSet eCoreResourceSet = new ResourceSetImpl();
      Resource eCoreResource =
          eCoreResourceSet.createResource(
              URI.createFileURI(ecoreResource.getLocation().toOSString()));
      eCoreResource.load(null);
      EPackage ePackage = (EPackage) eCoreResource.getContents().get(0);
      for (EAnnotation eAnnotation : ePackage.getEAnnotations()) {
        if (eAnnotation.getSource().equalsIgnoreCase("emf.gen.usedGenPackage")) {
          ResourceSet genModelResourceSet = new ResourceSetImpl();
          Resource genModelResource =
              genModelResourceSet.createResource(
                  URI.createURI(eAnnotation.getDetails().get("genModel"), false));
          genModelResource.load(null);
          GenModel genModel = (GenModel) genModelResource.getContents().get(0);
          for (GenPackage genPackage : genModel.getGenPackages()) {
            String usedGenPackageName = eAnnotation.getDetails().get("name");
            if (genPackage.getEcorePackage().getName().equals(usedGenPackageName)) {
              usedGenPackages.put(usedGenPackageName, genPackage);
            }
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    variables.put("usedGenPackages", usedGenPackages);
    variables.put("pluginName", ecoreResource.getProject().getName());
    variables.put("foreignModel", ecoreResource.getName());

    File webContentModelsPath =
        new File(
            ecoreResource.getProject().findMember("WebContent/models").getLocation().toOSString());
    String modelName = "";
    for (File file : webContentModelsPath.listFiles()) {
      if (file.getName().endsWith(".xmi")) {
        modelName = file.getName().substring(0, file.getName().lastIndexOf("."));
      }
    }
    variables.put("modelName", modelName);

    MTCChoreographer choreographer =
        MTCStandaloneUtils.startMTC(
            ecoreResource.getProject(),
            "emf-rest",
            "default.mtc",
            variables,
            (tag == null ? null : Collections.singletonList(tag)));
    ArrayList<IResource> resourceToRefresh = new ArrayList<IResource>();
    resourceToRefresh.add(ecoreResource.getProject());
    choreographer.addExecutionListener(
        new GenerationListener(
            ecoreResource.getProject(), Platform.getBundle("emf-rest"), copyLibraries));
    choreographer.start();
    choreographer.modelReady("Ecore Fully Annotated", null);
    return null;
  }
  @Override
  public EObject eObjectForURIFragmentSegment(String uriFragmentSegment) {
    int length = uriFragmentSegment.length();
    if (length > 0) {
      // Is the first character a special character, i.e., something other than '@'?
      //
      char firstCharacter = uriFragmentSegment.charAt(0);
      if (firstCharacter != '@') {
        // Is it the start of a source URI of an annotation?
        //
        if (firstCharacter == '%') {
          // Find the closing '%'
          //
          int index = uriFragmentSegment.lastIndexOf("%");
          boolean hasCount = false;
          if (index == length - 1 || (hasCount = uriFragmentSegment.charAt(index + 1) == '.')) {
            // Decode all encoded characters.
            //
            String source = URI.decode(uriFragmentSegment.substring(1, index));

            // Check for a count, i.e., a '.' followed by a number.
            //
            int count = 0;
            if (hasCount) {
              try {
                count = Integer.parseInt(uriFragmentSegment.substring(index + 2));
              } catch (NumberFormatException exception) {
                throw new WrappedException(exception);
              }
            }

            // Look for the annotation with the matching source.
            //
            for (Object object : eContents()) {
              if (object instanceof EAnnotation) {
                EAnnotation eAnnotation = (EAnnotation) object;
                if (source.equals(eAnnotation.getSource()) && count-- == 0) {
                  return eAnnotation;
                }
              }
            }
            return null;
          }
        }

        // Look for trailing count.
        //
        int index = uriFragmentSegment.lastIndexOf(".");
        String name = index == -1 ? uriFragmentSegment : uriFragmentSegment.substring(0, index);
        int count = 0;
        if (index != -1) {
          try {
            count = Integer.parseInt(uriFragmentSegment.substring(index + 1));
          } catch (NumberFormatException exception) {
            // Interpret it as part of the name.
            //
            name = uriFragmentSegment;
          }
        }

        name = URI.decode(name);

        // Look for a matching named element.
        //
        for (Object object : eContents()) {
          if (object instanceof ENamedElement) {
            ENamedElement eNamedElement = (ENamedElement) object;
            if (name.equals(eNamedElement.getName()) && count-- == 0) {
              return eNamedElement;
            }
          }
        }

        return null;
      }
    }

    return super.eObjectForURIFragmentSegment(uriFragmentSegment);
  }