private void addImports(final ICompilationUnit unit, ClipboardData data) throws CoreException {
    final ImportRewrite rewrite = StubUtility.createImportRewrite(unit, true);
    String[] imports = data.getTypeImports();
    for (int i = 0; i < imports.length; i++) {
      rewrite.addImport(imports[i]);
    }
    String[] staticImports = data.getStaticImports();
    for (int i = 0; i < staticImports.length; i++) {
      String name = Signature.getSimpleName(staticImports[i]);
      boolean isField = !name.endsWith("()"); // $NON-NLS-1$
      if (!isField) {
        name = name.substring(0, name.length() - 2);
      }
      String qualifier = Signature.getQualifier(staticImports[i]);
      rewrite.addStaticImport(qualifier, name, isField);
    }

    try {
      getProgressService()
          .busyCursorWhile(
              new IRunnableWithProgress() {
                public void run(IProgressMonitor monitor)
                    throws InvocationTargetException, InterruptedException {
                  try {
                    JavaModelUtil.applyEdit(unit, rewrite.rewriteImports(monitor), false, null);
                  } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                  }
                }
              });
    } catch (InvocationTargetException e) {
      Throwable cause = e.getCause();
      if (cause instanceof CoreException) throw (CoreException) cause;
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              JavaUI.ID_PLUGIN,
              IJavaStatusConstants.INTERNAL_ERROR,
              JavaUIMessages.JavaPlugin_internal_error,
              cause));
    } catch (InterruptedException e) {
      // Canceled by the user
    }
  }
  public static URL getJavadocLocation(IJavaElement element, boolean includeMemberReference)
      throws JavaModelException {
    URL baseLocation = getJavadocBaseLocation(element);
    if (baseLocation == null) {
      return null;
    }

    String urlString = baseLocation.toExternalForm();

    StringBuffer urlBuffer = new StringBuffer(urlString);
    if (!urlString.endsWith("/")) { // $NON-NLS-1$
      urlBuffer.append('/');
    }

    StringBuffer pathBuffer = new StringBuffer();
    StringBuffer fragmentBuffer = new StringBuffer();

    switch (element.getElementType()) {
      case IJavaElement.PACKAGE_FRAGMENT:
        appendPackageSummaryPath((IPackageFragment) element, pathBuffer);
        break;
      case IJavaElement.JAVA_PROJECT:
      case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        appendIndexPath(pathBuffer);
        break;
      case IJavaElement.IMPORT_CONTAINER:
        element = element.getParent();
        // $FALL-THROUGH$
      case IJavaElement.COMPILATION_UNIT:
        IType mainType = ((ICompilationUnit) element).findPrimaryType();
        if (mainType == null) {
          return null;
        }
        appendTypePath(mainType, pathBuffer);
        break;
      case IJavaElement.CLASS_FILE:
        appendTypePath(((IClassFile) element).getType(), pathBuffer);
        break;
      case IJavaElement.TYPE:
        appendTypePath((IType) element, pathBuffer);
        break;
      case IJavaElement.FIELD:
        IField field = (IField) element;
        appendTypePath(field.getDeclaringType(), pathBuffer);
        if (includeMemberReference) {
          appendFieldReference(field, fragmentBuffer);
        }
        break;
      case IJavaElement.METHOD:
        IMethod method = (IMethod) element;
        appendTypePath(method.getDeclaringType(), pathBuffer);
        if (includeMemberReference) {
          appendMethodReference(method, fragmentBuffer);
        }
        break;
      case IJavaElement.INITIALIZER:
        appendTypePath(((IMember) element).getDeclaringType(), pathBuffer);
        break;
      case IJavaElement.IMPORT_DECLARATION:
        IImportDeclaration decl = (IImportDeclaration) element;

        if (decl.isOnDemand()) {
          IJavaElement cont =
              JavaModelUtil.findTypeContainer(
                  element.getJavaProject(), Signature.getQualifier(decl.getElementName()));
          if (cont instanceof IType) {
            appendTypePath((IType) cont, pathBuffer);
          } else if (cont instanceof IPackageFragment) {
            appendPackageSummaryPath((IPackageFragment) cont, pathBuffer);
          }
        } else {
          IType imp = element.getJavaProject().findType(decl.getElementName());
          appendTypePath(imp, pathBuffer);
        }
        break;
      case IJavaElement.PACKAGE_DECLARATION:
        IJavaElement pack = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        if (pack != null) {
          appendPackageSummaryPath((IPackageFragment) pack, pathBuffer);
        } else {
          return null;
        }
        break;
      default:
        return null;
    }

    try {
      String fragment = fragmentBuffer.length() == 0 ? null : fragmentBuffer.toString();
      try {
        URI relativeURI = new URI(null, null, pathBuffer.toString(), fragment);
        urlBuffer.append(relativeURI.toString());
        return new URL(urlBuffer.toString());
      } catch (URISyntaxException e) {
        JavaPlugin.log(e);
        return new URL(urlBuffer.append(pathBuffer).toString());
      }
    } catch (MalformedURLException e) {
      JavaPlugin.log(e);
    }
    return null;
  }
  /*
   * @see org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal#computeReplacementString()
   */
  @Override
  protected String computeReplacementString() {
    String replacement = super.computeReplacementString();

    /* No import rewriting ever from within the import section. */
    if (isImportCompletion()) return replacement;

    /* Always use the simple name for non-formal javadoc references to types. */
    // TODO fix
    if (fProposal.getKind() == CompletionProposal.TYPE_REF
        && fInvocationContext.getCoreContext().isInJavadocText()) return getSimpleTypeName();

    String qualifiedTypeName = getQualifiedTypeName();

    // Type in package info must be fully qualified.
    if (fCompilationUnit != null && JavaModelUtil.isPackageInfo(fCompilationUnit))
      return qualifiedTypeName;

    if (qualifiedTypeName.indexOf('.') == -1 && replacement.length() > 0)
      // default package - no imports needed
      return qualifiedTypeName;

    /*
     * If the user types in the qualification, don't force import rewriting on him - insert the
     * qualified name.
     */
    IDocument document = fInvocationContext.getDocument();
    if (document != null) {
      String prefix = getPrefix(document, getReplacementOffset() + getReplacementLength());
      int dotIndex = prefix.lastIndexOf('.');
      // match up to the last dot in order to make higher level matching still work (camel case...)
      if (dotIndex != -1
          && qualifiedTypeName
              .toLowerCase()
              .startsWith(prefix.substring(0, dotIndex + 1).toLowerCase()))
        return qualifiedTypeName;
    }

    /*
     * The replacement does not contain a qualification (e.g. an inner type qualified by its
     * parent) - use the replacement directly.
     */
    if (replacement.indexOf('.') == -1) {
      if (isInJavadoc())
        return getSimpleTypeName(); // don't use the braces added for javadoc link proposals
      return replacement;
    }

    /* Add imports if the preference is on. */
    if (fImportRewrite == null) fImportRewrite = createImportRewrite();
    if (fImportRewrite != null) {
      return fImportRewrite.addImport(qualifiedTypeName, fImportContext);
    }

    // fall back for the case we don't have an import rewrite (see allowAddingImports)

    /* No imports for implicit imports. */
    if (fCompilationUnit != null
        && JavaModelUtil.isImplicitImport(
            Signature.getQualifier(qualifiedTypeName), fCompilationUnit)) {
      return Signature.getSimpleName(qualifiedTypeName);
    }

    /* Default: use the fully qualified type name. */
    return qualifiedTypeName;
  }