How do I access a TableEditor or an EditingSupport that has been attached to a TableViewer?
I am currently working on an application that uses a TableViewer in several places to display formatted tabular data. Each table must have an export feature where all its content is exported in an Excel file.
In order to avoid unnecessery code duplication, I thought it would be nice to rely upon the viewer framework of SWT and use it to get the formatted tabular data using the registered label providers.
This approach works well with standard read-only tables, either with table-level and column-level label providers. However, I am stuck when an EditingSupport or TableEditors have been set on the table.
In such cases, we often had label providers to return blank values and let the TableViewer deal with the EditingSupport or the TableEditor to get the representation of the cell data.
Is there any way for me to access a TableEditor or an EditingSupport that has been attached to a TableViewer (without keepin开发者_运维知识库g a separate reference to said objects) so I can use them to retrieve a proper representation of the cell data ?
If not, we will probably rewrite our label providers so that they handle columns with EditingSupport as well, but it would be nice if we did not have to.
There is no way, that I can find, to retrieve the EditingSupport or TableEditor objects from a TableViewer. We Store the EditingSupport objects separately for our uses but it sounds like this is not an option for you so you could store the EditingSupport object for a given column in the data map of the column itself. Something like:
TableColumn column = new TableColumn(table, SWT.RIGHT);
EditingSupport editingSupport = new TableEditingSupport();
column.setData("editing_support", editingSupport);
This gives you access to the EditingSupport objects through a single reference to the TableViewer and when you wanted to retrieve them you could do something like:
final Table table = tableViewer.getTable();
for(TableColumn column : table.getColumns())
{
EditingSupport editingSupport = (EditingSupport)column.getData("editing_support");
}
Its fairly ugly and hacky and, depending on your circumstances, I would probably suggest rewriting the LabelProviders as you said but if you choose not to do that, this is an option. Obviously if you have access to either the table or the list of columns you can bypass some of the mess in retrieval but the core idea remains unchanged.
精彩评论