private void doPasteWithImportsOperation() {
    ITextEditor editor = getTextEditor();
    IJavaElement inputElement = JavaUI.getEditorInputTypeRoot(editor.getEditorInput());

    Clipboard clipboard = new Clipboard(getDisplay());
    try {
      ClipboardData importsData = (ClipboardData) clipboard.getContents(fgTransferInstance);
      if (importsData != null
          && inputElement instanceof ICompilationUnit
          && !importsData.isFromSame(inputElement)) {
        // combine operation and adding of imports
        IRewriteTarget target = (IRewriteTarget) editor.getAdapter(IRewriteTarget.class);
        if (target != null) {
          target.beginCompoundChange();
        }
        try {
          fOperationTarget.doOperation(fOperationCode);
          addImports((ICompilationUnit) inputElement, importsData);
        } catch (CoreException e) {
          JavaPlugin.log(e);
        } finally {
          if (target != null) {
            target.endCompoundChange();
          }
        }
      } else {
        fOperationTarget.doOperation(fOperationCode);
      }
    } finally {
      clipboard.dispose();
    }
  }
  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
    }
  }