今天我要继续修改前面的例子.
运行效果如下图所示:

GEF学习教程3-Unplugged版

代码下载

为了达到这样的效果,我们必须修改原来的model,现在的model UML图是这样的:

GEF学习教程3-Unplugged版

一些需要考虑的问题:
1.Layout:
因为这里出现了好3个组合的关系,意味着有parent--children的关系,那么我们必须考虑为parent的figure设置什么Layout来管理children.
2.Command:
不同的事件需要不同的Command,这一次我还是只是增加和修改了对应的CreateCommand.
3.Policy:
根据不同的Layout使用不同的LayoutPolicy
下面的表格显示了我的结构:
model Figure Figure使用的Layout

EditPart EditPart安装的EditPolicy
Content ContentFigure XYLayout ContentPart XYLayoutEditPolicy
Container ContainerFigure ToolbarLayout ContainerPart OrderLayoutEditPolicy
Column ColumnFigure FlowLayout ColumnPart FlowLayoutEditPolicy
Node NodeFigure ToolbarLayout NodePart

因为model做了比较大的修改,所以源代码很多地方都改过了:P

有些地方千万不要忘记了:
比如ContainerPart里的两个方法需要覆写:
ContainerEditPart.java
GEF学习教程3-Unplugged版@Override
GEF学习教程3-Unplugged版GEF学习教程3-Unplugged版
protectedListgetModelChildren()...{
GEF学习教程3-Unplugged版
return((Container)getModel()).getChildren();
GEF学习教程3-Unplugged版}

GEF学习教程3-Unplugged版
GEF学习教程3-Unplugged版@Override
GEF学习教程3-Unplugged版GEF学习教程3-Unplugged版
protectedvoidrefreshVisuals()...{
GEF学习教程3-Unplugged版Rectangleconstraint
=((Container)getModel()).getConstraint();
GEF学习教程3-Unplugged版((GraphicalEditPart)getParent()).setLayoutConstraint(
this,
GEF学习教程3-Unplugged版getFigure(),constraint);
GEF学习教程3-Unplugged版}

代码下载

参考资源:
GEF学习教程1-Unplugged版
GEF学习教程2-Unplugged版

相关文章: