GEF中拖拽来创建图元的实现(GraphicalEditorWithFlyoutPalette版)
来源:互联网 发布:数据质量检测系统 编辑:程序博客网 时间:2024/05/02 02:09
GraphicalEditor是来让用户快速开始GEF的Editor,它其实是一个包含了一个GraphicalViewer 的Editor。
看API可以知道,GraphicalEditorWithFlyoutPalette和GraphicalEditorWithPalette都继承于GraphicalEditor,他们都包含了一个被分割的Composite,一部分是用来画图的GraphicalViewer,一部分是用来装画图工具的PaletteViewer,GraphicalViewer类就比较简单了,实现EditPartViewer接口,只额外添加了一个通过指定的Point来获取Handler的方法,Handler对于选择工具(SelectionTool)是很有用的,选择工具通过它来决定是否发动相应的事件消息,如果选择工具得到的Handler为空,则什么也不做,如果不为空,则选择工具可以通过Handler来跟踪用户鼠标拖动的轨迹。记住,选择工具只在用户第一次按住鼠标时才去获取Handler。而PaletteViewer继承于ScrollingGraphicalViewer(一个实现了本地滚动功能的GraphicalViewerImpl),是GEF调色板的Graphical viewer。
顾名思义,GraphicalEditorWithFlyoutPalette与GraphicalEditorWithPalette最大的区别是GraphicalEditorWithFlyoutPalette的调色板(Palette)是弹出窗口形式的,可以隐藏。闲话少说,直接切入主题,看看在GraphicalEditorWithFlyoutPalette中怎么实现拖动调色板的工具来画图。
第一步:GraphicalEditorWithFlyoutPalette和GraphicalEditorWithPalette在这一步是一样的,就是在protected void initializeGraphicalViewer()方法(在初始化GraphicalViewer时会被调用)中给GraphicalViewer添加作为选择工具拖动目标的事件监听,因为从调色板拖到我这里我得知道啊,参考代码如下:
getGraphicalViewer().addDropTargetListener(new TemplateTransferDropTargetListener(getGraphicalViewer()) {
protected CreationFactory getFactory(Object template) {
return new SimpleFactory((Class) template);
}
});
看到第一步,大家就自然会想到第二步,没错,第二步就是给调色板添加作为选择工具拖动源的事件监听
,这一步实现上GraphicalEditorWithFlyoutPalette和GraphicalEditorWithPalette是有区别的。
对于GraphicalEditorWithFlyoutPalette,我们要覆盖 protected PaletteViewerProvider createPaletteViewerProvider()方法,在那里面添加,参考代码如下:
protected PaletteViewerProvider createPaletteViewerProvider() {
return new PaletteViewerProvider(getEditDomain()) {
protected void configurePaletteViewer(PaletteViewer viewer) {
super.configurePaletteViewer(viewer);
viewer.addDragSourceListener(new TemplateTransferDragSourceListener(viewer));
}
};
}
对于GraphicalEditorWithPalette,我们只需要在其 protected void initializePaletteViewer()方法中添加即可,参考代码如下:
protected void initializePaletteViewer() {
getPaletteViewer().addDragSourceListener(
new TemplateTransferDragSourceListener(getPaletteViewer()));
}
第三步都相同,就是修改我们在 protected PaletteRoot getPaletteRoot() 中返回的自己写的PaletteRoot ,把里面创建工具的CreationToolEntry改成CombinedTemplateCreationEntry(当然如果你用的就是CombinedTemplateCreationEntry,那就不用改),参考代码如下:
CombinedTemplateCreationEntry entry = new CombinedTemplateCreationEntry(
"模型1", "创建模型1", EnteringModel.class, new SimpleFactory(
EnteringModel.class), s_descriptor, descriptor);
baseWorkFlow.add(entry);
注意我加黑的部分EnteringModel.class,这个参数记得加(和后面的new SimpleFactory(
EnteringModel.class)中的Class保持一致),不加也不会报错,网上是这么说的。。。
CombinedTemplateCreationEntry是继承自CreationToolEntry,其实它就是一个支持拖拽源(DragSource)的ToolEntry,里面有两个构造函数:
public CombinedTemplateCreationEntry(String label, String shortDesc,
Object template, CreationFactory factory,
ImageDescriptor iconSmall, ImageDescriptor iconLarge) {
super(label, shortDesc, factory, iconSmall, iconLarge);
setTemplate(template);
}
和
public CombinedTemplateCreationEntry(String label, String shortDesc,
CreationFactory factory, ImageDescriptor iconSmall,
ImageDescriptor iconLarge) {
this(label, shortDesc, factory, factory, iconSmall, iconLarge);
}
而我们当然选用的是第一种哦!
此文要感谢网上那些先驱们,没有你们,我怎么可能实现拖拽,先谢谢各位了!
- GEF中拖拽来创建图元的实现(GraphicalEditorWithFlyoutPalette版)
- GEF源码分析(三) 模拟GEF设计思路,解剖GEF2 附图:包含GEF的Editor创建时序图
- GEF 创建连线的优化
- GEF常见问题4:非矩形图元
- GEF专题训练(一) RCP工程的创建1
- GEF 学习系列之六:图元的选择框和反馈显示
- GEF原理及实现系列(一、GEF概述)
- 在GEF中实现悬空的连线
- GEF快捷键操作图元
- GEF创建流程
- 使用图形编辑框架GEF创建基于Eclipse的应用程序
- SWT 应用程序 实现的Drag and Drop(DND) 和 GEF 树上元素的拖拽
- gef实现属性页
- GEF-实现控件面板Palette的动态刷新
- GEF源码分析(一) eclipse的GEF相关项目说明
- GEF源码分析(三) GEF 的树状构架 ___ Model/EditPart/Figure
- GEF源码分析(四) GEF 消息转义 ___ Tool的作用
- GEF 学习系列之五:给图元添加DirectEdit功能
- JavaScript 数组去重的常见方法!
- 谈谈创业公司的技术选型
- GEF调色板中的多级树结构
- 环形蛇形矩阵
- RCP中带复选框的表格应用
- GEF中拖拽来创建图元的实现(GraphicalEditorWithFlyoutPalette版)
- 简单好用的自定义DialogFragment
- git基本操作(1)
- GEF小技巧之设定GraphicalEditor中图元为选中状态
- 配置安卓中间件的开发环境
- mysql 数据库重命名
- RCP小技巧之带勾(toggle)的菜单
- HDU1789
- Java Socket编程入门