Bug 123205 [Contributions] ToolBarManger displays wrong number of items, if pack() is not called
I have added bunch of Action items into the ToolBarManager. After each addition, I call an update, which is kind of required for the design, I currently have. Here are my observations: 1. If I do not call pack(), only one button shows up. 2. If I only use the LAST update, then all button shows up, no matter, whether I use size(), pack(), or leave both those out. Please find below the SWT code snippet to reproduce the problem. I am using 3.2M3 Build Id: I20051102-1600 <code> import org.eclipse.jface.action.Action; import org.eclipse.jface.action.ToolBarManager; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.ToolBar; public class SampleUI { public static void main( String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout( new GridLayout(1, false)); //Create Images Image blueImage = new Image ( Display.getCurrent(), 20, 20); Color color = Display.getCurrent().getSystemColor (SWT.COLOR_BLUE); GC gc = new GC ( blueImage); gc.setBackground (color); gc.fillRectangle ( blueImage.getBounds ()); gc.dispose(); Image greenImage = new Image ( Display.getCurrent(), 20, 20); color = Display.getCurrent().getSystemColor (SWT.COLOR_GREEN); gc = new GC ( greenImage); gc.setBackground (color); gc.fillRectangle ( greenImage.getBounds ()); gc.dispose(); Image redImage = new Image ( Display.getCurrent(), 20, 20); color = Display.getCurrent().getSystemColor (SWT.COLOR_RED); gc = new GC ( redImage); gc.setBackground (color); gc.fillRectangle ( redImage.getBounds ()); gc.dispose(); ToolBarManager toolBarManager = new ToolBarManager( new ToolBar( shell, SWT.FLAT)); toolBarManager.add( new TestAction( blueImage)); toolBarManager.update( true); toolBarManager.add( new TestAction( greenImage)); toolBarManager.update( true); toolBarManager.add( new TestAction( redImage)); toolBarManager.update( true); //Only leaving this one, eliminates the problem shell.setSize( 200, 200); //Setting the SIZE shows only 1 button //shell.pack(); //Calling PACK, shows all buttons shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } class TestAction extends Action { public TestAction( Image image) { setImageDescriptor( ImageDescriptor.createFromImage( image)); } public void run() { System.out.println( "Key Pressed"); } } </code>
bundles/org.eclipse.jface/src/org/eclipse/jface/action/ToolBarManager.java
try {
                    if (useRedraw) {
                        toolBar.setRedraw(false);
                    }
                    
                    for (int i = toRemove.size(); --i >= 0;) {
                        ToolItem item = (ToolItem) toRemove.get(i);
                        if (!item.isDisposed()) {
                            Control ctrl = item.getControl();
                            if (ctrl != null) {
                                item.setControl(null);
                                ctrl.dispose();
                            }
                            item.dispose();
                        }
                    }
                    
                    IContributionItem src, dest;
                    mi = toolBar.getItems();
                    int srcIx = 0;
                    int destIx = 0;
                    for (Iterator e = clean.iterator(); e.hasNext();) {
                        src = (IContributionItem) e.next();
                        
                        if (srcIx < mi.length) {
							dest = (IContributionItem) mi[srcIx].getData();
						} else {
							dest = null;
						}
                        if (dest != null && src.equals(dest)) {