现在开始学习GEF,我觉得学习GEF是非常有挑战性和从满乐趣的,我希望能够把平时的学习进程记录下来,既可以帮助入门者,也做为自己的笔记,能够加深自己对GEF的理解.

现在网上有许多GEF的教程,有些非常优秀,比如dudu的GEF教程,非常适合入门,八进制的教程,讲解了许多高级知识点,但是我还是觉得有很多他们的教程中没有或是说得不详细,我的目的不是取代他们的教程,而是一个补充:P

因为大部分教程都是以连接为重点,而我希望做一些和连接无关的GEF例子.
比如这个例子就是用了draw2d自带的ToolbarLayout来为column布局.

代码下载

GEF学习教程1-Unplugged版

1.model
作为开始,model非常简单,uml图如下:
GEF学习教程1-Unplugged版
为了让model的变化能够通知给editpart,我让所有model继承了AbstractModel类:
AbstractModel.java
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版publicclassAbstractModel...{
GEF学习教程1-Unplugged版
privatePropertyChangeSupportlisteners=newPropertyChangeSupport(this);
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
publicvoidaddPropertyChangeListener(PropertyChangeListenerlistener)...{
GEF学习教程1-Unplugged版listeners.addPropertyChangeListener(listener);
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版
publicvoidfirePropertyChange(StringpropName,ObjectoldValue,
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版ObjectnewValue)
...{
GEF学习教程1-Unplugged版listeners.firePropertyChange(propName,oldValue,newValue);
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
publicvoidremovePropertyChangeListener(PropertyChangeListenerlistener)...{
GEF学习教程1-Unplugged版listeners.removePropertyChangeListener(listener);
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版}


具体的model代码就不贴在这里了.可以查看下载plugin中的源代码.

2.EditPart:
写好model的代码后,开始写每个model对应的EditPart,这里分别是ContentEditPart和ColumnEditPart:
因为ContentEditPart作为最高层容器,我们是在它的上面添加Column,所以它负责为Column布局,给它设置ToolbarLayout.

首先给所有editPart写一个超类,让它自动注册为model的listener:
AbstractModel.java
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版publicclassAbstractModel...{
GEF学习教程1-Unplugged版
privatePropertyChangeSupportlisteners=newPropertyChangeSupport(this);
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
publicvoidaddPropertyChangeListener(PropertyChangeListenerlistener)...{
GEF学习教程1-Unplugged版listeners.addPropertyChangeListener(listener);
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
publicvoidfirePropertyChange(StringpropName,ObjectoldValue,ObjectnewValue)...{
GEF学习教程1-Unplugged版listeners.firePropertyChange(propName,oldValue,newValue);
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
publicvoidremovePropertyChangeListener(PropertyChangeListenerlistener)...{
GEF学习教程1-Unplugged版listeners.removePropertyChangeListener(listener);
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版}

然后开始写每个EditPart:
下面是ContentEditPart中的createFigure()方法:
ContentEditPart.java
GEF学习教程1-Unplugged版@Override
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
protectedIFigurecreateFigure()...{
GEF学习教程1-Unplugged版Layerlayer
=newFreeformLayer();
GEF学习教程1-Unplugged版ToolbarLayoutlayout
=newToolbarLayout();
GEF学习教程1-Unplugged版layout.setVertical(
false);
GEF学习教程1-Unplugged版layout.setSpacing(
5);
GEF学习教程1-Unplugged版layout.setStretchMinorAxis(
true);
GEF学习教程1-Unplugged版layer.setLayoutManager(layout);
GEF学习教程1-Unplugged版layer.setBorder(
newMarginBorder(5));
GEF学习教程1-Unplugged版
returnlayer;
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
Column的EditPart的createFigure就简单得多,只是一个Label,如下所示:
ColumnEditPart.java
GEF学习教程1-Unplugged版@Override
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
protectedIFigurecreateFigure()...{
GEF学习教程1-Unplugged版Columncolumn
=(Column)getModel();
GEF学习教程1-Unplugged版Labellabel
=newLabel();
GEF学习教程1-Unplugged版label.setText(column.getName());
GEF学习教程1-Unplugged版label.setBorder(
newCompoundBorder(newLineBorder(),
GEF学习教程1-Unplugged版
newMarginBorder(3)));
GEF学习教程1-Unplugged版label.setBackgroundColor(ColorConstants.green);
GEF学习教程1-Unplugged版label.setOpaque(
true);
GEF学习教程1-Unplugged版label.setSize(
40,60);
GEF学习教程1-Unplugged版
returnlabel;
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
3.PartFactory:
PartFactory就象其名字一样,很简单的Factory对象,用来连接EditPart和Model,只有一个方法,createEditPart()如下:
PartFactory.java
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版publicEditPartcreateEditPart(EditPartcontext,Objectmodel)...{
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
if(modelinstanceofContent)...{
GEF学习教程1-Unplugged版EditPartpart
=newContentEditPart();
GEF学习教程1-Unplugged版part.setModel(model);
GEF学习教程1-Unplugged版
returnpart;
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版}
elseif(modelinstanceofColumn)...{
GEF学习教程1-Unplugged版EditPartpart
=newColumnEditPart();
GEF学习教程1-Unplugged版part.setModel(model);
GEF学习教程1-Unplugged版
returnpart;
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
thrownewRuntimeException();
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
4.Command:
然后就是开始写Command了.
这个例子只是简单的把Column加入到编辑器中,所以只有一个CreateCommand:
CreateCommand.java

GEF学习教程1-Unplugged版@Override
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
publicvoidexecute()...{
GEF学习教程1-Unplugged版content.addChild(column);
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版

4.1command执行后,model将发生变化,那么editPart如何得到通知并将变化转交给figure呢?
在这里editPart中加入以下两个方法.(EditPart是实现了PropertyChangeListener接口)
ContentEditPart.java
GEF学习教程1-Unplugged版@Override
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
protectedListgetModelChildren()...{
GEF学习教程1-Unplugged版
return((Content)getModel()).getChildren();
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
publicvoidpropertyChange(PropertyChangeEventevt)...{
GEF学习教程1-Unplugged版
if(evt.getPropertyName().equals(Content.P_CHILDREN))
GEF学习教程1-Unplugged版refreshChildren();
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版

5.Policy:
tool接收到request,然后发给对应的EditPart,EditPart会遍历所有安装过的Policy,找到能够处理request的Policy,然后构造Command,并在适当时刻执行Command.
因为这个例子中的最高层Container我们使用的是ToolbarLayout,所以我这里没有象以往一样安装XYLayout可以使用的XYLayoutEditPolicy,而是选择了继承OrderedLayoutEditPolicy .为什么呢?
因为Layout是来安排每个child的位置,而XYLayout是用坐标进行绝对定位,所以不仅model需要拥有对应的坐标属性,而且相应的policy在创建Command时需要得到相应的坐标信息.这个例子里面我们使用的是ToolbarLayout,它不是以坐标来定位的,所以我们不需要用XYLayoutEditPolicy.
GEF中自带的LayoutPolicy结构如下:
GEF学习教程1-Unplugged版
这样可以很清晰得看出来GEF为不同的Layout已经设计过不同的policy,Draw2D中主要有以坐标定位的XYLayout和按位置定位的ToolbarLayout和FlowLayout.(后两者的区别主要是FlowLayout可以转行).暂时我是用的ToolbarLayout最简单的方式,所以选择了继承OrderedLayoutEditPolicy.
因为只要实现创建Column,所以我只覆写了OrderedLayoutEditPolicy里的getCreateCommand方法,如下:
CustomOrderedLayoutEditPolicy.java
GEF学习教程1-Unplugged版@Override
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
protectedCommandgetCreateCommand(CreateRequestrequest)...{
GEF学习教程1-Unplugged版CreateCommandcommand
=newCreateCommand();
GEF学习教程1-Unplugged版Columncolumn
=(Column)request.getNewObject();
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版command.setContent((Content)getHost().getModel());
GEF学习教程1-Unplugged版command.setColumn(column);
GEF学习教程1-Unplugged版
returncommand;
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
5.1安装policy
写好policy后就是安装它到editPart上了,回到ContentEditPart,覆写createEditPolicies方法:
ContentEditPart.java
GEF学习教程1-Unplugged版@Override
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
protectedvoidcreateEditPolicies()...{
GEF学习教程1-Unplugged版installEditPolicy(EditPolicy.LAYOUT_ROLE,
newCustomOrderedLayoutEditPolicy());
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版
6.Editor:
主要是getPaletteRoot方法:
Editor.java
GEF学习教程1-Unplugged版@Override
GEF学习教程1-Unplugged版GEF学习教程1-Unplugged版
protectedPaletteRootgetPaletteRoot()...{
GEF学习教程1-Unplugged版PaletteRootroot
=newPaletteRoot();
GEF学习教程1-Unplugged版PaletteGrouptoolGroup
=newPaletteGroup("tools");
GEF学习教程1-Unplugged版ToolEntrytool
=newSelectionToolEntry();
GEF学习教程1-Unplugged版toolGroup.add(tool);
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版tool
=newMarqueeToolEntry();
GEF学习教程1-Unplugged版toolGroup.add(tool);
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版PaletteDrawerdrawer
=newPaletteDrawer("draw");
GEF学习教程1-Unplugged版ImageDescriptordescriptor
=ImageDescriptor
GEF学习教程1-Unplugged版.getMissingImageDescriptor();
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版CreationToolEntrycreationEntry
=newCreationToolEntry(
GEF学习教程1-Unplugged版
"drawcolumn","createcolumn",newSimpleFactory(
GEF学习教程1-Unplugged版Column.
class),descriptor,descriptor);
GEF学习教程1-Unplugged版drawer.add(creationEntry);
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版root.add(toolGroup);
GEF学习教程1-Unplugged版root.add(drawer);
GEF学习教程1-Unplugged版
GEF学习教程1-Unplugged版
returnroot;
GEF学习教程1-Unplugged版}

GEF学习教程1-Unplugged版

参考资源:
eclipse cvs上的draw2d examples
gef源代码
dudu的教程
八进制的教程

相关文章:

  • 2021-10-08
  • 2021-09-26
  • 2021-12-03
  • 2022-01-06
  • 2021-10-25
  • 2021-05-22
  • 2021-09-24
  • 2021-07-06
猜你喜欢
  • 2021-10-17
  • 2021-09-17
  • 2021-10-22
  • 2021-10-08
  • 2021-12-18
  • 2021-10-10
  • 2021-07-13
相关资源
相似解决方案