Happy Coder
Happy Coder

Reputation: 1423

programmatically scroll ScrolledComposite TableViewer item

I have a dialog with large table viewer that lays on ScrolledComposite. I need programmatically scroll ScrolledComposite to select item from TableViewer. Looks like a easy task but I really got stack. I tried number of thinks and non of them are working.

There is my sample code:

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

/**
* Scroll a Viewer 99th element
*
*/
public class Snippet008RevealElement {

public class MyModel {
    public int counter;

    public MyModel(int counter) {
        this.counter = counter;
    }

    @Override
    public String toString() {
        return "Item " + this.counter;
    }
}

public Snippet008RevealElement(Shell shell) {
    ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);

    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    scrolledComposite.setLayoutData(data);

    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setExpandVertical(true);

    Composite main = new Composite(scrolledComposite, SWT.NONE);
    main.setLayout(new GridLayout());
    main.setLayoutData(new GridData(GridData.FILL_BOTH));


    final TableViewer v = new TableViewer(main);
    v.setLabelProvider(new LabelProvider());
    v.setContentProvider(ArrayContentProvider.getInstance());
    MyModel[] model = createModel();
    v.setInput(model);
    v.getTable().setLinesVisible(true);

 //     v.reveal(model[99]);
 //     v.getTable().setSelection(99);

    TableItem[] items = v.getTable().getItems();
    TableItem item = items[99];
    scrolledComposite.getVerticalBar().setSelection(item.getBounds().y);

    scrolledComposite.setContent(main);
    scrolledComposite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}

private MyModel[] createModel() {
    MyModel[] elements = new MyModel[100];

    for( int i = 0; i < 100; i++ ) {
        elements[i] = new MyModel(i);
    }

    return elements;
}

/**
 * @param args
 */
public static void main(String[] args) {
    Display display = new Display ();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    new Snippet008RevealElement(shell);
    shell.open ();

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

    display.dispose ();

}

}

the real challenge or a bug is to find real bounds for TableItem item = items[99]; that not visible on composite.

Upvotes: 0

Views: 992

Answers (1)

greg-449
greg-449

Reputation: 111197

You can use the setOrigin method of ScrolledComposite for this using something like:

scrolledComposite.setContent(main);
scrolledComposite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));

// Need to run the calculations after all size calculations have been done
// So do asynchronously. 

final Display display = scrolledComposite.getDisplay();

display.asyncExec(() ->
{
  final Table table = v.getTable();

  final TableItem item = table.getItem(99);

  Rectangle itemBounds = item.getBounds();

  // Convert to be relative to scrolled composite

  itemBounds = display.map(viewer.getTable(), scrolledComposite, itemBounds);

  scrolledComposite.setOrigin(0, itemBounds.y);
 });

Note: The bounds calculations are not accurate if you call this code during the initialization of the controls so I have shown it being done asynchronously here. The asyncExec is not needed if you run the code from a Button or something like that.

Upvotes: 2

Related Questions