Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Saving as jpeg!!!
Saving as jpeg!!! [message #180291] Wed, 04 May 2005 09:08 Go to next message
Eclipse UserFriend
Originally posted by: gautamn.iitk.ac.in

Hi,
i am developing a pluign for eclipse to make architecture diagrams. I
want to save my diagram files as jpeg,gif etc. How to do that???My save as
code is like this:

/**
* @see org.eclipse.ui.ISaveablePart#doSaveAs()
*/
public void doSaveAs() {// Show a SaveAs dialog
Shell shell = getSite().getWorkbenchWindow().getShell();
SaveAsDialog dialog = new SaveAsDialog(shell);
dialog.setOriginalFile(((IFileEditorInput) getEditorInput()).getFile());
dialog.open();

IPath path = dialog.getResult();
if (path != null) {
// try to save the editor's contents under a different file name
final IFile file =
ResourcesPlugin.getWorkspace().getRoot().getFile(path);
try {
new ProgressMonitorDialog(shell).run(
false, // don't fork
false, // not cancelable
new WorkspaceModifyOperation() { // run this operation
public void execute(final IProgressMonitor monitor) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
createOutputStream(out);
file.create(
new ByteArrayInputStream(out.toByteArray()), // contents
true, // keep saving, even if IFile is out of sync with the
Workspace
monitor); // progress monitor
}
catch (CoreException ce) { ce.printStackTrace(); }
catch (IOException ioe) { ioe.printStackTrace(); }
}
});
// set input to the new file
setInput(new FileEditorInput(file));
getCommandStack().markSaveLocation();
}
catch (InterruptedException ie) {
// should not happen, since the monitor dialog is not cancelable
ie.printStackTrace();
}
catch (InvocationTargetException ite) {
ite.printStackTrace();
}
} // if
}
private void createOutputStream(OutputStream os)
throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(getModel());
oos.close();
}

Regards,
Nitin Gautam.
Re: Saving as jpeg!!! [message #180301 is a reply to message #180291] Wed, 04 May 2005 11:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Lamont_Gilbert.rigidsoftware.com

I would look at the print features for this. It should give you an idea
of how to capture the diagram graphically.

CL


Nitin Gautam wrote:
> Hi, i am developing a pluign for eclipse to make architecture diagrams.
> I want to save my diagram files as jpeg,gif etc. How to do that???My
> save as code is like this:
>
> /**
> * @see org.eclipse.ui.ISaveablePart#doSaveAs()
> */
> public void doSaveAs() {// Show a SaveAs dialog
> Shell shell = getSite().getWorkbenchWindow().getShell();
> SaveAsDialog dialog = new SaveAsDialog(shell);
> dialog.setOriginalFile(((IFileEditorInput)
> getEditorInput()).getFile());
> dialog.open();
>
> IPath path = dialog.getResult();
> if (path != null) {
> // try to save the editor's contents under a different file
> name
> final IFile file =
> ResourcesPlugin.getWorkspace().getRoot().getFile(path);
> try {
> new ProgressMonitorDialog(shell).run(
> false, // don't fork
> false, // not cancelable
> new WorkspaceModifyOperation() { // run this
> operation
> public void execute(final IProgressMonitor
> monitor) {
> try {
> ByteArrayOutputStream out = new
> ByteArrayOutputStream();
> createOutputStream(out);
> file.create(
> new
> ByteArrayInputStream(out.toByteArray()), // contents
> true, // keep saving, even if
> IFile is out of sync with the Workspace
> monitor); // progress monitor
> }
> catch (CoreException ce) {
> ce.printStackTrace(); }
> catch (IOException ioe) {
> ioe.printStackTrace(); } }
> });
> // set input to the new file
> setInput(new FileEditorInput(file));
> getCommandStack().markSaveLocation();
> }
> catch (InterruptedException ie) {
> // should not happen, since the monitor dialog is not
> cancelable
> ie.printStackTrace(); }
> catch (InvocationTargetException ite) {
> ite.printStackTrace(); }
> } // if
> }
> private void createOutputStream(OutputStream os) throws
> IOException {
> ObjectOutputStream oos = new ObjectOutputStream(os);
> oos.writeObject(getModel());
> oos.close();
> }
>
> Regards,
> Nitin Gautam.
>
>
implementing multiple connection types [message #180309 is a reply to message #180301] Wed, 04 May 2005 12:53 Go to previous messageGo to next message
benedict heal is currently offline benedict healFriend
Messages: 28
Registered: July 2009
Junior Member
I have an editor in which I want to have two types of connection -
e.g. 'childof' and 'spouseof'.

What is the recommended way of handling this?

Should I have

- two palette tools, two requests, two commands
or
- 2 tools, and a single request with a 'connection type' field
or
- something else.
do
The GEF sample shape editor solves a similar problem of passing a line
style by having a single request type, and fiddling with
request.getNewObjectType() to return an Integer line style, but there's
a comment in the code suggesting the author isn't v. happy with this.

thanks,
Benedict
Re: implementing multiple connection types [message #180317 is a reply to message #180309] Wed, 04 May 2005 12:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Lamont_Gilbert.rigidsoftware.com

benedict heal wrote:
> I have an editor in which I want to have two types of connection - e.g.
> 'childof' and 'spouseof'.
>
> What is the recommended way of handling this?
>
> Should I have
>
> - two palette tools, two requests, two commands
> or
> - 2 tools, and a single request with a 'connection type' field
> or
> - something else.
> do
> The GEF sample shape editor solves a similar problem of passing a line
> style by having a single request type, and fiddling with
> request.getNewObjectType() to return an Integer line style, but there's
> a comment in the code suggesting the author isn't v. happy with this.
>
> thanks,
> Benedict

depends on how different the two types behave. is the method of
creation exactly the same? if so just use a type field.

All my various shapes use the same tool to create them, since they are
created by the same technique.

So 1 tool, 2 instances of the tool, but different types.

I go for multiple requests as well since you never know if you want to
make them more different at some point.


CL
Re: Saving as jpeg!!! [message #180331 is a reply to message #180301] Wed, 04 May 2005 13:58 Go to previous messageGo to next message
Sapna George is currently offline Sapna GeorgeFriend
Messages: 76
Registered: July 2009
Member
If you arrive at a solution for this, could you please share it with the
rest of us? I want this feature as well. I will do the same.

thanks a bunch
Sapna
Re: Saving as jpeg!!! [message #180396 is a reply to message #180291] Wed, 04 May 2005 21:17 Go to previous messageGo to next message
Sapna George is currently offline Sapna GeorgeFriend
Messages: 76
Registered: July 2009
Member
Well, i got curious and dug around a bit. I found an old post in the
archive about the same thing. The subject is "Can i capture the snapshot
of the editor in a JPEG file?".

I tried the code suggested in this post and it worked, sort of. I had to
tweek it a bit to get the result i wanted. I'm not claiming that this is
the best way to do it, but it serves my purpose for now. The code is given
below. Also, below the code i have listed the changes i made to the
original code.

public Image createScreenshot(String fileName) {
Image im = null;
ScalableFreeformRootEditPart editPart = (ScalableFreeformRootEditPart)
getGraphicalViewer().
getContents().getRoot();
LayerManager lm =
(LayerManager)getGraphicalViewer().getEditPartRegistry().get (LayerManager.ID);
IFigure f = lm.getLayer(LayerConstants.PRINTABLE_LAYERS);
Rectangle r = f.getBounds();

// getting zoom value - for scaling translations
double zoom = editPart.getZoomManager().getZoom();
zoom = 1 / zoom;
// viewport which displays my diagram
Viewport viewer = (Viewport)editPart.getFigure();

// margin
int MARGIN = 10;
// my diagram width - using for image width
int imageWidth = r.width;
// my diagram height - using for image height
int imageHeight = r.height;
// scaling image - using diagram width and height multiplied by zoom
// value and adding margins
int imageWidthScaled = (int) (imageWidth*zoom) + 2 * (int)(MARGIN*zoom);
int imageHeightScaled = (int) (imageHeight*zoom) + 2 *
(int)(MARGIN*zoom);

// creating new Image
im = new Image(Display.getDefault(), imageWidthScaled,
imageHeightScaled);
GC gc = new GC(im);
SWTGraphics g = new SWTGraphics(gc);
// paint contents on my Image
viewer.paint(g);
// we do not need g and gc anymore
g.dispose();
gc.dispose();

// saving an image to the disk
try {
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[1];
loader.data[0] = im.getImageData();
loader.save(fileName, SWT.IMAGE_JPEG);
}
catch (Exception e) {
e.printStackTrace();
}
return im;
}

Problems in the original:
1. It uses ScalableFreeformRootEditPart's getFigure() method which returns
a Viewport to determine the contents to be saved. This will give you only
that part of the diagram that's within the viewport. Evrything outside
will not be saved. So, i used
LayerManager lm =
(LayerManager)getGraphicalViewer).getEditPartRegistry().get( LayerManager.ID);
IFigure f = lm.getLayer(LayerConstants.PRINTABLE_LAYERS);

This gives the ENITRE diagram. and i can get the size directly from this
figure.

2. it does some translations of the image. i'm not sure of its purpose. In
fact it messed up my view on the editor. So, i took it
out completely.

Sapna
Re: Saving as jpeg!!! [message #180419 is a reply to message #180396] Thu, 05 May 2005 04:23 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gautamn.iitk.ac.in

Hi,
I do not want the sreenshot of the editor but I want to save the
diagrams that I am creating in my editor(i am saving it as ".mvt"
extension) in various formats like jpeg,gif,png,etc. That's why I was
doing this in my "save as" part of the editor. Kindly tell me how to do
that...
Regards,
Nitin Gautam.
Re: Saving as jpeg!!! [message #180435 is a reply to message #180419] Thu, 05 May 2005 06:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wjancz.\/\/asko.pl(change \/\/ with w)

> Hi,
> I do not want the sreenshot of the editor but I want to save the diagrams
> that I am creating in my editor(i am saving it as ".mvt" extension) in
> various formats like jpeg,gif,png,etc. That's why I was doing this in my
> "save as" part of the editor. Kindly tell me how to do that...

Hmmm
So what is the difference ?
The code (which I am the author) is saving a diagram into a jpeg file. so ?
If you want to save diagram and open it again for editing you should save
model (in your file format).
I am still not sure what You want to do.

...::WojT::..
Re: Saving as jpeg!!! [message #180442 is a reply to message #180435] Thu, 05 May 2005 08:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gautamn.iitk.ac.in

Hi,
in my diagram editor that I created using GEF and Draw2d, I just want
to add a fuctionality that a particular diagram can also be saved in
various formats that an image viewer can open. For this reason I want code
for saving the file generated by Eclipse in an image file format. I used
the specified code and it was giving errors. Can u help in sorting out the
problem???
Regards,
Nitin Gautam.

My save code is:
/*********************************************************** **********/
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(out);
objectOut.writeObject(model);
objectOut.close();
IFile file = ((IFileEditorInput) getEditorInput()).getFile();
file.setContents(new ByteArrayInputStream(out.toByteArray()), true,
false, monitor);
out.close();

/*********************************************************** *************/
and save as code:
/*********************************************************** *************/
Shell shell = getSite().getWorkbenchWindow().getShell();
SaveAsDialog dialog = new SaveAsDialog(shell);
dialog.setOriginalFile(((IFileEditorInput) getEditorInput()).getFile());
dialog.open();

IFigure figure =
((AbstractGraphicalEditPart)getGraphicalViewer().getRootEdit Part()).getFigure();

String desn="";

IPath path = dialog.getResult();
if (path != null) {
// try to save the editor's contents under a different file name
final IFile file =
ResourcesPlugin.getWorkspace().getRoot().getFile(path);


try {
new ProgressMonitorDialog(shell).run(
false, // don't fork
false, // not cancelable
new WorkspaceModifyOperation() { // run this operation
public void execute(final IProgressMonitor monitor) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
createOutputStream(out);
file.create(
new ByteArrayInputStream(out.toByteArray()), // contents
true, // keep saving, even if IFile is out of sync with the
Workspace
monitor); // progress monitor
}
catch (CoreException ce) { ce.printStackTrace(); }
catch (IOException ioe) { ioe.printStackTrace(); }
}
});
// set input to the new file
setInput(new FileEditorInput(file));
getCommandStack().markSaveLocation();
}
catch (InterruptedException ie) {
// should not happen, since the monitor dialog is not cancelable
ie.printStackTrace();
}
catch (InvocationTargetException ite) {
ite.printStackTrace();
}
*/
} // if
}
private void createOutputStream(OutputStream os)
throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(getModel());
oos.close();
}
/*********************************************************** *************/
Re: Saving as jpeg!!! [message #180450 is a reply to message #180442] Thu, 05 May 2005 12:11 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wjancz.\/\/asko.pl(change \/\/ with w)

> Hi,
> in my diagram editor that I created using GEF and Draw2d, I just want to
> add a fuctionality that a particular diagram can also be saved in various
> formats that an image viewer can open. For this reason I want code for
> saving the file generated by Eclipse in an image file format. I used the
> specified code and it was giving errors. Can u help in sorting out the
> problem???
> Regards,
> Nitin Gautam.
>
do not invent wheel from beginig.
use the ImageLoader class like me:

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[1];
loader.data[0] = im.getImageData();
loader.save(fileName, SWT.IMAGE_JPEG);

in Your SaveAs dialog only describe a filename and image type.
You can override CTRL+S action too (by default saving into default name as
JPG or executing doSaveAs() which will open SaveAs dialog).

I think that it will help You.

...::WojT::..
Re: Saving as jpeg!!! [message #180484 is a reply to message #180450] Fri, 06 May 2005 04:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gautamn.iitk.ac.in

Hi,
thanks its working fine now...I will discuss other issues of my editor
soon..
Regards,
Nitin Gautam.
Re: Saving as jpeg!!! [message #180492 is a reply to message #180484] Fri, 06 May 2005 06:03 Go to previous message
Eclipse UserFriend
Originally posted by: wjancz.\/\/asko.pl(change \/\/ with w)

> Hi,
> thanks its working fine now...I will discuss other issues of my editor
> soon..
> Regards,
> Nitin Gautam.

BTW:
It should be done as Export not as Save option.
Saving is saving a model....


...::WojT::..
Previous Topic:FlowLayout & zoom issues
Next Topic:printing problem
Goto Forum:
  


Current Time: Sun Jan 29 19:39:04 GMT 2023

Powered by FUDForum. Page generated in 0.09406 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top