上次做的例子非常非常简单,可以算是没有连接的GEF最简单的了吧:P
现在我要在上一个例子的基础上做点修改,让它稍微有点意思.
代码下载最后的样子如下:
主要是增加了一个model叫Node,可以自由地增加到Column对象中.
UML图如下:
下面讲讲主要思路,还是按部就班,一个一个来:
1.model
除了增加一个简单的POJO对象Node,我们必须修改Column对象:
以下是在Column代码中增加的部分:
Column.java
privateListchildren=newArrayList();
publicListgetChildren()...{
returnchildren;
}
publicvoidsetChildren(Listchildren)...{
this.children=children;
}
publicvoidaddChild(Nodenode)...{
addChild(node,-1);
}
publicvoidremoveChild(Nodenode)...{
children.remove(node);
}
publicvoidaddChild(Nodenode,intindex)...{
if(index>=0)
children.add(index,node);
else
children.add(node);
firePropertyChange(Content.P_CHILDREN,index,node);
}
这里有一个需要注意的地方是index,因为node对象可以随便插入到column的List中,所以我们需要gef告诉我们在图形化编辑时,新建的node放在column中的位置,不过等下就知道,还是很简单的.
Node的代码就略掉了.
2.Figure
写完model当然是写对应的Figure了,上一次Figure的代码是放在EditPart中的createFigure方法中的,这次我做了一点修改,增加了geftest3.figure包,以及两个Figure类:ColumnFigure, NodeFigure.
因为node是加入到Column中的,所以我们需要ColumnFigure来管理NodeFigure的布局. (布局的主要作用就是来安排所有子对象的位置).我在ColumnFigure中使用的是FlowLayout.
ColumnFigure.java
publicColumnFigure()...{
setSize(200,100);
FlowLayoutlayout=newFlowLayout();
layout.setHorizontal(false);
layout.setMajorSpacing(2);
setLayoutManager(layout);
setBorder(newLineBorder(ColorConstants.black,1));
setOpaque(true);
}
然后就是NodeFigure,很简单:
NodeFigure.java
publicNodeFigure()...{
Labelname=newLabel("nodeName");
ToolbarLayoutlayout=newToolbarLayout();
setLayoutManager(layout);
setBorder(newLineBorder(ColorConstants.black,1));
setBackgroundColor(background);
setOpaque(true);
add(name);
}
2.EditPart
写完figure后我会马上更新或增加对应的EditPart以及PartFactory:
NodeEditPart和PartFactory没有什么特别之处,这里就省去了,可以下载插件查看代码.
这里关键是要更新ColumnEditPart:
我们告诉gef Column的子对象是哪些.以及在增加node后通知figure及时更新.
ContentEditPart.java
@Override
protectedListgetModelChildren()...{
return((Column)getModel()).getChildren();
}
publicvoidpropertyChange(PropertyChangeEventevt)...{
if(evt.getPropertyName().equals(Content.P_CHILDREN))
refreshChildren();
}
3.Command
增加一个CreateNodeCommand:
它的execute方法和一个setIndex方法如下:
CreateNodeCommand.java
@Override
publicvoidexecute()...{
if(index<0)
column.addChild(node);
else
column.addChild(node,index);
}
publicvoidsetIndex(intindex)...{
this.index=index;
}
注意这里,我们如果index > 0,会把node加入到parent里的List的index位置.
4.Policy
因为是在Column里加子对象,所以我们必须为Column安装一个Policy来创建CreateNodeCommand.又因为Column是使用的FlowLayout,所以我就写了CustomFlowLayoutEditPolicy 继承GEF里的FlowLayoutEditPolicy.
只覆写了它的getCreateCommand方法:
CustomFlowLayoutEditPolicy .java
@Override
protectedCommandgetCreateCommand(CreateRequestrequest)...{
CreateNodeCommandcommand=newCreateNodeCommand();
command.setColumn((Column)getHost().getModel());
Objectobject=request.getNewObject();
if(objectinstanceofNode)...{
command.setNode((Node)request.getNewObject());
EditPartafter=getInsertionReference(request);
intindex=getHost().getChildren().indexOf(after);
command.setIndex(index);
returncommand;
}
returnnull;
}
在这个方法中可以用GEF的api得到index.
4.1安装policy
回到ColumnEditPart,安装policy
ColumnEditPart.java
@Override
protectedvoidcreateEditPolicies()...{
installEditPolicy(EditPolicy.LAYOUT_ROLE,
newCustomFlowLayoutEditPolicy());
}
5.Editor:
最后更新Editor的palette.
在getPaletteRoot方法中增加这段代码:
Editor.java
CreationToolEntrycreationNode=newCreationToolEntry(
"drawnode","createnode",newSimpleFactory(
Node.class),descriptor,descriptor);
drawer.add(creationNode);
OK,大工告成:P
参考资源:
GEF学习教程1-Unplugged版GEF logic example.
分享到:
相关推荐
GEF-ALL-3.7 GEF-ALL-3.8 官方包。 GEF Draw2d 学习资料
GEF简易教程-学习GEF的入门教程,不错的gef入门教程
GEF入门必读 GEF入门系列 GEF-whole-upload 感谢八进制
目前的最新的GEF,用于eclipse是实现可视化。
GEF: Graphical Editing Framework <br>GEF是一套MVC Framework,它能帮你比较容易的...依赖:org.eclipse.draw2d*** 本软件GEF-ALL-3.2.2完整版包含(Draw2D, GEF and Zest) 大小:6.6M 请用于Eclipse3.2.2环境下.
解压GEF-ALL-3.4.1.zip,得到一个eclipse文件夹,打开可看到: 下面有三个文件夹:features,plugins,readme 分别拷贝上面三个文件夹下面的内容把它们对应放到Eclipse安装目录D:\eclipse下面对应的同 名文件夹下...
GEF 图形编辑框架,是Eclipse旗下一个重要的插件,用于开发图形界面。这是系列教程源码之三。
这本书非常好,对GEF做了很详细的讲解,相信对初学者有很大的帮助
GEF-Update-5.3.3,eclipse4.18离线安装包,eclipse有自带gef,这个是增强包,可以不安装
GEF-ALL-3.1.zip eclipse
将GEF-ALL-XX.zip解压缩,然后拷贝解压后的eclipse目录下的两个文件夹到 WINDOWS 下你的Eclipse安装目录。 UBuntu下到 usr/lib/eclispe和usr/share/eclipse, Merge合并所有的现有文件夹。
本实例是根据gef-whole-upload教程制作,实现了该教程中的所有功能,很适合新手入门! 建议结合gef-whole-upload教程以及教程中的问题http://blog.csdn.net/zhaoxy2850/article/details/9497753阅读。
GEF-whole-upload.pdf附书源码
GEF-ALL-3.9.100.zip
RCP GEF 入门教程,从法语班翻译过来的,自认为是比较全面细致的GEF入门教程,比八进制的《GEF入门系列》要浅显易懂。
GEF-SDK,eclipse pulgin
GEF-SDK-3.1.1.zip 可用的资源 大家有问题联系我。
对应版本有用,要把解压后的文件复制占到对应的插件目录,就可以使用
最新gef eclipse ve
GEF-II电场仪使用手册