gef中将流程图转换图片复制粘贴原理以及实现

设计思路如下:将一个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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值