Search in sources :

Example 11 with ImageLoader

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);
    }
}
Also used : Rectangle(org.eclipse.draw2d.geometry.Rectangle) Image(org.eclipse.swt.graphics.Image) Shell(org.eclipse.swt.widgets.Shell) FileOutputStream(java.io.FileOutputStream) GC(org.eclipse.swt.graphics.GC) ImageLoader(org.eclipse.swt.graphics.ImageLoader) FileDialog(org.eclipse.swt.widgets.FileDialog)

Example 12 with ImageLoader

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();
}
Also used : Graphics(org.eclipse.draw2d.Graphics) SWTGraphics(org.eclipse.draw2d.SWTGraphics) SWTGraphics(org.eclipse.draw2d.SWTGraphics) Rectangle(org.eclipse.draw2d.geometry.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC) ImageLoader(org.eclipse.swt.graphics.ImageLoader)

Example 13 with ImageLoader

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();
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IBrandingService(org.talend.core.ui.branding.IBrandingService) Image(org.eclipse.swt.graphics.Image) ImageLoader(org.eclipse.swt.graphics.ImageLoader) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

ImageLoader (org.eclipse.swt.graphics.ImageLoader)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Image (org.eclipse.swt.graphics.Image)7 ImageData (org.eclipse.swt.graphics.ImageData)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 File (java.io.File)4 InputStream (java.io.InputStream)3 GC (org.eclipse.swt.graphics.GC)3 ImageException (edu.cmu.cs.hcii.cogtool.util.GraphicsUtil.ImageException)2 FileOutputStream (java.io.FileOutputStream)2 Rectangle (org.eclipse.draw2d.geometry.Rectangle)2 FillLayout (org.eclipse.swt.layout.FillLayout)2 FileDialog (org.eclipse.swt.widgets.FileDialog)2 Shell (org.eclipse.swt.widgets.Shell)2 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)1 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)1 BufferedWriter (java.io.BufferedWriter)1 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1