GEF调色板中的多级树结构

来源:互联网 发布:数据质量检测系统 编辑:程序博客网 时间:2024/05/02 02:18

在GEF中的调色板(工具箱)是个树形结构,如果你GEF的Editor是继承至GraphicalEditorWithFlyoutPalette类的话,其中必须实现一个叫getPaletteRoot()的方法,它要求你返回一个PaletteRoot对象,其实它就是调色板树的根节点对象。

      在调色板树的根节点(PaletteRoot)下,我们一般会放一些工具组(PaletteGroup)或抽屉(PaletteDrawer)来装一些画图的模型,最常见的就是选择工具(SelectionToolEntry)和连接线工具(CreationToolEntry)了,如果我想在“工具组”文件夹下放一个选择工具和连接线工具,我们可以这样写:

// 建立调色板的根节点对象root,最后返回给的也是root

PaletteRoot root = new PaletteRoot();

// 创建一个工具组用于放置常规Tools

PaletteGroup toolGroup = new PaletteGroup("工具组");

// 创建一个GEF提供的selection工具并将其放入toolgroup中
ToolEntry selectTool = new SelectionToolEntry();

// 添加到组中
toolGroup.add(selectTool);

  // 该选择的工具是缺省被选择的工具
  root.setDefaultEntry(selectTool);

ImageDescriptor descriptor = AbstractUIPlugin
     .imageDescriptorFromPlugin(Activator.PLUGIN_ID,
       IDmImageKey.CONNECTION);
   String combineConnectionLine = "集成连接线";

// 创建连接线工具

   CreationToolEntry connectionLine = new ConnectionCreationToolEntry(
       combineConnectionLine, combineConnectionLine, new SimpleFactory  (NormalConnectionModel.class),descriptor, descriptor);
   connectionLine.setToolClass(ScoreCardConnectionCreationTool.class);
   // 将连接线添加到组中
   toolGroup.add(connectionLine);

   root.add(toolGroup);

    效果类似如下:

 



 

 


有人会问,为什么不见“工具组”文件夹,呵呵,这是因为PaletteGroup是不显示标题的。
如果是添加自定义的绘图工具,可以用到PaletteDrawer抽屉来存放,先上代码:
PaletteDrawer drawer = new PaletteDrawer("模型");
  // 模型工具所对应的图标
  ImageDescriptor descriptor = AbstractUIPlugin
    .imageDescriptorFromPlugin(Activator.PLUGIN_ID,
      IDmImageKey.START_IMAGE);
  ImageDescriptor s_descriptor = AbstractUIPlugin
    .imageDescriptorFromPlugin(Activator.PLUGIN_ID,
      IDmImageKey.S_START_IMAGE);
  // 创建“开 始”工具
CreationToolEntry startToolEntry = new CreationToolEntry(
    "开始" , "创建开始图元", new SimpleFactory(
StartModel.class), s_descriptor, descriptor);
drawer.add(startToolEntry);
root.add(drawer);
效果如下图:

 



 

 


 
有人会问,如果要添加多级文件夹来分类存放绘图工具,那该怎么办呢?
默认的PaletteDrawer类是不行的,也就是说PaletteDrawer下不能再包含多个PaletteDrawer,虽然PaletteGroup 下能包含PaletteGroup 和PaletteDrawer,但由于其不能展开和收缩子节点,而且没有标题,所有要了也没用。
看下PaletteDrawer的源代码,我们马上发现他不能包含PaletteDrawer的原因了:
 /**
  * Returns true if this type can be a child of this container
  *
  * @param type
  *            the type being requested
  * @return true if this can be a child of this container
  */
 public boolean acceptsType(Object type) {
  if (type.equals(PALETTE_TYPE_DRAWER)
    || type.equals(PaletteGroup.PALETTE_TYPE_GROUP))
   return false;
  return super.acceptsType(type);
 }
这是PaletteDrawer类覆盖其父类PaletteContainer的一个方法,type.equals(PALETTE_TYPE_DRAWER) || type.equals(PaletteGroup.PALETTE_TYPE_GROUP))表明如果用户往其下添加的是PaletteDrawer或PaletteGroup ,就不接受,于是,我们只要自定义一个类,继承PaletteDrawer,覆盖掉这方法,就可以做成多级树了!给一个实现:
public class MyPaletteDrawer extends PaletteDrawer {
 public MyPaletteDrawer(String label) {
      super(label);
 }
 
 public MyPaletteDrawer(String label, ImageDescriptor icon) {
      super(label, icon);
 }
 
 public boolean acceptsType(Object type) {
// 这里强迫接受PaletteDrawer和PaletteGroup
  if (type.equals(PALETTE_TYPE_DRAWER)
    || type.equals(PaletteGroup.PALETTE_TYPE_GROUP))
   return true;
  return super.acceptsType(type);
 }
}
这样以后,你就可以在  MyPaletteDrawer下用add方法添加多个MyPaletteDrawer对象了,就可以做成多级树来存放工具了!
效果图: 



 

 

 注意,由于这样做的话,没有传统树级结构的缩进,所以我在子MyPaletteDrawer对象中用了

 public MyPaletteDrawer(String label, ImageDescriptor icon) {
      super(label, icon);
 }
这个构造方法来换了一个不同的而且窄一点的图标,来制造缩进效果,先凑合用吧,以后大家有好的方法可以相互交流哦!