Java Code Examples for org.eclipse.jface.text.source.IAnnotationModelExtension#replaceAnnotations()

The following examples show how to use org.eclipse.jface.text.source.IAnnotationModelExtension#replaceAnnotations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationModelHelper.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Removes annotations and replaces them in one step.
 *
 * @param model The {@link IAnnotationModel} that should be cleaned.
 * @param annotationsToRemove The annotations to remove.
 * @param annotationsToAdd The annotations to add.
 */
public void replaceAnnotationsInModel(
    IAnnotationModel model,
    Collection<? extends Annotation> annotationsToRemove,
    Map<? extends Annotation, Position> annotationsToAdd) {

  if (model instanceof IAnnotationModelExtension) {
    IAnnotationModelExtension extension = (IAnnotationModelExtension) model;
    extension.replaceAnnotations(
        annotationsToRemove.toArray(new Annotation[annotationsToRemove.size()]),
        annotationsToAdd);
  } else {
    if (log.isTraceEnabled())
      log.trace("AnnotationModel " + model + " does not support IAnnotationModelExtension");

    for (Annotation annotation : annotationsToRemove) {
      model.removeAnnotation(annotation);
    }

    for (Entry<? extends Annotation, Position> entry : annotationsToAdd.entrySet()) {
      model.addAnnotation(entry.getKey(), entry.getValue());
    }
  }
}
 
Example 2
Source File: AnnotationEditor.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a collection of annotations.
 *
 * @param deletedAnnotations the deleted annotations
 */
@Override
public void removedAnnotation(Collection<AnnotationFS> deletedAnnotations) {

  if (getSite().getPage().getActivePart() == AnnotationEditor.this) {
    mFeatureStructureSelectionProvider.clearSelection();
  } else {
    mFeatureStructureSelectionProvider.clearSelectionSilently();
  }

  highlight(0, 0); // TODO: only if removed annotation was selected

  IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider().getAnnotationModel(getEditorInput());
  
  Annotation removeAnnotations[] = new Annotation[deletedAnnotations.size()];
  int removeAnnotationsIndex = 0;
  for (AnnotationFS annotation : deletedAnnotations) {
    removeAnnotations[removeAnnotationsIndex++] = new EclipseAnnotationPeer(annotation);
  }
  
  annotationModel.replaceAnnotations(removeAnnotations, null);
}
 
Example 3
Source File: OverrideIndicatorModelListener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private IStatus updateAnnotationModel(IProgressMonitor monitor) {
	if (xtextEditor == null || xtextEditor.getDocument() == null
			|| xtextEditor.getInternalSourceViewer().getAnnotationModel() == null) {
		return Status.OK_STATUS;
	}
	IXtextDocument xtextDocument = xtextEditor.getDocument();
	IAnnotationModel annotationModel = xtextEditor.getInternalSourceViewer().getAnnotationModel();
	Map<Annotation, Position> annotationToPosition = xtextDocument
			.readOnly(new CancelableUnitOfWork<Map<Annotation, Position>, XtextResource>() {
				@Override
				public Map<Annotation, Position> exec(XtextResource xtextResource, CancelIndicator cancelIndicator) {
					if (xtextResource == null)
						return Collections.emptyMap();
					return createOverrideIndicatorAnnotationMap(xtextResource, cancelIndicator);
				}
	});
	if (monitor.isCanceled())
		return Status.CANCEL_STATUS;
	if (annotationModel instanceof IAnnotationModelExtension) {
		IAnnotationModelExtension annotationModelExtension = (IAnnotationModelExtension) annotationModel;
		Object lockObject = getLockObject(annotationModel);
		synchronized (lockObject) {
			annotationModelExtension.replaceAnnotations(
					overrideIndicatorAnnotations.toArray(new Annotation[overrideIndicatorAnnotations.size()]),
					annotationToPosition);
		}
		overrideIndicatorAnnotations = annotationToPosition.keySet();
	}
	return Status.OK_STATUS;
}
 
Example 4
Source File: PyEditBreakpointSync.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
private void updateAnnotations() {
    if (edit == null) {
        return;
    }
    IDocumentProvider provider = edit.getDocumentProvider();
    if (provider == null) {
        return;
    }
    IAnnotationModel model = provider.getAnnotationModel(edit.getEditorInput());
    if (model == null) {
        return;
    }

    IAnnotationModelExtension modelExtension = (IAnnotationModelExtension) model;

    List<Annotation> existing = new ArrayList<Annotation>();
    Iterator<Annotation> it = model.getAnnotationIterator();
    if (it == null) {
        return;
    }
    while (it.hasNext()) {
        existing.add(it.next());
    }

    IDocument doc = edit.getDocument();
    IResource resource = PyMarkerUIUtils.getResourceForTextEditor(edit);
    IEditorInput externalFileEditorInput = AbstractBreakpointRulerAction.getExternalFileEditorInput(edit);
    List<IMarker> markers = AbstractBreakpointRulerAction.getMarkersFromEditorResource(resource, doc,
            externalFileEditorInput, 0, false, model);

    Map<Annotation, Position> annotationsToAdd = new HashMap<Annotation, Position>();
    for (IMarker m : markers) {
        Position pos = PyMarkerUIUtils.getMarkerPosition(doc, m, model);
        MarkerAnnotation newAnnotation = new MarkerAnnotation(m);
        annotationsToAdd.put(newAnnotation, pos);
    }

    //update all in a single step
    modelExtension.replaceAnnotations(existing.toArray(new Annotation[0]), annotationsToAdd);
}
 
Example 5
Source File: AnnotationEditor.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a collection of annotations.
 *
 * @param annotations the annotations
 */
@Override
public void addedAnnotation(Collection<AnnotationFS> annotations) {
  IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider().getAnnotationModel(getEditorInput());
  
  Map<Annotation, Position> addAnnotationMap = new HashMap<>();
  
  for (AnnotationFS annotation : annotations) {
    addAnnotationMap.put(new EclipseAnnotationPeer(annotation), new Position(annotation.getBegin(), 
            annotation.getEnd() - annotation.getBegin()));
  }
  
  annotationModel.replaceAnnotations(null, addAnnotationMap);
}