|
|
|
|
Re: Export NatTable "as is" to image [message #1078191 is a reply to message #1078137] |
Fri, 02 August 2013 16:52 |
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());
}
|
|
|
Powered by
FUDForum. Page generated in 0.02498 seconds