void removeOccurrenceAnnotations() {
    fMarkOccurrenceModificationStamp = IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
    fMarkOccurrenceTargetRegion = null;

    final IDocumentProvider documentProvider = erlangEditor.getDocumentProvider();
    if (documentProvider == null) {
      return;
    }

    final IAnnotationModel annotationModel =
        documentProvider.getAnnotationModel(erlangEditor.getEditorInput());
    if (annotationModel == null || fOccurrenceAnnotations == null) {
      return;
    }

    synchronized (erlangEditor.getLockObject(annotationModel)) {
      if (annotationModel instanceof IAnnotationModelExtension) {
        ((IAnnotationModelExtension) annotationModel)
            .replaceAnnotations(fOccurrenceAnnotations, null);
      } else {
        for (int i = 0, length = fOccurrenceAnnotations.length; i < length; i++) {
          annotationModel.removeAnnotation(fOccurrenceAnnotations[i]);
        }
      }
      fOccurrenceAnnotations = null;
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  protected void createProblems(
      IProgressMonitor monitor,
      IDocument document,
      IRegion region,
      List<ValidationProblem> problems)
      throws CoreException {
    Object lockObject;
    if (annotationModel instanceof ISynchronizable) {
      lockObject = ((ISynchronizable) annotationModel).getLockObject();
    } else {
      lockObject = annotationModel;
    }
    synchronized (lockObject) {
      List<Annotation> toRemove = null;
      Iterator<Annotation> annotationIterator = annotationModel.getAnnotationIterator();
      while (annotationIterator.hasNext()) {
        Annotation annotation = annotationIterator.next();
        if (ValidationProblemAnnotation.isValidationAnnotation(annotation)) {
          Position position = annotationModel.getPosition(annotation);
          int offset = position.getOffset();
          if (overlaps(region, offset, position.getLength()) || offset >= document.getLength()) {
            if (toRemove == null) {
              toRemove = new ArrayList<Annotation>();
            }
            toRemove.add(annotation);
          }
        }
      }

      Map<Annotation, Position> annotationsToAdd = new HashMap<Annotation, Position>();
      for (ValidationProblem problem : problems) {
        annotationsToAdd.put(
            new ValidationProblemAnnotation(problem),
            new Position(problem.getOffset(), problem.getLength()));
      }

      if (toRemove != null && annotationModel instanceof IAnnotationModelExtension) {
        Annotation[] annotationsToRemove = toRemove.toArray(new Annotation[toRemove.size()]);
        ((IAnnotationModelExtension) annotationModel)
            .replaceAnnotations(annotationsToRemove, annotationsToAdd);
      } else {
        if (toRemove != null) {
          for (Annotation annotation : toRemove) {
            annotationModel.removeAnnotation(annotation);
          }
        }
        for (Map.Entry<Annotation, Position> entry : annotationsToAdd.entrySet()) {
          annotationModel.addAnnotation(entry.getKey(), entry.getValue());
        }
      }
    }
  }
 private void replaceAnnotations(Annotation[] remove, Map<Annotation, Position> add) {
   final IAnnotationModel model = viewer.getAnnotationModel();
   if (model instanceof IAnnotationModelExtension) {
     final IAnnotationModelExtension eModel = (IAnnotationModelExtension) model;
     eModel.replaceAnnotations(remove, add);
   } else {
     for (final Annotation annotation : remove) {
       model.removeAnnotation(annotation);
     }
     for (final Annotation annotation : add.keySet()) {
       model.addAnnotation(annotation, add.get(annotation));
     }
   }
 }
    /*
     * @see Job#run(org.eclipse.core.runtime.IProgressMonitor)
     */
    public IStatus run(IProgressMonitor progressMonitor) {
      if (isCanceled(progressMonitor)) {
        return Status.CANCEL_STATUS;
      }

      ITextViewer textViewer = editor.getISourceViewer();
      if (textViewer == null) {
        return Status.CANCEL_STATUS;
      }

      IDocument document = textViewer.getDocument();
      if (document == null) {
        return Status.CANCEL_STATUS;
      }

      IDocumentProvider documentProvider = editor.getDocumentProvider();
      if (documentProvider == null) {
        return Status.CANCEL_STATUS;
      }

      IAnnotationModel annotationModel =
          documentProvider.getAnnotationModel(editor.getEditorInput());
      if (annotationModel == null) {
        return Status.CANCEL_STATUS;
      }

      int length = fLocations.length;
      Map<Annotation, Position> annotationMap = new HashMap<Annotation, Position>(length);
      for (int i = 0; i < length; i++) {

        if (isCanceled(progressMonitor)) {
          return Status.CANCEL_STATUS;
        }

        OccurrenceLocation occurrence = fLocations[i];
        Position position = new Position(occurrence.getOffset(), occurrence.getLength());

        String description = occurrence.getDescription();
        String annotationType =
            (occurrence.getFlags() == IOccurrencesFinder.F_WRITE_OCCURRENCE)
                ? WRITE_OCCURRENCE_ID
                : READ_OCCURRENCE_ID;

        /*
         * // create an annotation to mark the occurrence ReconcileAnnotationKey reconcileAnnotationKey = new
         * ReconcileAnnotationKey(null, PHPPartitionTypes.PHP_DEFAULT, ReconcileAnnotationKey.TOTAL);
         * TemporaryAnnotation annotation = new TemporaryAnnotation(position, annotationType, description,
         * reconcileAnnotationKey) {
         */
        /**
         * Paint the ruler annotation.
         *
         * @param gc
         * @param canvas
         * @param r
         */
        /*
         * @Override public void paint(GC gc, Canvas canvas, Rectangle r) { // TODO - Shalom: Change this image
         * location ImageUtilities.drawImage(PHPUiPlugin.getImageDescriptorRegistry().get(
         * PHPPluginImages.DESC_OBJS_OCCURRENCES), gc, canvas, r, SWT.CENTER, SWT.TOP); } };
         */
        Annotation annotation = new Annotation(annotationType, false, description);
        annotationMap.put(annotation, position);
      }

      if (isCanceled(progressMonitor)) return Status.CANCEL_STATUS;

      synchronized (getAnnotationModelLock(annotationModel)) {
        if (annotationModel instanceof IAnnotationModelExtension) {
          ((IAnnotationModelExtension) annotationModel)
              .replaceAnnotations(fOccurrenceAnnotations, annotationMap);
        } else {
          removeOccurrenceAnnotations();
          for (Map.Entry<Annotation, Position> entry : annotationMap.entrySet()) {
            annotationModel.addAnnotation(entry.getKey(), entry.getValue());
          }
        }
        fOccurrenceAnnotations =
            annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
      }

      return Status.OK_STATUS;
    }
    /*
     * @see Job#run(org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public IStatus run(final IProgressMonitor progressMonitor) {
      findRefs(module, selection, fHasChanged);
      if (fRefs == null) {
        return Status.CANCEL_STATUS;
      }

      if (isCanceled(progressMonitor)) {
        return Status.CANCEL_STATUS;
      }

      final ITextViewer textViewer = erlangEditor.getViewer();
      if (textViewer == null) {
        return Status.CANCEL_STATUS;
      }

      final IDocument document = textViewer.getDocument();
      if (document == null) {
        return Status.CANCEL_STATUS;
      }

      final IDocumentProvider documentProvider = erlangEditor.getDocumentProvider();
      if (documentProvider == null) {
        return Status.CANCEL_STATUS;
      }

      final IAnnotationModel annotationModel =
          documentProvider.getAnnotationModel(erlangEditor.getEditorInput());
      if (annotationModel == null) {
        return Status.CANCEL_STATUS;
      }

      // Add occurrence annotations
      final HashMap<Annotation, Position> annotationMap =
          new HashMap<Annotation, Position>(fRefs.size());
      for (final MarkOccurencesHandler.ErlangRef ref : fRefs) {
        if (isCanceled(progressMonitor)) {
          return Status.CANCEL_STATUS;
        }

        final Position position = new Position(ref.getOffset(), ref.getLength());

        final String description = ref.getDescription();
        final String annotationType =
            ref.isDef()
                ? "org.erlide.ui.occurrences.definition" //$NON-NLS-1$
                : "org.erlide.ui.occurrences";

        annotationMap.put(new Annotation(annotationType, false, description), position);
      }

      if (isCanceled(progressMonitor)) {
        return Status.CANCEL_STATUS;
      }

      synchronized (erlangEditor.getLockObject(annotationModel)) {
        if (annotationModel instanceof IAnnotationModelExtension) {
          ((IAnnotationModelExtension) annotationModel)
              .replaceAnnotations(
                  erlangEditor.markOccurencesHandler.fOccurrenceAnnotations, annotationMap);
        } else {
          erlangEditor.markOccurencesHandler.removeOccurrenceAnnotations();
          for (final Map.Entry<Annotation, Position> mapEntry : annotationMap.entrySet()) {
            annotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue());
          }
        }
        erlangEditor.markOccurencesHandler.fOccurrenceAnnotations =
            annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
      }

      return Status.OK_STATUS;
    }