/** * 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; }
@Override public void documentChanged(DocumentEvent event) { IDocument newDoc = event.getDocument(); if (docLines != newDoc.getNumberOfLines()) { updateAnnotations(false); docLines = newDoc.getNumberOfLines(); docLength = newDoc.getLength(); } else { changeAnnotations(event.getOffset(), newDoc.getLength()); } }
private ColorAnnotationModel( IDocument document, IFile file, IFeatureProject project, ITextEditor editor) { this.document = document; this.project = project; this.file = file; composer = project.getComposer(); composer.initialize(project); docLines = document.getNumberOfLines(); docLength = document.getLength(); updateAnnotations(true); editor.addPropertyListener( new IPropertyListener() { @Override public void propertyChanged(Object source, int propId) { if (propId == IEditorPart.PROP_DIRTY && !((ITextEditor) source).isDirty()) { updateAnnotations(true); } } }); }