@Override
  public RefactoringStatus checkInitialConditions(final IProgressMonitor monitor)
      throws CoreException {
    final SubMonitor progress = SubMonitor.convert(monitor, 6);
    try {
      if (fSelectionRegion != null) {
        fSourceUnit.connect(progress.newChild(1));
        try {
          final AbstractDocument document = fSourceUnit.getDocument(monitor);

          final IRModelInfo modelInfo =
              (IRModelInfo)
                  fSourceUnit.getModelInfo(
                      RModel.TYPE_ID, IRModelManager.MODEL_FILE, progress.newChild(1));
          if (modelInfo != null) {
            final IRegion region = fAdapter.trimToAstRegion(document, fSelectionRegion);
            ISourceStructElement element =
                LTKUtil.getCoveringSourceElement(modelInfo.getSourceElement(), region);
            while (element != null) {
              if (element instanceof IRMethod) {
                fFunction = (IRMethod) element;
                break;
              }
              element = element.getSourceParent();
            }
          }

          if (fFunction != null) {
            final ISourceStructElement source = (ISourceStructElement) fFunction;
            fOperationRegion =
                fAdapter.expandSelectionRegion(document, source.getSourceRange(), fSelectionRegion);
          }
        } finally {
          fSourceUnit.disconnect(progress.newChild(1));
        }
      }

      if (fFunction == null) {
        return RefactoringStatus.createFatalErrorStatus(
            Messages.FunctionToS4Method_error_InvalidSelection_message);
      }
      final RefactoringStatus result = new RefactoringStatus();
      fAdapter.checkInitialForModification(result, fElementSet);
      progress.worked(1);

      if (result.hasFatalError()) {
        return result;
      }

      checkFunction(result);
      progress.worked(2);
      return result;
    } finally {
      progress.done();
    }
  }