GMF中定制自己的outline

本文介绍如何针对GMF生成的Outline进行定制,以显示更详细的层级结构,并提供了具体的实现方法及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GMF默认生成的outline很成问题,只能显示top level的图形,比如在logic例子中,画板中图形元素和outline对应如下:

image0_thumb.png image0_thumb1.png

 

可见非top level的图形并没有与之对应的Tree Item显示在outline tree上。

其实用过gef的人都很容易知道原因,因为outline上每一个节点对应的也是一个tree editpart,每个tree editpart复用对应画板图形元素相同的数据模型。因此很显然,GMF生成的outline 和TreeEditPart并不能满足具体应用的需求,用户需要自己订制每个图形元素对应的outline 元素,也就是为每个图形元素实现一个TreeEditPart。

如何定制outlinepage:

在LogicNotationEditor中重载方法getOutlineViewEditPartFactory(),不过要注意的是这个方法在gmf1.0.1之后再加入DiagramEditor中,在gmf1.0.0里面没有这个方法,由于DiagramEditor中的内部类DiagramOutlinePage是友好的,无法继承,所以要想在gmf1.0.0中定制自己的outline就只能重写自己的outline page类了。

ExpandedBlockStart.gif ContractedBlock.gif protected  EditPartFactory getOutlineViewEditPartFactory()  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
return new EditPartFactory() dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public EditPart createEditPart(EditPart context, Object model) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (model instanceof Diagram) dot.gif{
InBlock.gif                    
return new LogicDiagramTreeEditPart(model);
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 else dot.gif{
InBlock.gif                    
return this.getRightTreeEditPart(model);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
private TreeEditPart getRightTreeEditPart(Object view) dot.gif{
InBlock.gif                
if(view instanceof Node &&
ExpandedSubBlockStart.gifContractedSubBlock.gif                        ((Node) view).getElement() 
instanceof ContainerElement) dot.gif{
InBlock.gif                    
InBlock.gif                    
return new LogicContainerTreeEditPart(view);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return new LogicChildTreeEditPart(view);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
;
ExpandedBlockEnd.gif    }

 

LogicDiagramTreeEditPart.java

ExpandedBlockStart.gif ContractedBlock.gif public   class  LogicDiagramTreeEditPart  extends  TreeDiagramEditPart dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public LogicDiagramTreeEditPart(Object model) dot.gif{
InBlock.gif        
super(model);
InBlock.gif        
// TODO Auto-generated constructor stub
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    @Override
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected Image getImage() dot.gif{
InBlock.gif        
return LogicDiagramPlugin.getInstance().getBundledImage("icons/logic.gif");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    @Override
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected String getText() dot.gif{
InBlock.gif        
return "Logic Diagram";
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

LogicContainerTreeEditPart.java

ExpandedBlockStart.gif ContractedBlock.gif public   class  LogicContainerTreeEditPart  extends  TreeContainerEditPart  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public LogicContainerTreeEditPart(Object model) dot.gif{
InBlock.gif        
super(model);
InBlock.gif        
// TODO Auto-generated constructor stub
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    @Override
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected List getModelChildren() dot.gif{
InBlock.gif        List children 
= ((View) this.getModel()).getChildren();
InBlock.gif        
return children;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    @Override
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected Image getImage() dot.gif{
InBlock.gif        String path 
= "icons/logic.gif";
InBlock.gif        String type 
= getType();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if("circuit".equalsIgnoreCase(type)) dot.gif{
InBlock.gif            path 
= "icons/circuit16.gif";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if("FlowContainer".equalsIgnoreCase(type)) dot.gif{
InBlock.gif            path 
= "icons/logicflow16.gif";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return LogicDiagramPlugin.getInstance().getBundledImage(path);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    @Override
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected String getText() dot.gif{
InBlock.gif        
// TODO Auto-generated method stub
InBlock.gif
        return ((View) this.getModel()).getType();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private String getType() dot.gif{
InBlock.gif        
return ((View) getModel()).getType();
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

LogicChildTreeEditPart.java

ExpandedBlockStart.gif ContractedBlock.gif public   class  LogicChildTreeEditPart  extends  TreeEditPart  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public LogicChildTreeEditPart(Object model) dot.gif{
InBlock.gif        
super(model);
InBlock.gif        
// TODO Auto-generated constructor stub
ExpandedSubBlockEnd.gif
    }

InBlock.gif    @Override
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected Image getImage() dot.gif{
InBlock.gif        String path 
= "icons/logic.gif";
InBlock.gif        String type 
= getType();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if("LED".equalsIgnoreCase(type)) dot.gif{
InBlock.gif            path 
= "icons/ledicon16.gif";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if("AndGate".equalsIgnoreCase(type)) dot.gif{
InBlock.gif            path 
= "icons/and16.gif";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if("OrGate".equalsIgnoreCase(type)) dot.gif{
InBlock.gif            path 
= "icons/or16.gif";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if("XorGate".equalsIgnoreCase(type)) dot.gif{
InBlock.gif            path 
= "icons/xor16.gif";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return LogicDiagramPlugin.getInstance().getBundledImage(path);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    @Override
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected String getText() dot.gif{
InBlock.gif        
// TODO Auto-generated method stub
InBlock.gif
        return ((View) this.getModel()).getType();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private String getType() dot.gif{
InBlock.gif        
return ((View) getModel()).getType();
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

 

在LogicDiagramPlugin里面加入如下代码,主要用于获取icon image
ExpandedBlockStart.gif ContractedBlock.gif      public   static  ImageDescriptor getBundledImageDescriptor(String path)  dot.gif {
InBlock.gif        
return AbstractUIPlugin.imageDescriptorFromPlugin("org.eclipse.gmf.examples.runtime.diagram.logic", path);
ExpandedBlockEnd.gif    }

ExpandedBlockStart.gifContractedBlock.gif    
public  Image getBundledImage(String path)  dot.gif {
InBlock.gif        Image image 
= getImageRegistry().get(path);
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (image == nulldot.gif{
InBlock.gif            getImageRegistry().put(path, getBundledImageDescriptor(path));
InBlock.gif            image 
= getImageRegistry().get(path);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return image;
ExpandedBlockEnd.gif    }


经过改进后的logic diagram outline page:

logic_thumb14.jpg 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值