RSS

Search Engine

Monday, June 28, 2010

Cell Editors

A cell editor defines how the user can edit certain cells of a table.

We will define the editors for our table. The "First name" and "Last name" column will be editable by a textfield. The "Gende"’ column will be editable by a drop down list and the "Married" column will be editable by a checkbox.

Create a new class with the name "PersonEditingSupport" in package de.vogella.jface.tableviewer.providers.

   
package de.vogella.jface.tableviewer.providers;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;

import de.vogella.jface.tableviewer.model.Person;

public class PersonEditingSupport extends EditingSupport {
private CellEditor editor;
private int column;

public PersonEditingSupport(ColumnViewer viewer, int column) {
super(viewer);

String[] gender = new String[2];
gender[0] = "male";
gender[1] = "female";

// Create the correct editor based on the column index
switch (column) {
case 2:
editor = new ComboBoxCellEditor(((TableViewer) viewer).getTable(),
gender);
break;
case 3:
editor = new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
break;
default:
editor = new TextCellEditor(((TableViewer) viewer).getTable());
}
this.column = column;
}

@Override
protected boolean canEdit(Object element) {
return true;
}

@Override
protected CellEditor getCellEditor(Object element) {
return editor;
}

@Override
protected Object getValue(Object element) {
Person person = (Person) element;

switch (this.column) {
case 0:
return person.getFirstName();
case 1:
return person.getLastName();
case 2:
if (person.getGender().equals("male")) {
return 0;
}
return 1;
case 3:
return person.isMarried();
default:
break;
}
return null;
}

@Override
protected void setValue(Object element, Object value) {
Person pers = (Person) element;

switch (this.column) {
case 0:
pers.setFirstName(String.valueOf(value));
break;
case 1:
pers.setLastName(String.valueOf(value));
break;
case 2:
if (((Integer) value) == 0) {
pers.setGender("male");
} else {
pers.setGender("female");
}
break;
case 3:
pers.setMarried((Boolean) value);
break;
default:
break;
}

getViewer().update(element, null);
}

}

Explanation of the the main methods of "PersonEditingSupport":

  • ‘getCellEditor’ : In this method you return the celleditor you want to use (e.g. Texteditor). I work with a ‘switch-case-command’ to split my columns and give every column its own celleditor.

  • ‘setValue’: Receives the new value the user gives. You have to set the new value to the object which is given, too.

  • ‘getValue’: Receives the object which was changed and returns the value for the table. You have to return the new value of the object.

Now assign the editors to your columns. We do this in View.java in the method createColumns().

   
// This will create the columns for the table
private void createColumns(TableViewer viewer) {

String[] titles = { "First name", "Last name", "Gender", "Married" };
int[] bounds = { 100, 100, 100, 100 };

for (int i = 0; i < titles.length; i++) {
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
column.getColumn().setText(titles[i]);
column.getColumn().setWidth(bounds[i]);
column.getColumn().setResizable(true);
column.getColumn().setMoveable(true);
// enable editing support
column.setEditingSupport(new PersonEditingSupport(viewer, i));
}
Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
}

Run now your application. You should now be able to modify the content of the JFace table.

0 comments:

Post a Comment