简体   繁体   English

如何将带有控件的工具栏添加到 DefaultInformationControl?

[英]How do I add a toolbar with controls to the DefaultInformationControl?

What I managed to do so far is adding a ToolBarManager for the ToolBar I created, which contains the ToolItem I want to add but nothing is shown when the hover popup based on this DefaultInformationControl is open.到目前为止,我设法为我创建的 ToolBar 添加了一个 ToolBarManager,其中包含我要添加的 ToolItem,但是当基于此 DefaultInformationControl 的悬停弹出窗口打开时,没有显示任何内容。 I gave all the ToolItems text and style.我给了所有的 ToolItems 文本和样式。

My code is :我的代码是:

public class JavaTextHover implements IJavaEditorTextHover, ITextHoverExtension{
.
.
.
    @Override
    public IInformationControlCreator getHoverControlCreator() {
        return new IInformationControlCreator() {
            public IInformationControl createInformationControl(Shell parent) {
                ToolBar toolBar = new ToolBar(parent, SWT.NONE);
                ToolItem itemPush = new ToolItem(toolBar, SWT.PUSH);
                itemPush.setText("PUSH item");
                ToolItem itemCheck = new ToolItem(toolBar, SWT.CHECK);
                itemCheck.setText("CHECK item");
                ToolItem itemRadio1 = new ToolItem(toolBar, SWT.RADIO);
                itemRadio1.setText("RADIO item 1");
                ToolItem itemRadio2 = new ToolItem(toolBar, SWT.RADIO);
                itemRadio2.setText("RADIO item 2");
                ToolItem itemSeparator = new ToolItem(toolBar, SWT.SEPARATOR);
                Text text = new Text(toolBar, SWT.BORDER | SWT.SINGLE);
                text.pack();
                itemSeparator.setWidth(text.getBounds().width);
                itemSeparator.setControl(text);
                final ToolItem itemDropDown = new ToolItem(toolBar, SWT.DROP_DOWN);
                itemDropDown.setText("DROP_DOWN item");
                itemDropDown.setToolTipText("Click here to see a drop down menu ...");
                final Menu menu = new Menu(parent, SWT.POP_UP);
                new MenuItem(menu, SWT.PUSH).setText("Menu item 1");
                new MenuItem(menu, SWT.PUSH).setText("Menu item 2");
                new MenuItem(menu, SWT.SEPARATOR);
                new MenuItem(menu, SWT.PUSH).setText("Menu item 3");
                itemDropDown.addListener(SWT.Selection, new Listener() {
                  public void handleEvent(Event event) {
                    if(event.detail == SWT.ARROW) {
                      Rectangle bounds = itemDropDown.getBounds();
                      Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height);
                      menu.setLocation(point);
                      menu.setVisible(true);
                    }
                  }
                });
                Listener selectionListener = new Listener() {
                  public void handleEvent(Event event) {
                    ToolItem item = (ToolItem)event.widget;
                    System.out.println(item.getText() + " is selected");
                    if( (item.getStyle() & SWT.RADIO) != 0 || (item.getStyle() & SWT.CHECK) != 0 ) 
                      System.out.println("Selection status: " + item.getSelection());
                  }
                };
                itemPush.addListener(SWT.Selection, selectionListener);
                itemCheck.addListener(SWT.Selection, selectionListener);
                itemRadio1.addListener(SWT.Selection, selectionListener);
                itemRadio2.addListener(SWT.Selection, selectionListener);
                itemDropDown.addListener(SWT.Selection, selectionListener);
                toolBar.pack();
                toolBar.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
                ToolBarManager tbm=new ToolBarManager(toolBar);
                System.out.println(tbm.getControl().getChildren().length);
                DefaultInformationControl dic=new DefaultInformationControl(parent, tbm);
                dic.setBackgroundColor(new Color(null, 98,201,145));
                return dic;
            }
        };
    }
.
.
.
}

The only example of using the tool bar that I can see in the Eclipse core code does things this way:我可以在 Eclipse 核心代码中看到的使用工具栏的唯一示例是这样做的:

private static final class PresenterControlCreator extends AbstractReusableInformationControlCreator {

    @Override
    public IInformationControl doCreateInformationControl(Shell parent) {
        ToolBarManager tbm= new ToolBarManager(SWT.FLAT);
        NLSHoverControl iControl= new NLSHoverControl(parent, tbm);
        OpenPropertiesFileAction openPropertiesFileAction= new OpenPropertiesFileAction(iControl);
        tbm.add(openPropertiesFileAction);
        tbm.update(true);
        return iControl;
    }
}

NLSHoverControl extends DefaultInformationControl . NLSHoverControl扩展了DefaultInformationControl

So it is adding to the tool bar after creating the DefaultInformationControl and using actions rather than tool items.因此,它在创建 DefaultInformationControl 并使用操作而不是工具项后添加到工具栏。

static class NLSHoverControl extends DefaultInformationControl implements IInformationControlExtension2 {

    /**
     * The NLS control input.
     */
    private NLSHoverControlInput fInput;

    /**
     * Creates a resizable NLS hover control with the given shell as parent.
     * 
     * @param parent the parent shell
     * @param tbm the toolbar manager or <code>null</code> if toolbar is not desired
     */
    public NLSHoverControl(Shell parent, ToolBarManager tbm) {
        super(parent, tbm);

    }

    /**
     * Creates an NLS hover control with the given shell as parent.
     * 
     * @param parent the parent shell
     * @param tooltipAffordanceString the text to be used in the status field or
     *            <code>null</code> to hide the status field
     */
    public NLSHoverControl(Shell parent, String tooltipAffordanceString) {
        super(parent, tooltipAffordanceString);
    }

    /**
     * {@inheritDoc} This control can handle {@link NLSStringHover.NLSHoverControlInput}.
     */
    public void setInput(Object input) {
        Assert.isLegal(input instanceof NLSHoverControlInput);

        NLSHoverControlInput info= (NLSHoverControlInput)input;
        setInformation(info.fInformation);
        fInput= info;
    }

    /**
     * Returns the control input.
     * 
     * @return the control input
     */
    public NLSHoverControlInput getInput() {
        return fInput;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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