Tuesday, July 30, 2013

ToolTip information on NatTable cells


Default implementation is provided by ‘DefaultToolTip’ class from Jface, so we can override getText() method to provide tool tip information based on the specific criteria.

Example below:



import java.util.Properties;

import org.eclipse.jface.window.DefaultToolTip;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;


public class MyXToolTip extends DefaultToolTip {

private NatTable natTable;
private XSpanningDataProvider bodyDataProvider;


public MyXToolTip (NatTable natTable, XSpanningDataProvider bodyDataProvider) {
super(natTable, ToolTip.NO_RECREATE, false);
this.natTable = natTable;
this.bodyDataProvider = bodyDataProvider;
}


protected Object getToolTipArea(Event event) {
int col = natTable.getColumnPositionByX(event.x);
int row = natTable.getRowPositionByY(event.y);

//if any specific implementation here ex: You want to display tooltip information only on first column. if(col != 0) return;

return new Point(col, row);
}

protected String getText(Event event) {
int col = natTable.getColumnPositionByX(event.x);
int row = natTable.getRowPositionByY(event.y);

ILayerCell cell = natTable.getCellByPosition(col, row);
INode rowObject = bodyDataProvider.getRowObject(cell.getRowIndex()); //my INode Object which has specific cell information
String channelName = rowObject.getName();

//If any specific implementation here, ex: mixing column and row information
return channelName;
}

protected Composite createToolTipContentArea(Event event, Composite parent) {
// This is where you could get really creative with your tooltips...
return super.createToolTipContentArea(event, parent);
}
}


No comments:

Post a Comment