为什么添加项目会弄乱 Eclipse 3.2 插件中的工具栏?

Why does adding an item messes up toolbar in an Eclipse 3.2 Plugin?

我正在研究一项增强功能,以便将按钮添加到 Eclipse 插件的工具栏。该技术相当古老。我正在使用 IBM Rational Application Developer 7.0.10。这是我所看到的版本

JDK 1.5
Eclipse Platform 3.2.2
Eclipse Plugin Development 3.2.1
Eclipse RCP 3.2.2

当我的代码向工具栏添加新按钮时,它弄乱了工具栏。它只显示一些按钮并隐藏其余按钮。但是当我稍微调整视图大小时,所有按钮都会显示。这似乎不是屏幕空间的情况,因为放大视图无济于事。我是 Eclipse 插件开发的新手,所以我不确定是什么原因造成的。我似乎在做添加按钮所需的事情。我尝试过不同的东西,比如 insertBefore 而不是 add 等等。但似乎没有任何帮助。

我写了一些测试代码(使用 Eclipse 附带的示例插件)来隔离问题,但我没有成功。我已经给出了 2 classes.

的代码
// This class is generated from Elipse, to which I have added code
public class MenuBarTestView extends ViewPart {

    // Instance variables ...

   public void createPartControl(Composite parent) {
       viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
       drillDownAdapter = new DrillDownAdapter(viewer);
       viewer.setContentProvider(new ViewContentProvider());
       viewer.setLabelProvider(new ViewLabelProvider());
       viewer.setSorter(new NameSorter());
       viewer.setInput(getViewSite());

       // Selection change listener, which adds a button to tool bar
       // I added the following line
       viewer.addSelectionChangedListener(new TreeSelectionChangedListener(this));

       makeActions();
       hookContextMenu();
       hookDoubleClickAction();
       contributeToActionBars();
   }

   .
   .  // Other Eclipse generated code for the sample plugin goes here
   .
   .

   // I added this method to add a button
   public void addButton () {

       IActionBars bars = getViewSite().getActionBars();
       IToolBarManager tbm = bars.getToolBarManager();

       Action action = new Action() {
          public void run() {
              showMessage("Action 1 executed");
          }
      };
      action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
            getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER));
      tbm.add(action);
      tbm.update(false);

   }
}

我为选择更改事件

写了以下class
package menubartest.views;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;

public class TreeSelectionChangedListener implements ISelectionChangedListener {

    private MenuBarTestView view;

    public TreeSelectionChangedListener(MenuBarTestView view) {
        super();
        this.view = view;
    }

    public void selectionChanged(SelectionChangedEvent event) {
        // TODO Auto-generated method stub
        view.addButton();
    }
}

应用程序出现时

这是树上的选择

这是在稍微调整大小之后的结果

有人能帮忙吗?

看来你需要在添加到工具栏后调用IActionBars.updateActionBars()方法。

这似乎是 Eclipse 3.2(或至少是与 RAD 7.0 捆绑的版本)的错误。我在 Eclipse 3.7 上试过了,没有发现问题。