Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Problem adding a vertical ruler listener(Can't add a vertical ruler listener to the java editor.)
Problem adding a vertical ruler listener [message #851228] Fri, 20 April 2012 19:38 Go to next message
Pedro Pires is currently offline Pedro PiresFriend
Messages: 3
Registered: April 2012
Junior Member
I'm trying to add a listener to the vertical ruler column, but it seens to not be working. I created a new class called VerticalRulerListener that implements the IVerticalRulerListener interface. The implemented methods only have a System.out.println("some text").
I add the listener by doing:
IVerticalRulerListener listener = new VerticalRulerListener();
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null) {
	IWorkbenchPage page = window.getActivePage();
	if (page != null) {
		IEditorPart editor = page.getActiveEditor();
		if (editor instanceof ITextEditor) {
			Object adapted = editor.getAdapter(IVerticalRuler.class);
			if (adapted instanceof IVerticalRulerInfoExtension) {
				((IVerticalRulerInfoExtension)adapted).addVerticalRulerListener(listener);
			}
		}
	}
}


After executing the code, the listener is never called when I left/right click some annotation in the vertical ruler. The object returned by getAdapter() is the editor's CompositeRuler. Perhaps I should add the listener directly to the annotations ruler, instead of the composite ruler?
Re: Problem adding a vertical ruler listener [message #869757 is a reply to message #851228] Fri, 04 May 2012 08:37 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
On 20.04.2012 21:38, Pedro Pires wrote:
> I'm trying to add a listener to the vertical ruler column, but it
> seens to not be working. I created a new class called
> VerticalRulerListener that implements the IVerticalRulerListener
> interface. The implemented methods only have a
> System.out.println("some text").
> I add the listener by doing:
>
> IVerticalRulerListener listener = new VerticalRulerListener();
> IWorkbenchWindow window =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow();
> if (window != null) {
> IWorkbenchPage page = window.getActivePage();
> if (page != null) {
> IEditorPart editor = page.getActiveEditor();
> if (editor instanceof ITextEditor) {
> Object adapted = editor.getAdapter(IVerticalRuler.class);
> if (adapted instanceof IVerticalRulerInfoExtension) {
>
> ((IVerticalRulerInfoExtension)adapted).addVerticalRulerListener(listener);
> }
> }
> }
> }
>
>
> After executing the code, the listener is never called when I
> left/right click some annotation in the vertical ruler. The object
> returned by getAdapter() is the editor's CompositeRuler. Perhaps I
> should add the listener directly to the annotations ruler, instead of
> the composite ruler?

I think this should work. If it still doesn't, please file a bug against
Platform Text with a complete test case.

Dani
Re: Problem adding a vertical ruler listener [message #870569 is a reply to message #869757] Tue, 08 May 2012 19:13 Go to previous messageGo to next message
Pedro Pires is currently offline Pedro PiresFriend
Messages: 3
Registered: April 2012
Junior Member
I tested it again, and the code I pasted here isn't working. The editor.getAdapter(IVerticalRuler.class) returns null.

I extended the java editor, and overrided the createJavaSourceViewer() method. In this method I have access to the vertical ruler, which now is the editor's composite ruler for sure (not null). I added the listener to this ruler, and the problem remained the same.

The drawback of this implementation is that I had to extend the CompilationUnitEditor class, which is discouraged. Maybe the problem is because of this, and I don't want to file a bug while extending the java editor. If I find a suitable way to add the listener, and the problem still remains, I'll file the bug.

Is there a way to add a vertical ruler listener without extending the java editor?

OBS: right after registering the ruler listener, I called the CompositeRuler.fireAnnotationSelected() method, and my listener was called as expected. When executing the plugin in debug mode, I can see that the editor has 2 ruler listeners: [cideplus.ui.presentation.VerticalRulerListener@ 11c048e, org.eclipse.jdt.internal.ui.javaeditor.JavaSelectMarkerRulerAction2@ 77bcd5]


Pedro

[Updated on: Tue, 08 May 2012 19:13]

Report message to a moderator

Re: Problem adding a vertical ruler listener [message #870740 is a reply to message #870569] Wed, 09 May 2012 13:54 Go to previous message
Pedro Pires is currently offline Pedro PiresFriend
Messages: 3
Registered: April 2012
Junior Member
Created a fresh plugin just to test this.

Listener code:
public class VerticalRulerListener implements IVerticalRulerListener {
	public VerticalRulerListener() {
		System.out.println("VerticalRulerListener.VerticalRulerListener()");
	}
	public void annotationSelected(VerticalRulerEvent event) {
		System.out.println("VerticalRulerListener.annotationSelected()");
	}
	public void annotationDefaultSelected(VerticalRulerEvent event) {
		System.out.println("VerticalRulerListener.annotationDefaultSelected()");
	}
	public void annotationContextMenuAboutToShow(VerticalRulerEvent event, Menu menu) {
		System.out.println("VerticalRulerListener.annotationContextMenuAboutToShow()");
	}
}


Editor code:
public class MyJavaEditor extends CompilationUnitEditor {
	@Override
	protected ISourceViewer createJavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean isOverviewRulerVisible, int styles, IPreferenceStore store) {
		ISourceViewer javaSourceViewer = super.createJavaSourceViewer(parent, verticalRuler, overviewRuler, isOverviewRulerVisible, styles, store);
		if (verticalRuler instanceof IVerticalRulerInfoExtension)
			((IVerticalRulerInfoExtension) verticalRuler).addVerticalRulerListener(new VerticalRulerListener());
		((CompositeRuler) verticalRuler).fireAnnotationSelected(null);
		return javaSourceViewer;
	}
}


When I call fireAnnotationSelected() the listener is called, but when I click on any annotation in the vertical ruler, it isn't called.
Previous Topic:[Common Navigator View] How to restore expanded nodes?
Next Topic:com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl cannot be cast to javax.xml.soap.SOAPElement
Goto Forum:
  


Current Time: Thu Mar 28 08:18:00 GMT 2024

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

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

Back to the top