problem with table viewer comparator [message #639456] |
Tue, 16 November 2010 16:16 |
Luca Ferrari Messages: 159 Registered: November 2009 |
Senior Member |
|
|
Hi all,
I've got a problem with a table viewer that I really cannot understand: the following snippet when run on my system produces a null pointer exception after the first element addition in the table viewer:
public class View extends ViewPart {
public static final String ID = "RCPEXAMPLE.view";
private TableViewer viewer;
class ViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public String getColumnText(Object obj, int index) {
return getText(obj);
}
public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}
public Image getImage(Object obj) {
return PlatformUI.getWorkbench().getSharedImages().getImage(
ISharedImages.IMG_OBJ_ELEMENT);
}
}
class Comparator extends ViewComparator{
public Comparator(ViewRegistry reg) {
super(reg);
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see org.eclipse.ui.internal.dialogs.ViewComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
*/
@Override
public int compare(Viewer viewer, Object e1, Object e2) {
String s1 = (String) e1;
String s2 = (String) e2;
System.out.println("Comparing " + s1 + " against " + s2);
return s1.compareTo(s2);
}
}
class ViewContentProvider implements IStructuredContentProvider{
private List<String> content = null;
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
if( newInput instanceof List ){
content = (List<String>) newInput;
for(String s : content )
((TableViewer) v).add(s);
}
v.refresh();
}
public void dispose() {
}
public Object[] getElements(Object parent) {
return (content != null ? content.toArray() : new String[]{"AA", "BB", "CC"});
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.VIRTUAL
| SWT.V_SCROLL);
ViewContentProvider provider = new ViewContentProvider( );
viewer.setContentProvider( provider );
viewer.setLabelProvider(new ViewLabelProvider());
String[] contents = new String[]{" First", "Second", "third" };
viewer.setInput( contents );
viewer.setComparator( new Comparator( null ) );
PlatformUI.getWorkbench().getDisplay().asyncExec( new Runnable(){
@Override
public void run() {
int j = 10;
LinkedList<String> newContent = new LinkedList<String>();
newContent.add("ZZZ");
newContent.add("FFF");
newContent.add("GGG");
newContent.add("AAA");
viewer.setInput( newContent );
}
}
);
}
public void setFocus() {
viewer.getControl().setFocus();
}
}
The above does the following:
- create a table viewer and fill it with an initial content
- add a single entry at the time forcing the comparator triggering
Now, after the first addition (ZZZ) there is a null pointer exception within the compare method. I've debugged a lot and found that the method AbstractTableviewer.indexForElement, even if computes right min/max, get a null object from the doGetItem(mid) even if the mid is, of course, within min and max (and so the item should be valid).
Am I doing something wrong?
Thanks.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04830 seconds