Code and Stuff

Mar 6, 2012

JFace TableViewer Row Numbering

Here a small class that can be used as a column label provider to show the row number in a JFace TableViewer. Works only for TableViewers.
import java.util.Arrays;

import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerColumn;

public class RowNumberLabelProvider extends CellLabelProvider {

  private TableViewer viewer;

  protected void initialize(ColumnViewer viewer, ViewerColumn column) {
    super.initialize(viewer, column);
    this.viewer = null;
    if (viewer instanceof TableViewer) {
      this.viewer = (TableViewer) viewer;

  public void update(ViewerCell cell) {
    if (viewer != null) {
      int index = Arrays.asList(viewer.getTable().getItems()).indexOf(cell.getItem());
      cell.setText("" + (index + 1));
To use it, simply create a column and set it's label provider to an instance of RowNumberLabelProvider
// given a TableViewer viewer

TableViewerColumn numberColumn = new TableViewerColumn(viewer, SWT.RIGHT);
numberColumn.setLabelProvider(new RowNumberLabelProvider());

Blender Camera Tracking Tests

Blender has some new and amazing features. For instance camera tracking.
Works quite well especially if you have some tracking points. Starts showing some problems when autofocus decides to blur a little the image.

Mar 3, 2012

Graphiti PlatformGraphicsAlgorithms

Graphiti's PlatformGraphicsAlgorithms allow the user to implement custom rendering for some shapes in a diagram. They are created just like any other GraphicsAlgorithm with the GaCreateService. When displayed, however, the diagram type provider is asked for a IGraphicsAlgorithmRendererFactory which in turn will be asked to create a IGraphicsAlgorithmRenderer for the specific PlatformGraphicsAlgorithm. The renderer interface is just a marker. The returned value can (or maybe even must) be a Draw2D object.


Some code!
In the add feature where we want to create such a shape we will have something like:
import org.eclipse.graphiti.features.*;

public class SomeAddFeature extends AbstractAddFeature {
  // ... usual stuff (contructor, canAdd, etc)

  public PictogramElement add(IAddContext context) {
    IPeCreateService peCreateService = Graphiti.getPeCreateService();
    Shape shape = peCreateService.createShape(context.getTargetContainer(), true);
    IGaCreateService gaCreateService = Graphiti.getGaCreateService();
    gaCreateService.createPlatformGraphicsAlgorithm(shape, "SOME_ID");

    link(shape, context.getNewObject());
    return shape;
As explained above we will also need an implementation of IGraphicsAlgorithmRendererFactory

public class GraphicsAlgorithmRendererFactory implements IGraphicsAlgorithmRendererFactory {

  public IGraphicsAlgorithmRenderer createGraphicsAlgorithmRenderer(IRendererContext rendererContext) {
    String id = rendererContext.getPlatformGraphicsAlgorithm().getId();
    if ("SOME_ID".equals(id)) {
      return new SomeIdGARenderer();
    return null;
A new instance of this class has to be returned by the DiagramTypeProvider class upon calls to the getGraphicsAlgorithmRendererFactory method.

We still missing the implementation of the actual renderer.

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Shape;

public class SomeIdGARenderer extends Shape implements IGraphicsAlgorithmRenderer {

  protected void fillShape(Graphics graphics) {
    // your code here

  protected void outlineShape(Graphics graphics) {
    // draw outline here


  • The example shows a GA used as the whole elements visual. PlatformGraphicsAlgorithms can obviously be used also in a PictogramElement composition.
  • Since the renderer is a Figure, we can add children and use layouts.
  • The minimum/maximum and preferred size of the renderer are not used to limit or initialize the bounds of the PictogramElement. Since PE are EObjects we could register an adapter and, at each change of location or size, query the renderer for the limits. (Never really tried this)