简体   繁体   中英

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

I am working on an enhancement to add buttons to a toolbar for an Eclipse plugin. The technology is fairly old. I am using IBM Rational Application Developer 7.0.10. Here is what I see as far as the versions

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

When my code adds a new button to the toolbar, it messes up the toolbar. It only shows some of the buttons and hides the rest. But when I resize the view even the slightest bit, all the buttons show. It does not appear to be a case of screen real estate as making the view larger does not help. I am new to Eclipse plug-in development, so I am not sure what is causing this. I seem to be doing what is needed to add a button. I have tried different things, like insertBefore instead of add and so on. But nothing seems to help.

I wrote some test code (using the sample plugins that come with Eclipse) to isolate the issue, but I have not had success. I have given the code for 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);

   }
}

I wrote the following class for the selection change event

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

When the application comes up

这是应用程序首次出现的时间

This is upon a selection on the Tree

在此处输入图片说明

This is after an ever so slight resize

在此处输入图片说明

Can anyone help?

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

It would appear that this is a bug with Eclipse 3.2 (or at least the version bundled with RAD 7.0). I tried this with Eclipse 3.7 and did not see the problem.

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