`
lggege
  • 浏览: 372830 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

GEF实践总结(五)大小调整、按键监听、撤销重做

    博客分类:
  • GEF
阅读更多

一. 目标

  1. 能够选中Table,移动Table的位置,调整Table的大小。
  2. 可以支持撤销Undo和重做Redo。


二. 给DataBase书写XYLayoutEditPolicy
书写一个DataBaseXYLayoutEditPolicy 类继承与XYLayoutEditPolicy

public class DataBaseXYLayoutEditPolicy extends XYLayoutEditPolicy {
@Override
protected Command createChangeConstraintCommand(EditPart child, Object constraint) {
return null;
}
@Override
protected Command getCreateCommand(CreateRequest request) {
return null;
}
}

 

将其注册于DataBaseEditPart

@Override
protected void createEditPolicies() {
installEditPolicy(EditPolicy.LAYOUT_ROLE, new DataBaseXYLayoutEditPolicy());
}
 


启动后查看效果:


好了,就这么简单,已经可以选中Table了,选中后会出现边框,而且边框还有8个可用于调整大小的瞄点出现。


问题一: 为什么为了操作Table,而去给DataBase注册LayoutEditPolicy呢?
这是由于LayoutEditPolicy是用于对它上面的子EditPart而言的,这就SWT中给Composite设置GridLayout或FillLayout,具体怎么布局也是对于Composite上的子控件而言的。


问题二: 为什么注册了LayoutEditPolicy就会有选择框呢?
关心的话,可以去看ConstrainedLayoutEditPolicy.createChildEditPolicy的具体方法。


三. 书写Table执行调整大小的Command

public class TableChangeConstraitCommand extends Command {
private TableModel table;
private Rectangle oldRect;
private Rectangle newRect;
public TableChangConstraitCommand(TableModel table, Rectangle newRect) {
this.table = table;
this.newRect = newRect;
}
@Override
public void execute() {
oldRect = new Rectangle(table.getX(), table.getY(), table.getW(), table.getH());
table.setXYWH(newRect.x, newRect.y, newRect.width, newRect.height);
}
@Override
public void undo() {
table.setXYWH(oldRect.x, oldRect.y, oldRect.width, oldRect.height);
}
}
 



Command 用于具体执行对模型的修改:

  1. execute() 执行操作,在这里就是执行大小或位置调整,并保存旧的现场。
  2. undo()    撤销操作,其实就是从前面保存的现场,恢复到原来的大小和位置。
  3. redo()    重新执行 (如果不覆盖,其实就是执行execute方法)


这就是设计模型里面的Command模式,Command对象在被执行后,会保存在EditDomain CommandStack 中,用以支持Ctrl+Z的undo撤销操作,或者Ctry+Y的redo重新执行。

四. 改写DataBase的XYLayoutEditPolicy,支持大小调整

public class DataBaseXYLayoutEditPolicy extends XYLayoutEditPolicy {
@Override
protected Command createChangeConstraintCommand(EditPart child, Object constraint) {
TableModel table = (TableModel) child.getModel();
Rectangle rect = (Rectangle) constraint;

TableChangeConstraitCommand command = new TableChangeConstraitCommand(table, rect);
return command;
}
让DataBaseXYLayoutEditPolicy在createChangeConstraintCommand方法中返回一个TableChangeConstraitCommand即可。
 


五. 启动,查看效果



六. 给GraphicalViewer注册KeyHandle

        KeyHandler keyHandler = new KeyHandler();
        graphicalViewer.setKeyHandler(new GraphicalViewerKeyHandler(graphicalViewer).setParent(keyHandler));
        keyHandler.put(KeyStroke.getPressed(SWT.F1, SWT.NONE), new RedoAction(this));
        keyHandler.put(KeyStroke.getPressed(SWT.F2, SWT.NONE), new UndoAction(this));


KeyHandle :是对于按键的监听
KeyHandle可以使用setParent方法将多个KeyHandle组成按键的监听链,按键的监听从最下面一个KeyHandle一直往上遍历,只要其中一个KeyHandle对某一按键监听了,就执行对应的Action。
KeyHandler通过KeyStorke(按键、复合按键) 和 Action进行对应。
注意: 按键有按下(getPressed)和弹起(getReleased)的区别。

  1. Command在执行后,都会被放入CommandStack。
  2. UndoAction其实就是在CommandStack中,找到最后执行过的Command,调用其Undo方法。
  3. 同样的,RedoAction就是在CommandStatck中,找到最后一次Undo的Command,执行其redo方法。


七. 为ViewPart返回CommandStack

由于RedoAction UndoAction 需要依赖CommandStack ,通过ViewPart.getAdapter(CommandStack.class) 获得CommandStack对象,于是,我们需要给ViewPart返回CommandStack 对象,而它又存在于在EditDomain 中。

    @Override
    public Object getAdapter(Class adapter)
    {
        if ( CommandStack.class.equals(adapter) )
        {
            return this.graphicalViewer.getEditDomain().getCommandStack();
        }
        return super.getAdapter(adapter);
    }

 

好了,完成了,在你调整完了Table的大小后,可以使用F1、F2进行撤销和重做。

八. 改用ActionRegistry
由于在其他地方还需要使用RedoAction UndoAction ,那么你可以考虑使用ActionRegister ,将Action对象初始化完毕后,再缓存在ActionRegister 里面。

        //
        actionRegistry.registerAction(new RedoAction(this));
        IAction action = actionRegistry.getAction(ActionFactory.REDO.getId());
        keyHandler.put(KeyStroke.getPressed(SWT.F1, SWT.NONE), action);
       
        actionRegistry.registerAction(new UndoAction(this));
        action = actionRegistry.getAction(ActionFactory.UNDO.getId());
        keyHandler.put(KeyStroke.getPressed(SWT.F2, SWT.NONE), action);
    }
   
    private ActionRegistry actionRegistry = new ActionRegistry();
 



九. 其他
问题: 为什么按键选用F1和F2,而不是通常的Ctrl+Z和Ctrl+Y
是由于复合按键比单按键复杂,可以见我的另一篇博客。

十. 总结

  1. 认识了XYLayoutEditPolicy和简单的Command
  2. 认识了和Action想关的KeyHandle、KeyStroke、CommandStack、ActionRegistry

 

 

2
0
分享到:
评论
1 楼 xmind 2010-05-25  
 

相关推荐

    GEF学习总结

    GEF 全称 Graphical Editor Framework 。它是一个基于 eclipse 的图形化编辑框架。通过它,开发人员可以方便的以图形化的方式(而非文本的方式)展示和编辑模型。

    Gef 学习总结

    Gef 学习总结,Gef 相对资料比较少没有完整的文档都要靠自己学习

    GEF研究总结

    关于gef的一点研究总结

    GEF学习体会与经验总结(.doc)

    该资料对GEF这个框架的一些理解,以及自己的一些学习体会,其中大部分是从网上搜到的一些关于GEF框架的质料,这里已经经过了整理,对于一个初学者很有帮助。

    GEF-ALL-3.2.2

    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入门系列.rar

    GEF入门系列,八进制 的GEF入门系列教程, 由社区经作者授权后整理而成GEF(Graphical Editor Framework)是一个图形化编辑框架,它允许开发人员以图形化的方式展示和编辑模型,从而提升用户体验。这样的应用程序有很...

    GEF入门必读 GEF入门系列 GEF-whole-upload

    GEF入门必读 GEF入门系列 GEF-whole-upload 感谢八进制

    GEF入门详解DOC文档

    GEF(Graphical Editor Framework)是一个图形化编辑框架,它允许开发人员以图形化的方式展示和编辑模型,从而提升用户体验。这样的应用程序有很多,例如:UML类图编辑器、图形化XML编辑...支持撤消/重做功能; 等等。

    GEF开发实践

    GEF开发实践说明,简单的实例说明。有什么问题请留言

    自己下的GEF资源打包

    我下的资源的打包,希望有帮助,包括:GEF_Tutorial,GEF实例,入门教程,GEF-whole-upload

    GEF框架入门学习

    这是我自己在学习GEF的时候做的一些总结。 1、GEF中的概念比如Command,Tool,Request等等。 2、GEF中使用到的几种设计模式。 3、GEF中对鼠标键盘事件处理的机制、流程。

    GEF简易教程-学习GEF的入门教程

    GEF简易教程-学习GEF的入门教程,不错的gef入门教程

    gef 转折线的相关方法实现和 GEF的API chm 格式

    gef 绘图折线的实现方法。 gef API chm 格式

    GEF 3.10 eclipse 插件

    GEF eclipse 插件

    GEF.rarGEF.rar

    GEF.rar GEF.rar GEF.rar

    IBM GEF 推广资料

    Agenda Start things off What is GEF? GEF Demo Draw2d Overview Example GEF Overview Break Hands-on Activity: Shapes Example

    GEF理解系列三

    GEF理解之第三部分,学习gef必备文档资料啊,值得下载

    GEF理解系列1

    GEF理解之第一部分,学习gef必备文档资料啊,值得下载

    Eclipse的GEF学习

    Eclipse插件GEF的介绍,对于学习GEF有很大帮助

Global site tag (gtag.js) - Google Analytics