Problem adding a vertical ruler listener [message #851228] |
Fri, 20 April 2012 15:38  |
Eclipse User |
|
|
|
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 #870569 is a reply to message #869757] |
Tue, 08 May 2012 15:13   |
Eclipse User |
|
|
|
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 15:13] by Moderator
|
|
|
Re: Problem adding a vertical ruler listener [message #870740 is a reply to message #870569] |
Wed, 09 May 2012 09:54  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.26618 seconds