简体   繁体   中英

How to add toolbar to java text hover eclipse

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;
            }

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
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM