Question

I have a little problem and I'm asking for help.

I have a TableViewer in my Master part and a lot of entries updating it very quickly. My details pages display the selected item infos. I would like to make this details pages persistent and only be notified when selection is changing.

The problem I'm facing is that when a new item is added to the tableviewer, the selection changes automatically.

EDITING : I have a Handler which update the TableViewer, adding a new item at the beginning of a list which is the input of my TableViewer.

When the TableViewer is SWT.VIRTUAL the selection is on the index and when an item is added, the selected row is the same but items have moved.

When TableViewer is not SWT.VIRTUAL the selection occurs on the object but performances are very low !

Any piece of advice ? Thank you

public void update(Object obj_p) {
    if (obj_p instanceof IMessageEvent) {
        IMessageEvent event = (IMessageEvent) obj_p;
        synchronized (_currentListEvents) {
            _currentListEvents.addFirst(event);
            if (_currentListEvents.size() > _maxEvents) {
                _currentListEvents.removeLast();
            }
        }
    }
    if (null == obj_p) {
        synchronized (_currentListEvents) {
            _currentListEvents.clear();
        }
    }
}
Était-ce utile?

La solution

When creating TableViewer use SWT.FULL_SELECTION style.

There is a simple runable code. It behaves exactly as you posted, the selection is being changed, but when you use the commented TableViewer constructor, it works for me.

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestCase {

private static List<String> model = new ArrayList<String>();
private static TableViewer viewer;

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

//  viewer = new TableViewer(shell, SWT.SINGLE | SWT.FULL_SELECTION |    SWT.H_SCROLL | SWT.V_SCROLL | SWT.VIRTUAL);
    viewer = new TableViewer(shell, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.VIRTUAL);

    viewer.getTable().setHeaderVisible(true);
    viewer.getTable().setLinesVisible(true);
    viewer.setUseHashlookup(true);

    viewer.setContentProvider(ArrayContentProvider.getInstance());
    viewer.setLabelProvider(new LabelProvider());

    model.add("element2");
    model.add("element1");
    model.add("element0");

    viewer.setInput(model);

    viewer.setSelection(new StructuredSelection(model.get(1)));

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            System.out.println("selection changed");
        }
    });

    addContent();

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

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

private static void addContent() {
    Display.getCurrent().timerExec(2000, new Runnable() {

        @Override
        public void run() {
            model.add(0, "element" + model.size());
            viewer.refresh();
            addContent();
        }
    });
}

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top