Showing link inside custom hover (Eclipse plugin development)

Viewed 257

I have a custom hover inside CDT editor (see the linked SO question) and now I want to show link inside my IAnnotationHover hover:

public class MyAwesomeHover implements IAnnotationHover {
    @Override
    public String getHoverInfo(ISourceViewer sw, int ln) {
        return "<a href='www.stackoverflow.com'>so</a>"
    }   
}

Unfortunately the link is not shown - the hover window shows only simple text (i.e "so"). Other HTML elements I tried work OK (ul, li, p, font ...). Can anyone help me please?

1 Answers

As was mentioned in the comments, the RevisionHover was a good starting point. The magic is in implementing the IAnnotationHoverExtension and creation of a custom AbstractReusableInformationControlCreator. I am posting a code snippet with solution that worked for me.

public class MyHover implements IAnnotationHover, IAnnotationHoverExtension {
...
    @Override
    public IInformationControlCreator getHoverControlCreator() {
        return new MyCreator();
    }
...
    @Override
    public Object getHoverInfo(ISourceViewer sv, ILineRange lr, int vnl) {
        return "<a href='www.stackoverflow.com'>so</a>";
    }
...
   private final class MyCreator extends AbstractReusableInformationControlCreator {

        protected IInformationControl doCreateInformationControl(Shell parent) {

                BrowserInformationControl control = 
                                    new BrowserInformationControl(
                                        parent, 
                                        JFaceResources.DIALOG_FONT, 
                                        false);
                control.addLocationListener(
                                    new LocationAdapter() {
                                        @Override
                                        public void changing(LocationEvent ev) {
                                            if (ev.location.startsWith("file:")) {
                                                // !This opens the link!
                                                openUrl(ev.location)    
                                            }
                                        }
                                    });
                return control;
        }
    }
}
Related