GEF简单工作流,保存为xml和图片


 Eclipse
flow插件开发

工作需要,让研究eclipseflow插件开发,通过搜索资料发现在05-06年期间eclipse插件开发曾经火过一段时间,很多优秀的教程都是在那时出现的.不过现在貌似已经没落了.想想也是eclipse插件的整个体系比较庞大,同时也有很多优秀框架.但是学插件干嘛呢?一般公司不会用,除非专业出这样产品的公司,这样算下来市场的需求量确实是少之又少, 同事又缺乏像样的教程和书籍(英文的很多,中文的稀少……)。可是公司既然这样要求了也只有硬着头皮上了。下面介绍正文:

 

 

前面可以参见《八进制》的博客

http://bjzhanghao.cnblogs.com/archive/2005/02/19/106000.html

05年的教程,不得不说八进制写的很好.

 

实现了基本的添加,删除,拖拽,连线,属性显示等功能.但缺少非常关键的saveload方法(绘制的图像保存为xml,或图片,读取是回显上次编辑的内容)

下面就将先实现save方法(将编辑后的东东保存为xmlpng图片):

 

 

1.在实现GraphicalEditorWithPalette的类中找到doSave()方法,每次保存是都会调用该方法

2.要保存为图片,并且在本目录下,首先要获取本次打开的文件

IFile file =((IFileEditorInput)getEditorInput()).getFile();

File currentFile = file.getLocation().toFile();

String fileName = currentFile.getName();

String filePath = currentFile.getPath();

 

关于getEditorInput有这样一句解释:

当你在Resource Navigator里双击打开一个文件时,这个文件就和editor关联上了,因此可以通过editorInput得到文件。

 

3.保存为图片,添加export方法

 

 

public void export(GraphicalViewer viewer, String location, int format) 

    { 

        try 

        { 

           IFigure figure = ((AbstractGraphicalEditPart) viewer.getRootEditPart()).getFigure(); 

       

       

           File file = new File(location); 

           if (file.exists()) 

           { 

               if (!MessageDialog.openQuestion(null, "系统提示", 

               "该文件已经存在. 要重新覆盖它吗 ?")) 

               { 

                   return; 

               }

           } 

           else 

           { 

           file.createNewFile(); 

           } 

       

       

           FileOutputStream fos = new FileOutputStream(file); 

       

       

           if (figure instanceof Viewport) 

           { 

           // Reinit the figure 

           ((Viewport) figure).setViewLocation(0, 0); 

           } 

       

       

           Dimension size = figure.getPreferredSize(); 

           Image image = new Image(Display.getDefault(), size.width, size.height); 

           GC gc = new GC(image); 

           SWTGraphics graphics = new SWTGraphics(gc); 

           figure.paint(graphics); 

       

       

           ImageLoader loader = new ImageLoader(); 

           loader.data = new ImageData[] {image.getImageData()}; 

           loader.save(fos, format); 

           fos.close();

           

        } 

        catch (Exception e) 

        { 

       //记录该异常

//         ModelerPlugin.displayDialog(null, "An error occured during export. See the error log for more details.", IStatus.ERROR); 

//         ModelerPlugin.log(e); 

        }finally{

            

        }

    }

 

  

 

 

4.添加保存xml的方法xmlSave():

public void xmlSave(IProgressMonitor progressMonitor) {

          editorSaving = true;

          Platform.run(new SafeRunnable() {

          public void run() throws Exception {

            

          //以下存储xml文件

            

          Document document = DocumentHelper.createDocument();

          

          Element logicDiagramElement = document.addElement("LogicDiagram");

          /*中间是你的xml*/

          try{

            

          OutputFormat format = OutputFormat.createPrettyPrint();

//        IFile file = ((IFileEditorInput)getEditorInput()).getFile();

//        final IFile file = (IFile) ((DiagramEditorInput)getEditorInput()).getFile();

          IFile file =((IFileEditorInput)getEditorInput()).getFile(); 

          File f = file.getLocation().toFile();

          System.out.println("fileName:"+f.getName()); 

          XMLWriter output = new XMLWriter(new FileOutputStream( f ), format);

          

          output.write( document );   

          output.close();

          } catch(IOException e){

          System.out.println(e.getMessage());

          }

          getCommandStack().markSaveLocation();

          }

          });

          editorSaving = false;

}

  

 

 

 

5.doSave()中调用:

 

//保存为 xml

        xmlSave(monitor);

//保存为图片export(getGraphicalViewer(),filePath+".png",SWT.IMAGE_PNG);

 

  

 

 

 

问题:保存xml使用的是dom4j这个外部jar,但是插件的寻包机制又和普通项目不同,所以需要处理一下:

a.  在项目下新建lib目录存放外部jar

b.  MANIFEST.MFRuntime页签,在右下角的classpath中添lib/dom4j-1.6.1.jar



 

c.  在左上角exported package中添加需要用到的包

 

d.  添加选中工程,buildPath中添加lib中的jar

在当前工程目录下添加lib文件夹,将用到的第三方jar包放入lib文件夹中。在runtime右下角的classpath中添加/lib/*.jar,最后在runtime左上角的exported package中添加需要用到的包

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值