How to align image to center of table cell (SWT Table) RAP Platform

682 views Asked by At

I found some solutions for this question, but they are for RCP Platform only. I'm using RAP Platform and I can't find a solution.

1

There are 1 answers

0
RĂ¼diger Herrmann On BEST ANSWER

To custom-align images, you have two choices in RAP:

Markup

After preparing the table accordingly, you can use a subset of HTML with the table items's text.

Table table = new Table( parent, SWT.NONE );
table.setData( RWT.MARKUP_ENABLED, Boolean.TRUE );
table.setData( RWT.CUSTOM_ITEM_HEIGHT, Integer.valueOf( 80 ) );

TableItem item = new TableItem( table, SWT.NONE );
String imageUrl = RWT.getRequest().getContextPath() + "/" + registerImage( "foo.png" );
item.setText( "<img src='" + imageUrl + "' width='10' height='10' style='padding-right:5px'/>" );

The code to register an image (here the image is assumed to be located on the class path) should look like this:

String registerImage( String resourceName ) throws IOException {
  IResourceManager resourceManager = RWT.getResourceManager();
  if( !resourceManager.isRegistered( resourceName ) ) {
    InputStream inputStream = CLASSLOADER.getResourceAsStream( resourceName );
    try {
      resourceManager.register( resourceName, inputStream );
    } finally {
      inputStream.close();
    }
  }
  return resourceManager.getLocation( resourceName );
}

More details can be found here: http://eclipsesource.com/blogs/2012/09/12/making-your-images-available-using-markup/

Row Templates

With row templates, the default column layout of tables can be replaced and the way items layout their cells can be changed.

To change to position of an image, it is set as usual with item.setImage( ... ) and then a template is created that positions the image as desired. Finally the table is told to make use of the template with table.setData( ... ).

Template template = new Template();
ImageCell imageCell = new ImageCell( template );
imageCell.setBindingIndex( 0 ); // bind to image from column 0
imageCell.setTop( 4 ).setLeft( 4 ).setWidth( 48 ).setHeight( 48 );

table.setData( RWT.ROW_TEMPLATE, template );

More on this here: http://eclipsesource.com/blogs/2013/11/14/rap-2-2m3-introducing-row-templates/