[GEF循序渐进系列之三]Hello world with GEF

本文最初发表于http://eclipse.kuwww.net 原文引用http://my.kuwww.net/eclipse/blogview.asp?logID=5&cateID=4

作者:齐好宗    hzqi@isoftstone.com

下面我们要做一个最简单的GEF应用程序。在大部分经典的语言教程中,总是已HelloWorld命名第一个小程序,这里我们也以HelloWorld开始。我们所应用的环境是Eclipse3.1 + GEF3.1,最终结果如下图所示:

按此在新窗口打开图片

     在开始之前你需要在eclipse网站下载GEF3.1。或者直接点击下面的连接下载GEF3.1 http://eclipse.stu.edu.tw/tools/gef/downloa ... 0507071758/GEF-ALL-3.1.zip
下载以后,将它解压到eclipse的目录下就可以了。
     Step1:新建一个插件工程,按下图设置:

按此在新窗口打开图片

     Step2:点击下一步,如下图所示:

按此在新窗口打开图片

     Step3:点击完成。此时,插件编辑器会自动打开。如下图:

按此在新窗口打开图片

     Step4:请选择Dependencies标签,点击Add,请选则org.eclipse.gef(3.1.0),点击OK。如下图:

按此在新窗口打开图片

     Step5:选择Extentions标签,点击Add,选择org.eclipse.ui.editors。点击Finish。

按此在新窗口打开图片

     Step6:右键点击扩展点org.eclipse.ui.editor,选择Neweditor

按此在新窗口打开图片

     Step7:按照如下图设置editor.

Name    Hello World Editor
Icon    Icons/eclipse_launcher.gif
class    gef.example.helloworld.HelloWorldEditor
default    true
filenames    hello.world


按此在新窗口打开图片


     Step8:使用Ctrl+S save一下当前文件。点击class:弹出如下图所示的对话框:

按此在新窗口打开图片

     Step9:注意将Superclass由org.eclipse.ui.parts.EditorPart变更为org.eclipse.gef.ui.parts.GraphicalEditor。将Constructors from superclass和Inherited abstract method前面的复选框构上。点击Finish,将生成HelloWorldEditor类。

     Step10:下面我们来建立模型,我们的模型很简单。请看我们的模型类gef.example.helloworld.HelloModel.java:
<script type="text/javascript"> window.attachEvent("onload",function (){AutoSizeDIV('CODE_7643')}) </script>

程序代码:[ 复制代码到剪贴板 ]

package gef.example.helloworld.model;

public class HelloModel {

    private String text = "Hello world usring GEF";

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }



     Step11:有了模型了,我们下一步来建立一个与这个Model相关联的Controller(EditPart)。

<script type="text/javascript"> window.attachEvent("onload",function (){AutoSizeDIV('CODE_6266')}) </script>

程序代码:[ 复制代码到剪贴板 ]

package gef.example.helloworld.editparts;
public class HelloEditPart extends AbstractGraphicalEditPart {

    //创建与模型相对应的图形。
    protected IFigure createFigure() {
        HelloModel model = (HelloModel)getModel();
        Label label = new Label();
        label.setText(model.getText());
        return label;
    }

    protected void createEditPolicies() {

    }



     Step12:创建MyEditPartFactory.
<script type="text/javascript"> window.attachEvent("onload",function (){AutoSizeDIV('CODE_6635')}) </script>

程序代码:[ 复制代码到剪贴板 ]

package gef.example.helloworld.editparts;
public class MyEditPartFactory implements EditPartFactory {

    public EditPart createEditPart(EditPart context, Object model) {
        EditPart part = null;
        if(model instanceof HelloModel)
        {
            part = new HelloEditPart();
            part.setModel(model);
        }
        return part;
    }
}



     Step13:在HelloEditor类中,覆盖父类的configureGraphicalViewer()方法:

<script type="text/javascript"> window.attachEvent("onload",function (){AutoSizeDIV('CODE_4316')}) </script>

程序代码:[ 复制代码到剪贴板 ]

protected void configureGraphicalViewer() {
        super.configureGraphicalViewer();
        GraphicalViewer viewer = getGraphicalViewer();
        //设置视图的EditPartFactory
        viewer.setEditPartFactory(new MyEditPartFactory());
    }



     Step14:在HelloEditor类中实现initializeGraphicalViewer()方法:
<script type="text/javascript"> window.attachEvent("onload",function (){AutoSizeDIV('CODE_4415')}) </script>

程序代码:[ 复制代码到剪贴板 ]

protected void initializeGraphicalViewer() {
        GraphicalViewer viewer = getGraphicalViewer();
        viewer.setContents(new HelloModel());
    }



     Step15:修改HelloEditor构造函数如下所示:
<script type="text/javascript"> window.attachEvent("onload",function (){AutoSizeDIV('CODE_8197')}) </script>

程序代码:[ 复制代码到剪贴板 ]

public HelloWorldEditor() {
        super();
        setEditDomain(new DefaultEditDomain(this));
    }



     Step16:选中工程的根节点。Run as Eclipse Application。新建一个Java project,在其中新建一个文件,命名为hello.world。你就看到文章开头时的效果了。

实例工程下载: http://my.kuwww.net/eclipse/attachments/month_0507/da3g_helloworld1.rar

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值