GMF的保存图片代码

      GMF有提供保存图片的功能,但有时我们想自己在其他地方添加菜单,然后调用GMF的保存图片的代码,实现保存图片的功能。可这样做:

          CopyToImageDialog dialog = new CopyToImageDialog(Display.getCurrent().getActiveShell(), path, fileName);

          if (dialog.open() == CopyToImageDialog.CANCEL) {

                 return;
         }

         if (!overwriteExisting()) {
                 return;
         }

          Trace.trace(DiagramUIRenderPlugin.getInstance(),
               "Copy Diagram to " + dialog.getDestination().toOSString() + " as " + dialog.getImageFormat().toString());

          final MultiStatus status = new MultiStatus(DiagramUIRenderPlugin
                     .getPluginId(), DiagramUIRenderStatusCodes.OK, DiagramUIRenderMessages.CopyToImageAction_Label, null);

          IRunnableWithProgress runnable = createRunnable(status);

          ProgressMonitorDialog progressMonitorDialog = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());

          try {

                  progressMonitorDialog.run(false, true, runnable);

          } catch (InvocationTargetException e) {

          Log.warning(DiagramUIRenderPlugin.getInstance(),

                  DiagramUIRenderStatusCodes.IGNORED_EXCEPTION_WARNING, e.getTargetException().getMessage(),

                          e.getTargetException());

          if (e.getTargetException() instanceof OutOfMemoryError) {

               if (dialog.exportToHTML()) {

                      openErrorDialog(DiagramUIRenderMessages.CopyToImageAction_outOfMemoryMessage);
               } else {

                                if (new MessageDialog(dialog.getShell(),

                                        DiagramUIRenderMessages.CopyToImageOutOfMemoryDialog_title, null,

                                                DiagramUIRenderMessages.CopyToImageOutOfMemoryDialog_message,

                                                      MessageDialog.ERROR,  new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL },

                                                               0).open() == 0) {

                                                                            runCopyToImageUI(dialog);
                                                                  }
                      }
           } else if (e.getTargetException() instanceof SWTError) {
                 /**
                   * SWT returns an out of handles error when processing large
                   * diagrams
                 */
                 if (dialog.exportToHTML()) {
                         openErrorDialog(DiagramUIRenderMessages.CopyToImageAction_outOfMemoryMessage);
                 } else {
                                    if (new MessageDialog(dialog.getShell(),
                                          DiagramUIRenderMessages.CopyToImageOutOfMemoryDialog_title,  null,

                                                  DiagramUIRenderMessages.CopyToImageOutOfMemoryDialog_message,

                                                           MessageDialog.ERROR, 

                                                                new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0)

                                                                      .open() == 0) {
                                                                                runCopyToImageUI(dialog);
                                                                      }
                         }
                   } else {
                              openErrorDialog(e.getTargetException().getMessage());
            }
            return;

      } catch (InterruptedException e) {

                       /* the user pressed cancel */

                       Log.warning(DiagramUIRenderPlugin.getInstance(),

                       DiagramUIRenderStatusCodes.IGNORED_EXCEPTION_WARNING, e.getMessage(), e);
              }

      if (!status.isOK()) {

              openErrorDialog(status.getChildren()[0].getMessage());
       }

 

当然,可以这么简化:

            CopyToImageUtil copyToImageUtil = new CopyToImageUtil();

            ImageFileFormat imageFormat =ImageFileFormat.resolveImageFormat(3); //0表示png,1表示....

            FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);

            ...

            String filePath = dialog.open();

            IPath ipath = new Path(filePath);

            copyToImageUtil.copyToImage((DiagramEditPart) diagramEditPart, ipath, imageFormat,new NullProgressMonitor());

           

posted @ 2012-03-22 21:41  supermmao  阅读(266)  评论(0编辑  收藏  举报