Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Export NatTable "as is" to image
Export NatTable "as is" to image [message #1073270] Wed, 24 July 2013 08:49 Go to next message
Daniil Yaroslavtsev is currently offline Daniil Yaroslavtsev
Messages: 3
Registered: July 2013
Junior Member
Hello,

I would like to export the whole nattable to image with preserving of table visual config & bindings. Because of table virtuality, I can`t write the piece of code which export`s whole table to image (not only current visible grid part).

My current suggestion is:
GC gc = new GC(natTable);
Display display = shell.getDisplay();
Image image = new Image(display, tableSize.x, tableSize.y);
gc.copyArea(image, 0, 0);
gc.dispose();

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.save(new FileOutputStream(new File(fileNameAndPath)), SWT.IMAGE_PNG);

This code correctly exports to image only visible part of nattable. I beliewe that there is more elegant solution to export table using it`s DataProvider and configuration.
Re: Export NatTable "as is" to image [message #1073294 is a reply to message #1073270] Wed, 24 July 2013 09:34 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1214
Registered: July 2012
Senior Member
Have a look at the print functionality of NatTable.
Re: Export NatTable "as is" to image [message #1078125 is a reply to message #1073294] Fri, 02 August 2013 11:11 Go to previous messageGo to next message
Daniil Yaroslavtsev is currently offline Daniil Yaroslavtsev
Messages: 3
Registered: July 2013
Junior Member
Still can`t export the whole table. Only visible table part can be exported using 'Control.print()' functonality as nattable is a virtual grid. My code is below:

        NatTable table = dsmTableController.getTable();
        Shell shell = table.getShell();
        
        FileDialog dialog = new FileDialog(shell, SWT.SAVE);
        dialog.setFilterNames(new String[] { "Bitmap Files (*.bmp)", "All Files (*.*)" });
        dialog.setFilterExtensions(new String[] { "*.bmp", "*.*" });
        dialog.setFileName("screenshot.bmp");
        String fileNameAndPath = dialog.open();

        if (fileNameAndPath != null) {

            Image image = new Image(shell.getDisplay(), table.getBounds());
            GC gc = new GC(image);
            table.print(gc);
            gc.dispose();
            
            ImageLoader loader = new ImageLoader();
            loader.data = new ImageData[] { image.getImageData() };
            try {
                loader.save(new FileOutputStream(new File(fileNameAndPath)), SWT.IMAGE_BMP);
            } catch (FileNotFoundException e) {
                logger.error("Error while saving screenshot to file " + fileNameAndPath, e);
            }
            
            image.dispose();
        }
Re: Export NatTable "as is" to image [message #1078137 is a reply to message #1078125] Fri, 02 August 2013 11:29 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1214
Registered: July 2012
Senior Member
NatTable comes with a print functionality out of the box. Have a look at the GridLayerPrinter. On pressing Ctrl+P it is possible to print the whole table.
Re: Export NatTable "as is" to image [message #1078191 is a reply to message #1078137] Fri, 02 August 2013 12:52 Go to previous message
Daniil Yaroslavtsev is currently offline Daniil Yaroslavtsev
Messages: 3
Registered: July 2013
Junior Member
Got it, thanks a lot! Here is the code:

public void exportToImage(NatTable table) {

    Shell shell = table.getShell();

    FileDialog dialog = new FileDialog(shell, SWT.SAVE);
    dialog.setFilterNames(new String[] { "Png Files", "All Files (*.*)" });
    dialog.setFilterExtensions(new String[] { "*.png", "*.*" });
    dialog.setFileName("screenshot.png");
    String fileNameAndPath = dialog.open();

    if (fileNameAndPath != null) {

        final Image image = internalExportToImage(shell, table);

        ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[] { image.getImageData() };
        try {
            loader.save(new FileOutputStream(new File(fileNameAndPath)), SWT.IMAGE_PNG);
        } catch (FileNotFoundException e) {
            logger.error("Error while saving screenshot to file " + fileNameAndPath, e);
        }
        image.dispose();
    }
}

private Image internalExportToImage(Shell shell, NatTable table) {

    ILayer layer = table.getLayer();
		
    int width = layer.getWidth();		
    int height = layer.getHeight();

    final Image image = new Image(shell.getDisplay(), width, height);
    GC gc = new GC(image);

    setViewportSizeToMaximum(layer);

    Rectangle layerBounds = new Rectangle(0, 0, width, height);
    IConfigRegistry configRegistry = table.getConfigRegistry();
    layer.getLayerPainter().paintLayer(layer, gc, 0, 0, layerBounds, configRegistry);
		
    restoreViewPortSize(layer);

    gc.dispose();

    return image;
}

/**
  * Expand the client area of the layer such that
  * all the contents fit in the viewport. This ensures that when the grid prints
  * we print the <i>entire</i> table.
  */
private void setViewportSizeToMaximum(final ILayer layer) {		

    final Rectangle layerBounds = new Rectangle(0, 0, layer.getWidth(), layer.getHeight());

    layer.setClientAreaProvider(new IClientAreaProvider(){
        public Rectangle getClientArea() {
	    return layerBounds;
	}
    });

    layer.doCommand(new PrintEntireGridCommand());
}

/**
  * Restores the viewport to its normal operation.
  */
private void restoreViewPortSize(ILayer layer) {
    layer.doCommand(new TurnViewportOnCommand());
}
Previous Topic:Wrong calculation of column startX after loadState()
Next Topic:Selection multiple cells - possible bug
Goto Forum:
  


Current Time: Sat Jul 26 05:24:00 EDT 2014

Powered by FUDForum. Page generated in 0.01491 seconds