本文最初发表于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,选择Neweditor
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>
程序代码: | [ 复制代码到剪贴板 ] |
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>
程序代码: | [ 复制代码到剪贴板 ] |
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>
程序代码: | [ 复制代码到剪贴板 ] |
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>
程序代码: | [ 复制代码到剪贴板 ] |
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>
程序代码: | [ 复制代码到剪贴板 ] |
GraphicalViewer viewer = getGraphicalViewer();
viewer.setContents(new HelloModel());
}
Step15:修改HelloEditor构造函数如下所示:
<script type="text/javascript"> window.attachEvent("onload",function (){AutoSizeDIV('CODE_8197')}) </script>
程序代码: | [ 复制代码到剪贴板 ] |
super();
setEditDomain(new DefaultEditDomain(this));
}
Step16:选中工程的根节点。Run as Eclipse Application。新建一个Java project,在其中新建一个文件,命名为hello.world。你就看到文章开头时的效果了。