/**
   * Attaches a coverage annotation model for the given editor if the editor can be annotated. Does
   * nothing if the model is already attached.
   *
   * @param editor Editor to attach a annotation model to
   */
  public static boolean attach(ITextEditor editor) {
    IDocumentProvider provider = editor.getDocumentProvider();
    IEditorInput input = editor.getEditorInput();

    if (provider != null && input instanceof FileEditorInput) {
      IAnnotationModel model = provider.getAnnotationModel(input);

      if (model instanceof IAnnotationModelExtension) {
        IAnnotationModelExtension modelex = (IAnnotationModelExtension) model;

        ColorAnnotationModel colormodel = (ColorAnnotationModel) modelex.getAnnotationModel(KEY);

        if (colormodel == null) {
          IFile file = ((FileEditorInput) input).getFile();
          IFeatureProject project = CorePlugin.getFeatureProject(file);
          if (project != null
              && project.getComposer() != null
              && project.getComposer().needColor()) {
            IDocument document = provider.getDocument(input);
            colormodel = new ColorAnnotationModel(document, file, project, editor);
            modelex.addAnnotationModel(KEY, colormodel);

            return true;
          }
        } else {
          colormodel.updateAnnotations(!editor.isDirty());
        }
      }
    }
    return false;
  }
  /**
   * Attaches a coverage annotation model for the given editor if the editor can be annotated. Does
   * nothing if the model is already attached.
   *
   * @param editor Editor to attach a annotation model to
   */
  public static void attach(ITextEditor editor) {
    IDocumentProvider provider = editor.getDocumentProvider();
    // there may be text editors without document providers (SF #1725100)
    if (provider == null) return;
    IAnnotationModel model = provider.getAnnotationModel(editor.getEditorInput());
    if (!(model instanceof IAnnotationModelExtension)) return;
    IAnnotationModelExtension modelex = (IAnnotationModelExtension) model;

    IDocument document = provider.getDocument(editor.getEditorInput());

    CoverageAnnotationModel coveragemodel =
        (CoverageAnnotationModel) modelex.getAnnotationModel(KEY);
    if (coveragemodel == null) {
      coveragemodel = new CoverageAnnotationModel(editor, document);
      modelex.addAnnotationModel(KEY, coveragemodel);
    }
  }
  public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
    if (oldInput != null) {
      annotationModel.disconnect(oldInput);
    }
    if (newInput != null && sourceViewer != null) {
      IAnnotationModel originalAnnotationModel = sourceViewer.getAnnotationModel();
      if (originalAnnotationModel instanceof IAnnotationModelExtension) {
        IAnnotationModelExtension annotationModelExtension =
            (IAnnotationModelExtension) originalAnnotationModel;
        annotationModelExtension.addAnnotationModel(
            ReviewsUiPlugin.PLUGIN_ID, originalAnnotationModel);
      } else {
        try {
          Class<SourceViewer> sourceViewerClazz = SourceViewer.class;
          Field declaredField2 = sourceViewerClazz.getDeclaredField("fVisualAnnotationModel");
          declaredField2.setAccessible(true);
          Method declaredMethod =
              sourceViewerClazz.getDeclaredMethod(
                  "createVisualAnnotationModel", IAnnotationModel.class);
          declaredMethod.setAccessible(true);
          originalAnnotationModel =
              (IAnnotationModel) declaredMethod.invoke(sourceViewer, annotationModel);
          declaredField2.set(sourceViewer, originalAnnotationModel);
          originalAnnotationModel.connect(newInput);
          sourceViewer.showAnnotations(true);

          createVerticalRuler(newInput, sourceViewerClazz);
          createOverviewRuler(newInput, sourceViewerClazz);
          createHighlighting(sourceViewerClazz);
        } catch (Throwable t) {
          StatusHandler.log(
              new Status(
                  IStatus.ERROR, ReviewsUiPlugin.PLUGIN_ID, "Error attaching annotation model", t));
        }
      }
    }
  }