Custom tool tips on a GEF palette

Here is a solution to have custom tool tips on a GEF palette.
The solution is not perfect because it relies on non-API code. But it works.

You must use a different EditPartFactory.

PaletteViewer paletteViewer = new PaletteViewer();
paletteViewer.setEditPartFactory( new PaletteEditPartFactory() {

	@Override
	protected EditPart createEntryEditPart(EditPart parentEditPart, Object model) {
		return new EipToolEntryEditPart((PaletteEntry) model);
	}
});

This EditPartFactory returns a new kind of edit part for tool entries.

public class EipToolEntryEditPart extends ToolEntryEditPart {

 	public EipToolEntryEditPart( PaletteEntry paletteEntry ) {
		super( paletteEntry );
	}

 	@Override
	protected IFigure createToolTip() {
 		IFigure result = null;
		if( getModel() instanceof CombinedTemplateCreationEntry ) {
 			Object tpl = ((CombinedTemplateCreationEntry) getModel()).getTemplate();
			if( tpl instanceof EIPtype )
				result = new EipPaletteTooltipFigure((EIPtype) tpl);
			else
				result = super.createToolTip();
		}
		else
			result = super.createToolTip();

 		return result;
	}
}

And here is an example of figure I used.

public class EipPaletteTooltipFigure extends Figure {

 	public EipPaletteTooltipFigure( EIPtype eipType ) {

 		setLayoutManager( new ToolbarLayout());
		setBackgroundColor( ColorConstants.white );
		setOpaque( true );
		setBorder( new CompoundBorder(
					new LineBorder( ColorConstants.black, 2 ),
					new MarginBorder( 12 )));

 		FontData fontData = new FontData( "Arial", 12, SWT.BOLD );
		Font font = new Font( Display.getDefault(), fontData );

 		Label l = new Label( eipType.getPrettyName());
		l.setFont( font );
		l.setLabelAlignment( PositionConstants.LEFT );
		l.setBorder( new MarginBorder( 0, 4, 12, 4 ));
		add( l );

 		l = new Label( eipType.getExplaination());
		l.setBorder( new MarginBorder( 4, 4, 6, 4 ));
		add( l );
	}
}

 

Here is an overview of this tool tip.

A custom tool tip for a GEF palette


About this entry