GEF原理及实现系列(五、请求和编辑策略)
来源:互联网 发布:纸白银行情软件 编辑:程序博客网 时间:2024/05/20 17:23
关键字: policies
请求和编辑策略是GEF框架中减轻控制器的负担、减小代码耦合度而实现的一种解决方案。
1.请求和编辑策略(Request and EditPolicies)
请求和编辑策略对初学者来说是比较难理解的部分,但正是因为这种机制才使得GEF框架功能强大,而且非常灵活。在EditPart中,可以通过设置不同的编辑策略(EditPolicies)来处理不同的请求,这样,一方面,可以把代码从EditPart中解放处 理,分别由不同的EditPolicies进行处理,另一方面,用户可以着力于自己的关注点,但由此也增加了学习GEF框架的时间。另外,在EditPart中设置编辑策略时,要指定相应的角色(Role),角色只是一个标识,在同一个EditPart中不能存在两个相同角色的编辑策略,读者可以在GEF的联机文档(http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.gef.doc.isv/guide/guide.html )中找到详细的编辑策略、请求和角色说明。
2.编辑策略的实现
控制器中通过createEditPolicies()方法添加编辑策略,每种编辑策略负责处理相应的请求。通常请求一般会对模型进行操作,在EditPolicies中,可以通过命令的方式操作模型,命令将在后面介绍。EditPolicies代码如下:
java 代码
1.package com.example.policies; 2. 3.import org.eclipse.draw2d.geometry.Rectangle; 4.import org.eclipse.gef.EditPart; 5.import org.eclipse.gef.Request; 6.import org.eclipse.gef.commands.Command; 7.import org.eclipse.gef.editpolicies.XYLayoutEditPolicy; 8.import org.eclipse.gef.requests.CreateRequest; 9. 10.import com.example.commands.CreateNodeCommand; 11.import com.example.commands.MoveNodeCommand; 12.import com.example.model.Diagram; 13.import com.example.model.Node; 14.import com.example.parts.NodePart; 15. 16.public class DiagramLayoutEditPolicy extends XYLayoutEditPolicy { 17. 18. protected Command createAddCommand(EditPart child, Object constraint) { 19. return null; 20. } 21. //创建模型位置改变的命令 22. protected Command createChangeConstraintCommand(EditPart child, Object constraint) { 23. //如果位置改变的不是Node则返回 24. if (!(child instanceof NodePart)) 25. return null; 26. if (!(constraint instanceof Rectangle)) 27. return null; 28. 29. MoveNodeCommand cmd = new MoveNodeCommand(); 30. cmd.setNode((Node) child.getModel()); 31. //设置模型新的位置信息 32. cmd.setLocation(((Rectangle) constraint).getLocation()); 33. return cmd; 34. 35. } 36. //获得创建模型的命令 37. protected Command getCreateCommand(CreateRequest request) { 38. //判断请求创建的是否为Node 39. if (request.getNewObject() instanceof Node) { 40. //新建CreateNodeCommand 41. CreateNodeCommand cmd = new CreateNodeCommand(); 42. //设置父模型 43. cmd.setDiagram((Diagram) getHost().getModel()); 44. //设置当前模型 45. cmd.setNode((Node) request.getNewObject()); 46. Rectangle constraint = (Rectangle) getConstraintFor(request); 47. //设置模型的位置信息 48. cmd.setLocation(constraint.getLocation()); 49. //返回Command对象 50. return cmd; 51. } 52. return null; 53. } 54. 55. protected Command getDeleteDependantCommand(Request request) { 56. return null; 57. } 58.}
通过实现此编辑策略,GEF编辑器将能够处理XYLayoutEditPolicy所能响应的相关请求,并交由相应的Command进行处理。
0 0
- GEF原理及实现系列(五、请求和编辑策略)
- GEF原理及实现系列(一、GEF概述)
- GEF原理及实现系列(二、模型)
- GEF原理及实现系列(三、视图)
- GEF原理及实现系列(四、控制器)
- GEF原理及实现系列(六、命令)
- GEF原理及实现系列(七、模型同步)
- GEF原理及实现系列(八、选项板)
- GEF Programmers' Guide 中文版(编辑和策略)
- GEF入门系列(五、浅谈布局)
- GEF入门系列(二、GEF概述)
- GEF入门系列(六、添加菜单和工具条)
- 数据结构和算法系列 - geohash算法原理及实现方式
- BTrace系列之五:实现原理
- BTrace系列之五:实现原理
- 编辑原理系列学习:编译器简介及Lexical analyzer
- H.264-AVC视频编码原理及实现(五)
- H.264-AVC视频编码原理及实现(五)
- TCP三次握手详解及释放连接过程
- 剑指offer(40)-左旋转字符串
- GEF原理及实现系列(四、控制器)
- OOP-----类与对象对象
- php面向对象编程
- GEF原理及实现系列(五、请求和编辑策略)
- GEF原理及实现系列(六、命令)
- 海量数据处理面试题与十个方法大总结
- PHP
- android studio native 方法一直显示 红色
- HQl查询例子和讲解(转载的!---)
- js柯里化
- Guava教程-Cache讲解
- _stdcall和_cdecl区别