下面列出了怎么用org.eclipse.jface.text.source.IAnnotationModelExtension的API类实例代码及写法,或者点击链接到github查看源代码。
protected void updateAnnotations(IProgressMonitor monitor, List<Annotation> toBeRemoved,
Map<Annotation, Position> annotationToPosition) {
if (monitor.isCanceled()) {
return;
}
if (annotationModel instanceof IAnnotationModelExtension) {
Annotation[] removedAnnotations = toBeRemoved.toArray(new Annotation[toBeRemoved.size()]);
((IAnnotationModelExtension) annotationModel).replaceAnnotations(removedAnnotations, annotationToPosition);
} else {
for (Annotation annotation : toBeRemoved) {
if (monitor.isCanceled()) {
return;
}
annotationModel.removeAnnotation(annotation);
}
for (Map.Entry<Annotation, Position> entry : annotationToPosition.entrySet()) {
if (monitor.isCanceled()) {
return;
}
annotationModel.addAnnotation(entry.getKey(), entry.getValue());
}
}
}
private void removeOccurrenceAnnotations() {
// fMarkOccurrenceModificationStamp=
// IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
// fMarkOccurrenceTargetRegion= null;
IDocumentProvider documentProvider = getDocumentProvider();
if (documentProvider == null)
return;
IAnnotationModel annotationModel = documentProvider.getAnnotationModel(getEditorInput());
if (annotationModel == null || fOccurrenceAnnotations == null)
return;
synchronized (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;
}
}
void removeOccurrenceAnnotations() {
fMarkOccurrenceModificationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
fMarkOccurrenceTargetRegion= null;
IDocumentProvider documentProvider= getDocumentProvider();
if (documentProvider == null)
return;
IAnnotationModel annotationModel= documentProvider.getAnnotationModel(getEditorInput());
if (annotationModel == null || fOccurrenceAnnotations == null)
return;
synchronized (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;
}
}
/**
* 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());
}
}
}
public ExternalBreakpointWatcher(IEditorInput input, IDocument document, IAnnotationModel annotationModel) {
this.location = EditorUtils.getLocationOrNull(input);
this.document = assertNotNull(document);
this.annotationModel = assertNotNull(annotationModel);
if(annotationModel instanceof IAnnotationModelExtension) {
this.annotationModelExt = (IAnnotationModelExtension) annotationModel;
} else {
this.annotationModelExt = null;
}
if(location != null) {
ResourceUtils.connectResourceListener(resourceListener, this::initializeFromResources,
getWorkspaceRoot(), owned);
}
}
/**
* 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);
}
/**
* Sync annotations.
*/
private void syncAnnotations() {
removeAllAnnotations();
// Remove all annotation from the model
IAnnotationModel annotationModel = getDocumentProvider().getAnnotationModel(getEditorInput());
((IAnnotationModelExtension) annotationModel).removeAllAnnotations();
// Add all annotation to the model
// copy annotations into annotation model
final Iterator<AnnotationFS> mAnnotations = getDocument().getCAS().getAnnotationIndex()
.iterator();
// TODO: Build first a map, and then pass all annotations at once
Map annotationsToAdd = new HashMap();
while (mAnnotations.hasNext()) {
AnnotationFS annotationFS = mAnnotations.next();
annotationsToAdd.put(new EclipseAnnotationPeer(annotationFS), new Position(
annotationFS.getBegin(), annotationFS.getEnd() - annotationFS.getBegin()));
}
((IAnnotationModelExtension) annotationModel).replaceAnnotations(null, annotationsToAdd);
}
public RevisionInformation getRevisionInformation(IResource resource, ITextViewer viewer, ITextEditor textEditor) {
for (IRevisionInformationProvider provider : providers) {
if (provider.canApply(resource)) {
RevisionInformation info = provider.getRevisionInformation(resource);
if (info != null) {
ILineDiffer differ = provider.getDocumentLineDiffer(viewer, textEditor);
// Connect line differ
IAnnotationModelExtension ext = (IAnnotationModelExtension) ((ISourceViewer) viewer).getAnnotationModel();
ext.addAnnotationModel(RevisionInformationSupport.MY_QUICK_DIFF_MODEL_ID, (IAnnotationModel) differ);
return info;
}
}
}
return null;
}
/**
* Sets the annotation model.
*
* @param model the annotation model, possibly <code>null</code>
* @see IVerticalRulerColumn#setModel(IAnnotationModel)
*/
private void setModel(IAnnotationModel model) {
IAnnotationModel diffModel;
if (model instanceof IAnnotationModelExtension)
diffModel = ((IAnnotationModelExtension) model).getAnnotationModel(MY_QUICK_DIFF_MODEL_ID);
else
diffModel = model;
if (diffModel == null) {
//diffModel = new DocumentLineDiffer();
}
setDiffer(diffModel);
// setAnnotationModel(model);
}
protected void announceAnnotationChanged(Annotation annotation) {
if (annotationModel instanceof XtextResourceMarkerAnnotationModel)
((XtextResourceMarkerAnnotationModel) annotationModel).fireAnnotationChangedEvent(annotation);
else {
Position position = annotationModel.getPosition(annotation);
if (annotationModel instanceof IAnnotationModelExtension)
((IAnnotationModelExtension) annotationModel).modifyAnnotationPosition(annotation, position);
else {
annotationModel.removeAnnotation(annotation);
annotationModel.addAnnotation(annotation, position);
}
}
}
@Override
protected IStatus run(IProgressMonitor monitor) {
final XtextEditor editor = initialEditor;
final boolean isMarkOccurrences = initialIsMarkOccurrences;
final ITextSelection selection = initialSelection;
final SubMonitor progress = SubMonitor.convert(monitor, 2);
if (!progress.isCanceled()) {
final Map<Annotation, Position> annotations = (isMarkOccurrences) ? occurrenceComputer.createAnnotationMap(editor, selection,
progress.newChild(1)) : Collections.<Annotation, Position>emptyMap();
if (!progress.isCanceled()) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
if (!progress.isCanceled()) {
final IAnnotationModel annotationModel = getAnnotationModel(editor);
if (annotationModel instanceof IAnnotationModelExtension)
((IAnnotationModelExtension) annotationModel).replaceAnnotations(
getExistingOccurrenceAnnotations(annotationModel), annotations);
else if(annotationModel != null)
throw new IllegalStateException(
"AnnotationModel does not implement IAnnotationModelExtension"); //$NON-NLS-1$
}
}
});
return Status.OK_STATUS;
}
}
return Status.CANCEL_STATUS;
}
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;
}
/**
* Removes all override indicators from this manager's annotation model.
*/
void removeAnnotations() {
if (fOverrideAnnotations == null)
return;
synchronized (fAnnotationModelLockObject) {
if (fAnnotationModel instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension)fAnnotationModel).replaceAnnotations(fOverrideAnnotations, null);
} else {
for (int i= 0, length= fOverrideAnnotations.length; i < length; i++)
fAnnotationModel.removeAnnotation(fOverrideAnnotations[i]);
}
fOverrideAnnotations= null;
}
}
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);
}
/**
* 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);
}
/**
* Updated annotation.
*
* @param annotations the annotations
*/
@Override
public void updatedAnnotation(Collection<AnnotationFS> annotations) {
IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider().getAnnotationModel(getEditorInput());
for (AnnotationFS annotation : annotations) {
annotationModel.modifyAnnotationPosition(new EclipseAnnotationPeer(annotation),
new Position(annotation.getBegin(), annotation.getEnd() - annotation.getBegin()));
}
selectionChanged(getSite().getPage().getActivePart(), mFeatureStructureSelectionProvider.getSelection());
}
public IStatus run(IProgressMonitor progressMonitor) {
fProgressMonitor = progressMonitor;
if (isCanceled()) {
if (LinkedModeModel.hasInstalledModel(fDocument)) {
// Template completion applied, remove occurrences
removeOccurrenceAnnotations();
}
return Status.CANCEL_STATUS;
}
ITextViewer textViewer = getViewer();
if (textViewer == null)
return Status.CANCEL_STATUS;
IDocument document = textViewer.getDocument();
if (document == null)
return Status.CANCEL_STATUS;
IAnnotationModel annotationModel = getAnnotationModel();
if (annotationModel == null)
return Status.CANCEL_STATUS;
// Add occurrence annotations
int length = fPositions.length;
Map annotationMap = new HashMap(length);
for (int i = 0; i < length; i++) {
if (isCanceled())
return Status.CANCEL_STATUS;
String message;
Position position = fPositions[i];
// Create & add annotation
try {
message = document.get(position.offset, position.length);
} catch (BadLocationException ex) {
// Skip this match
continue;
}
annotationMap.put(new Annotation("org.eclipse.wst.jsdt.ui.occurrences", false, message), //$NON-NLS-1$
position);
}
if (isCanceled())
return Status.CANCEL_STATUS;
synchronized (getLockObject(annotationModel)) {
if (annotationModel instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations,
annotationMap);
} else {
removeOccurrenceAnnotations();
Iterator iter = annotationMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry mapEntry = (Map.Entry) iter.next();
annotationModel.addAnnotation((Annotation) mapEntry.getKey(), (Position) mapEntry.getValue());
}
}
fOccurrenceAnnotations = (Annotation[]) annotationMap.keySet()
.toArray(new Annotation[annotationMap.keySet().size()]);
}
return Status.OK_STATUS;
}
@Override
public IStatus run(IProgressMonitor progressMonitor) {
if (isCanceled(progressMonitor))
return Status.CANCEL_STATUS;
ITextViewer textViewer= getViewer();
if (textViewer == null)
return Status.CANCEL_STATUS;
IDocument document= textViewer.getDocument();
if (document == null)
return Status.CANCEL_STATUS;
IDocumentProvider documentProvider= getDocumentProvider();
if (documentProvider == null)
return Status.CANCEL_STATUS;
IAnnotationModel annotationModel= documentProvider.getAnnotationModel(getEditorInput());
if (annotationModel == null)
return Status.CANCEL_STATUS;
// Add occurrence annotations
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 location= fLocations[i];
Position position= new Position(location.getOffset(), location.getLength());
String description= location.getDescription();
String annotationType= (location.getFlags() == IOccurrencesFinder.F_WRITE_OCCURRENCE) ? "org.eclipse.jdt.ui.occurrences.write" : "org.eclipse.jdt.ui.occurrences"; //$NON-NLS-1$ //$NON-NLS-2$
annotationMap.put(new Annotation(annotationType, false, description), position);
}
if (isCanceled(progressMonitor))
return Status.CANCEL_STATUS;
synchronized (getLockObject(annotationModel)) {
if (annotationModel instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension)annotationModel).replaceAnnotations(fOccurrenceAnnotations, annotationMap);
} else {
removeOccurrenceAnnotations();
Iterator<Entry<Annotation, Position>> iter= annotationMap.entrySet().iterator();
while (iter.hasNext()) {
Entry<Annotation, Position> mapEntry= iter.next();
annotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue());
}
}
fOccurrenceAnnotations= annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
}
return Status.OK_STATUS;
}
/**
* Updates the override and implements annotations based
* on the given AST.
*
* @param ast the compilation unit AST
* @param progressMonitor the progress monitor
* @since 3.0
*/
protected void updateAnnotations(CompilationUnit ast, IProgressMonitor progressMonitor) {
if (ast == null || progressMonitor.isCanceled())
return;
final Map<Annotation, Position> annotationMap= new HashMap<Annotation, Position>(50);
ast.accept(new ASTVisitor(false) {
/*
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodDeclaration)
*/
@Override
public boolean visit(MethodDeclaration node) {
IMethodBinding binding= node.resolveBinding();
if (binding != null) {
IMethodBinding definingMethod= Bindings.findOverriddenMethod(binding, true);
if (definingMethod != null) {
ITypeBinding definingType= definingMethod.getDeclaringClass();
String qualifiedMethodName= definingType.getQualifiedName() + "." + binding.getName(); //$NON-NLS-1$
boolean isImplements= JdtFlags.isAbstract(definingMethod);
String text;
if (isImplements)
text= Messages.format(JavaEditorMessages.OverrideIndicatorManager_implements, BasicElementLabels.getJavaElementName(qualifiedMethodName));
else
text= Messages.format(JavaEditorMessages.OverrideIndicatorManager_overrides, BasicElementLabels.getJavaElementName(qualifiedMethodName));
SimpleName name= node.getName();
Position position= new Position(name.getStartPosition(), name.getLength());
annotationMap.put(
new OverrideIndicator(isImplements, text, binding.getKey()),
position);
}
}
return true;
}
});
if (progressMonitor.isCanceled())
return;
synchronized (fAnnotationModelLockObject) {
if (fAnnotationModel instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension)fAnnotationModel).replaceAnnotations(fOverrideAnnotations, annotationMap);
} else {
removeAnnotations();
Iterator<Entry<Annotation, Position>> iter= annotationMap.entrySet().iterator();
while (iter.hasNext()) {
Entry<Annotation, Position> mapEntry= iter.next();
fAnnotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue());
}
}
fOverrideAnnotations= annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
}
}
/**
* @param annotationModel
*/
protected synchronized void removeOccurenceAnnotations(IAnnotationModel annotationModel, BaseEditor pyEdit) {
//remove the annotations
Map<String, Object> cache = pyEdit.cache;
if (cache == null) {
return;
}
//let other threads execute before getting the lock on the annotation model
Thread.yield();
Thread thread = Thread.currentThread();
int initiaThreadlPriority = thread.getPriority();
//before getting the lock, let's execute with normal priority, to optimize the time that we'll
//retain that object locked (the annotation model is used on lots of places, so, retaining the lock
//on it on a minimum priority thread is not a good thing.
thread.setPriority(Thread.NORM_PRIORITY);
try {
synchronized (getLockObject(annotationModel)) {
List<Annotation> annotationsToRemove = getOccurrenceAnnotationsInEditor(pyEdit);
if (annotationModel instanceof IAnnotationModelExtension) {
//replace those
((IAnnotationModelExtension) annotationModel).replaceAnnotations(
annotationsToRemove.toArray(new Annotation[annotationsToRemove.size()]),
new HashMap<Annotation, Position>());
} else {
Iterator<Annotation> annotationIterator = annotationsToRemove.iterator();
while (annotationIterator.hasNext()) {
annotationModel.removeAnnotation(annotationIterator.next());
}
}
cache.put(getOccurrenceAnnotationsCacheKey(), null);
}
//end remove the annotations
} finally {
thread.setPriority(initiaThreadlPriority);
}
}
@Override
public void endCollecting() {
List<Object> toRemove = new ArrayList<Object>();
Object fLockObject;
if (fAnnotationModel instanceof ISynchronizable) {
fLockObject = ((ISynchronizable) fAnnotationModel).getLockObject();
} else {
fLockObject = new Object();
}
//let other threads execute before getting the lock on the annotation model
Thread.yield();
Thread thread = Thread.currentThread();
int initiaThreadlPriority = thread.getPriority();
try {
//before getting the lock, let's execute with normal priority, to optimize the time that we'll
//retain that object locked (the annotation model is used on lots of places, so, retaining the lock
//on it on a minimum priority thread is not a good thing.
thread.setPriority(Thread.NORM_PRIORITY);
Iterator<Annotation> iter;
synchronized (fLockObject) {
iter = fAnnotationModel.getAnnotationIterator();
while (iter.hasNext()) {
Object n = iter.next();
if (n instanceof SpellingAnnotation) {
toRemove.add(n);
}
}
iter = null;
}
Annotation[] annotationsToRemove = toRemove.toArray(new Annotation[toRemove.size()]);
//let other threads execute before getting the lock (again) on the annotation model
Thread.yield();
synchronized (fLockObject) {
if (fAnnotationModel instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension) fAnnotationModel).replaceAnnotations(annotationsToRemove,
fAddAnnotations);
} else {
for (int i = 0; i < annotationsToRemove.length; i++) {
fAnnotationModel.removeAnnotation(annotationsToRemove[i]);
}
for (iter = fAddAnnotations.keySet().iterator(); iter.hasNext();) {
Annotation annotation = iter.next();
fAnnotationModel.addAnnotation(annotation, fAddAnnotations.get(annotation));
}
}
}
} finally {
thread.setPriority(initiaThreadlPriority);
}
fAddAnnotations = null;
}
/**
* Removes the all annotations.
*/
private void removeAllAnnotations() {
// Remove all annotation from the model
IAnnotationModel annotationModel = getDocumentProvider().getAnnotationModel(getEditorInput());
((IAnnotationModelExtension) annotationModel).removeAllAnnotations();
}