| Copy & Paste to Windows clipboard [message #79067] |
Tue, 13 May 2003 06:04  |
Eclipse User |
|
|
|
Originally posted by: lauri.pesonen.iki.fi
Hi,
I searched through the articles on the server and noticed that there has
been some discussion about Copy&Paste to the windows clipboard.
I'm interested in copying a GEF diagram to word. It would be great if I
could somehow export it to some vector format so that the diagram could be
scaled when the user resizes his Word window (see Rational Rose and MS
Word), but I would be very happy even with a plain image.
As far as I can tell, for C&P to work I should implement the
org.eclipse.swt.dnd.Transfer interface (or extend ByteArrayTransfer) so
that my implementation would convert the GEF diagram into a Windows
understandable format.
I have no experience with the WMF format, so I am not going to start
implementing a vector transfer, as cool as it would be, unless someone has
done some work on that front already. So my implementation would translate
the GEF diagram into a PNG / GIF image. Any pointerson how to make a bitmap
image aout of my diagram?
The actual clipboard functionality is pretty simple. Something like this
would do it:
copyAction = new Action() {
public void run() {
Mydiagram diagram = getDiagram();
PngTransfer pngTransfer = PngTransfer.getInstance();
clipboard.setContents(new Object[]{diagram}, new Transfer[]{pngTransfer});
}
};
I'm not interested in pasting anything to my GEF diagram. I just want to be
able to attach my diagrams to Word documents.
Thanks for all your help.
--
Lauri
|
|
|
| Re: Copy & Paste to Windows clipboard [message #80001 is a reply to message #79067] |
Mon, 19 May 2003 09:51  |
Eclipse User |
|
|
|
Originally posted by: lauri.pesonen.iki.fi
Answering to myself...
I've implemented an action for running the actual copy operation and a
Transfer for trnaslating the GEF diagram to a BMP image. The end result is
that the Windows clipboard does not accept the byte[] and responds with
"Item not Collected: Format not supported by Office Clipboard" (I'm using
the Office Clipboard helper to see what's actually on the Clipboard). No
the debug code in the Transfer code which writes the same byte[] to a
file. The generated file is a valid BMP file so I seem to be missing
something between the Eclipse and Windows clipboards. Any ideas?
Br, Lauri
This is the code I've got so far.
CopyAction:
public void run() {
GraphicalViewer viewer =
(GraphicalViewer)getEditorPart().getAdapter(GraphicalViewer. class);
LayerManager lm = (LayerManager)viewer.getEditPartRegistry().get(LayerManager. ID);
IFigure f = lm.getLayer(LayerConstants.PRINTABLE_LAYERS);
Clipboard clipboard =
(Clipboard) getEditorPart().getAdapter(Clipboard.class);
clipboard.setContents(new IFigure[] {f}, new Transfer[] {new GefToImgTransfer()} );
}
And GetToImgTransfer:
protected void javaToNative(Object object, TransferData transferData) {
if (object == null || !(object instanceof IFigure)) return;
if (isSupportedType(transferData)) {
IFigure figure = (IFigure) object;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int w = figure.getSize().width;
int h = figure.getSize().height;
Image image = new Image(Display.getDefault(), w, h);
GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
figure.paint(graphics);
graphics.dispose();
gc.dispose();
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save(out, SWT.IMAGE_BMP);
byte[] buffer = out.toByteArray();
out.close();
// FileOutputStream fos = new FileOutputStream("test.bmp");
// fos.write(buffer);
// fos.close();
super.javaToNative(buffer, transferData);
} catch (IOException e) {
e.printStackTrace();
}
}
}
--
Lauri
|
|
|
Powered by
FUDForum. Page generated in 0.03893 seconds