/**
   * @see
   *     edu.buffalo.cse.green.RefactorHandler#handleMove(edu.buffalo.cse.green.editor.model.RootModel,
   *     org.eclipse.jdt.core.IJavaElement, org.eclipse.jdt.core.IJavaElement)
   */
  public void handleMove(RootModel root, E sourceElement, E targetElement) {
    // get all types represented in the diagram
    String scuId = sourceElement.getHandleIdentifier();
    String tcuId = targetElement.getHandleIdentifier();

    String scuName = scuId.substring(scuId.indexOf('{') + 1, scuId.indexOf(".java"));
    String tcuName = tcuId.substring(tcuId.indexOf('{') + 1, tcuId.indexOf(".java"));

    List<IJavaElement> cuTypes = root.getElementsOfKind(TYPE);

    // see if any types belong to the compilation unit
    // that is undergoing the move event
    for (IJavaElement oType : cuTypes) {
      if (JavaModelListener.sameElements(sourceElement, oType.getAncestor(COMPILATION_UNIT))) {
        String oId = oType.getHandleIdentifier();
        String oName = oId.substring(oId.indexOf('['));

        oName = oName.replaceAll("\\[" + scuName, "[" + tcuName);

        IJavaElement nType = JavaCore.create(tcuId + oName);
        TypeModel oModel = (TypeModel) root.getModelFromElement(oType);

        TypeModel nModel = root.createTypeModel((IType) nType);
        if (oModel != null) {
          // TODO We tried to catch a ResourceException,
          // but it is caught in MemberModel
          oModel.removeFromParent();
          nModel.setLocation(oModel.getLocation());
          nModel.setSize(oModel.getSize());
        }
      }
    }
  }
  /**
   * @see
   *     edu.buffalo.cse.green.RefactorHandler#handleMove(edu.buffalo.cse.green.editor.model.RootModel,
   *     org.eclipse.jdt.core.IJavaElement, org.eclipse.jdt.core.IJavaElement)
   */
  public void handleMove(RootModel root, E sourceElement, E targetElement) {
    root.changeProjectElement(sourceElement, targetElement);

    try {
      List<IJavaElement> packages = root.getElementsOfKind(PACKAGE_FRAGMENT);

      // look through packages to match up source and destination elements
      for (IJavaElement packElement : packages) {
        IPackageFragment packFrag = (IPackageFragment) packElement;

        if (JavaModelListener.sameElements(
            packFrag.getAncestor(JAVA_PROJECT), sourceElement.getPrimaryElement())) {
          for (IPackageFragment packFrags : targetElement.getPackageFragments()) {
            // check for the same name of the two packages
            if (packFrag.getElementName().equals(packFrags.getElementName())) {
              PackageRefactorHandler.instance().handleMove(root, packFrag, packFrags);
            }
          }
        }
      }

      root.dispose();
    } catch (JavaModelException e) {
      e.printStackTrace();
    }
  }
  /**
   * @see
   *     edu.buffalo.cse.green.RefactorHandler#handleRemove(edu.buffalo.cse.green.editor.model.RootModel,
   *     org.eclipse.jdt.core.IJavaElement)
   */
  public void handleRemove(RootModel root, E element) {
    List<IJavaElement> cus = root.getElementsOfKind(COMPILATION_UNIT);

    for (IJavaElement cu : cus) {
      if (JavaModelListener.sameElements(cu.getAncestor(PACKAGE_FRAGMENT), element)) {
        CompilationUnitRefactorHandler.instance().handleRemove(root, (ICompilationUnit) cu);
      }
    }
  }
  /**
   * @see
   *     edu.buffalo.cse.green.RefactorHandler#handleRemove(edu.buffalo.cse.green.editor.model.RootModel,
   *     org.eclipse.jdt.core.IJavaElement)
   */
  public void handleRemove(RootModel root, E element) {
    List<IJavaElement> packages = root.getElementsOfKind(PACKAGE_FRAGMENT);

    for (IJavaElement pack : packages) {
      // get the project that contains the package
      IJavaElement proj = pack.getAncestor(JAVA_PROJECT);

      // if the package belongs to the project, remove it
      if (JavaModelListener.sameElements(proj, element)) {
        PackageRefactorHandler.instance().handleRemove(root, (IPackageFragment) pack);
      }
    }
  }
  /**
   * @see
   *     edu.buffalo.cse.green.RefactorHandler#handleRemove(edu.buffalo.cse.green.editor.model.RootModel,
   *     org.eclipse.jdt.core.IJavaElement)
   */
  public void handleRemove(RootModel root, E element) {
    // ignore this if the CU removed is a working copy (not actual removal)
    if (element.isWorkingCopy()) return;

    List<IJavaElement> types = new ArrayList<IJavaElement>(root.getElementsOfKind(TYPE));

    // remove any types that belong to the compilation unit
    for (IJavaElement type : types) {
      if (JavaModelListener.sameElements(element, type.getAncestor(COMPILATION_UNIT))) {
        if (root.getModelFromElement(type) != null) {
          root.getModelFromElement(type).removeFromParent();
        }
      }
    }
  }
  /**
   * @see
   *     edu.buffalo.cse.green.RefactorHandler#handleMove(edu.buffalo.cse.green.editor.model.RootModel,
   *     org.eclipse.jdt.core.IJavaElement, org.eclipse.jdt.core.IJavaElement)
   */
  public void handleMove(RootModel root, E sourceElement, E targetElement) {
    try {
      List<IJavaElement> cus = root.getElementsOfKind(COMPILATION_UNIT);
      ICompilationUnit[] newCUs = targetElement.getCompilationUnits();

      for (IJavaElement cuElement : cus) {
        ICompilationUnit iCU = (ICompilationUnit) cuElement;
        if (JavaModelListener.sameElements(iCU.getAncestor(PACKAGE_FRAGMENT), sourceElement)) {
          for (ICompilationUnit cu : newCUs) {
            if (cu.getElementName().equals(iCU.getElementName())) {
              CompilationUnitRefactorHandler.instance().handleMove(root, iCU, cu);
            }
          }
        }
      }
    } catch (JavaModelException e) {
      e.printStackTrace();
    }
  }