Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Generic Editor PDE Wizard example: no access to underlying editor widget?
Generic Editor PDE Wizard example: no access to underlying editor widget? [message #1806937] Sat, 18 May 2019 09:10 Go to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Hello,

I was studying the example created by the PDE wizard "Textual editor, relying on Generic Editor" and specifically the use IDocumentSetupParticipant used from the "org.eclipse.core.filebuffers.documentSetup" extension point.

It uses an IDocumentListener to trigger parsing of text to create annotations for errors in the document. I noticed however that the example creates file markers associated with the buffer: this is a problem because they stay in the problems view even when you close the editor without saving! So if I introduce an error by typing text, the marker shows up but if I close the editor without saving it stays there (even though I modified the example to set the non-persistent attribute).

I want my IDocumentSetupParticipant to have access to the underlying text editor widget, so that it can register (for example) to the close event of the editor and react by removing the markers, but I cannot find any API to do this (I checked the extension IDocumentSetupParticipantExtension as well as IDocument and all its extensions).

How can I create truly transient (i.e. visible only while editor is open) markers for my editor in the generated ValidatorDocumentSetupParticipant? Is there any way to achieve this?

This is what the example generates:

package org.myeditor.experiment;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
import org.eclipse.core.filebuffers.IDocumentSetupParticipantExtension;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

public class ValidatorDocumentSetupParticipant implements IDocumentSetupParticipant, IDocumentSetupParticipantExtension {

	private final class DocumentValidator implements IDocumentListener {
		private final IFile file;
		private IMarker marker;

		private DocumentValidator(IFile file) {
			this.file = file;
		}

		@Override
		public void documentChanged(DocumentEvent event) {
			if (this.marker != null) {
				try {
					this.marker.delete();
				} catch (CoreException e) {
					e.printStackTrace();
				}
				this.marker = null;
			}
			try (StringReader reader = new StringReader(event.getDocument().get());) {
				DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				documentBuilder.parse(new InputSource(reader));
			} catch (Exception ex) {
				try {
					this.marker = file.createMarker(IMarker.PROBLEM);
					this.marker.setAttribute(IMarker.TRANSIENT, true); // ADDED THIS MYSELF TO AT LEAST NOT SURVIVE WORKSPACE RESTART
					this.marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
					this.marker.setAttribute(IMarker.MESSAGE, ex.getMessage());
					if (ex instanceof SAXParseException) {
						SAXParseException saxParseException = (SAXParseException)ex;
						int lineNumber = saxParseException.getLineNumber();
						int offset = event.getDocument().getLineInformation(lineNumber - 1).getOffset() + saxParseException.getColumnNumber() - 1;
						this.marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
						this.marker.setAttribute(IMarker.CHAR_START, offset);
						this.marker.setAttribute(IMarker.CHAR_END, offset + 1);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		@Override
		public void documentAboutToBeChanged(DocumentEvent event) {
		}
	}

	@Override
	public void setup(IDocument document) {
	}

	@Override
	public void setup(IDocument document, IPath location, LocationKind locationKind) {
		if (locationKind == LocationKind.IFILE) {
			IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(location);
			document.addDocumentListener(new DocumentValidator(file));
		}
	}

}
Re: Generic Editor PDE Wizard example: no access to underlying editor widget? [message #1807050 is a reply to message #1806937] Tue, 21 May 2019 19:35 Go to previous message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
I've resorted to setting up a "service" in my plugin that I look up using the activator. This way the document setup code simply invokes the parser (when text changes) and stores the parsed model in the service without affecting presentation at all. The latter is done by the presentation reconciler (IPresentationReconciler instance) which has access to the text viewer and its annotation model.
Previous Topic:Problem not due to Eclipse, please remove post.
Next Topic:Linking Handled Tool and Menu Items
Goto Forum:
  


Current Time: Thu Apr 25 12:41:31 GMT 2024

Powered by FUDForum. Page generated in 0.02759 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top