How to add toolbar to java text hover eclipse

馋奶兔 提交于 2019-12-25 15:50:30

问题


I am tring to create my own text hover plugin for eclipse. I success to write my own code in my hover, but I try to add a toolbar to the hover (inside the new tooltip opened). I read that I need to use the getHoverControlCreator function, and I managed to add the toolbar manager that I see when the text hover is opened while running the plugin,in the debbuger I can see that the ToolBarManger has the ToolBar that has the ToolItems, but I can't see them in the real text hover when I opened it.

this is my code:

public IInformationControlCreator getHoverControlCreator() {
        return new IInformationControlCreator() {
            public IInformationControl createInformationControl(Shell parent) {
                ToolBar tb = new ToolBar(parent, SWT.HORIZONTAL);
                ToolBarManager tbm = new ToolBarManager(tb);
                DefaultInformationControl dic = new DefaultInformationControl(parent, tbm);
                ToolItem ti = new ToolItem(tb, SWT.PUSH);
                ti.setText("hello");
    tb.update();
    tb.redraw();
    tbm.update(true);
    parent.update();
    parent.redraw();

    parent.layout();

                return dic;
            }

回答1:


This is what one of the Eclipse hover controls does:

@Override
public IInformationControl doCreateInformationControl(Shell parent) {
    ToolBarManager tbm = new ToolBarManager(SWT.FLAT);

    DefaultInformationControl iControl = new DefaultInformationControl(parent, tbm);

    IAction action = new MyAction();
    tbm.add(action);

    tbm.update(true);

    return iControl;
}

So it does not create the ToolBar - leave that up to DefaultInformationControl. It uses an Action in the tool bar and adds it after creating the DefaultInformationControl. It just calls update(true) at the end.

(This is a modified version of parts of org.eclipse.jdt.internal.ui.text.java.hover.NLSStringHover)

MyAction would be something like:

private class MyAction extends Action
{
  MyAction()
  {
    super("Title", .. image descriptor ..);

    setToolTipText("Tooltip");
  }

  @Override
  public void run()
  {
    // TODO your code for the action
  }
}


来源:https://stackoverflow.com/questions/37098305/how-to-add-toolbar-to-java-text-hover-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!