I use a TableViewer
to display comma separated String
s. The implementation looks like this:
TableViewer tblvwrGroupAssignments = new TableViewer(container, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Table tblGroupAssignments = tblvwrGroupAssignments.getTable();
tblGroupAssignments.setLinesVisible(true);
tblGroupAssignments.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 2));
final TableViewerColumn clmnGroupAssignments = new TableViewerColumn(tblvwrGroupAssignments, SWT.NONE);
tblGroupAssignments.addListener(SWT.Resize, new Listener()
{
@Override
public void handleEvent(Event event)
{
clmnGroupAssignments.getColumn().pack();
}
});
tblvwrGroupAssignments.setContentProvider(new IStructuredContentProvider()
{
public void inputChanged(Viewer viewer, Object oldInput, Object newInput){}
public void dispose(){}
@Override
public Object[] getElements(Object inputElement)
{
String[] toRet = ((String)inputElement).split(",");
for (int i = 0; i < toRet.length; i++)
{
toRet[i] = toRet[i].trim();
}
return toRet;
}
});
tblvwrGroupAssignments.setUseHashlookup(true);
tblvwrGroupAssignments.setLabelProvider(new GroupAssignmentLabelProvider()); //responsible for displaying the last line italic
tblvwrGroupAssignments.setInput("HW_ADMIN, <Add new value here>");
The code above works just fine. The problem I have is about EditingSupport
, to be precise about following method:
@Override
protected void setValue(Object element, Object value)
{
element = value;
viewer.update(element, null);
}
Usually I am supposed to cast element
to the data type I used for the input and then invoke a setter. But since I work with String
s which are immutable I can only replace old String
with the new one. Apparently the element
instance is not the same as an instance used by TableViewer
, because the values in the table aren't being set.
Do I need to encapsulate String
to some class or is there a more elegant solution?
Yes, you need to wrap the strings in a mutable object so that you can change the value to the new string.
All the uses of EditingSupport that I can see in the Eclipse source do this.