use of org.eclipse.swt.graphics.ImageLoader in project dbeaver by serge-rider.
the class ERDEditorPart method saveDiagramAs.
public void saveDiagramAs() {
final Shell shell = getSite().getShell();
FileDialog saveDialog = new FileDialog(shell, SWT.SAVE);
saveDialog.setFilterExtensions(new String[] { "*.png", "*.gif", "*.bmp", "*.graphml" });
saveDialog.setFilterNames(new String[] { "PNG format (*.png)", "GIF format (*.gif)", // "JPEG format (*.jpg)",
"Bitmap format (*.bmp)", "GraphML (*.graphml)" });
String filePath = DialogUtils.openFileDialog(saveDialog);
if (filePath == null || filePath.trim().length() == 0) {
return;
}
int imageType = SWT.IMAGE_BMP;
if (filePath.toLowerCase().endsWith(".jpg")) {
imageType = SWT.IMAGE_JPEG;
} else if (filePath.toLowerCase().endsWith(".png")) {
imageType = SWT.IMAGE_PNG;
} else if (filePath.toLowerCase().endsWith(".gif")) {
imageType = SWT.IMAGE_GIF;
} else if (filePath.toLowerCase().endsWith(".graphml")) {
new ERDExportGraphML(getDiagram(), getDiagramPart()).exportDiagramToGraphML(filePath);
return;
}
IFigure figure = rootPart.getLayer(ScalableFreeformRootEditPart.PRINTABLE_LAYERS);
Rectangle contentBounds = figure instanceof FreeformLayeredPane ? ((FreeformLayeredPane) figure).getFreeformExtent() : figure.getBounds();
try {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
Rectangle r = figure.getBounds();
GC gc = null;
Graphics g = null;
try {
Image image = new Image(null, contentBounds.x * 2 + contentBounds.width, contentBounds.y * 2 + contentBounds.height);
try {
gc = new GC(image);
gc.setClipping(contentBounds.x, contentBounds.y, contentBounds.width, contentBounds.height);
g = new SWTGraphics(gc);
g.translate(r.x * -1, r.y * -1);
figure.paint(g);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[1];
if (imageType != SWT.IMAGE_JPEG) {
// Convert to 8bit color
imageLoader.data[0] = ImageUtils.makeWebImageData(image);
} else {
// Use maximum colors for JPEG
imageLoader.data[0] = image.getImageData();
}
imageLoader.save(fos, imageType);
} finally {
UIUtils.dispose(image);
}
} finally {
if (g != null) {
g.dispose();
}
UIUtils.dispose(gc);
}
fos.flush();
}
UIUtils.launchProgram(filePath);
//UIUtils.showMessageBox(shell, "Save ERD", "Diagram has been exported to " + filePath, SWT.ICON_INFORMATION);
} catch (Exception e) {
UIUtils.showErrorDialog(getSite().getShell(), "Save ERD as image", null, e);
}
}
use of org.eclipse.swt.graphics.ImageLoader in project cubrid-manager by CUBRID.
the class ExportPictureTask method createImage.
private byte[] createImage(IFigure figure, int format) {
Rectangle r = figure.getBounds();
ByteArrayOutputStream result = new ByteArrayOutputStream();
Image image = null;
GC gc = null;
Graphics g = null;
try {
image = new Image(null, r.width, r.height);
gc = new GC(image);
g = new SWTGraphics(gc);
g.translate(r.x * -1, r.y * -1);
figure.paint(g);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save(result, format);
} finally {
if (g != null) {
g.dispose();
}
if (gc != null) {
gc.dispose();
}
if (image != null) {
image.dispose();
}
}
return result.toByteArray();
}
use of org.eclipse.swt.graphics.ImageLoader in project tdi-studio-se by Talend.
the class HTMLDocGenerator method saveLogoImage.
protected void saveLogoImage(int type, File file) throws IOException {
boolean documentationPluginLoaded = PluginChecker.isDocumentationPluginLoaded();
// get image from cache
ByteArrayOutputStream result = logoImageCache.get(type);
if (documentationPluginLoaded) {
String userLogoPath = CorePlugin.getDefault().getPreferenceStore().getString(ITalendCorePrefConstants.DOC_USER_LOGO);
if (userLogoPath != null && !"".equals(userLogoPath)) {
//$NON-NLS-1$
if (result == null || !userLogoPath.equals(userDocImageOldPath)) {
userDocImageOldPath = userLogoPath;
result = new ByteArrayOutputStream(3072);
File userLogo = new File(userLogoPath);
if (userLogo.exists()) {
Image image = new Image(Display.getCurrent(), userLogoPath);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save(result, type);
logoImageCache.put(type, result);
image.dispose();
}
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(result.toByteArray());
fos.close();
return;
}
}
// if (result == null) {
result = new ByteArrayOutputStream(3072);
IBrandingService brandingService = (IBrandingService) GlobalServiceRegister.getDefault().getService(IBrandingService.class);
ImageData imageData = brandingService.getLoginHImage().getImageData();
new ByteArrayOutputStream();
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { imageData };
imageLoader.save(result, type);
// put image to cache, no need to generate next time
logoImageCache.put(type, result);
// }
FileOutputStream fos = new FileOutputStream(file);
fos.write(result.toByteArray());
fos.close();
}
Aggregations