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);
 }

 

而我们当然选用的是第一种哦!

 

此文要感谢网上那些先驱们,没有你们,我怎么可能实现拖拽,先谢谢各位了!

 

0 0
原创粉丝点击