Bug 123205 - [Contributions] ToolBarManger displays wrong number of items, if pack() is not called
Summary: [Contributions] ToolBarManger displays wrong number of items, if pack() is no...
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P5 normal (vote)
Target Milestone: 3.4 M2   Edit
Assignee: Paul Webster CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed, helpwanted
Depends on:
Blocks:
 
Reported: 2006-01-10 02:56 EST by Sanjay Chaudhuri CLA
Modified: 2008-05-16 14:09 EDT (History)
1 user (show)

See Also:


Attachments
Snap-shot showing the problem along with the code snippet (28.82 KB, image/gif)
2006-01-10 03:09 EST, Sanjay Chaudhuri CLA
no flags Details
Patch to correct the relayout strategy of the ToolBarManager. (721 bytes, patch)
2007-08-29 09:04 EDT, Remy Suen CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sanjay Chaudhuri CLA 2006-01-10 02:56:38 EST
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>
Comment 1 Sanjay Chaudhuri CLA 2006-01-10 03:09:37 EST
Created attachment 32748 [details]
Snap-shot showing the problem along with the code snippet
Comment 2 Steve Northover CLA 2006-01-10 11:41:05 EST
If the size of the tool bar is too small, then it won't show all the buttons.  Is ToolBarManager setting the size?
Comment 3 Sanjay Chaudhuri CLA 2006-01-10 12:33:45 EST
Well, the size is much bigger than required. I tried setting different sizes. In the attachment you would see that there is enough room, for the buttons to display.

I am not setting any size on the ToolbarManager. I was setting the size on the Shell. Infact, while using this in a Plug-in environment, I do not set any size.

And as mentioned, everything works out okay, when there is just ONE update, and not updates after every toolBarManager.add(...)

The given code snippet should reproduce the problem.
Comment 4 Michael Van Meekeren CLA 2006-04-21 13:56:39 EDT
Moving Dougs bugs
Comment 5 Paul Webster CLA 2007-04-05 19:04:53 EDT
Assigning to component owner
PW
Comment 6 Remy Suen CLA 2007-08-29 09:04:18 EDT
Created attachment 77246 [details]
Patch to correct the relayout strategy of the ToolBarManager.

As can be seen, the current code only calls layout() on the parent control if the item count went from 0 to N or from N to 0. Thus, if it was N to N + 1 (in the given scenario), nothing would happen (shocking!).

This new call ensures that layout() is "always" called assuming that neither of them is zero. This is because if you were to swap two contribution items' positions, the size would still be the same but their positions are not so they would have to be re-laid-out.

Paul, I am a little concerned about this fix though as I am not sure if it still abides to the API contract of "The default implementation of this framework method re-lays out the parent when the number of items crosses the zero threshold.". I guess you can kind of make the argument that the number of items are non-zero or went from N to 0, it has crossed the zero threshold, per se.
Comment 7 Paul Webster CLA 2007-09-13 12:05:27 EDT
Released to HEAD >20070913

With a slight mod:
if ((oldCount != newCount) && (newCount!=0)) {

PW
Comment 8 Paul Webster CLA 2007-09-18 14:21:48 EDT
In I20070918-0010
PW