如何向DefaultInformationControl添加带控件的工具栏?

时间:2016-03-30 13:15:45

标签: java eclipse eclipse-plugin hover swt

到目前为止我设法做的是为我想要添加的ToolBar添加一个ToolBarManager,它包含我要添加的ToolItem但是当基于此DefaultInformationControl的悬停弹出窗口打开时会显示nothig。 我给了所有ToolItems文本和样式。

我的代码是:

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

1 个答案:

答案 0 :(得分:0)

使用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延伸DefaultInformationControl

因此,在创建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;
    }
}