|
Re: Diagram To Image [message #527383 is a reply to message #527311] |
Thu, 15 April 2010 06:49 |
|
Hi Fabrizio, you're lucky cause I had the same 'problem' last week, and I solve it basing on the class you referenced, CopyToImageUtils. I think is what you're finding. To get the Image of a Diagram, but not to save it into a file. The following code I attach to you, converts an existing Diagram to an image (GIF format). Please feel free to copy and try it.
public class CreateDiagramImage {
public static Image getDiagramImage(Diagram diagram,
PreferencesHint preferencesHint) {
Image image = null;
DiagramEditor openedDiagramEditor = DiagramEditorUtil.findOpenedDiagramEditorForID(ViewUtil.getIdStr(diagram));
if (openedDiagramEditor != null) {
image = getDiagramImage(openedDiagramEditor.getDiagramEditPart());
} else {
DiagramEditPart diagramEditPart = OffscreenEditPartFactory
.getInstance().createDiagramEditPart(diagram, new Shell(),
preferencesHint);
Assert.isNotNull(diagramEditPart);
image = getDiagramImage(diagramEditPart);
diagramEditPart.deactivate();
}
return image;
}
public static Image getDiagramImage(DiagramEditPart diagramEP) {
DiagramGenerator gen = new DiagramImageGenerator(diagramEP);
List<?> editParts = diagramEP.getPrimaryEditParts();
Rectangle rectangle = gen.calculateImageRectangle(editParts);
ImageDescriptor descriptor = gen.createSWTImageDescriptorForParts(
editParts, rectangle);
return descriptor.createImage();
}
}
If you has the DiagramEditPart, call directly the getDiagramImage(DiagramEditPart), but if you only has the Diagram, you must to call getDiagramImage(Diagram, PreferenceHint) that will get the DiagramEditPart from the editor if it's opened, or will create it (only in memory) with the OffscreenEditPartFactory factory.
Good luck
Marc
[Updated on: Thu, 15 April 2010 06:51] Report message to a moderator
|
|
|
|
Re: Diagram To Image [message #527511 is a reply to message #527383] |
Thu, 15 April 2010 13:50 |
Fabricio Pellegrini Messages: 36 Registered: July 2009 |
Member |
|
|
Thank you,
So I tried to do this:
IEditorPart editor = null
if (editorDescriptor == null) {
editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file, activate);
} else {
editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(new FileEditorInput(file),
editorDescriptor.getId(), activate);
}
try {
Image image = CreateDiagramImage.getDiagramImage((DiagramEditPart) editor);
ImageData data = image.getImageData();
IFile imageFile = project.getFile("Imagem.gif");
byte [] bytes = data.data;
InputStream source = new ByteArrayInputStream(bytes);
imageFile.create(source, IResource.NONE, null);
} catch (CoreException e) {
e.printStackTrace();
} catch (Exception exc)
{
exc.printStackTrace();
}
I used "IDE.openEditor();" and " PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().openEditor(); " because my application already open diagrams editors using it.
But this methods returns an EditorPart that is instance of my "ProcessActivityDiagramEditor".
When I cast it to a "DiagramEditPart", it didn't work.
But it was only a test. What I really want is to generate a Image for each one of my Diagrams on my diagram's folder. So a started to look how could get the Diagram from the File,but i didn't find anything usefully.
Any tip?
|
|
|
Re: Diagram To Image [message #527527 is a reply to message #527511] |
Thu, 15 April 2010 14:09 |
|
You can browse the Diagrams throught the resources placed in the Editing Domain.
If you have an EObject, you can get its eResource, and from this eResource you can get the ResourceSet. This ResourceSet has all the Resources loaded in the Editing Domain. You can browse all the Resources and check that the type is of GMFResource, and every content of this resource will be a Diagram.
Normally, when you're working with a GMF Diagram, you have loaded in the ResourceSet at least 2 resources: the one that contains the Diagrams, and the one that contains the Model.
Good Luck
Marc
|
|
|
|
|
Re: Diagram To Image [message #531136 is a reply to message #531072] |
Mon, 03 May 2010 20:16 |
Fabricio Pellegrini Messages: 36 Registered: July 2009 |
Member |
|
|
I looked at OffscreenEditPart and found where the null object was comming.
DiagramEventBroker.startListening(TransactionUtil.getEditingDomain(diagram));
The TransactionUtil.getEditingDomain(diagram) was returnig null.
The problem is that I didn't understand well how to registry the Editing Domain of my gmf diagram.
SOLUTION!!
//Method was added to Marc's CreateDiagramImage Class... It create the driagram's image from IFile and save it in JPEG format
public static void createImageFrom(IFile file, String target)
{
try {
EMFLoader loader = new EMFLoader();
EObject object = loader.loadDomain(file.getLocation()+"");
Resource resc = object.eResource();
// This create the Editing Domain
ResourceSet rset = resc.getResourceSet();
TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain(rset);
Image image = CreateDiagramImage.getDiagramImage((Diagram) resc.getContents().get(0),null);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] {image.getImageData()};
imageLoader.save(target+".jpg",SWT.IMAGE_JPEG);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
[Updated on: Wed, 05 May 2010 14:40] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03979 seconds