Search in sources :

Example 11 with SWTGraphics

use of org.eclipse.draw2d.SWTGraphics in project whole by wholeplatform.

the class WholeNonResizableEditPolicy method createFeedbackImage.

public static Image createFeedbackImage(IGraphicalEntityPart part, int alpha, boolean withBorder, IConstraintDimensionStrategy strategy, double scale) {
    IFigure figure = part.getFigure();
    Rectangle figureBounds = figure.getBounds();
    Control figureCanvas = part.getViewer().getControl();
    // calculate feedback maximum dimension
    Dimension dimension = strategy.constraintDimensions(new Dimension((int) Math.round(figureBounds.width * scale), (int) Math.round(figureBounds.height * scale)));
    if (dimension == null)
        return null;
    Image img = new Image(null, dimension.width, dimension.height);
    img.setBackground(ColorConstants.white);
    GC imageGC = new GC(img);
    Graphics imgGraphics = new SWTGraphics(imageGC);
    imgGraphics.scale(scale);
    imgGraphics.translate(-figureBounds.x, -figureBounds.y);
    figure.paint(imgGraphics);
    // draw dashed border
    if (withBorder) {
        imgGraphics.translate(figureBounds.x, figureBounds.y);
        imgGraphics.scale(1.0 / scale);
        imgGraphics.setForegroundColor(ColorConstants.white);
        imgGraphics.drawRectangle(0, 0, dimension.width - 1, dimension.height - 1);
        imgGraphics.setLineStyle(SWT.LINE_DASH);
        imgGraphics.setLineDash(new int[] { 4, 3 });
        imgGraphics.setForegroundColor(ColorConstants.black);
        imgGraphics.drawRectangle(0, 0, dimension.width - 1, dimension.height - 1);
    }
    ImageData data = img.getImageData();
    // alpha only
    for (int x = 0; x < data.width; x++) for (int y = 0; y < data.height; y++) data.setAlpha(x, y, alpha);
    Image feedbackImage = new Image(figureCanvas.getDisplay(), data);
    imageGC.dispose();
    img.dispose();
    return feedbackImage;
}
Also used : SWTGraphics(org.eclipse.draw2d.SWTGraphics) Graphics(org.eclipse.draw2d.Graphics) SWTGraphics(org.eclipse.draw2d.SWTGraphics) Control(org.eclipse.swt.widgets.Control) ImageData(org.eclipse.swt.graphics.ImageData) PrecisionRectangle(org.eclipse.draw2d.geometry.PrecisionRectangle) Rectangle(org.eclipse.draw2d.geometry.Rectangle) Dimension(org.eclipse.draw2d.geometry.Dimension) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC) IFigure(org.eclipse.draw2d.IFigure)

Example 12 with SWTGraphics

use of org.eclipse.draw2d.SWTGraphics in project archi by archimatetool.

the class GraphicsToGraphics2DAdaptor method createSWTGraphics.

/**
 * This is a helper method used to create the SWT Graphics object
 */
private void createSWTGraphics() {
    // we need this temp Rect just to instantiate an swt image in order to
    // keep
    // state, the size of this Rect is of no consequence and we just set it
    // to
    // such a small size in order to minimize memory allocation
    org.eclipse.swt.graphics.Rectangle tempRect = new org.eclipse.swt.graphics.Rectangle(0, 0, 10, 10);
    image = new Image(Display.getCurrent(), tempRect);
    GC gc = new GC(image);
    swtGraphics = new SWTGraphics(gc);
}
Also used : SWTGraphics(org.eclipse.draw2d.SWTGraphics) Rectangle(org.eclipse.draw2d.geometry.Rectangle) Image(org.eclipse.swt.graphics.Image) BufferedImage(java.awt.image.BufferedImage) GC(org.eclipse.swt.graphics.GC)

Example 13 with SWTGraphics

use of org.eclipse.draw2d.SWTGraphics in project archi by archimatetool.

the class CachedLabel method paintFigure.

@Override
protected void paintFigure(Graphics graphics) {
    if (graphics.getClass() == ScaledGraphics.class) {
        if (((ScaledGraphics) graphics).getAbsoluteScale() < 0.30) {
            return;
        }
    }
    if (!cacheLabel) {
        if (isOpaque()) {
            super.paintFigure(graphics);
        }
        Rectangle bounds = getBounds();
        graphics.translate(bounds.x, bounds.y);
        if (getIcon() != null) {
            graphics.drawImage(getIcon(), getIconLocation());
        }
        if (!isEnabled()) {
            graphics.translate(1, 1);
            graphics.setForegroundColor(ColorConstants.buttonLightest);
            graphics.drawText(getSubStringText(), getTextLocation());
            graphics.translate(-1, -1);
            graphics.setForegroundColor(ColorConstants.buttonDarker);
        }
        graphics.drawText(getText(), getTextLocation());
        graphics.translate(-bounds.x, -bounds.y);
        return;
    }
    if (isOpaque()) {
        graphics.fillRectangle(getBounds());
    }
    Rectangle bounds = getBounds();
    graphics.translate(bounds.x, bounds.y);
    Image icon = getIcon();
    if (icon != null) {
        graphics.drawImage(icon, getIconLocation());
    }
    int width = getSubStringTextSize().width;
    int height = getSubStringTextSize().height;
    if (cachedImage == null || shouldInvalidateCache()) {
        invalidationRequired = false;
        cleanImage();
        cachedImage = new Image(Display.getCurrent(), width, height);
        // @tag TODO : Dispose of the image properly
        // ZestPlugin.getDefault().addImage(cachedImage.toString(), cachedImage);
        GC gc = new GC(cachedImage);
        Graphics graphics2 = new SWTGraphics(gc);
        graphics2.setBackgroundColor(getBackgroundTextColor());
        graphics2.fillRectangle(0, 0, width, height);
        graphics2.setForegroundColor(getForegroundColor());
        // graphics2.drawText(getSubStringText(), new Point(0, 0));
        graphics2.drawText(getText(), new Point(0, 0));
        gc.dispose();
    }
    graphics.drawImage(cachedImage, getTextLocation());
    graphics.translate(-bounds.x, -bounds.y);
    this.paintBorder(graphics);
}
Also used : Graphics(org.eclipse.draw2d.Graphics) ScaledGraphics(org.eclipse.draw2d.ScaledGraphics) SWTGraphics(org.eclipse.draw2d.SWTGraphics) SWTGraphics(org.eclipse.draw2d.SWTGraphics) Rectangle(org.eclipse.draw2d.geometry.Rectangle) Point(org.eclipse.draw2d.geometry.Point) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.draw2d.geometry.Point)

Example 14 with SWTGraphics

use of org.eclipse.draw2d.SWTGraphics in project xtext-eclipse by eclipse.

the class ExportToFileAction method run.

@Override
public void run() {
    IFigure contents = railroadView.getContents();
    if (contents != null) {
        FileDialog fileDialog = new FileDialog(this.railroadView.getSite().getShell(), SWT.SAVE);
        fileDialog.setFilterExtensions(new String[] { "*.png" });
        fileDialog.setText("Choose diagram file");
        String fileName = fileDialog.open();
        if (fileName == null) {
            return;
        }
        Dimension preferredSize = contents.getPreferredSize();
        Image image = new Image(Display.getDefault(), preferredSize.width + 2 * PADDING, preferredSize.height + 2 * PADDING);
        GC gc = new GC(image);
        SWTGraphics graphics = new SWTGraphics(gc);
        graphics.translate(PADDING, PADDING);
        graphics.translate(contents.getBounds().getLocation().getNegated());
        contents.paint(graphics);
        ImageData imageData = image.getImageData();
        ImageLoader imageLoader = new ImageLoader();
        imageLoader.data = new ImageData[] { imageData };
        imageLoader.save(fileName, SWT.IMAGE_PNG);
    }
}
Also used : SWTGraphics(org.eclipse.draw2d.SWTGraphics) ImageData(org.eclipse.swt.graphics.ImageData) Dimension(org.eclipse.draw2d.geometry.Dimension) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC) ImageLoader(org.eclipse.swt.graphics.ImageLoader) FileDialog(org.eclipse.swt.widgets.FileDialog) IFigure(org.eclipse.draw2d.IFigure)

Example 15 with SWTGraphics

use of org.eclipse.draw2d.SWTGraphics in project dbeaver by serge-rider.

the class ERDExportRasterImage method exportDiagram.

@Override
public void exportDiagram(EntityDiagram diagram, IFigure figure, DiagramPart diagramPart, File targetFile) throws DBException {
    int imageType = SWT.IMAGE_BMP;
    {
        String filePath = targetFile.getName().toLowerCase();
        if (filePath.endsWith(".jpg")) {
            imageType = SWT.IMAGE_JPEG;
        } else if (filePath.endsWith(".png")) {
            imageType = SWT.IMAGE_PNG;
        } else if (filePath.endsWith(".gif")) {
            imageType = SWT.IMAGE_GIF;
        }
    }
    Rectangle contentBounds = figure instanceof FreeformLayeredPane ? ((FreeformLayeredPane) figure).getFreeformExtent() : figure.getBounds();
    try {
        if (contentBounds.isEmpty()) {
            throw new DBException("Can't serializeDiagram empty diagram");
        }
        try (FileOutputStream fos = new FileOutputStream(targetFile)) {
            Rectangle r = figure.getBounds();
            GC gc = null;
            Graphics graphics = null;
            try {
                Image image = new Image(null, contentBounds.width + MARGIN_X * 4, contentBounds.height + MARGIN_Y * 4);
                try {
                    gc = new GC(image);
                    // gc.setClipping(0, 0, contentBounds.width + MARGIN_X * 2, contentBounds.height + MARGIN_Y * 2);
                    graphics = new SWTGraphics(gc);
                    graphics.translate(r.x * -1 + MARGIN_X, r.y * -1 + MARGIN_Y);
                    figure.paint(graphics);
                    ImageLoader imageLoader = new ImageLoader();
                    imageLoader.data = new ImageData[1];
                    if (imageType == SWT.IMAGE_GIF) {
                        // Convert to 8bit color
                        imageLoader.data[0] = ImageUtils.makeWebImageData(image);
                    } else {
                        // Use maximum colors for JPEG, PNG
                        imageLoader.data[0] = ImageUtils.getImageDataAtCurrentZoom(image);
                    }
                    imageLoader.save(fos, imageType);
                } finally {
                    UIUtils.dispose(image);
                }
            } finally {
                if (graphics != null) {
                    graphics.dispose();
                }
                UIUtils.dispose(gc);
            }
            fos.flush();
        }
        UIUtils.launchProgram(targetFile.getAbsolutePath());
    } catch (Throwable e) {
        DBWorkbench.getPlatformUI().showError("Save ERD as image", null, e);
    }
}
Also used : Graphics(org.eclipse.draw2d.Graphics) SWTGraphics(org.eclipse.draw2d.SWTGraphics) DBException(org.jkiss.dbeaver.DBException) SWTGraphics(org.eclipse.draw2d.SWTGraphics) FileOutputStream(java.io.FileOutputStream) Rectangle(org.eclipse.draw2d.geometry.Rectangle) FreeformLayeredPane(org.eclipse.draw2d.FreeformLayeredPane) GC(org.eclipse.swt.graphics.GC) Image(org.eclipse.swt.graphics.Image) ImageLoader(org.eclipse.swt.graphics.ImageLoader)

Aggregations

SWTGraphics (org.eclipse.draw2d.SWTGraphics)18 Image (org.eclipse.swt.graphics.Image)18 GC (org.eclipse.swt.graphics.GC)16 Rectangle (org.eclipse.draw2d.geometry.Rectangle)12 Graphics (org.eclipse.draw2d.Graphics)10 ImageLoader (org.eclipse.swt.graphics.ImageLoader)6 BufferedImage (java.awt.image.BufferedImage)4 IFigure (org.eclipse.draw2d.IFigure)4 FileOutputStream (java.io.FileOutputStream)3 FreeformLayeredPane (org.eclipse.draw2d.FreeformLayeredPane)3 Dimension (org.eclipse.draw2d.geometry.Dimension)3 ImageData (org.eclipse.swt.graphics.ImageData)3 DBException (org.jkiss.dbeaver.DBException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Point (org.eclipse.draw2d.geometry.Point)2 org.eclipse.swt.graphics (org.eclipse.swt.graphics)2 ArchiLabelProvider (com.archimatetool.editor.ui.ArchiLabelProvider)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 FreeformViewport (org.eclipse.draw2d.FreeformViewport)1