如何在viewpart内创建工具栏(不使用plugin.xml)

时间:2014-10-31 09:27:03

标签: swt toolbar jface

我创建了一个ToolItem"另存为"像上面的图像,但它不显示在工具栏位置。那么如何在viewpart中创建一个工具栏(不使用plugin.xml)

IMAGE EXAMPLE

这是我的代码创建工具栏:

public void createToolbar(Composite parent) {
        // Create composite Toolbar and set layout
        toolBarComposite = new Composite(parent, SWT.NONE);
        gridLayout = new GridLayout(1, false);
        toolBarComposite.setLayout(gridLayout);

        gridData = new GridData(SWT.RIGHT, SWT.NONE, true, false);
        toolBarComposite.setLayoutData(gridData);

        // Create Toolbar
        gridData = new GridData(SWT.RIGHT, SWT.NONE, true, false);
        toolBar = new ToolBar(toolBarComposite, SWT.FLAT);
        toolBar.setLayoutData(gridData);

        // Create Item
        item = new ToolItem(toolBar, SWT.PUSH);
        item.setImage(SAVE_IMAGE);
        item.setToolTipText("Save (Ctrl + S)");
        item.setEnabled(true);
        item.addSelectionListener(new SelectionAdapter() {
            private static final long serialVersionUID = -102212312093090431L;

            @Override
            public void widgetSelected(SelectionEvent e) {
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });
    }

感谢您的进步!

1 个答案:

答案 0 :(得分:1)

您将不得不在视图网站的操作栏上使用贡献。

实施例

// Copy-pasted from an existing project, so the code can be made nicer
private void createAdditionalToolbarActions()
{
    getViewSite().getActionBars().getToolBarManager().add(new GroupMarker("additions")); //$NON-NLS-1$
    getViewSite().getActionBars().getToolBarManager().prependToGroup("additions", new SaveAction()); //$NON-NLS-1$
    getViewSite().getActionBars().updateActionBars();
}

方法getViewSiteViewPart的一部分。在创建视图的内容后调用此方法。

SaveAction必须实施IActionIContributionItem。为方便起见,只需从SaveAction扩展org.eclipse.jface.action.Action并调用setImageDescriptorsetToolTipText等方法。

让您的所有商家登录run覆盖。