Tim
Tim

Reputation: 389

Programmatically scroll ExpandBar

In my code, org.eclipse.swt.widgets.ExpandBar contains multiple ExpandItems. The ExpandBar is setup to scroll. How do I programmetically scroll the ExpandBar? I looked for examples and API but no luck.

Upvotes: 2

Views: 1300

Answers (1)

Baz
Baz

Reputation: 36904

Alright, wrap your ExpandBar in a ScrolledComposite and let it handle scrolling.

The advantage of this is that ScrolledComposite has a method called .setOrigin(int, int) which you can use to scroll to a position.

Here is some example code:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("ExpandBar Example");

    final ScrolledComposite scrolledComp = new ScrolledComposite(shell, SWT.V_SCROLL);

    final ExpandBar bar = new ExpandBar(scrolledComp, SWT.NONE);

    for (int i = 0; i < 3; i++)
    {
        Composite composite = new Composite(bar, SWT.NONE);
        composite.setLayout(new GridLayout());

        for (int j = 0; j < 10; j++)
            new Label(composite, SWT.NONE).setText("Label " + i + " " + j);

        ExpandItem item = new ExpandItem(bar, SWT.NONE, 0);
        item.setText("Item " + i);
        item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item.setControl(composite);
    }

    bar.getItem(1).setExpanded(true);
    bar.setSpacing(8);

    /* Make sure to update the scrolled composite when we collapse/expand
     * items */
    Listener updateScrolledSize = new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            display.asyncExec(new Runnable()
            {
                @Override
                public void run()
                {
                    scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
                }
            });
        }
    };

    bar.addListener(SWT.Expand, updateScrolledSize);
    bar.addListener(SWT.Collapse, updateScrolledSize);

    scrolledComp.setContent(bar);
    scrolledComp.setExpandHorizontal(true);
    scrolledComp.setExpandVertical(true);
    scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    shell.setSize(400, 200);
    shell.open();

    /* Jump to the end */
    scrolledComp.setOrigin(0, scrolledComp.getSize().y);

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

Looks like this after running:

enter image description here

As you can see it's scrolled to the end.


Update

Ok, if you want to jump to the position of a specific item, do the following:

Add a Button to test the functionality. Inside the Listener, get the y position and scroll to it:

Button jumpTo = new Button(shell, SWT.PUSH);
jumpTo.setText("Jump to item");
jumpTo.addListener(SWT.Selection, new Listener()
{
    private int counter = 0;
    @Override
    public void handleEvent(Event e)
    {
        int y = getYPosition(bar, counter);

        /* Increment the counter */
        counter = (counter + 1) % bar.getItemCount();

        /* Scroll into view */
        scrolledComp.setOrigin(0, y);
    }
});

Use this method to get the y position:

private static int getYPosition(ExpandBar bar, int position)
{
    /* Calculate the position */
    int y = 0;
    for(int i = 0; i < position; i++)
    {
        /* Incorporate the spacing */
        y += bar.getSpacing();

        /* Get the item (On LINUX, use this line) */
        ExpandItem item = bar.getItem(bar.getItemCount() - 1 - i);
        /* Get the item (On WINDOWS, use this line) */
        //ExpandItem item = bar.getItem(i);

        /* Add the header height */
        y += item.getHeaderHeight();

        /* If the item is expanded, add it's height as well */
        if(item.getExpanded())
            y += item.getHeight();
    }

    return y;
}

Upvotes: 3

Related Questions