Over a million developers have joined DZone.

Simplified TableViewer for Editing Purposes

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

Using the JFace TableViewer for editing purposes can be rather tedious and is certainly not a job for an Eclipse beginner. The problems essentially come up when we start using TableCellModifier. Some of these problems could be removed by providing simple interfaces like TableRowData which allows setting and getting values from model.

/**
* TableRowData acts as a holder of a model object represented
* as a row in a TableViewer.
*
* TableRowData can be interpreted by TableDataLabelProvider as a row
* in a TableViewer.
*/
public abstract class TableRowData
{
/**
* Subclasses should return the display value of a specified column.
*
* If null is returned, then null is again passed back in setColumnValue.
* A null for a combo implies, nothing would get selected.
*/
public abstract String getColumnDisplayValue(String columnName);

/**
* Subclasses should set the displayValue in their underlying model
* object representing the row.
*
* The displayValue can be transformed into a business specific value
* and then stored inside the model object representing the row.
*
* If nothing is selected in a combo, then null is passed in displayValue
* If nothing or empty-text is typed in a text-field, null is passed in displayValue
*/
public abstract void setColumnValue(String columnName, String displayValue);

/**
* Subclasses can override this to display an image besides each cell text.
*/
public Image getColumnImage(String columnName) {
return null;
}

/**
* Subclasses can override this to indicate that a row is not modifiable.
*/
public boolean canModify() {
return true;
}
}

 A table viewer itself could be setup as ....

private void createBookTable(Composite parent)
{
/* create table viewer */
tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
tableViewer.getTable().setHeaderVisible(true);
tableViewer.getTable().setLinesVisible(true);

GridData data = new GridData(GridData.FILL_BOTH);
data.widthHint = 500;
data.heightHint = 200;
data.verticalSpan = 5;
tableViewer.getTable().setLayoutData(data);

/* add columns */
List<TableColumnDescriptor> columnDescriptors = new ArrayList<TableColumnDescriptor>();

/* title */
TableColumnDescriptor columnDesciptor = new TableColumnDescriptor(TableColumnDescriptor.TEXT, BookRowData.COLUMN_TITLE, 200);
columnDescriptors.add(columnDesciptor);

/* author */
columnDesciptor = new TableColumnDescriptor(TableColumnDescriptor.TEXT, BookRowData.COLUMN_AUTHOR, 200);
columnDescriptors.add(columnDesciptor);

/* author */
columnDesciptor = new TableColumnDescriptor(TableColumnDescriptor.COMBO, BookRowData.COLUMN_CATEGORY,
new String[]{
BookRowData.CATEGORY_OPTION_FICTION,
BookRowData.CATEGORY_OPTION_ENGLISH,
BookRowData.CATEGORY_OPTION_MATHEMATICS,
BookRowData.CATEGORY_OPTION_SCIENCE},
100);
columnDescriptors.add(columnDesciptor);

/* setup table for editing */
TableViewerUtil.setupTableForEditing(tableViewer, columnDescriptors);

/* set input model to tableviewer */
BookTableData input = new BookTableData(getModel());
tableViewer.setInput(input);
}

The above is an example for editing a list of books. It illustrates the usage of the above discussed framework on top of JFace TableViewer. Features include adding a book, deleting a book, editing a book, moving a book up or down in the displayed list of books.

 

 

 

 

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}