Reputation: 1482
I'm unable to find any examples that use CheckBoxTableCell
in a TableViewerColumn
if someone could provide me with an example implementation I would be very grateful.
I already have a working model that shows some string values but I'm unable to represent a boolean value as a checkbox.
Is it even possible to show anything else except a String in a TableViewerColumn?
Upvotes: 2
Views: 2147
Reputation: 14641
The solution described above is perfectly ok.
But there is an alternative solution for displaying checkboxes in a TableViewerColumn
which allows you to draw a native checkbox instead of using your own images. You can simply create a new ColumnLabelProvider
which renders the native images for you.
The source code for this alternative implementation on this example:
Here is the code I have used in one of my projects with a small tweak which allows the combo box to be centered:
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.OwnerDrawLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
/**
* Emulates a native checkbox in a column label provider which is also centered
* on screen. This should also work well, in case the field is editable.
*
* @author Gil Fernandes
*
*/
public abstract class EmulatedNativeCheckBoxLabelProvider extends OwnerDrawLabelProvider {
private static final String CHECKED_KEY = "CHECKED";
private static final String UNCHECK_KEY = "UNCHECKED";
private Image image;
public EmulatedNativeCheckBoxLabelProvider(ColumnViewer viewer) {
if (JFaceResources.getImageRegistry().getDescriptor(CHECKED_KEY) == null) {
Shell shell = viewer.getControl().getShell();
JFaceResources.getImageRegistry().put(UNCHECK_KEY, makeShot(shell, false, viewer));
JFaceResources.getImageRegistry().put(CHECKED_KEY, makeShot(shell, true, viewer));
}
}
private Image makeShot(Shell shell, boolean type, ColumnViewer viewer) {
Shell s = new Shell(shell, SWT.NO_TRIM);
Button b = new Button(s, SWT.CHECK);
b.setSelection(type);
Point bsize = b.computeSize(SWT.DEFAULT, SWT.DEFAULT);
b.setSize(bsize);
b.setLocation(0, 0);
s.setSize(bsize);
s.open();
GC gc = new GC(b);
Image image = new Image(shell.getDisplay(), bsize.x, bsize.y);
gc.copyArea(image, 0, 0);
gc.dispose();
s.close();
return image;
}
public Image getImage(Object element) {
return JFaceResources.getImageRegistry().getDescriptor(isChecked(element) ? CHECKED_KEY : UNCHECK_KEY)
.createImage();
}
@Override
protected void paint(Event event, Object element) {
image = getImage(element);
if (image != null) {
Rectangle bounds = ((TableItem) event.item)
.getBounds(event.index);
Rectangle imgBounds = image.getBounds();
bounds.width /= 2;
bounds.width -= imgBounds.width / 2;
bounds.height /= 2;
bounds.height -= imgBounds.height / 2;
int x = bounds.width > 0 ? bounds.x + bounds.width : bounds.x;
int y = bounds.height > 0 ? bounds.y + bounds.height : bounds.y;
event.gc.drawImage(image, x, y);
}
}
protected abstract boolean isChecked(Object element);
@Override
protected void measure(Event event, Object element) {
}
}
You can then use this column label provider like this:
mappingRoutingColumn.setLabelProvider(new EmulatedNativeCheckBoxLabelProvider(viewer, viewer.getTable(), 2) {
@Override
protected boolean isChecked(Object element) {
return ((MappingTypeData) element).isRouting();
}
});
Upvotes: 0
Reputation: 32720
Here is an example with a checkbox in the TableViewerColumn
. It uses an image to represent a bolean in the cell.
It creates a custom view which extends ViewPart
with two static fields to hold the image :
Image CHECKED = Activator.getImageDescriptor("icons/checked.gif").createImage();
Image UNCHECKED = Activator.getImageDescriptor("icons/unchecked.gif").createImage();
Then in the createColumns
method just return one of the images depending in the value of the boolean :
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return null;
}
@Override
public Image getImage(Object element) {
if (((Person) element).isMarried()) {
return CHECKED;
} else {
return UNCHECKED;
}
}
});
Please check the link given above for more details.
If you want an editable checkbox, you need to create an EditingSupport object for the column you want to have a checkbox.
Here is an example :
public class CheckBoxColumnEditingSupport extends EditingSupport {
private TableViewer tableViewer;
public CheckBoxColumnEditingSupport(TableViewer viewer) {
super(viewer);
this.tableViewer = viewer;
}
@Override
protected CellEditor getCellEditor(Object o) {
return new CheckboxCellEditor(null, SWT.CHECK);
}
@Override
protected boolean canEdit(Object o) {
return true;
}
@Override
protected Object getValue(Object o) {
ORMData ormData = (ORMData) o;
return ormData.isOrmIndicator();
}
@Override
protected void setValue(Object element, Object value) {
ORMData ormData = (ORMData) element;
ormData.setOrmIndicator((Boolean) value);
tableViewer.refresh();
}
}
And then add that editing support to specific column in your table:
tableViewerColumn.setEditingSupport(new CheckBoxColumnEditingSupport (myTableViewer));
See this turotial on how to use Column Editing Support. Also this detailed post on How to add an editable checkbox at JFace TableViewer
Upvotes: 6