org.eclipse.draw2d.SWTGraphics Java Examples

The following examples show how to use org.eclipse.draw2d.SWTGraphics. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExportToFileAction.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@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);
	}
}
 
Example #2
Source File: SaveImageAction.java    From olca-app with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void run() {
	if (file == null)
		return;
	log.trace("export product graph as image: {}", file);
	ScalableRootEditPart editPart = (ScalableRootEditPart) editor.getGraphicalViewer().getRootEditPart();
	IFigure rootFigure = editPart.getLayer(LayerConstants.PRINTABLE_LAYERS);
	Rectangle bounds = rootFigure.getBounds();
	Image img = new Image(null, bounds.width, bounds.height);
	GC imageGC = new GC(img);
	Graphics graphics = new SWTGraphics(imageGC);
	rootFigure.paint(graphics);
	ImageLoader imgLoader = new ImageLoader();
	imgLoader.data = new ImageData[] { img.getImageData() };
	imgLoader.save(file.getAbsolutePath(), SWT.IMAGE_PNG);
}
 
Example #3
Source File: SingleSourceHelperImpl.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Image getInternalXYGraphSnapShot(IXYGraph xyGraph) {
	Rectangle bounds = xyGraph.getBounds();
	Image image = new Image(null, bounds.width + 6, bounds.height + 6);
	GC gc = GraphicsUtil.createGC(image);
	SWTGraphics graphics = new SWTGraphics(gc);
	// Needed because clipping is not set in GTK2
	graphics.setClip(new Rectangle(0, 0, image.getBounds().width, image.getBounds().height));
	graphics.translate(-bounds.x + 3, -bounds.y + 3);
	graphics.setForegroundColor(xyGraph.getForegroundColor());
	graphics.setBackgroundColor(xyGraph.getBackgroundColor());
	xyGraph.paint(graphics);
	gc.dispose();
	return image;
}
 
Example #4
Source File: SankeyImageAction.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private Image createImage() {
	try {
		ScalableRootEditPart root = (ScalableRootEditPart) sankeyDiagram.getGraphicalViewer().getRootEditPart();
		IFigure figure = root.getLayer(LayerConstants.PRINTABLE_LAYERS);
		Rectangle bounds = figure.getBounds();
		Image img = new Image(null, bounds.width, bounds.height);
		GC imageGC = new GC(img);
		Graphics graphics = new SWTGraphics(imageGC);
		figure.paint(graphics);
		return img;
	} catch (Exception e) {
		log.error("Could not create image", e);
		return null;
	}
}
 
Example #5
Source File: AnalysisView.java    From ice with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Given a GEF GraphicalViewer, this method creates a save file dialog and
 * will save the contents of the viewer as an image file. Supported
 * extensions include PNG, JPG, and BMP.
 * 
 * @param viewer
 *            The GraphicalViewer to save as an image.
 */
protected static void saveViewerImage(GraphicalViewer viewer) {

	// Store the allowed extensions in a HashMap.
	HashMap<String, Integer> extensions = new HashMap<String, Integer>(4);
	extensions.put("png", SWT.IMAGE_PNG);
	extensions.put("jpg", SWT.IMAGE_JPEG);
	extensions.put("bmp", SWT.IMAGE_BMP);

	// Make the array of strings needed to pass to the file dialog.
	String[] extensionStrings = new String[extensions.keySet().size()];
	int i = 0;
	for (String extension : extensions.keySet()) {
		extensionStrings[i++] = "*." + extension;
	}

	// Create the file save dialog.
	FileDialog fileDialog = new FileDialog(viewer.getControl().getShell(),
			SWT.SAVE);
	fileDialog.setFilterExtensions(extensionStrings);
	fileDialog.setOverwrite(true);

	// Get the path of the new/overwritten image file.
	String path = fileDialog.open();

	// Return if the user cancelled.
	if (path == null) {
		return;
	}

	// Get the image type to save from the path's extension.
	String[] splitPath = path.split("\\.");
	int extensionSWT = extensions.get(splitPath[splitPath.length - 1]);

	// Get the root EditPart and its draw2d Figure.
	SimpleRootEditPart rootEditPart = (SimpleRootEditPart) viewer
			.getRootEditPart();
	IFigure figure = rootEditPart.getFigure();

	// Get the image from the Figure.
	org.eclipse.draw2d.geometry.Rectangle bounds = figure.getBounds();
	Image image = new Image(null, bounds.width, bounds.height);
	GC gc = new GC(image);
	SWTGraphics g = new SWTGraphics(gc);
	figure.paint(g);
	g.dispose();
	gc.dispose();

	// Save the file.
	ImageLoader loader = new ImageLoader();
	loader.data = new ImageData[] { image.getImageData() };
	loader.save(path, extensionSWT);
	image.dispose();

	return;
}
 
Example #6
Source File: ReportPrintGraphicalViewerOperation.java    From birt with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new PrinterGraphics with Graphics g, using Printer p ;
 * 
 * @param g
 *            Graphics object to draw with
 * @param p
 *            Printer to print to
 */
public CompositePrinterGraphics( SWTGraphics g, Device p )
{
	super( g );
	printer = p;
}