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;
}
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);
}
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);
}
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);
}
}
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);
}
}
Aggregations