Java – How do I remove IMarkers after the editor is closed (or why does the IMarker.TRANSIENT property not work)?

How do I remove IMarkers after the editor is closed (or why does the IMarker.TRANSIENT property not work)?… here is a solution to the problem.

How do I remove IMarkers after the editor is closed (or why does the IMarker.TRANSIENT property not work)?

I’m writing a custom editor in Eclipse and just integrated custom error recognition. Now I’m facing a weird problem: I can add tags to my editor and those tags will display normally, and I can also remove them while the editor is running.

What doesn’t work: When I close my editor, I want the markup to disappear/be deleted.

What I’m doing now is

  • Create a tag with a transient attribute set as follows: marker.setAttribute(IMarker.TRANSIENT, true); That doesn’t seem to change anything.

  • Try to remove all comments via the source viewer annotation-model. This doesn’t work because when I try to connect to my editor dispose() method or add DisposeListener to my sourceviewers textwidget, sourceviewer has been released, getSourceViewer(). getAnnotationModel(); Returns null.

My deleteMarkers method:

private void deleteMarkers() {
        IAnnotationModel anmod = getSourceViewer().getAnnotationModel();
        Iterator<Annotation> it = anmod.getAnnotationIterator();

while (it.hasNext()) {
            SimpleMarkerAnnotation a = (SimpleMarkerAnnotation) it.next();
            anmod.removeAnnotation(a);

try {
                a.getMarker().delete();
            } catch (CoreException e) {
                e.printStackTrace();
            }

}
    }

Thanks for any help ^^

Solution

Connect to your editor close event, get a reference to IResource

for the editor (I believe you can get it on IEditorInput) and call IResource#deleteMarkers() on the relevant resource, which will remove them when you close the editor. By design, Eclipse does not remove markers when the editor closes.

Here are some quotes:
http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/ IResource.html#deleteMarkers(java.lang.String, boolean, int)

Related Problems and Solutions