设计思路如下:将一个gef图元转换为一个图片(createImage()方法是实际在内存里作画并转换为可写入为文件的二进制流),然后将图片转换为ImageData对象,通过系统的ImageTransfer转换器转换:
A.将gef图元转换为图片:
/**
* 将一个元件装换为一个图片的信息资源
* Returns the bytes of an encoded image for the specified
* IFigure in the specified format.
*
* @param figure the Figure to create an image for.
* @param format one of SWT.IMAGE_BMP, SWT.IMAGE_BMP_RLE, SWT.IMAGE_GIF
* SWT.IMAGE_ICO, SWT.IMAGE_JPEG, or SWT.IMAGE_PNG
* @return the bytes of an encoded image for the specified Figure
*/
privatebyte[] createImage(IFigure figure, int format) {
IWorkbenchPart activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
Device device =activePage.getTitleImage().getDevice();
Rectangle r = figure.getBounds();
ByteArrayOutputStream result = new ByteArrayOutputStream();
Image image = null;
GC gc = null;
Graphics g = null;
try {
image = new Image(device, r.width, r.height);
gc = new GC(image);
g = new SWTGraphics(gc);
g.translate(r.x * -1, r.y * -1);
figure.paint(g);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] {image.getImageData()};
imageLoader.save(result, format);
} finally {
if (g != null) {
g.dispose();
}
if (gc != null) {
gc.dispose();
}
if (image != null) {
image.dispose();
}
}
return result.toByteArray();
}
关于粘贴复制:
参考见文本复制倒粘贴板:
publicvoid copyToClipboard() {
if (text == null) {
return;
}
Clipboard clipboard = null;
try {
clipboard = new Clipboard(text.getShell().getDisplay());
String contents = text.getSelectionText();
if (contents.length() == 0)
contents = text.getText();
clipboard.setContents(new Object[] { contents },
new Transfer[] { TextTransfer.getInstance() });
} finally {
if (clipboard != null) {
clipboard.dispose();
}
}
}
获取粘贴板中的数据:
Object getClipboardContent(int clipboardType) {
TextTransfer plainTextTransfer = TextTransfer.getInstance();
returnclipboard.getContents(plainTextTransfer, clipboardType);
}