GMF默认生成的outline很成问题,只能显示top level的图形,比如在logic例子中,画板中图形元素和outline对应如下:
可见非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类了。
protected EditPartFactory getOutlineViewEditPartFactory() {
return new EditPartFactory() {
public EditPart createEditPart(EditPart context, Object model) {
if (model instanceof Diagram) {
return new LogicDiagramTreeEditPart(model);
} else {
return this.getRightTreeEditPart(model);
}
}
private TreeEditPart getRightTreeEditPart(Object view) {
if(view instanceof Node &&
((Node) view).getElement() instanceof ContainerElement) {
return new LogicContainerTreeEditPart(view);
}
return new LogicChildTreeEditPart(view);
}
};
}
return new EditPartFactory() {
public EditPart createEditPart(EditPart context, Object model) {
if (model instanceof Diagram) {
return new LogicDiagramTreeEditPart(model);
} else {
return this.getRightTreeEditPart(model);
}
}
private TreeEditPart getRightTreeEditPart(Object view) {
if(view instanceof Node &&
((Node) view).getElement() instanceof ContainerElement) {
return new LogicContainerTreeEditPart(view);
}
return new LogicChildTreeEditPart(view);
}
};
}
LogicDiagramTreeEditPart.java
public class LogicDiagramTreeEditPart extends TreeDiagramEditPart{
public LogicDiagramTreeEditPart(Object model) {
super(model);
// TODO Auto-generated constructor stub
}
@Override
protected Image getImage() {
return LogicDiagramPlugin.getInstance().getBundledImage("icons/logic.gif");
}
@Override
protected String getText() {
return "Logic Diagram";
}
}
public LogicDiagramTreeEditPart(Object model) {
super(model);
// TODO Auto-generated constructor stub
}
@Override
protected Image getImage() {
return LogicDiagramPlugin.getInstance().getBundledImage("icons/logic.gif");
}
@Override
protected String getText() {
return "Logic Diagram";
}
}
LogicContainerTreeEditPart.java
public class LogicContainerTreeEditPart extends TreeContainerEditPart {
public LogicContainerTreeEditPart(Object model) {
super(model);
// TODO Auto-generated constructor stub
}
@Override
protected List getModelChildren() {
List children = ((View) this.getModel()).getChildren();
return children;
}
@Override
protected Image getImage() {
String path = "icons/logic.gif";
String type = getType();
if("circuit".equalsIgnoreCase(type)) {
path = "icons/circuit16.gif";
}
if("FlowContainer".equalsIgnoreCase(type)) {
path = "icons/logicflow16.gif";
}
return LogicDiagramPlugin.getInstance().getBundledImage(path);
}
@Override
protected String getText() {
// TODO Auto-generated method stub
return ((View) this.getModel()).getType();
}
private String getType() {
return ((View) getModel()).getType();
}
}
public LogicContainerTreeEditPart(Object model) {
super(model);
// TODO Auto-generated constructor stub
}
@Override
protected List getModelChildren() {
List children = ((View) this.getModel()).getChildren();
return children;
}
@Override
protected Image getImage() {
String path = "icons/logic.gif";
String type = getType();
if("circuit".equalsIgnoreCase(type)) {
path = "icons/circuit16.gif";
}
if("FlowContainer".equalsIgnoreCase(type)) {
path = "icons/logicflow16.gif";
}
return LogicDiagramPlugin.getInstance().getBundledImage(path);
}
@Override
protected String getText() {
// TODO Auto-generated method stub
return ((View) this.getModel()).getType();
}
private String getType() {
return ((View) getModel()).getType();
}
}
LogicChildTreeEditPart.java
public class LogicChildTreeEditPart extends TreeEditPart {
public LogicChildTreeEditPart(Object model) {
super(model);
// TODO Auto-generated constructor stub
}
@Override
protected Image getImage() {
String path = "icons/logic.gif";
String type = getType();
if("LED".equalsIgnoreCase(type)) {
path = "icons/ledicon16.gif";
}
if("AndGate".equalsIgnoreCase(type)) {
path = "icons/and16.gif";
}
if("OrGate".equalsIgnoreCase(type)) {
path = "icons/or16.gif";
}
if("XorGate".equalsIgnoreCase(type)) {
path = "icons/xor16.gif";
}
return LogicDiagramPlugin.getInstance().getBundledImage(path);
}
@Override
protected String getText() {
// TODO Auto-generated method stub
return ((View) this.getModel()).getType();
}
private String getType() {
return ((View) getModel()).getType();
}
}
在LogicDiagramPlugin里面加入如下代码,主要用于获取icon imagepublic LogicChildTreeEditPart(Object model) {
super(model);
// TODO Auto-generated constructor stub
}
@Override
protected Image getImage() {
String path = "icons/logic.gif";
String type = getType();
if("LED".equalsIgnoreCase(type)) {
path = "icons/ledicon16.gif";
}
if("AndGate".equalsIgnoreCase(type)) {
path = "icons/and16.gif";
}
if("OrGate".equalsIgnoreCase(type)) {
path = "icons/or16.gif";
}
if("XorGate".equalsIgnoreCase(type)) {
path = "icons/xor16.gif";
}
return LogicDiagramPlugin.getInstance().getBundledImage(path);
}
@Override
protected String getText() {
// TODO Auto-generated method stub
return ((View) this.getModel()).getType();
}
private String getType() {
return ((View) getModel()).getType();
}
}
public static ImageDescriptor getBundledImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("org.eclipse.gmf.examples.runtime.diagram.logic", path);
}
public Image getBundledImage(String path) {
Image image = getImageRegistry().get(path);
if (image == null) {
getImageRegistry().put(path, getBundledImageDescriptor(path));
image = getImageRegistry().get(path);
}
return image;
}
return AbstractUIPlugin.imageDescriptorFromPlugin("org.eclipse.gmf.examples.runtime.diagram.logic", path);
}
public Image getBundledImage(String path) {
Image image = getImageRegistry().get(path);
if (image == null) {
getImageRegistry().put(path, getBundledImageDescriptor(path));
image = getImageRegistry().get(path);
}
return image;
}
经过改进后的logic diagram outline page: