Home » Eclipse Projects » GEF » Export/Save diagram to GIF/JPG file
Export/Save diagram to GIF/JPG file [message #201866] |
Fri, 04 November 2005 01:14  |
Eclipse User |
|
|
|
Originally posted by: jsk_lam.hotmail.com
Hi,
I would like to export/save the GEF digram to a GIF, JPG, or BMP file.
Could anyone please help? Do I need a third-party tool or are there already
APIs in GEF that I can use?
Thanks
skl
|
|
|
Re: Export/Save diagram to GIF/JPG file [message #201876 is a reply to message #201866] |
Fri, 04 November 2005 01:55   |
Eclipse User |
|
|
|
Hi,
There is no problem to achieve the export with GEF.
Here is a sample code :
public void export(GraphicalViewer viewer, String location, int format)
{
try
{
IFigure figure = ((AbstractGraphicalEditPart) viewer.getRootEditPart()).getFigure();
File file = new File(location);
if (file.exists())
{
if (!MessageDialog.openQuestion(modeler.getSite().getShell(), "Overwrite",
"The file already exists. Do you really want to overwrite it ?"))
{
return;
}
}
else
{
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
if (figure instanceof Viewport)
{
// Reinit the figure
((Viewport) figure).setViewLocation(0, 0);
}
Dimension size = figure.getPreferredSize();
Image image = new Image(Display.getDefault(), size.width, size.height);
GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
figure.paint(graphics);
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] {image.getImageData()};
loader.save(os, format);
}
catch (Exception e)
{
ModelerPlugin.displayDialog(null, "An error occured during export. See the error log for more details.", IStatus.ERROR);
ModelerPlugin.log(e);
}
}
Enjoy !
David
sl a écrit :
> Hi,
>
> I would like to export/save the GEF digram to a GIF, JPG, or BMP file.
> Could anyone please help? Do I need a third-party tool or are there already
> APIs in GEF that I can use?
>
> Thanks
> skl
>
>
|
|
|
Re: Export/Save diagram to GIF/JPG file [message #201909 is a reply to message #201876] |
Fri, 04 November 2005 06:14   |
Eclipse User |
|
|
|
Check this one..
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.LayerManager;
import org.eclipse.gef.editparts.ScalableRootEditPart;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IEditorPart;
/**
* Save format should be any of ... SWT.IMAGE_BMP, SWT.IMAGE_JPEG, SWT.IMAGE_ICO
*/
public class ImageSaveUtil
{
public static boolean save(IEditorPart editorPart, GraphicalViewer viewer, String saveFilePath, int format)
{
Assert.isNotNull(editorPart, "null editorPart passed to ImageSaveUtil::save");
Assert.isNotNull(viewer, "null viewer passed to ImageSaveUtil::save");
Assert.isNotNull(saveFilePath, "null saveFilePath passed to ImageSaveUtil::save");
if( format != SWT.IMAGE_BMP && format != SWT.IMAGE_JPEG && format != SWT.IMAGE_ICO )
throw new IllegalArgumentException("Save format not supported");
try {
saveEditorContentsAsImage(editorPart, viewer, saveFilePath, format);
} catch (Exception ex) {
MessageDialog.openError(editorPart.getEditorSite().getShell(), "Save Error", "Could not save editor contents");
return false;
}
return true;
}
public static boolean save(IEditorPart editorPart, GraphicalViewer viewer)
{
Assert.isNotNull(editorPart, "null editorPart passed to ImageSaveUtil::save");
Assert.isNotNull(viewer, "null viewer passed to ImageSaveUtil::save");
String saveFilePath = getSaveFilePath(editorPart, viewer, -1);
if( saveFilePath == null ) return false;
int format = SWT.IMAGE_JPEG;
if( saveFilePath.endsWith(".jpeg") )
format = SWT.IMAGE_JPEG;
else if( saveFilePath.endsWith(".bmp") )
format = SWT.IMAGE_BMP;
else if( saveFilePath.endsWith(".ico") )
format = SWT.IMAGE_ICO;
return save(editorPart, viewer, saveFilePath, format);
}
private static String getSaveFilePath(IEditorPart editorPart, GraphicalViewer viewer, int format)
{
FileDialog fileDialog = new FileDialog(editorPart.getEditorSite().getShell(), SWT.SAVE);
String[] filterExtensions = new String[] {"*.jpeg", "*.bmp", "*.ico"/*, "*.png", "*.gif"*/};
if( format == SWT.IMAGE_BMP )
filterExtensions = new String[] {"*.bmp"};
else if( format == SWT.IMAGE_JPEG )
filterExtensions = new String[] {"*.jpeg"};
else if( format == SWT.IMAGE_ICO )
filterExtensions = new String[] {"*.ico"};
fileDialog.setFilterExtensions(filterExtensions);
return fileDialog.open();
}
private static void saveEditorContentsAsImage(IEditorPart editorPart, GraphicalViewer viewer, String saveFilePath, int format)
{
/* 1. First get the figure whose visuals we want to save as image.
* So we would like to save the rooteditpart which actually hosts all the printable layers.
*
* NOTE: ScalableRootEditPart manages layers and is registered graphicalviewer's editpartregistry with
* the key LayerManager.ID ... well that is because ScalableRootEditPart manages all layers that
* are hosted on a FigureCanvas. Many layers exist for doing different things */
ScalableRootEditPart rootEditPart = (ScalableRootEditPart)viewer.getEditPartRegistry().get(LayerManager.ID);
IFigure rootFigure = ((LayerManager)rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);//rootEditPart.getFigure();
Rectangle rootFigureBounds = rootFigure.getBounds();
/* 2. Now we want to get the GC associated with the control on which all figures are
* painted by SWTGraphics. For that first get the SWT Control associated with the viewer on which the
* rooteditpart is set as contents */
Control figureCanvas = viewer.getControl();
GC figureCanvasGC = new GC(figureCanvas);
/* 3. Create a new Graphics for an Image onto which we want to paint rootFigure */
Image img = new Image(null, rootFigureBounds.width, rootFigureBounds.height);
GC imageGC = new GC(img);
imageGC.setBackground(figureCanvasGC.getBackground());
imageGC.setForeground(figureCanvasGC.getForeground());
imageGC.setFont(figureCanvasGC.getFont());
imageGC.setLineStyle(figureCanvasGC.getLineStyle());
imageGC.setLineWidth(figureCanvasGC.getLineWidth());
imageGC.setXORMode(figureCanvasGC.getXORMode());
Graphics imgGraphics = new SWTGraphics(imageGC);
/* 4. Draw rootFigure onto image. After that image will be ready for save */
rootFigure.paint(imgGraphics);
/* 5. Save image */
ImageData[] imgData = new ImageData[1];
imgData[0] = img.getImageData();
ImageLoader imgLoader = new ImageLoader();
imgLoader.data = imgData;
imgLoader.save(saveFilePath, format);
/* release OS resources */
figureCanvasGC.dispose();
imageGC.dispose();
img.dispose();
}
}
Serves the same as the last-post, but has capabilities to open a file-dialog if location is not specified.
Thanks
Venkat
|
|
|
Saving PNG etc. in EDiagram editor, was Re: Export/Save diagram to GIF/JPG file [message #201925 is a reply to message #201909] |
Fri, 04 November 2005 09:15   |
Eclipse User |
|
|
|
Originally posted by: joerg.von.frantzius.artnology.nospam.com
Superb! I got it to work with the ediagram editor and enabled PNG and
GIF, please see https://bugs.eclipse.org/bugs/show_bug.cgi?id=115066
for an according patch to the ediagram project.
Thanks,
Jörg
venkataramana schrieb:
>Check this one..
>
>
>import org.eclipse.draw2d.Graphics;
>import org.eclipse.draw2d.IFigure;
>import org.eclipse.draw2d.SWTGraphics;
>import org.eclipse.draw2d.geometry.Rectangle;
>import org.eclipse.gef.GraphicalViewer;
>import org.eclipse.gef.LayerConstants;
>import org.eclipse.gef.editparts.LayerManager;
>import org.eclipse.gef.editparts.ScalableRootEditPart;
>import org.eclipse.jface.dialogs.MessageDialog;
>import org.eclipse.jface.util.Assert;
>import org.eclipse.swt.SWT;
>import org.eclipse.swt.graphics.GC;
>import org.eclipse.swt.graphics.Image;
>import org.eclipse.swt.graphics.ImageData;
>import org.eclipse.swt.graphics.ImageLoader;
>import org.eclipse.swt.widgets.Control;
>import org.eclipse.swt.widgets.FileDialog;
>import org.eclipse.ui.IEditorPart;
>
>/**
> * Save format should be any of ... SWT.IMAGE_BMP, SWT.IMAGE_JPEG, SWT.IMAGE_ICO
> */
>public class ImageSaveUtil
>{
> public static boolean save(IEditorPart editorPart, GraphicalViewer viewer, String saveFilePath, int format)
> {
> Assert.isNotNull(editorPart, "null editorPart passed to ImageSaveUtil::save");
> Assert.isNotNull(viewer, "null viewer passed to ImageSaveUtil::save");
> Assert.isNotNull(saveFilePath, "null saveFilePath passed to ImageSaveUtil::save");
>
> if( format != SWT.IMAGE_BMP && format != SWT.IMAGE_JPEG && format != SWT.IMAGE_ICO )
> throw new IllegalArgumentException("Save format not supported");
>
> try {
> saveEditorContentsAsImage(editorPart, viewer, saveFilePath, format);
> } catch (Exception ex) {
> MessageDialog.openError(editorPart.getEditorSite().getShell(), "Save Error", "Could not save editor contents");
> return false;
> }
>
> return true;
> }
>
> public static boolean save(IEditorPart editorPart, GraphicalViewer viewer)
> {
> Assert.isNotNull(editorPart, "null editorPart passed to ImageSaveUtil::save");
> Assert.isNotNull(viewer, "null viewer passed to ImageSaveUtil::save");
>
> String saveFilePath = getSaveFilePath(editorPart, viewer, -1);
> if( saveFilePath == null ) return false;
>
> int format = SWT.IMAGE_JPEG;
> if( saveFilePath.endsWith(".jpeg") )
> format = SWT.IMAGE_JPEG;
> else if( saveFilePath.endsWith(".bmp") )
> format = SWT.IMAGE_BMP;
> else if( saveFilePath.endsWith(".ico") )
> format = SWT.IMAGE_ICO;
>
> return save(editorPart, viewer, saveFilePath, format);
> }
>
> private static String getSaveFilePath(IEditorPart editorPart, GraphicalViewer viewer, int format)
> {
> FileDialog fileDialog = new FileDialog(editorPart.getEditorSite().getShell(), SWT.SAVE);
>
> String[] filterExtensions = new String[] {"*.jpeg", "*.bmp", "*.ico"/*, "*.png", "*.gif"*/};
> if( format == SWT.IMAGE_BMP )
> filterExtensions = new String[] {"*.bmp"};
> else if( format == SWT.IMAGE_JPEG )
> filterExtensions = new String[] {"*.jpeg"};
> else if( format == SWT.IMAGE_ICO )
> filterExtensions = new String[] {"*.ico"};
> fileDialog.setFilterExtensions(filterExtensions);
>
> return fileDialog.open();
> }
>
> private static void saveEditorContentsAsImage(IEditorPart editorPart, GraphicalViewer viewer, String saveFilePath, int format)
> {
> /* 1. First get the figure whose visuals we want to save as image.
> * So we would like to save the rooteditpart which actually hosts all the printable layers.
> *
> * NOTE: ScalableRootEditPart manages layers and is registered graphicalviewer's editpartregistry with
> * the key LayerManager.ID ... well that is because ScalableRootEditPart manages all layers that
> * are hosted on a FigureCanvas. Many layers exist for doing different things */
> ScalableRootEditPart rootEditPart = (ScalableRootEditPart)viewer.getEditPartRegistry().get(LayerManager.ID);
> IFigure rootFigure = ((LayerManager)rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);//rootEditPart.getFigure();
> Rectangle rootFigureBounds = rootFigure.getBounds();
>
> /* 2. Now we want to get the GC associated with the control on which all figures are
> * painted by SWTGraphics. For that first get the SWT Control associated with the viewer on which the
> * rooteditpart is set as contents */
> Control figureCanvas = viewer.getControl();
> GC figureCanvasGC = new GC(figureCanvas);
>
> /* 3. Create a new Graphics for an Image onto which we want to paint rootFigure */
> Image img = new Image(null, rootFigureBounds.width, rootFigureBounds.height);
> GC imageGC = new GC(img);
> imageGC.setBackground(figureCanvasGC.getBackground());
> imageGC.setForeground(figureCanvasGC.getForeground());
> imageGC.setFont(figureCanvasGC.getFont());
> imageGC.setLineStyle(figureCanvasGC.getLineStyle());
> imageGC.setLineWidth(figureCanvasGC.getLineWidth());
> imageGC.setXORMode(figureCanvasGC.getXORMode());
> Graphics imgGraphics = new SWTGraphics(imageGC);
>
> /* 4. Draw rootFigure onto image. After that image will be ready for save */
> rootFigure.paint(imgGraphics);
>
> /* 5. Save image */
> ImageData[] imgData = new ImageData[1];
> imgData[0] = img.getImageData();
>
> ImageLoader imgLoader = new ImageLoader();
> imgLoader.data = imgData;
> imgLoader.save(saveFilePath, format);
>
> /* release OS resources */
> figureCanvasGC.dispose();
> imageGC.dispose();
> img.dispose();
> }
>}
>
>
>Serves the same as the last-post, but has capabilities to open a file-dialog if location is not specified.
>
>Thanks
>Venkat
>
>
|
|
|
Re: Saving PNG etc. in EDiagram editor, was Re: Export/Save diagram to [message #201964 is a reply to message #201925] |
Fri, 04 November 2005 15:40  |
Eclipse User |
|
|
|
Jorg,
I remember ImageLoader::save not supporting saving in PNG, GIF formats when I started making ImageSaveUtil using SWT API bundled with Eclipse 3.0 (Some exception was being thrown which I didn't care to track down). I don't know about the current status with SWT of Eclipse 3.1.1;
If it is anyways supporting PNG, GIF, then some additional changes may need to be done apart from the enablement which you have done by uncommenting some part of code in the method getSaveFilePath. Add PNG,GIF to all places where you get to see other formats :-)
Thanks
Venkat
|
|
|
Goto Forum:
Current Time: Mon Jun 16 09:57:46 EDT 2025
Powered by FUDForum. Page generated in 0.08681 seconds
|