/** @see MultiOperation */
  protected void verify(IJavaElement element) throws JavaModelException {
    if (element == null || !element.exists())
      error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);

    if (element.isReadOnly() && (isRename() || isMove()))
      error(IJavaModelStatusConstants.READ_ONLY, element);

    IResource resource = ((JavaElement) element).resource();
    if (resource instanceof IFolder) {
      if (resource.isLinked()) {
        error(IJavaModelStatusConstants.INVALID_RESOURCE, element);
      }
    }

    int elementType = element.getElementType();

    if (elementType == IJavaElement.COMPILATION_UNIT) {
      org.eclipse.jdt.internal.core.CompilationUnit compilationUnit =
          (org.eclipse.jdt.internal.core.CompilationUnit) element;
      if (isMove() && compilationUnit.isWorkingCopy() && !compilationUnit.isPrimary())
        error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
    } else if (elementType != IJavaElement.PACKAGE_FRAGMENT) {
      error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
    }

    JavaElement dest = (JavaElement) getDestinationParent(element);
    verifyDestination(element, dest);
    if (this.renamings != null) {
      verifyRenaming(element);
    }
  }
 protected IType[] findTypes(Object[] elements, IRunnableContext context)
     throws InterruptedException, CoreException {
   IJavaElement[] javaElements = getJavaElements(elements);
   if (javaElements.length == 1 && javaElements[0] instanceof CompilationUnit) {
     CompilationUnit unit = (CompilationUnit) javaElements[0];
     return new IType[] {unit.getTypeRoot().findPrimaryType()};
   }
   return new IType[] {};
 }
 @Override
 public void rename(String newName, boolean force, IProgressMonitor monitor)
     throws JavaModelException {
   super.rename(newName, force, monitor);
   // FIXADE we should not have to do this. Somewhere, a working copy is being created and not
   // discarded
   if (this.isWorkingCopy()) {
     this.discardWorkingCopy();
   }
 }
 @Override
 public void discardWorkingCopy() throws JavaModelException {
   // GRECLIPSE-804 must synchronize
   ModuleNodeMapper.getInstance().lock();
   try {
     PerWorkingCopyInfo info = getPerWorkingCopyInfo();
     if (workingCopyInfoWillBeDiscarded(info)) {
       ModuleNodeMapper.getInstance().remove(info);
     }
     super.discardWorkingCopy();
   } finally {
     ModuleNodeMapper.getInstance().unlock();
   }
 }
  protected void codeComplete(
      org.eclipse.jdt.internal.compiler.env.ICompilationUnit cu,
      org.eclipse.jdt.internal.compiler.env.ICompilationUnit unitToSkip,
      int position,
      CompletionRequestor requestor,
      WorkingCopyOwner owner,
      ITypeRoot typeRoot,
      IProgressMonitor monitor)
      throws JavaModelException {

    // allow a delegate to perform completion if required
    // this is used by the grails plugin when editing in gsp editor
    ICodeCompletionDelegate delegate =
        (ICodeCompletionDelegate) getAdapter(ICodeCompletionDelegate.class);
    if (delegate != null && delegate.shouldCodeComplete(requestor, typeRoot)) {
      delegate.codeComplete(cu, unitToSkip, position, requestor, owner, typeRoot, monitor);
    } else {
      super.codeComplete(cu, unitToSkip, position, requestor, owner, typeRoot, monitor);
    }
  }
  /**
   * Copies/moves a compilation unit with the name <code>newCUName</code> to the destination
   * package.<br>
   * The package statement in the compilation unit is updated if necessary. The main type of the
   * compilation unit is renamed if necessary.
   *
   * @exception JavaModelException if the operation is unable to complete
   */
  private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest)
      throws JavaModelException {
    String newCUName = getNewNameFor(source);
    String destName = (newCUName != null) ? newCUName : source.getElementName();
    TextEdit edit = updateContent(source, dest, newCUName); // null if unchanged

    // TODO (frederic) remove when bug 67606 will be fixed (bug 67823)
    // store encoding (fix bug 66898)
    IFile sourceResource = (IFile) source.getResource();
    String sourceEncoding = null;
    try {
      sourceEncoding = sourceResource.getCharset(false);
    } catch (CoreException ce) {
      // no problem, use default encoding
    }
    // end todo
    // copy resource
    IContainer destFolder = (IContainer) dest.getResource(); // can be an IFolder or an IProject
    IFile destFile = destFolder.getFile(new Path(destName));
    org.eclipse.jdt.internal.core.CompilationUnit destCU =
        new org.eclipse.jdt.internal.core.CompilationUnit(
            dest, destName, DefaultWorkingCopyOwner.PRIMARY);
    if (!destFile.equals(sourceResource)) {
      try {
        if (!destCU.isWorkingCopy()) {
          if (destFile.exists()) {
            if (this.force) {
              // we can remove it
              deleteResource(destFile, IResource.KEEP_HISTORY);
              destCU.close(); // ensure the in-memory buffer for the dest CU is closed
            } else {
              // abort
              throw new JavaModelException(
                  new JavaModelStatus(
                      IJavaModelStatusConstants.NAME_COLLISION,
                      Messages.bind(
                          Messages.status_nameCollision, destFile.getFullPath().toString())));
            }
          }
          int flags = this.force ? IResource.FORCE : IResource.NONE;
          if (isMove()) {
            flags |= IResource.KEEP_HISTORY;
            sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1));
          } else {
            if (edit != null) flags |= IResource.KEEP_HISTORY;
            sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1));
          }
          setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
        } else {
          destCU.getBuffer().setContents(source.getBuffer().getContents());
        }
      } catch (JavaModelException e) {
        throw e;
      } catch (CoreException e) {
        throw new JavaModelException(e);
      }

      // update new resource content
      if (edit != null) {
        boolean wasReadOnly = destFile.isReadOnly();
        try {
          saveContent(dest, destName, edit, sourceEncoding, destFile);
        } catch (CoreException e) {
          if (e instanceof JavaModelException) throw (JavaModelException) e;
          throw new JavaModelException(e);
        } finally {
          Util.setReadOnly(destFile, wasReadOnly);
        }
      }

      // register the correct change deltas
      prepareDeltas(source, destCU, isMove());
      if (newCUName != null) {
        // the main type has been renamed
        String oldName = Util.getNameWithoutJavaLikeExtension(source.getElementName());
        String newName = Util.getNameWithoutJavaLikeExtension(newCUName);
        prepareDeltas(source.getType(oldName), destCU.getType(newName), isMove());
      }
    } else {
      if (!this.force) {
        throw new JavaModelException(
            new JavaModelStatus(
                IJavaModelStatusConstants.NAME_COLLISION,
                Messages.bind(Messages.status_nameCollision, destFile.getFullPath().toString())));
      }
      // update new resource content
      // in case we do a saveas on the same resource we have to simply update the contents
      // see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351
      if (edit != null) {
        saveContent(dest, destName, edit, sourceEncoding, destFile);
      }
    }
  }